Skip to content

Instantly share code, notes, and snippets.

@cmicali
Last active August 29, 2015 14:10
Show Gist options
  • Save cmicali/2ee7b3b6941005dc5596 to your computer and use it in GitHub Desktop.
Save cmicali/2ee7b3b6941005dc5596 to your computer and use it in GitHub Desktop.
/**
* Replaces the DropWizard exception mappers with our own mappers that return exceptions in JSON format
* @param environment
*/
private void configureExceptionMappers(Environment environment) {
ResourceConfig jrConfig = environment.jersey().getResourceConfig();
Set<Object> dwSingletons = jrConfig.getSingletons();
List<ExceptionMapper> singletonsToRemove = new ArrayList<>();
// Find all ExceptionMappers that are already registered in the jersey configuration
for (Object s : dwSingletons) {
String className = s.getClass().getName();
if (s instanceof ExceptionMapper && (className.startsWith("com.yammer.dropwizard.jersey.") || className.startsWith("com.codahale.dropwizard.jersey.") || className.startsWith("io.dropwizard.jersey."))) {
singletonsToRemove.add((ExceptionMapper)s);
}
}
// Register our exception mappers
environment.jersey().register(new JsonLoggingExceptionMapper());
environment.jersey().register(new JsonConstraintViolationExceptionMapper());
environment.jersey().register(new JsonJsonProcessingExceptionMapper());
environment.jersey().register(new JsonParseExceptionMapper());
environment.jersey().register(new JsonEarlyEofExceptionMapper());
JerseyHacks.removeComponents(jrConfig, singletonsToRemove);
}
import java.util.Set;
public class CollectionUtil {
// Warning: This is JVM implementation-specific
@SuppressWarnings("unchecked")
public static <T> Set<T> getModifiableSet(Set<T> collection) {
Object orig = ReflectionUtil.getFieldValue(collection, "c");
if (orig != null) {
return (Set<T>)orig;
}
return collection;
}
}
import com.sagedevices.platform.util.CollectionUtil;
import com.sagedevices.platform.util.ReflectionUtil;
import org.glassfish.jersey.model.internal.CommonConfig;
import org.glassfish.jersey.model.internal.ComponentBag;
import org.glassfish.jersey.server.ResourceConfig;
import java.util.Collection;
import java.util.Set;
public class JerseyHacks {
public static void removeComponents(ResourceConfig config, Collection<?> components) {
Set<Object> objects = CollectionUtil.getModifiableSet(config.getSingletons());
objects.removeAll(components);
Set<Object> cachedSingletons = ReflectionUtil.getFieldValue(config, "cachedSingletons");
cachedSingletons.removeAll(components);
CommonConfig commonConfig = ReflectionUtil.getFieldValue(config, "state");
ComponentBag componentBag = ReflectionUtil.getFieldValue(commonConfig, "componentBag");
Set<Object> instances = ReflectionUtil.getFieldValue(componentBag, "instances");
instances.remove(components);
Set<Object> instancesView = ReflectionUtil.getFieldValue(componentBag, "instances");
instancesView = CollectionUtil.getModifiableSet(instancesView);
instancesView.remove(components);
}
}
@SuppressWarnings("unchecked")
public static <E> E getFieldValue(Object object, String fieldName) {
Class<?> clazz = object.getClass();
while (clazz != null) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return (E) field.get(object);
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
} catch (Exception e) {
return null;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment