Skip to content

Commit

Permalink
#784 - Introduce UBER+JSON mediatype.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregturn committed Jan 9, 2019
1 parent 820f661 commit 0aedb79
Show file tree
Hide file tree
Showing 40 changed files with 3,362 additions and 12 deletions.
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 @@ -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,7 +87,8 @@ public static List<String> findPropertyNames(Class<?> clazz) {
.collect(Collectors.toList());
}

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

Object obj = BeanUtils.instantiateClass(clazz);

Expand All @@ -102,7 +105,7 @@ public static Object createObjectFromProperties(Class<?> clazz, Map<String, Obje
});
});

return obj;
return (T) obj;
}

/**
Expand Down
Loading

0 comments on commit 0aedb79

Please sign in to comment.