package com.fasterxml.jackson.jaxrs.cfg;
import java.lang.annotation.Annotation;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.jaxrs.annotation.JacksonFeatures;
public abstract class EndpointConfigBase<THIS extends EndpointConfigBase<THIS>>
{
protected final MapperConfig<?> _config;
protected Class<?> _activeView;
protected String _rootName;
protected DeserializationFeature[] _deserEnable;
protected DeserializationFeature[] _deserDisable;
protected ObjectReader _reader;
protected SerializationFeature[] _serEnable;
protected SerializationFeature[] _serDisable;
protected ObjectWriter _writer;
protected EndpointConfigBase(MapperConfig<?> config) {
_config = config;
}
@Deprecated
protected EndpointConfigBase() {
_config = null;
}
@SuppressWarnings("unchecked")
protected THIS add(Annotation[] annotations, boolean forWriting)
{
if (annotations != null) {
for (Annotation annotation : annotations) {
addAnnotation(annotation.annotationType(), annotation, forWriting);
}
}
return (THIS) this;
}
protected void addAnnotation(Class<? extends Annotation> type,
Annotation annotation, boolean forWriting)
{
if (type == JsonView.class) {
Class<?>[] views = ((JsonView) annotation).value();
_activeView = (views.length > 0) ? views[0] : null;
} else if (type == JacksonFeatures.class) {
JacksonFeatures feats = (JacksonFeatures) annotation;
if (forWriting) {
_serEnable = nullIfEmpty(feats.serializationEnable());
_serDisable = nullIfEmpty(feats.serializationDisable());
} else {
_deserEnable = nullIfEmpty(feats.deserializationEnable());
_deserDisable = nullIfEmpty(feats.deserializationDisable());
}
} else if (type == JsonRootName.class) {
_rootName = ((JsonRootName) annotation).value();
} else if (type == JacksonAnnotationsInside.class) {
} else {
JacksonAnnotationsInside inside = type.getAnnotation(JacksonAnnotationsInside.class);
if (inside != null) {
add(type.getAnnotations(), forWriting);
}
}
}
@SuppressWarnings("unchecked")
protected THIS initReader(ObjectReader reader)
{
if (_activeView != null) {
reader = reader.withView(_activeView);
}
if (_rootName != null) {
reader = reader.withRootName(_rootName);
}
if (_deserEnable != null) {
reader = reader.withFeatures(_deserEnable);
}
if (_deserDisable != null) {
reader = reader.withoutFeatures(_deserDisable);
}
_reader = reader;
return (THIS) this;
}
@SuppressWarnings("unchecked")
protected THIS initWriter(ObjectWriter writer)
{
if (_activeView != null) {
writer = writer.withView(_activeView);
}
if (_rootName != null) {
writer = writer.withRootName(_rootName);
}
if (_serEnable != null) {
writer = writer.withFeatures(_serEnable);
}
if (_serDisable != null) {
writer = writer.withoutFeatures(_serDisable);
}
_writer = writer;
return (THIS) this;
}
public String getRootName() {
return _rootName;
}
public Class<?> getActiveView() {
return _activeView;
}
public final ObjectReader getReader() {
if (_reader == null) {
throw new IllegalStateException();
}
return _reader;
}
public final ObjectWriter getWriter() {
if (_writer == null) {
throw new IllegalStateException();
}
return _writer;
}
public abstract Object modifyBeforeWrite(Object value);
protected static <T> T[] nullIfEmpty(T[] arg) {
if (arg == null || arg.length == 0) {
return null;
}
return arg;
}
}