package org.glassfish.jersey.moxy.json.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.glassfish.jersey.message.filtering.spi.ObjectProvider;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.ObjectGraph;
@Singleton
public class FilteringMoxyJsonProvider extends ConfigurableMoxyJsonProvider {
@Inject
private Provider<ObjectProvider<ObjectGraph>> provider;
@Override
protected void preWriteTo(final Object object, final Class<?> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
final Marshaller marshaller) throws JAXBException {
super.preWriteTo(object, type, genericType, annotations, mediaType, httpHeaders, marshaller);
if (marshaller.getProperty(MarshallerProperties.OBJECT_GRAPH) == null) {
final Object objectGraph = provider.get().getFilteringObject(genericType, true, annotations);
if (objectGraph != null) {
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, objectGraph);
}
}
}
@Override
protected void preReadFrom(final Class<Object> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType, final MultivaluedMap<String, String> httpHeaders,
final Unmarshaller unmarshaller) throws JAXBException {
super.preReadFrom(type, genericType, annotations, mediaType, httpHeaders, unmarshaller);
if (unmarshaller.getProperty(MarshallerProperties.OBJECT_GRAPH) == null) {
final Object objectGraph = provider.get().getFilteringObject(genericType, false, annotations);
if (objectGraph != null) {
unmarshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, objectGraph);
}
}
}
}