diff --git a/src/main/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupport.java b/src/main/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupport.java new file mode 100644 index 00000000..31fc0938 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupport.java @@ -0,0 +1,93 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.hateoas.mvc; + +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.springframework.hateoas.Identifiable; +import org.springframework.hateoas.ResourceAssembler; +import org.springframework.hateoas.ResourceSupport; +import org.springframework.util.Assert; + +/** + * Base class to implement {@link ResourceAssembler}s. Will automate {@link ResourceSupport} instance creation and make + * sure a self-link is always added. + * + * @author Oliver Gierke + */ +public abstract class IdentifiableResourceAssemblerSupport, D extends ResourceSupport> + extends ResourceAssemblerSupport { + + private final Class controllerClass; + + /** + * Creates a new {@link ResourceAssemblerSupport} using the given controller class and resource type. + * + * @param controllerClass must not be {@literal null}. + * @param resourceType must not be {@literal null}. + */ + public IdentifiableResourceAssemblerSupport(Class controllerClass, Class resourceType) { + + super(controllerClass, resourceType); + this.controllerClass = controllerClass; + } + + /** + * Creates a new resource and adds a self link to it consisting using the {@link Identifiable}'s id. + * + * @param entity must not be {@literal null}. + * @return + */ + protected D createResource(T entity) { + return createResource(entity, new Object[0]); + } + + protected D createResource(T entity, Object... parameters) { + return createResourceWithId(entity.getId(), entity, parameters); + } + + @Override + protected D createResourceWithId(Object id, T entity, Object... parameters) { + + Assert.notNull(entity); + Assert.notNull(id); + + D instance = instantiateResource(entity); + instance.add(linkTo(controllerClass, unwrapIdentifyables(parameters)).slash(id).withSelfRel()); + return instance; + } + + /** + * Extracts the ids of the given values in case they're {@link Identifiable}s. Returns all other objects as they are. + * + * @param values must not be {@literal null}. + * @return + */ + private Object[] unwrapIdentifyables(Object[] values) { + + List result = new ArrayList(values.length); + + for (Object element : Arrays.asList(values)) { + result.add(element instanceof Identifiable ? ((Identifiable) element).getId() : element); + } + + return result.toArray(); + } +} diff --git a/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java b/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java index 51aa7bbb..13a3648c 100755 --- a/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java +++ b/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java @@ -1,13 +1,13 @@ /* - * 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. - * You may obtain a coimport javax.xml.bind.annotation.XmlNs; -import javax.xml.bind.annotation.XmlSchema; - - -eed to in writing, software + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and @@ -18,11 +18,9 @@ package org.springframework.hateoas.mvc; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.springframework.beans.BeanUtils; -import org.springframework.hateoas.Identifiable; import org.springframework.hateoas.ResourceAssembler; import org.springframework.hateoas.ResourceSupport; import org.springframework.util.Assert; @@ -33,8 +31,7 @@ import org.springframework.util.Assert; * * @author Oliver Gierke */ -public abstract class ResourceAssemblerSupport, D extends ResourceSupport> implements - ResourceAssembler { +public abstract class ResourceAssemblerSupport implements ResourceAssembler { private final Class controllerClass; private final Class resourceType; @@ -73,20 +70,6 @@ public abstract class ResourceAssemblerSupport, D exte return result; } - /** - * Creates a new resource and adds a self link to it consisting using the {@link Identifiable}'s id. - * - * @param entity must not be {@literal null}. - * @return - */ - protected D createResource(T entity) { - return createResource(entity, new Object[0]); - } - - protected D createResource(T entity, Object... parameters) { - return createResourceWithId(entity.getId(), entity, parameters); - } - /** * Creates a new resource with a self link to the given id. * @@ -104,27 +87,10 @@ public abstract class ResourceAssemblerSupport, D exte Assert.notNull(id); D instance = instantiateResource(entity); - instance.add(linkTo(controllerClass, unwrapIdentifyables(parameters)).slash(id).withSelfRel()); + instance.add(linkTo(controllerClass, parameters).slash(id).withSelfRel()); return instance; } - /** - * Extracts the ids of the given values in case they're {@link Identifiable}s. Returns all other objects as they are. - * - * @param values must not be {@literal null}. - * @return - */ - private Object[] unwrapIdentifyables(Object[] values) { - - List result = new ArrayList(values.length); - - for (Object element : Arrays.asList(values)) { - result.add((element instanceof Identifiable) ? ((Identifiable) element).getId() : element); - } - - return result.toArray(); - } - /** * Instantiates the resource object. Default implementation will assume a no-arg constructor and use reflection but * can be overridden to manually set up the object instance initially (e.g. to improve performance if this becomes an diff --git a/src/test/java/org/springframework/hateoas/mvc/ResourceAssemblerSupportUnitTest.java b/src/test/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupportUnitTest.java similarity index 93% rename from src/test/java/org/springframework/hateoas/mvc/ResourceAssemblerSupportUnitTest.java rename to src/test/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupportUnitTest.java index 5f1915b8..53b6d76b 100644 --- a/src/test/java/org/springframework/hateoas/mvc/ResourceAssemblerSupportUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupportUnitTest.java @@ -32,11 +32,11 @@ import org.springframework.hateoas.TestUtils; import org.springframework.web.bind.annotation.RequestMapping; /** - * Unit tests for {@link ResourceAssemblerSupport}. + * Unit tests for {@link IdentifiableResourceAssemblerSupport}. * * @author Oliver Gierke */ -public class ResourceAssemblerSupportUnitTest extends TestUtils { +public class IdentifiableResourceAssemblerSupportUnitTest extends TestUtils { PersonResourceAssembler assembler = new PersonResourceAssembler(); Person person; @@ -124,7 +124,7 @@ public class ResourceAssemblerSupportUnitTest extends TestUtils { } - class PersonResourceAssembler extends ResourceAssemblerSupport { + class PersonResourceAssembler extends IdentifiableResourceAssemblerSupport { public PersonResourceAssembler() { this(PersonController.class);