package com.hubspot.dropwizard.guice;

import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.servlet.GuiceFilter;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.setup.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.Path;
import java.lang.reflect.Type;

/**
 * Functionality used to help link Guice with Jersey
 */
public class JerseyUtil {

    final static Logger logger = LoggerFactory.getLogger(JerseyUtil.class);

    /**
     * Registers any Guice-bound providers or root resources.
     */
    public static void registerGuiceBound(Injector injector, final JerseyEnvironment environment) {
        while (injector != null) {
            for (Key<?> key : injector.getBindings().keySet()) {
                Type type = key.getTypeLiteral().getType();
                if (type instanceof Class) {
                    Class<?> c = (Class) type;
                    if (isProviderClass(c)) {
                        logger.info("Registering {} as a provider class", c.getName());
                        environment.register(c);
                    } else if (isRootResourceClass(c)) {
                        logger.info("Registering {} as a root resource class", c.getName());
                        environment.register(c);
                    }

                }
            }
            injector = injector.getParent();
        }
    }

    private static boolean isProviderClass(Class<?> c) {
        return c != null && c.isAnnotationPresent(javax.ws.rs.ext.Provider.class);
    }

    private static boolean isRootResourceClass(Class<?> c) {
        if (c == null)
            return false;

        if (c.isAnnotationPresent(Path.class)) return true;

        for (Class i : c.getInterfaces())
            if (i.isAnnotationPresent(Path.class)) return true;

        return false;
    }

    public static void registerGuiceFilter(Environment environment) {
        environment.servlets().addFilter("Guice Filter", GuiceFilter.class)
                   .addMappingForUrlPatterns(null, false, environment.getApplicationContext().getContextPath() + "*");
    }
}

@@ -232,24 +232,75 @@
 nvironment;%0A
+import org.glassfish.jersey.server.model.Resource;%0A
 import org.s
@@ -1229,32 +1229,290 @@
 urceClass(c)) %7B%0A
+                        // Jersey rejects resources that it doesn't think are acceptable%0A                        // Including abstract classes and interfaces, even if there is a valid Guice binding.%0A                        if(Resource.isAcceptable(c)) %7B%0A    
                 
@@ -1604,32 +1604,36 @@
                 
+    
 environment.regi
@@ -1633,32 +1633,231 @@
 nt.register(c);%0A
+                        %7D else %7B%0A                            logger.warn(%22Class %7B%7D was not registered as a resource. Bind a concrete implementation instead.%22, c.getName());%0A                        %7D%0A
                 

Add check to ensure we don't auto-register resources that Jersey will reject, even if there is a valid Guice binding.