Skip to content

Instantly share code, notes, and snippets.

@dpoggi
Last active September 4, 2017 02:17
Show Gist options
  • Save dpoggi/0097809621792d7e919e56a8cf55729d to your computer and use it in GitHub Desktop.
Save dpoggi/0097809621792d7e919e56a8cf55729d to your computer and use it in GitHub Desktop.
Helper class template for Spring HATEOAS in plain Java 6 and Kotlin (Apache 2.0 License)
/*
* Copyright 2017 Dan Poggi
*
* 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 com.example;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.core.EmbeddedWrapper;
import org.springframework.hateoas.core.EmbeddedWrappers;
import org.springframework.util.Assert;
public class HypermediaResources<T> {
private static final EmbeddedWrappers EMBEDDED_WRAPPERS = new EmbeddedWrappers(false);
private final Class<T> entityClass;
private final IdGetter<T> idGetter;
private final EntityLinks entityLinks;
private final EmbeddedWrapper emptyCollectionWrapper;
public HypermediaResources(Class<T> entityClass, IdGetter<T> idGetter, EntityLinks entityLinks) {
Assert.notNull(entityClass, "EntityClass must not be null");
Assert.notNull(idGetter, "IdGetter must not be null");
Assert.notNull(entityLinks, "EntityLinks must not be null");
this.entityClass = entityClass;
this.idGetter = idGetter;
this.entityLinks = entityLinks;
this.emptyCollectionWrapper = EMBEDDED_WRAPPERS.emptyCollectionOf(entityClass);
}
public Resources<EmbeddedWrapper> wrap(Collection<? extends T> entities) {
Assert.notNull(entities, "Entities must not be null");
EmbeddedWrapper embeddedWrapper;
if (!entities.isEmpty()) {
Collection<Resource<T>> resources = new ArrayList<Resource<T>>(entities.size());
for (T entity : entities) {
resources.add(resourceOf(entity));
}
embeddedWrapper = EMBEDDED_WRAPPERS.wrap(resources);
} else {
embeddedWrapper = this.emptyCollectionWrapper;
}
Iterable<EmbeddedWrapper> content = Collections.singletonList(embeddedWrapper);
Link link = this.entityLinks.linkToCollectionResource(this.entityClass);
return new Resources<EmbeddedWrapper>(content, link);
}
public Resource<T> resourceOf(T entity) {
Assert.notNull(entity, "Entity must not be null");
Object id = this.idGetter.getId(entity);
Link link = this.entityLinks.linkToSingleResource(this.entityClass, id);
return new Resource<T>(entity, link);
}
public interface IdGetter<T> {
Object getId(T entity);
}
}
/*
* Copyright 2017 Dan Poggi
*
* 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 com.example
import kotlin.reflect.KClass
import org.springframework.hateoas.*
import org.springframework.hateoas.core.EmbeddedWrapper
import org.springframework.hateoas.core.EmbeddedWrappers
class HypermediaResources<T : Any>(
entityClass: KClass<T>,
private val idGetter: T.() -> Any,
private val entityLinks: EntityLinks) {
private companion object {
private val embeddedWrappers = EmbeddedWrappers(false)
}
private val entityClass = entityClass.java
private val emptyCollectionWrapper = embeddedWrappers.emptyCollectionOf(this.entityClass)
fun wrap(entities: Collection<T>): Resources<EmbeddedWrapper> {
val embeddedWrapper = if (entities.isNotEmpty()) {
embeddedWrappers.wrap(entities.map { resourceOf(it) })
} else {
emptyCollectionWrapper
}
val link = entityLinks.linkToCollectionResource(entityClass)
return Resources(listOf(embeddedWrapper), link)
}
fun resourceOf(entity: T): Resource<T> {
val link = entityLinks.linkToSingleResource(entityClass, entity.idGetter())
return Resource(entity, link)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment