#408 - Form relative links when outside a Spring web request

If forming a link outside a Spring web request, fallback to relative links.

Original pull-request: #410.
Related issues: #330, #143, #516.
This commit is contained in:
Greg Turnquist
2017-05-25 12:50:51 -05:00
committed by Oliver Gierke
parent c784df964e
commit b346366f73
3 changed files with 21 additions and 30 deletions

View File

@@ -17,15 +17,15 @@ package org.springframework.hateoas.mvc;
import static org.springframework.util.StringUtils.*;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.TemplateVariables;
import org.springframework.hateoas.core.AnnotationMappingDiscoverer;
@@ -53,6 +53,7 @@ import org.springframework.web.util.UriTemplate;
* @author Kevin Conaway
* @author Andrew Naydyonock
* @author Oliver Trosien
* @author Greg Turnquist
*/
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
@@ -257,11 +258,17 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
* Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with scheme tweaked in case the
* request contains an {@code X-Forwarded-Ssl} header, which is not (yet) supported by the underlying
* {@link UriComponentsBuilder}.
*
* If no {@link RequestContextHolder} exists (you're outside a Spring Web call), fall back to relative URIs.
*
* @return
*/
static UriComponentsBuilder getBuilder() {
if (RequestContextHolder.getRequestAttributes() == null) {
return UriComponentsBuilder.fromPath("/");
}
HttpServletRequest request = getCurrentRequest();
UriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);

View File

@@ -6,6 +6,8 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.hateoas.Link;
import org.springframework.web.context.request.RequestContextHolder;
/**
@@ -24,18 +26,15 @@ public class ControllerLinkBuilderOutsideSpringMvcUnitTest {
}
/**
* @see #342
* @see #408
*/
@Test(expected = IllegalStateException.class)
public void createsLinkToMethodOnParameterizedControllerRoot() {
@Test
public void requestingLinkOutsideWebRequest() {
try {
linkTo(methodOn(ControllerLinkBuilderUnitTest.PersonsAddressesController.class, 15)
.getAddressesForCountry("DE")).withSelfRel();
} catch (IllegalStateException e) {
assertThat(e.getMessage(), equalTo("Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?"));
throw e;
}
Link link = linkTo(methodOn(ControllerLinkBuilderUnitTest.PersonsAddressesController.class, 15)
.getAddressesForCountry("DE")).withSelfRel();
assertThat(link, is(new Link("/people/15/addresses/DE").withSelfRel()));
}
}

View File

@@ -28,6 +28,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.springframework.hateoas.Identifiable;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.TemplateVariable;
@@ -39,7 +40,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -53,6 +53,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* @author Greg Turnquist
* @author Kevin Conaway
* @author Oliver Trosien
* @author Greg Turnquist
*/
public class ControllerLinkBuilderUnitTest extends TestUtils {
@@ -461,22 +462,6 @@ public class ControllerLinkBuilderUnitTest extends TestUtils {
assertThat(link.getHref(), endsWith("/bar"));
}
/**
* @see #342
*/
@Test
public void mentionsRequiredUsageWithinWebRequestInException() {
exception.expect(IllegalStateException.class);
exception.expectMessage("request");
exception.expectMessage("Spring MVC");
RequestContextHolder.setRequestAttributes(null);
linkTo(methodOn(ControllerLinkBuilderUnitTest.PersonsAddressesController.class, 15).getAddressesForCountry("DE"))
.withSelfRel();
}
/**
* @see #398
*/