View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe;
12  
13  import com.opensymphony.sitemesh.webapp.SiteMeshFilter;
14  
15  import jakarta.servlet.DispatcherType;
16  import jakarta.servlet.FilterRegistration;
17  import jakarta.servlet.MultipartConfigElement;
18  import jakarta.servlet.ServletContext;
19  import jakarta.servlet.ServletException;
20  import jakarta.servlet.ServletRegistration;
21  import jakarta.servlet.SessionTrackingMode;
22  
23  import java.util.EnumSet;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.springframework.web.WebApplicationInitializer;
28  import org.springframework.web.context.ContextLoaderListener;
29  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
30  import org.springframework.web.filter.DelegatingFilterProxy;
31  
32  /**
33   * The class ProbeInitializer.
34   */
35  public class ProbeInitializer implements WebApplicationInitializer {
36  
37    @Override
38    public void onStartup(ServletContext servletContext) throws ServletException {
39  
40      // Set spring config location
41      try (AnnotationConfigWebApplicationContext rootContext =
42          new AnnotationConfigWebApplicationContext()) {
43        rootContext.register(ProbeConfig.class);
44  
45        // Set context loader listener
46        servletContext.addListener(new ContextLoaderListener(rootContext));
47      }
48  
49      // Set probe servlet
50      ServletRegistration.Dynamic probe = servletContext.addServlet("probe", ProbeServlet.class);
51  
52      // Use temp directory unlimited in size for multipart uploads
53      MultipartConfigElement multipartConfig = new MultipartConfigElement(null, -1L, -1L, 0);
54      probe.setMultipartConfig(multipartConfig);
55  
56      // Set Role that can view session attribute values
57      servletContext.setInitParameter("attribute.value.roles", "ROLE_MANAGER,ROLE_MANAGER-GUI");
58  
59      // Set Initial Parameters
60      Map<String, String> initParameters = new HashMap<>();
61      initParameters.put("contextConfigLocation", "");
62      probe.setInitParameters(initParameters);
63  
64      // Set load on startup
65      probe.setLoadOnStartup(0);
66  
67      // Add Mapping
68      probe.addMapping("*.htm", "*.ajax", "/logs/*", "/chart.png");
69  
70      // Set sitemesh filter
71      FilterRegistration.Dynamic sitemesh =
72          servletContext.addFilter("sitemesh", SiteMeshFilter.class);
73      sitemesh.addMappingForUrlPatterns(
74          EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ERROR), false,
75          "/*");
76  
77      // Set security filter
78      FilterRegistration.Dynamic security =
79          servletContext.addFilter("filterChainProxy", DelegatingFilterProxy.class);
80      security.addMappingForUrlPatterns(
81          EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ERROR), false,
82          "/*");
83  
84      // Set session cookie config
85      servletContext.getSessionCookieConfig().setHttpOnly(true);
86      servletContext.getSessionCookieConfig().setSecure(true);
87  
88      // Set session tracking mode
89      EnumSet<SessionTrackingMode> trackingMode = EnumSet.of(SessionTrackingMode.COOKIE);
90      servletContext.setSessionTrackingModes(trackingMode);
91    }
92  
93  }