diff --git a/src/main/java/org/springframework/hateoas/LinkBuilderFactory.java b/src/main/java/org/springframework/hateoas/LinkBuilderFactory.java index 6a09a66f..d9481ba0 100644 --- a/src/main/java/org/springframework/hateoas/LinkBuilderFactory.java +++ b/src/main/java/org/springframework/hateoas/LinkBuilderFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2016 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. @@ -21,6 +21,8 @@ import java.util.Map; * Factory for {@link LinkBuilder} instances. * * @author Ricardo Gladwell + * @author Andrew Naydyonock + * @author Oliver Gierke */ public interface LinkBuilderFactory { diff --git a/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilder.java b/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilder.java index b2492169..ed11de18 100644 --- a/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilder.java +++ b/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2016 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. @@ -15,23 +15,25 @@ */ package org.springframework.hateoas.jaxrs; +import java.util.Map; + import javax.ws.rs.Path; import org.springframework.hateoas.LinkBuilder; import org.springframework.hateoas.core.AnnotationMappingDiscoverer; import org.springframework.hateoas.core.LinkBuilderSupport; import org.springframework.hateoas.core.MappingDiscoverer; +import org.springframework.util.Assert; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; -import java.util.Map; - /** * {@link LinkBuilder} to derive URI mappings from a JAX-RS {@link Path} annotation. * * @author Oliver Gierke * @author Kamill Sokol + * @author Andrew Naydyonock */ public class JaxRsLinkBuilder extends LinkBuilderSupport { @@ -60,36 +62,40 @@ public class JaxRsLinkBuilder extends LinkBuilderSupport { * Creates a new {@link JaxRsLinkBuilder} instance to link to the {@link Path} mapping tied to the given class binding * the given parameters to the URI template. * - * @param service the class to discover the annotation on, must not be {@literal null}. + * @param resourceType the class to discover the annotation on, must not be {@literal null}. * @param parameters additional parameters to bind to the URI template declared in the annotation, must not be * {@literal null}. * @return */ - public static JaxRsLinkBuilder linkTo(Class service, Object... parameters) { + public static JaxRsLinkBuilder linkTo(Class resourceType, Object... parameters) { - JaxRsLinkBuilder builder = new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping()); + Assert.notNull(resourceType, "Controller type must not be null!"); + Assert.notNull(parameters, "Parameters must not be null!"); - UriComponents uriComponents = UriComponentsBuilder.fromUriString(DISCOVERER.getMapping(service)).build(); + UriComponents uriComponents = UriComponentsBuilder.fromUriString(DISCOVERER.getMapping(resourceType)).build(); UriComponents expandedComponents = uriComponents.expand(parameters); - return builder.slash(expandedComponents); + + return new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping()).slash(expandedComponents); } /** * Creates a new {@link JaxRsLinkBuilder} instance to link to the {@link Path} mapping tied to the given class binding * the given parameters to the URI template. * - * @param service the class to discover the annotation on, must not be {@literal null}. + * @param resourceType the class to discover the annotation on, must not be {@literal null}. * @param parameters map of additional parameters to bind to the URI template declared in the annotation, must not be * {@literal null}. * @return */ - public static JaxRsLinkBuilder linkTo(Class service, Map parameters) { + public static JaxRsLinkBuilder linkTo(Class resourceType, Map parameters) { - JaxRsLinkBuilder builder = new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping()); + Assert.notNull(resourceType, "Controller type must not be null!"); + Assert.notNull(parameters, "Parameters must not be null!"); - UriComponents uriComponents = UriComponentsBuilder.fromUriString(DISCOVERER.getMapping(service)).build(); + UriComponents uriComponents = UriComponentsBuilder.fromUriString(DISCOVERER.getMapping(resourceType)).build(); UriComponents expandedComponents = uriComponents.expand(parameters); - return builder.slash(expandedComponents); + + return new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping()).slash(expandedComponents); } /* diff --git a/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactory.java b/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactory.java index 30930dd1..c80ae0d2 100644 --- a/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactory.java +++ b/src/main/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2016 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. @@ -15,16 +15,17 @@ */ package org.springframework.hateoas.jaxrs; +import java.util.Map; + import org.springframework.hateoas.LinkBuilder; import org.springframework.hateoas.LinkBuilderFactory; -import java.util.Map; - /** * Factory for {@link LinkBuilder} instances based on the path mapping annotated on the given JAX-RS service. * * @author Ricardo Gladwell * @author Oliver Gierke + * @author Andrew Naydyonock */ public class JaxRsLinkBuilderFactory implements LinkBuilderFactory { diff --git a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java index 053b8a7d..a71d057a 100755 --- a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java +++ b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java @@ -19,7 +19,7 @@ import static org.springframework.util.StringUtils.*; import java.lang.reflect.Method; import java.net.URI; -import java.util.Map; +import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -45,6 +45,7 @@ import org.springframework.web.util.UriTemplate; * @author Kamill Sokol * @author Greg Turnquist * @author Kevin Conaway + * @author Andrew Naydyonock */ public class ControllerLinkBuilder extends LinkBuilderSupport { @@ -91,45 +92,44 @@ public class ControllerLinkBuilder extends LinkBuilderSupport controller, Object... parameters) { - Assert.notNull(controller); + Assert.notNull(controller, "Controller must not be null!"); + Assert.notNull(parameters, "Parameters must not be null!"); - ControllerLinkBuilder builder = new ControllerLinkBuilder(getBuilder()); String mapping = DISCOVERER.getMapping(controller); UriComponents uriComponents = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping).build(); UriComponents expandedComponents = uriComponents.expand(parameters); - return builder.slash(expandedComponents); + return new ControllerLinkBuilder(getBuilder()).slash(expandedComponents); + } + + /** + * Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class. + * Parameter map is used to fill up potentially available path variables in the class scope request mapping. + * + * @param controller the class to discover the annotation on, must not be {@literal null}. + * @param parameters additional parameters to bind to the URI template declared in the annotation, must not be + * {@literal null}. + * @return + */ + public static ControllerLinkBuilder linkTo(Class controller, Map parameters) { + + Assert.notNull(controller, "Controller must not be null!"); + Assert.notNull(parameters, "Parameters must not be null!"); + + String mapping = DISCOVERER.getMapping(controller); + + UriComponents uriComponents = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping).build(); + UriComponents expandedComponents = uriComponents.expand(parameters); + + return new ControllerLinkBuilder(getBuilder()).slash(expandedComponents); } - /** - * Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class. - * Parameter map is used to fill up potentially available path variables in the class scope request - * mapping. - * - * @param controller the class to discover the annotation on, must not be {@literal null}. - * @param parameters additional parameters to bind to the URI template declared in the annotation, must not be - * {@literal null}. - * @return - */ - public static ControllerLinkBuilder linkTo(Class controller, Map parameters) { - - Assert.notNull(controller); - - ControllerLinkBuilder builder = new ControllerLinkBuilder(getBuilder()); - String mapping = DISCOVERER.getMapping(controller); - - UriComponents uriComponents = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping).build(); - UriComponents expandedComponents = uriComponents.expand(parameters); - - return builder.slash(expandedComponents); - } - /* * @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(Method, Object...) */ public static ControllerLinkBuilder linkTo(Method method, Object... parameters) { - return linkTo(method.getDeclaringClass(), method, parameters); + return linkTo(method.getDeclaringClass(), method, parameters); } /* diff --git a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java index 1c3084bc..a2e5ce85 100644 --- a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java +++ b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java @@ -56,6 +56,7 @@ import org.springframework.web.util.UriTemplate; * @author Ross Turner * @author Oemer Yildiz * @author Kevin Conaway + * @author Andrew Naydyonock */ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory { diff --git a/src/test/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactoryUnitTest.java b/src/test/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactoryUnitTest.java index d49511f2..910b4fc2 100644 --- a/src/test/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactoryUnitTest.java +++ b/src/test/java/org/springframework/hateoas/jaxrs/JaxRsLinkBuilderFactoryUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2014 the original author or authors. + * Copyright 2011-2016 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. @@ -18,21 +18,21 @@ package org.springframework.hateoas.jaxrs; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import java.util.Collections; + import javax.ws.rs.Path; import org.junit.Test; import org.springframework.hateoas.Link; import org.springframework.hateoas.TestUtils; -import java.util.LinkedHashMap; -import java.util.Map; - /** * Unit test for {@link JaxRsLinkBuilderFactory}. * * @author Ricardo Gladwell * @author Oliver Gierke * @author Kamill Sokol + * @author Andrew Naydyonock */ public class JaxRsLinkBuilderFactoryUnitTest extends TestUtils { @@ -68,12 +68,13 @@ public class JaxRsLinkBuilderFactoryUnitTest extends TestUtils { assertThat(link.getHref(), endsWith("/people/with%20blank/addresses")); } + /** + * @see #372 + */ @Test public void createsLinkToParameterizedServiceRootWithParameterMap() { - Map pathParams = new LinkedHashMap(); - pathParams.put("id", "17"); - Link link = factory.linkTo(PersonsAddressesService.class, pathParams).withSelfRel(); + Link link = factory.linkTo(PersonsAddressesService.class, Collections.singletonMap("id", "17")).withSelfRel(); assertThat(link.getRel(), is(Link.REL_SELF)); assertThat(link.getHref(), endsWith("/people/17/addresses")); diff --git a/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java b/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java index 9c8c0734..8989e885 100644 --- a/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; import java.util.Arrays; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -74,18 +75,6 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils { assertThat(link.getHref(), endsWith("/people/15/addresses")); } - @Test - public void createsLinkToParameterizedControllerRootWithParameterMap() { - Map pathParams = new LinkedHashMap(); - pathParams.put("id", "17"); - - Link link = factory.linkTo(PersonsAddressesController.class, pathParams).withSelfRel(); - - assertPointsToMockServer(link); - assertThat(link.getRel(), is(Link.REL_SELF)); - assertThat(link.getHref(), endsWith("/people/17/addresses")); - } - @Test public void appliesParameterValueIfContributorConfigured() { @@ -172,6 +161,19 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils { endsWith("/sample/multivaluemapsupport?key1=value1a&key1=value1b&key2=value2a&key2=value2b")); } + /** + * @see #372 + */ + @Test + public void createsLinkToParameterizedControllerRootWithParameterMap() { + + Link link = factory.linkTo(PersonsAddressesController.class, Collections.singletonMap("id", "17")).withSelfRel(); + + assertPointsToMockServer(link); + assertThat(link.getRel(), is(Link.REL_SELF)); + assertThat(link.getHref(), endsWith("/people/17/addresses")); + } + static interface SampleController { @RequestMapping("/sample/{id}")