Polish affordances section.

This commit is contained in:
Greg Turnquist
2019-02-04 10:24:44 -06:00
parent 2723283640
commit e315d878f6
7 changed files with 32 additions and 336 deletions

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2017 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;
/**
* Analogous to {@link ResourceAssembler} but for resource collections.
*
* @author Greg Turnquist
*/
public interface ResourcesAssembler<T, D extends ResourceSupport> {
/**
* Converts all given entities into resources and wraps the collection as a resource as well.
*
* @see ResourceAssembler#toResource(Object)
* @param entities must not be {@literal null}.
* @return {@link Resources} containing {@link Resource} of {@code T}.
*/
Resources<D> toResources(Iterable<? extends T> entities);
}

View File

@@ -25,9 +25,12 @@ import org.springframework.hateoas.core.EvoInflectorRelProvider;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
/**
* A {@link SimpleResourceAssembler} that mixes together a Spring web controller and a {@link RelProvider} to build links
* upon a certain strategy.
*
* @author Greg Turnquist
*/
public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> extends SimpleResourceAssembler<T> {
public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> implements SimpleResourceAssembler<T> {
/**
* The Spring MVC class for the {@link Identifiable} from which links will be built.
@@ -77,35 +80,36 @@ public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> exte
}
/**
* Define links to add to every {@link Resource}.
* Add single item self link based on {@link Identifiable} and link back to aggregate root of the {@literal T} domain
* type using {@link RelProvider#getCollectionResourceRelFor(Class)}}.
*
* @param resource
*/
@Override
protected void addLinks(Resource<T> resource) {
public void addLinks(Resource<T> resource) {
resource.add(getCollectionLinkBuilder().slash(resource.getContent()).withSelfRel());
resource.add(getCollectionLinkBuilder().withRel(this.relProvider.getCollectionResourceRelFor(this.resourceType)));
}
/**
* Define links to add to {@link Resources} collection.
* Add a self link to the aggregate root.
*
* @param resources
*/
@Override
protected void addLinks(Resources<Resource<T>> resources) {
public void addLinks(Resources<Resource<T>> resources) {
resources.add(getCollectionLinkBuilder().withSelfRel());
}
/**
* Build up a URI for the collection using the Spring MVC controller followed by the resource type transformed
* Build up a URI for the collection using the Spring web controller followed by the resource type transformed
* by the {@link RelProvider}.
*
* Assumption is that an {@link org.springframework.hateoas.examples.EmployeeController} serving up {@link org.springframework.hateoas.examples.Employee}
* Assumption is that an {@literal EmployeeController} serving up {@literal Employee}
* objects will be serving resources at {@code /employees} and {@code /employees/1}.
*
* If this is not the case, simply override this method in your concrete instance, or simply resort to
* If this is not the case, simply override this method in your concrete instance, or resort to
* overriding {@link #addLinks(Resource)} and {@link #addLinks(Resources)} where you have full control over exactly
* what links are put in the individual and collection resources.
*
@@ -124,6 +128,9 @@ public class SimpleIdentifiableResourceAssembler<T extends Identifiable<?>> exte
return linkBuilder;
}
/**
* Provide opportunity to override the base path for the URI.
*/
private String getPrefix() {
return getBasePath().isEmpty() ? "" : getBasePath() + "/";
}

View File

@@ -1,88 +0,0 @@
/*
* Copyright 2017 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;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.Assert;
/**
* A {@link ResourceAssembler}/{@link ResourcesAssembler} that focuses purely on the domain type,
* returning back {@link Resource} and {@link Resources} for that type instead of
* {@link org.springframework.hateoas.ResourceSupport}.
*
* @author Greg Turnquist
*/
public class SimpleResourceAssembler<T> implements ResourceAssembler<T, Resource<T>>, ResourcesAssembler<T, Resource<T>> {
/**
* Converts the given entity into a {@link Resource}.
*
* @param entity
* @return
*/
@Override
public Resource<T> toResource(T entity) {
Resource<T> resource = new Resource<T>(entity);
addLinks(resource);
return resource;
}
/**
* Converts all given entities into resources and wraps the collection as a resource as well.
*
* @see #toResource(Object)
* @param entities must not be {@literal null}.
* @return {@link Resources} containing {@link Resource} of {@code T}.
*/
public Resources<Resource<T>> toResources(Iterable<? extends T> entities) {
Assert.notNull(entities, "Entities must not be null!");
List<Resource<T>> result = new ArrayList<Resource<T>>();
for (T entity : entities) {
result.add(toResource(entity));
}
Resources<Resource<T>> resources = new Resources<>(result);
addLinks(resources);
return resources;
}
/**
* Define links to add to every individual {@link Resource}.
*
* @param resource
*/
protected void addLinks(Resource<T> resource) {
// Default adds no links
}
/**
* Define links to add to the {@link Resources} collection.
*
* @param resources
*/
protected void addLinks(Resources<Resource<T>> resources) {
// Default adds no links.
}
}