Skip to content

Instantly share code, notes, and snippets.

@Zenedith
Created April 9, 2014 11:03
Show Gist options
  • Save Zenedith/10255206 to your computer and use it in GitHub Desktop.
Save Zenedith/10255206 to your computer and use it in GitHub Desktop.
package net.stepniak.common.server.context;
import net.stepniak.common.server.util.ServerApiAuthorizationFilterImpl;
import net.stepniak.common.server.util.ServerApiOriginFilter;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.spring.SpringWebApplicationInitializer;
import org.glassfish.jersey.servlet.ServletContainer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public abstract class ApplicationInitializer extends SpringWebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
registerInContext(rootContext);
rootContext.setDisplayName(getApplicationName());
rootContext.scan(getBaseScanPackages());
// The following line is required to avoid having jersey-spring3 registering it's own Spring root context.
servletContext.setInitParameter("contextConfigLocation", "");
servletContext.setInitParameter("com.newrelic.agent.APPLICATION_NAME", getApplicationName());
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.addListener(new RequestContextListener());
initJersey(servletContext);
initSpringSecurity(rootContext, servletContext);
initDefaultJaxrsConfig(servletContext);
initBootstrap(servletContext);
final FilterRegistration.Dynamic apiOriginFilter = servletContext.addFilter(
"ApiOriginFilter",
ServerApiOriginFilter.class
);
apiOriginFilter.addMappingForUrlPatterns(null, false, "/*");
}
private void initSpringSecurity(AnnotationConfigWebApplicationContext rootContext, ServletContext servletContext) {
FilterRegistration.Dynamic springSecurity = servletContext.addFilter(
"springSecurityFilterChain",
new DelegatingFilterProxy("springSecurityFilterChain", rootContext)
);
springSecurity.addMappingForUrlPatterns(null, true, "/*");
}
protected void initJersey(ServletContext servletContext) {
final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("jersey", new ServletContainer());
dispatcher.setInitParameter("com.newrelic.agent.APPLICATION_NAME", getApplicationName());
dispatcher.setInitParameter("javax.ws.rs.Application", getApplicationClassName());
dispatcher.setInitParameter(ServerProperties.WADL_FEATURE_DISABLE, "true");
dispatcher.setInitParameter(ServerProperties.APPLICATION_NAME, getApplicationName());
dispatcher.addMapping("/*");
dispatcher.setLoadOnStartup(1);
}
protected void initDefaultJaxrsConfig(ServletContext servletContext) {
final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DefaultJaxrsConfig", "com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig");
dispatcher.setInitParameter("com.newrelic.agent.APPLICATION_NAME", getApplicationName());
dispatcher.setInitParameter("api.version", "1.0.0");
// dispatcher.setInitParameter("swagger.api.basepath", "http://localhost:8080");
dispatcher.setInitParameter("swagger.filter", ServerApiAuthorizationFilterImpl.class.getName());
dispatcher.setLoadOnStartup(2);
}
protected void initBootstrap(ServletContext servletContext) {
final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SwaggerBootstrap", "net.stepniak.common.server.SwaggerBootstrap");
dispatcher.setLoadOnStartup(2);
}
protected abstract void registerInContext(AnnotationConfigWebApplicationContext rootContext);
protected abstract String[] getBaseScanPackages();
protected abstract String getApplicationName();
protected abstract String getApplicationClassName();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment