Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract out JsonEnvironment.

  • Loading branch information...
commit 03bc4d07609075c6a605fc741ca93677c566d569 1 parent 47b07a0
@codahale codahale authored
View
2  dropwizard-client/src/main/java/com/yammer/dropwizard/client/JerseyClientBuilder.java
@@ -177,7 +177,7 @@ public Client build() {
configuration.getMaxThreads(),
60,
TimeUnit.SECONDS),
- environment.getObjectMapperFactory().build());
+ environment.getJsonEnvironment().buildObjectMapper());
}
private Client build(ExecutorService threadPool,
View
18 ...zard-client/src/test/java/com/yammer/dropwizard/client/tests/JerseyClientBuilderTest.java
@@ -8,7 +8,7 @@
import com.yammer.dropwizard.client.JerseyClientConfiguration;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.jersey.JacksonMessageBodyProvider;
-import com.yammer.dropwizard.json.ObjectMapperFactory;
+import com.yammer.dropwizard.setup.JsonEnvironment;
import com.yammer.dropwizard.setup.LifecycleEnvironment;
import org.junit.Before;
import org.junit.Test;
@@ -49,6 +49,7 @@ public JerseyClientBuilderTest readFrom(Class<JerseyClientBuilderTest> type, Typ
private static final Annotation[] NO_ANNOTATIONS = new Annotation[0];
private final JerseyClientBuilder builder = new JerseyClientBuilder();
+ private final JsonEnvironment jsonEnvironment = mock(JsonEnvironment.class);
private final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class);
private final Environment environment = mock(Environment.class);
private final ExecutorService executorService = mock(ExecutorService.class);
@@ -57,6 +58,7 @@ public JerseyClientBuilderTest readFrom(Class<JerseyClientBuilderTest> type, Typ
@Before
public void setUp() throws Exception {
when(environment.getLifecycleEnvironment()).thenReturn(lifecycleEnvironment);
+ when(environment.getJsonEnvironment()).thenReturn(jsonEnvironment);
}
@Test
@@ -178,10 +180,8 @@ public void usesAnObjectMapperFromTheEnvironment() throws Exception {
configuration.getMaxThreads(),
60,
TimeUnit.SECONDS)).thenReturn(executorService);
- final ObjectMapperFactory factory = mock(ObjectMapperFactory.class);
- when(factory.build()).thenReturn(objectMapper);
- when(environment.getObjectMapperFactory()).thenReturn(factory);
+ when(jsonEnvironment.buildObjectMapper()).thenReturn(objectMapper);
final Client client = builder.using(environment).build();
@@ -203,11 +203,13 @@ public void usesAnExecutorServiceFromTheEnvironment() throws Exception {
configuration.setMinThreads(7);
configuration.setMaxThreads(532);
- when(lifecycleEnvironment.managedExecutorService("jersey-client-%d", 7, 532, 60, TimeUnit.SECONDS)).thenReturn(executorService);
- final ObjectMapperFactory factory = mock(ObjectMapperFactory.class);
- when(factory.build()).thenReturn(objectMapper);
+ when(lifecycleEnvironment.managedExecutorService("jersey-client-%d",
+ 7,
+ 532,
+ 60,
+ TimeUnit.SECONDS)).thenReturn(executorService);
- when(environment.getObjectMapperFactory()).thenReturn(factory);
+ when(jsonEnvironment.buildObjectMapper()).thenReturn(objectMapper);
final ApacheHttpClient4 client = (ApacheHttpClient4) builder.using(configuration)
.using(environment).build();
View
21 dropwizard-core/src/main/java/com/yammer/dropwizard/config/Environment.java
@@ -1,14 +1,12 @@
package com.yammer.dropwizard.config;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import com.yammer.dropwizard.jersey.DropwizardResourceConfig;
import com.yammer.dropwizard.json.ObjectMapperFactory;
-import com.yammer.dropwizard.setup.AdminEnvironment;
-import com.yammer.dropwizard.setup.JerseyEnvironment;
-import com.yammer.dropwizard.setup.LifecycleEnvironment;
-import com.yammer.dropwizard.setup.ServletEnvironment;
+import com.yammer.dropwizard.setup.*;
import com.yammer.dropwizard.validation.Validator;
import com.yammer.metrics.core.HealthCheck;
import org.eclipse.jetty.server.Server;
@@ -26,7 +24,7 @@
private final String name;
private final Set<HealthCheck> healthChecks;
- private final ObjectMapperFactory objectMapperFactory;
+ private final JsonEnvironment jsonEnvironment;
private Validator validator;
private final AtomicReference<ServletContainer> jerseyServletContainer;
@@ -51,7 +49,7 @@ public Environment(String name,
ObjectMapperFactory objectMapperFactory,
Validator validator) {
this.name = name;
- this.objectMapperFactory = objectMapperFactory;
+ this.jsonEnvironment = new JsonEnvironment(objectMapperFactory.copy());
this.validator = validator;
final DropwizardResourceConfig jerseyConfig = new DropwizardResourceConfig(false);
@@ -87,9 +85,8 @@ public ServletEnvironment getServletEnvironment() {
return servletEnvironment;
}
-
- public ObjectMapperFactory getObjectMapperFactory() {
- return objectMapperFactory;
+ public JsonEnvironment getJsonEnvironment() {
+ return jsonEnvironment;
}
public String getName() {
@@ -112,7 +109,7 @@ public void setValidator(Validator validator) {
return ImmutableSet.copyOf(healthChecks);
}
- ServletContextHandler getServletContextHandler() {
+ ServletContextHandler getServletContext() {
return servletContext;
}
@@ -127,4 +124,8 @@ Server getServer() {
ServletContextHandler getAdminContext() {
return adminContext;
}
+
+ ObjectMapper buildObjectMapper() {
+ return jsonEnvironment.buildObjectMapper();
+ }
}
View
7 dropwizard-core/src/main/java/com/yammer/dropwizard/config/ServerFactory.java
@@ -343,13 +343,14 @@ private SecurityHandler basicAuthHandler(String username, String password) {
}
private Handler createExternalServlet(Environment env) {
- final ServletContextHandler handler = env.getServletContextHandler();
+ final ServletContextHandler handler = env.getServletContext();
handler.addFilter(ThreadNameFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
final ServletContainer jerseyContainer = env.getJerseyServletContainer();
if (jerseyContainer != null) {
- env.getJerseyEnvironment().addProvider(new JacksonMessageBodyProvider(env.getObjectMapperFactory().build(),
- env.getValidator()));
+ env.getJerseyEnvironment().addProvider(
+ new JacksonMessageBodyProvider(env.buildObjectMapper(), env.getValidator())
+ );
final ServletHolder jerseyHolder = new ServletHolder(jerseyContainer);
jerseyHolder.setInitOrder(Integer.MAX_VALUE);
handler.addServlet(jerseyHolder, env.getJerseyEnvironment().getUrlPattern());
View
256 dropwizard-core/src/main/java/com/yammer/dropwizard/setup/JsonEnvironment.java
@@ -0,0 +1,256 @@
+package com.yammer.dropwizard.setup;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
+import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
+import com.fasterxml.jackson.databind.jsontype.SubtypeResolver;
+import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
+import com.fasterxml.jackson.databind.ser.FilterProvider;
+import com.fasterxml.jackson.databind.ser.SerializerFactory;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import com.yammer.dropwizard.json.ObjectMapperFactory;
+
+import java.text.DateFormat;
+import java.util.Locale;
+import java.util.Map;
+import java.util.TimeZone;
+
+public class JsonEnvironment {
+ private final ObjectMapperFactory objectMapperFactory;
+
+ public JsonEnvironment(ObjectMapperFactory objectMapperFactory) {
+ this.objectMapperFactory = objectMapperFactory;
+ }
+
+ public AnnotationIntrospector getAnnotationIntrospector() {
+ return objectMapperFactory.getAnnotationIntrospector();
+ }
+
+ public void enable(JsonFactory.Feature... features) {
+ objectMapperFactory.enable(features);
+ }
+
+ public void setHandlerInstantiator(HandlerInstantiator handlerInstantiator) {
+ objectMapperFactory.setHandlerInstantiator(handlerInstantiator);
+ }
+
+ public void setAnnotationIntrospector(AnnotationIntrospector annotationIntrospector) {
+ objectMapperFactory.setAnnotationIntrospector(annotationIntrospector);
+ }
+
+ public boolean isEnabled(DeserializationFeature feature) {
+ return objectMapperFactory.isEnabled(feature);
+ }
+
+ public void disable(DeserializationFeature... features) {
+ objectMapperFactory.disable(features);
+ }
+
+ public void enable(JsonGenerator.Feature... features) {
+ objectMapperFactory.enable(features);
+ }
+
+ public void setMixinAnnotations(Map<Class<?>, Class<?>> mixinAnnotations) {
+ objectMapperFactory.setMixinAnnotations(mixinAnnotations);
+ }
+
+ public FilterProvider getFilters() {
+ return objectMapperFactory.getFilters();
+ }
+
+ public void disable(JsonGenerator.Feature... features) {
+ objectMapperFactory.disable(features);
+ }
+
+ public VisibilityChecker<?> getVisibilityChecker() {
+ return objectMapperFactory.getVisibilityChecker();
+ }
+
+ public Map<Class<?>, Class<?>> getMixinAnnotations() {
+ return objectMapperFactory.getMixinAnnotations();
+ }
+
+ public JsonInclude.Include getSerializationInclusion() {
+ return objectMapperFactory.getSerializationInclusion();
+ }
+
+ public void setInjectableValues(InjectableValues injectableValues) {
+ objectMapperFactory.setInjectableValues(injectableValues);
+ }
+
+ public TypeResolverBuilder<?> getDefaultTyping() {
+ return objectMapperFactory.getDefaultTyping();
+ }
+
+ public void disable(MapperFeature... features) {
+ objectMapperFactory.disable(features);
+ }
+
+ public void setDateFormat(DateFormat dateFormat) {
+ objectMapperFactory.setDateFormat(dateFormat);
+ }
+
+ public void setVisibilityRules(PropertyAccessor accessor, JsonAutoDetect.Visibility visibility) {
+ objectMapperFactory.setVisibilityRules(accessor, visibility);
+ }
+
+ public void setNodeFactory(JsonNodeFactory nodeFactory) {
+ objectMapperFactory.setNodeFactory(nodeFactory);
+ }
+
+ public void setDefaultTyping(TypeResolverBuilder<?> defaultTyping) {
+ objectMapperFactory.setDefaultTyping(defaultTyping);
+ }
+
+ public boolean isEnabled(JsonFactory.Feature feature) {
+ return objectMapperFactory.isEnabled(feature);
+ }
+
+ public PropertyNamingStrategy getPropertyNamingStrategy() {
+ return objectMapperFactory.getPropertyNamingStrategy();
+ }
+
+ public void setSerializerProvider(DefaultSerializerProvider serializerProvider) {
+ objectMapperFactory.setSerializerProvider(serializerProvider);
+ }
+
+ public SubtypeResolver getSubtypeResolver() {
+ return objectMapperFactory.getSubtypeResolver();
+ }
+
+ public DefaultSerializerProvider getSerializerProvider() {
+ return objectMapperFactory.getSerializerProvider();
+ }
+
+ public void registerModule(Module module) {
+ objectMapperFactory.registerModule(module);
+ }
+
+ public boolean isEnabled(JsonGenerator.Feature feature) {
+ return objectMapperFactory.isEnabled(feature);
+ }
+
+ public void setTypeFactory(TypeFactory typeFactory) {
+ objectMapperFactory.setTypeFactory(typeFactory);
+ }
+
+ public void setFilters(FilterProvider filters) {
+ objectMapperFactory.setFilters(filters);
+ }
+
+ public void setVisibilityChecker(VisibilityChecker<?> visibilityChecker) {
+ objectMapperFactory.setVisibilityChecker(visibilityChecker);
+ }
+
+ public boolean isEnabled(SerializationFeature feature) {
+ return objectMapperFactory.isEnabled(feature);
+ }
+
+ public boolean isEnabled(JsonParser.Feature feature) {
+ return objectMapperFactory.isEnabled(feature);
+ }
+
+ public void enable(MapperFeature... features) {
+ objectMapperFactory.enable(features);
+ }
+
+ public boolean isEnabled(MapperFeature feature) {
+ return objectMapperFactory.isEnabled(feature);
+ }
+
+ public HandlerInstantiator getHandlerInstantiator() {
+ return objectMapperFactory.getHandlerInstantiator();
+ }
+
+ public void enable(SerializationFeature... features) {
+ objectMapperFactory.enable(features);
+ }
+
+ public JsonAutoDetect.Visibility getVisibility(PropertyAccessor accessor) {
+ return objectMapperFactory.getVisibility(accessor);
+ }
+
+ public InjectableValues getInjectableValues() {
+ return objectMapperFactory.getInjectableValues();
+ }
+
+ public void disable(SerializationFeature... features) {
+ objectMapperFactory.disable(features);
+ }
+
+ public Locale getLocale() {
+ return objectMapperFactory.getLocale();
+ }
+
+ public void setSubtypeResolver(SubtypeResolver subtypeResolver) {
+ objectMapperFactory.setSubtypeResolver(subtypeResolver);
+ }
+
+ public void setLocale(Locale locale) {
+ objectMapperFactory.setLocale(locale);
+ }
+
+ public void disable(JsonParser.Feature... features) {
+ objectMapperFactory.disable(features);
+ }
+
+ public DateFormat getDateFormat() {
+ return objectMapperFactory.getDateFormat();
+ }
+
+ public TypeFactory getTypeFactory() {
+ return objectMapperFactory.getTypeFactory();
+ }
+
+ public JsonNodeFactory getNodeFactory() {
+ return objectMapperFactory.getNodeFactory();
+ }
+
+ public void setTimeZone(TimeZone timeZone) {
+ objectMapperFactory.setTimeZone(timeZone);
+ }
+
+ public void setSerializerFactory(SerializerFactory serializerFactory) {
+ objectMapperFactory.setSerializerFactory(serializerFactory);
+ }
+
+ public void enable(JsonParser.Feature... features) {
+ objectMapperFactory.enable(features);
+ }
+
+ public TimeZone getTimeZone() {
+ return objectMapperFactory.getTimeZone();
+ }
+
+ public void setSerializationInclusion(JsonInclude.Include serializationInclusion) {
+ objectMapperFactory.setSerializationInclusion(serializationInclusion);
+ }
+
+ public void setPropertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy) {
+ objectMapperFactory.setPropertyNamingStrategy(propertyNamingStrategy);
+ }
+
+ public void disable(JsonFactory.Feature... features) {
+ objectMapperFactory.disable(features);
+ }
+
+ public void enable(DeserializationFeature... features) {
+ objectMapperFactory.enable(features);
+ }
+
+ public SerializerFactory getSerializerFactory() {
+ return objectMapperFactory.getSerializerFactory();
+ }
+
+ public ObjectMapper buildObjectMapper() {
+ return objectMapperFactory.build();
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.