|
| 1 | +/* |
| 2 | + * Copyright 2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.hateoas.mvc; |
| 17 | + |
| 18 | +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; |
| 19 | + |
| 20 | +import org.springframework.core.GenericTypeResolver; |
| 21 | +import org.springframework.hateoas.Identifiable; |
| 22 | +import org.springframework.hateoas.Link; |
| 23 | +import org.springframework.hateoas.LinkBuilder; |
| 24 | +import org.springframework.hateoas.RelProvider; |
| 25 | +import org.springframework.hateoas.Resource; |
| 26 | +import org.springframework.hateoas.Resources; |
| 27 | +import org.springframework.hateoas.SimpleResourceAssembler; |
| 28 | +import org.springframework.hateoas.core.EvoInflectorRelProvider; |
| 29 | + |
| 30 | +/** |
| 31 | + * Using a Spring MVC-based {@link ControllerLinkBuilder} and an {@link Identifiable} domain object, construct |
| 32 | + * a {@link SimpleResourceAssembler}. |
| 33 | + * |
| 34 | + * @author Greg Turnquist |
| 35 | + */ |
| 36 | +public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> extends SimpleResourceAssembler<T> { |
| 37 | + |
| 38 | + /** |
| 39 | + * The Spring MVC class for the {@link Identifiable} from which links will be built. |
| 40 | + */ |
| 41 | + private final Class<?> controllerClass; |
| 42 | + |
| 43 | + /** |
| 44 | + * A {@link RelProvider} to look up names of links as options for resource paths. |
| 45 | + */ |
| 46 | + private final RelProvider relProvider; |
| 47 | + |
| 48 | + /** |
| 49 | + * A {@link Class} depicting the {@link Identifiable}'s type. |
| 50 | + */ |
| 51 | + private final Class<?> resourceType; |
| 52 | + |
| 53 | + /** |
| 54 | + * Default base path as empty. |
| 55 | + */ |
| 56 | + private String basePath = ""; |
| 57 | + |
| 58 | + /** |
| 59 | + * Default assembler based on a Spring MVC controller, resource type, and {@link RelProvider}. With this combination |
| 60 | + * of information, resources can be defined. |
| 61 | + * |
| 62 | + * @see #setBasePath(String) to adjust base path to something like "/api"/ |
| 63 | + * |
| 64 | + * @param controllerClass - Spring MVC controller to base links off of |
| 65 | + * @param relProvider - generates the links items |
| 66 | + */ |
| 67 | + public SimpleIdentifiableResourceAssembler(Class<?> controllerClass, RelProvider relProvider) { |
| 68 | + |
| 69 | + this.controllerClass = controllerClass; |
| 70 | + this.relProvider = relProvider; |
| 71 | + |
| 72 | + // Find the "T" type contained in "T extends Identifiable<?>", e.g. SimpleIdentifiableResourceAssembler<User> -> User |
| 73 | + this.resourceType = GenericTypeResolver.resolveTypeArgument(this.getClass(), SimpleIdentifiableResourceAssembler.class); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Alternate constructor that falls back to {@link EvoInflectorRelProvider}. |
| 78 | + * |
| 79 | + * @param controllerClass |
| 80 | + */ |
| 81 | + public SimpleIdentifiableResourceAssembler(Class<?> controllerClass) { |
| 82 | + this(controllerClass, new EvoInflectorRelProvider()); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Define links to add to every {@link Resource}. |
| 87 | + * |
| 88 | + * @param resource |
| 89 | + */ |
| 90 | + @Override |
| 91 | + protected void addLinks(Resource<T> resource) { |
| 92 | + |
| 93 | + Link resourceSelfLink = getCollectionLinkBuilder().slash(resource.getContent()).withSelfRel(); |
| 94 | + Link resourceCollectionLink = getCollectionLinkBuilder().withRel(this.relProvider.getCollectionResourceRelFor(this.resourceType)); |
| 95 | + |
| 96 | + resource.add(resourceSelfLink, resourceCollectionLink); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Define links to add to {@link Resources} collection. |
| 101 | + * |
| 102 | + * @param resources |
| 103 | + */ |
| 104 | + @Override |
| 105 | + protected void addLinks(Resources<Resource<T>> resources) { |
| 106 | + |
| 107 | + Link resourceCollectionLink = getCollectionLinkBuilder().withSelfRel(); |
| 108 | + resources.add(resourceCollectionLink); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Build up a URI for the collection using the Spring MVC controller followed by the resource type transformed |
| 113 | + * by the {@link RelProvider}. |
| 114 | + * |
| 115 | + * Assumption is that a {@literal EmployeeController} serving up {@literal Employee} |
| 116 | + * objects will be serving resources at {@code /employees} and {@code /employees/1} respectively. |
| 117 | + * |
| 118 | + * If this is not the case, simply override this method in your concrete instance, or simply resort to |
| 119 | + * overriding {@link #addLinks(Resource)} and {@link #addLinks(Resources)} where you have full control over exactly |
| 120 | + * what links are put in the individual and collection resources. |
| 121 | + * |
| 122 | + * @return |
| 123 | + */ |
| 124 | + protected LinkBuilder getCollectionLinkBuilder() { |
| 125 | + |
| 126 | + String[] pathComponents = (getPrefix() + this.relProvider.getCollectionResourceRelFor(this.resourceType)).split("/"); |
| 127 | + |
| 128 | + ControllerLinkBuilder linkBuilder = linkTo(this.controllerClass); |
| 129 | + |
| 130 | + for (String pathComponent : pathComponents) { |
| 131 | + if (!pathComponent.isEmpty()) { |
| 132 | + linkBuilder = linkBuilder.slash(pathComponent); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return linkBuilder; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Construct the prefix to a resource, with {@literal "/"} being the basis for no prefix at all. |
| 141 | + * |
| 142 | + * @return |
| 143 | + */ |
| 144 | + private String getPrefix() { |
| 145 | + return getBasePath().isEmpty() ? "" : getBasePath() + "/"; |
| 146 | + } |
| 147 | + |
| 148 | + public String getBasePath() { |
| 149 | + return this.basePath; |
| 150 | + } |
| 151 | + |
| 152 | + public void setBasePath(String basePath) { |
| 153 | + this.basePath = basePath; |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | +} |
0 commit comments