Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#784 - Introduce UBER+JSON mediatype. #785

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/springframework/hateoas/MediaTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ public class MediaTypes {
* Public constant media type for {@code application/vnd.collection+json}.
*/
public static final MediaType COLLECTION_JSON = MediaType.valueOf(COLLECTION_JSON_VALUE);

/**
* A String equivalent of {@link MediaTypes#UBER_JSON_VALUE}.
*/
public static final String UBER_JSON_VALUE = "application/vnd.amundsen-uber+json";

/**
* Public constant media type for {@code application/vnd.amundsen-uber+json}.
*/
public static final MediaType UBER_JSON = MediaType.parseMediaType(UBER_JSON_VALUE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public Optional<Link> getId() {
* @param link
*/
public void add(Link link) {

Assert.notNull(link, "Link must not be null!");

this.links.add(link);
}

Expand All @@ -65,7 +67,9 @@ public void add(Link link) {
* @param links
*/
public void add(Iterable<Link> links) {

Assert.notNull(links, "Given links must not be null!");

links.forEach(this::add);
}

Expand All @@ -75,7 +79,9 @@ public void add(Iterable<Link> links) {
* @param links must not be {@literal null}.
*/
public void add(Link... links) {

Assert.notNull(links, "Given links must not be null!");

add(Arrays.asList(links));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.springframework.hateoas.hal.forms.Jackson2HalFormsModule;
import org.springframework.hateoas.hal.forms.Jackson2HalFormsModule.HalFormsHandlerInstantiator;
import org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
import org.springframework.hateoas.uber.Jackson2UberModule;
import org.springframework.hateoas.uber.Jackson2UberModule.UberHandlerInstantiator;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
Expand Down Expand Up @@ -96,7 +98,7 @@ public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
if (converters.stream()
.filter(MappingJackson2HttpMessageConverter.class::isInstance)
.map(AbstractJackson2HttpMessageConverter.class::cast)
.map(converter -> converter.getObjectMapper())
.map(AbstractJackson2HttpMessageConverter::getObjectMapper)
.anyMatch(Jackson2HalModule::isAlreadyRegisteredIn)) {

return;
Expand Down Expand Up @@ -124,8 +126,29 @@ public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
if (hypermediaTypes.contains(HypermediaType.COLLECTION_JSON)) {
converters.add(0, createCollectionJsonConverter(objectMapper, linkRelationMessageSource));
}

if (hypermediaTypes.contains(HypermediaType.UBER)) {
converters.add(0, createUberJsonConverter(objectMapper));
}
}

/**
* @param objectMapper
* @return
*/
protected MappingJackson2HttpMessageConverter createUberJsonConverter(ObjectMapper objectMapper) {

ObjectMapper mapper = objectMapper.copy();

mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.registerModule(new Jackson2UberModule());
mapper.setHandlerInstantiator(new UberHandlerInstantiator());

return new TypeConstrainedMappingJackson2HttpMessageConverter(
ResourceSupport.class, Arrays.asList(UBER_JSON), mapper);
}


/**
* @param objectMapper
* @param linkRelationMessageSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ enum HypermediaType {
*
* @see http://amundsen.com/media-types/collection/format/
*/
COLLECTION_JSON;
COLLECTION_JSON,

/**
* UBER Hypermedia
*
* @see http://uberhypermedia.org/
*/
UBER;

private static Set<HypermediaType> HAL_BASED_MEDIATYPES = EnumSet.of(HAL, HAL_FORMS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.hal.HalLinkDiscoverer;
import org.springframework.hateoas.hal.forms.HalFormsLinkDiscoverer;
import org.springframework.hateoas.uber.UberLinkDiscoverer;
import org.springframework.util.ClassUtils;

/**
Expand Down Expand Up @@ -96,6 +97,9 @@ private AbstractBeanDefinition getLinkDiscovererBeanDefinition(HypermediaType ty
case COLLECTION_JSON:
definition = new RootBeanDefinition(CollectionJsonLinkDiscoverer.class);
break;
case UBER:
definition = new RootBeanDefinition(UberLinkDiscoverer.class);
break;
default:
throw new IllegalStateException(String.format("Unsupported hypermedia type %s!", type));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.springframework.hateoas.support;

import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;

import com.fasterxml.jackson.databind.JavaType;

/**
Expand All @@ -36,4 +39,18 @@ public static JavaType findRootType(JavaType contentType) {
return contentType;
}
}

/**
* Is this a {@literal Resources<Resource<?>>}?
*
* @param type
* @return
*/
public static boolean isResourcesOfResource(JavaType type) {

return
Resources.class.isAssignableFrom(type.getRawClass())
&&
Resource.class.isAssignableFrom(type.containedType(0).getRawClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public static Map<String, Object> findProperties(Object object) {
.collect(HashMap::new,
(hashMap, descriptor) -> {
try {
hashMap.put(descriptor.getName(), descriptor.getReadMethod().invoke(object));
Method readMethod = descriptor.getReadMethod();
ReflectionUtils.makeAccessible(readMethod);
hashMap.put(descriptor.getName(), readMethod.invoke(object));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
Expand All @@ -85,9 +87,9 @@ public static List<String> findPropertyNames(Class<?> clazz) {
.collect(Collectors.toList());
}

public static Object createObjectFromProperties(Class<?> clazz, Map<String, Object> properties) {
public static <T> T createObjectFromProperties(Class<T> clazz, Map<String, Object> properties) {

Object obj = BeanUtils.instantiateClass(clazz);
T obj = BeanUtils.instantiateClass(clazz);

properties.forEach((key, value) -> {
Optional<PropertyDescriptor> possibleProperty = Optional.ofNullable(BeanUtils.getPropertyDescriptor(clazz, key));
Expand Down
Loading