spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
914 stars 560 forks source link

Absolute base uri with multiple components fails [DATAREST-564] #939

Open spring-projects-issues opened 9 years ago

spring-projects-issues commented 9 years ago

Dave Handy opened DATAREST-564 and commented

This test case fails, because BaseUri is trying to reverse an unmodifiable collection. A slightly broader question is why the getRepositoryLookup path logic is so different for absolute base URIs versus relative base URI.

import org.junit.Test; import org.springframework.data.rest.webmvc.BaseUri;

public class BaseUriTest { @Test public void testbaseURi() { BaseUri base = new BaseUri("https://example.com/services/v1/"); base.getRepositoryLookupPath(""); } }

Caused by: java.lang.UnsupportedOperationException: null at java.util.Collections$UnmodifiableList.set(Collections.java:1311) at java.util.Collections.swap(Collections.java:497) at java.util.Collections.reverse(Collections.java:378) at org.springframework.data.rest.webmvc.BaseUri.getRepositoryLookupPath(BaseUri.java:130)


Affects: 2.2.2 (Evans SR2)

1 votes, 4 watchers

spring-projects-issues commented 8 years ago

Martin Macko commented

I've submitted a pull request for this: https://github.com/spring-projects/spring-data-rest/pull/201

spring-projects-issues commented 8 years ago

Oliver Drotbohm commented

We dropped support for absolute base URIs as they have a couple of drawbacks.

  1. You put the host name, scheme and port into the configuration and would've to tweak that if you move the app into a different environment.
  2. An absolute base URI contains both context and servlet mapping which makes the resolution of what the actual base path segment is costly, error prone and even subject to ambiguities.

Any reason you need to stick to absolute ones?

spring-projects-issues commented 8 years ago

Dave Handy commented

Oliver- I was trying to set up apache/mod_proxy in front of tomcat, and the embedded HAL _links were being generated with the internal hostname. Since then I switched to mod_ajp (which sadly required an upgrade to apache that is not RHEL6 standard) and AJP forwards all the necessary information to tomcat to generate the links properly. AJP of course is a much better solution than using an absolute base URI; totally agree with your reasoning.

Since you've dropped support, does the test case above throw a more explanatory exception?

spring-projects-issues commented 8 years ago

William Gorder commented

How about using it with websockets? RequestContext holder is not going to find anything since the request came over a websocket. So how am I supposed to build my response with links???

java.lang.IllegalStateException: Could not find current request via RequestContextHolder
    at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.support.ServletUriComponentsBuilder.getCurrentRequest(ServletUriComponentsBuilder.java:183) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequest(ServletUriComponentsBuilder.java:175) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.data.web.PagedResourcesAssembler.getUriTemplate(PagedResourcesAssembler.java:227) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
    at org.springframework.data.web.PagedResourcesAssembler.addPaginationLinks(PagedResourcesAssembler.java:190) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
    at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:185) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
    at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:115) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
    at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:90) ~[spring-data-commons-1.11.4.RELEASE.jar:na]
    at com.api.chat.ChatController.getConversations(ChatController.java:41) ~[ChatController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_20]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_20]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_20]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_20]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-messaging-4.2.5.RELEASE.jar:4.2
spring-projects-issues commented 3 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.