#52 - ControllerLinkBuilder builds absolute URIs again.

Commit 8039c306e9 introduced a glitch in ControllerLinkBuilderFactory that caused URIs created not being absolute ones anymore. This commit fixes that glitch.
This commit is contained in:
Oliver Gierke
2013-03-01 12:20:11 +01:00
parent 0db1916dde
commit f568845146
4 changed files with 18 additions and 5 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
@@ -102,7 +103,10 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
Iterator<Object> classMappingParameters = invocations.getObjectParameters();
Method method = invocation.getMethod();
UriTemplate template = new UriTemplate(DISCOVERER.getMapping(method));
String mapping = DISCOVERER.getMapping(method);
UriComponentsBuilder builder = ControllerLinkBuilder.getBuilder().path(mapping);
UriTemplate template = new UriTemplate(mapping);
Map<String, Object> values = new HashMap<String, Object>();
if (classMappingParameters.hasNext()) {
@@ -112,7 +116,6 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
}
values.putAll(PATH_VARIABLE_ACCESSOR.getBoundParameters(invocation));
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(template.expand(values));
for (Entry<String, Object> param : REQUEST_PARAM_ACCESSOR.getBoundParameters(invocation).entrySet()) {
@@ -128,7 +131,8 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
}
}
return new ControllerLinkBuilder(applyUriComponentsContributer(builder, invocation));
UriComponents components = applyUriComponentsContributer(builder, invocation).buildAndExpand(values);
return new ControllerLinkBuilder(UriComponentsBuilder.fromUri(components.toUri()));
}
/*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,10 @@ public class TestUtils {
RequestContextHolder.setRequestAttributes(requestAttributes);
}
protected void assertPointsToMockServer(Link link) {
assertThat(link.getHref(), startsWith("http://localhost"));
}
public static void assertEqualAndSameHashCode(Object left, Object right) {
assertThat(left, is(right));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils {
Link link = factory.linkTo(PersonControllerImpl.class).withSelfRel();
assertPointsToMockServer(link);
assertThat(link.getRel(), is(Link.REL_SELF));
assertThat(link.getHref(), endsWith("/people"));
}
@@ -56,6 +57,7 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils {
Link link = factory.linkTo(PersonsAddressesController.class, 15).withSelfRel();
assertPointsToMockServer(link);
assertThat(link.getRel(), is(Link.REL_SELF));
assertThat(link.getHref(), endsWith("/people/15/addresses"));
}
@@ -70,6 +72,7 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils {
specialType.parameterValue = "value";
Link link = factory.linkTo(methodOn(SampleController.class).sampleMethod(1L, specialType)).withSelfRel();
assertPointsToMockServer(link);
assertThat(link.getHref(), endsWith("/sample/1?foo=value"));
}

View File

@@ -111,6 +111,7 @@ public class ControllerLinkBuilderUnitTest extends TestUtils {
public void linksToMethod() {
Link link = linkTo(methodOn(ControllerWithMethods.class).myMethod(null)).withSelfRel();
assertPointsToMockServer(link);
assertThat(link.getHref(), endsWith("/something/else"));
}
@@ -118,6 +119,7 @@ public class ControllerLinkBuilderUnitTest extends TestUtils {
public void linksToMethodWithPathVariable() {
Link link = linkTo(methodOn(ControllerWithMethods.class).methodWithPathVariable("1")).withSelfRel();
assertPointsToMockServer(link);
assertThat(link.getHref(), endsWith("/something/1/foo"));
}