package org.springframework.data.web;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
public class ProjectingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter
implements BeanClassLoaderAware, BeanFactoryAware {
private final SpelAwareProxyProjectionFactory projectionFactory;
private final Map<Class<?>, Boolean> supportedTypesCache = new ConcurrentReferenceHashMap<>();
public ProjectingJackson2HttpMessageConverter() {
this.projectionFactory = initProjectionFactory(getObjectMapper());
}
public ProjectingJackson2HttpMessageConverter(ObjectMapper mapper) {
super(mapper);
this.projectionFactory = initProjectionFactory(mapper);
}
private static SpelAwareProxyProjectionFactory initProjectionFactory(ObjectMapper mapper) {
Assert.notNull(mapper, "ObjectMapper must not be null!");
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
projectionFactory
.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper)));
return projectionFactory;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
projectionFactory.setBeanClassLoader(classLoader);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
projectionFactory.setBeanFactory(beanFactory);
}
@Override
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
if (!canRead(mediaType)) {
return false;
}
ResolvableType owner = contextClass == null ? null : ResolvableType.forClass(contextClass);
Class<?> rawType = ResolvableType.forType(type, owner).resolve(Object.class);
Boolean result = supportedTypesCache.get(rawType);
if (result != null) {
return result;
}
result = rawType.isInterface() && AnnotationUtils.findAnnotation(rawType, ProjectedPayload.class) != null;
supportedTypesCache.put(rawType, result);
return result;
}
@Override
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
return false;
}
@Override
public Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
return projectionFactory.createProjection(ResolvableType.forType(type).resolve(Object.class),
inputMessage.getBody());
}
}