Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.tiogasolutions.dev.jerseyspring;
  
 
 import java.util.List;
 
 // Order required to ensure this initializer is run before Jersey 2.x Spring WebApplicationInitializer
 @Order(0)
 public abstract class TiogaJerseyWebAppInitializer implements WebApplicationInitializer {

  
Identifies the Jersey ResourceConfig for this app

Parameters:
servletContext the servlet context for this app
appContext the application context for this app
Returns:
the ResourceConfig for this app
 
   public abstract Class<? extends ResourceConfiggetApplicationClass(ServletContext servletContextWebApplicationContext appContext);

  
Defines the name of the system property that is used to identify the application's current environment.

Parameters:
servletContext the servlet context for this app
appContext the application context for this app
Returns:
the property name
 
   public abstract String getEnvironmentPropertyName(ServletContext servletContextWebApplicationContext appContext);

  
Defines the name of the system property that is used to override the list of spring profiles for this application.

Parameters:
servletContext the servlet context for this app
appContext the application context for this app
Returns:
the property name
 
   protected abstract String getProfilesPropertyName(ServletContext servletContextWebApplicationContext appContext);

  
Defines the spring config classes to use for initialization - return null if a Spring XML file is used

Parameters:
servletContext the servlet context for this app
Returns:
the Spring configuration class.
 
   protected abstract Class<?>[] getSpringConfigClasses(ServletContext servletContext);

  
Defines the spring xml file to use for initialization - return null if Spring Config classes are used.

Parameters:
servletContext the servlet context for this app
Returns:
the location of the Spring configuration file.
 
   protected abstract String getSpringConfigLocation(ServletContext servletContext);
 
   }
 
   @Override
   public void onStartup(ServletContext servletContextthrows ServletException {
 
     WebApplicationContext appContext = createWebApplicationContext(servletContext);
 
     addListeners(servletContextappContext);
 
     addFilters(servletContextappContext);
 
     addServlets(servletContextappContext);
   }
 
   protected void addFilters(ServletContext servletContextWebApplicationContext appContext) {
 
     // Spring security filter chain
     addSpringSecurityFilter(servletContextappContext);
 
     addJerseyFilter(servletContextappContext);
   }
 
   protected void addListeners(ServletContext servletContextWebApplicationContext appContext) {
     // Add context listener with refreshed appContext
     servletContext.addListener(new ContextLoaderListener(appContext));
   }
 
 
     Class<?>[] configClasses = getSpringConfigClasses(servletContext);
     String configLocation = getSpringConfigLocation(servletContext);
 
    // We can only use one or the other...
    if (StringUtils.isNotBlank(configLocation) && configClasses != null) {
      String msg = String.format("Only XML or Annotation configuration can be used but not both.");
      throw new IllegalArgumentException(msg);
    } else if (configClasses != null) {
      appContext = new AnnotationConfigWebApplicationContext();
      ((AnnotationConfigWebApplicationContext)appContext).register(configClasses);
    } else if (StringUtils.isNotBlank(configLocation)) {
      appContext = new XmlWebApplicationContext();
      appContext.setConfigLocation(configLocation);
    } else {
      String msg = String.format("Either an XML or Annotation configuration can be specified.");
      throw new IllegalArgumentException(msg);
    }
    String[] profiles = getSpringProfiles(servletContextappContext);
    if (profiles != null && profiles.length > 0) {
      appContext.getEnvironment().setActiveProfiles(profiles);
    }
    // Refresh the spring context -- important, Jersey 2.x
    // Spring integration will not work with out it.
    appContext.setServletContext(servletContext);
    appContext.refresh();
    return appContext;
  }

  
Configures the Spring Security filter

Parameters:
servletContext the servlet context for this app
appContext the application context for this app
  protected void addSpringSecurityFilter(ServletContext servletContextWebApplicationContext appContext) {
    FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain"DelegatingFilterProxy.class);
    securityFilter.addMappingForUrlPatterns(nullfalse"/*");
  }

  
Configures the Jersey filter

Parameters:
servletContext the servlet context for this app
appContext the application context for this app
  protected void addJerseyFilter(ServletContext servletContextWebApplicationContext appContext) {
    servletContext.setInitParameter("contextConfigLocation"""); // Prevents Jersey Spring WebInitializer for do any work.
    FilterRegistration.Dynamic jerseyFilter = servletContext.addFilter("jersey-filter"ServletContainer.class);
    jerseyFilter.setInitParameter(.getApplicationClass(servletContextappContext).getName());
    jerseyFilter.addMappingForUrlPatterns(nullfalse"/*");
  }
  protected String buildStaticContentRegex() {
    String staticPattern = "";
    for (String urlPatterngetStaticResourceUrlPatterns()) {
      staticPattern += (staticPattern.isEmpty()) ? urlPattern : "|"+urlPattern;
    }
    staticPattern = "([^\\s]+(\\.(?i)(" + staticPattern + "))$)";
    return staticPattern;
  }
    return Arrays.asList("html","jpg","png","gif","bmp","css","js","ico","pdf","txt");
  }
  protected void addServlets(ServletContext servletContextWebApplicationContext appContext) {
    addJspfServlet(servletContextappContext);
  }

  
Includes jspf as servlet

Parameters:
servletContext the servlet context for this app
appContext the application context for this app
  protected void addJspfServlet(ServletContext servletContextWebApplicationContext appContext) {
    servletContext.getServletRegistration("jsp").addMapping("*.jspf");
  }
  protected String[] getSpringProfiles(ServletContext servletContextWebApplicationContext appContext) {
    String environmentName = System.getProperty(getEnvironmentPropertyName(servletContextappContext), "null");
    String envProfileName = "env-"+environmentName;
    String defaultProfiles = StringUtils.toDelineatedString(",""main""live"envProfileName);
    String profilesString = System.getProperty(getProfilesPropertyName(servletContextappContext), defaultProfiles);
    List<Stringprofiles = new ArrayList<>();
    for (String profile : profilesString.split(",")) {
      profiles.add(profile.trim());
    }
    return ReflectUtils.toArray(String.classprofiles);
  }
New to GrepCode? Check out our FAQ X