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.ServletContext;
18  import jakarta.servlet.ServletException;
19  import jakarta.servlet.ServletRegistration;
20  import jakarta.servlet.SessionTrackingMode;
21  
22  import java.util.EnumSet;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.springframework.web.WebApplicationInitializer;
27  import org.springframework.web.context.ContextLoaderListener;
28  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
29  import org.springframework.web.filter.DelegatingFilterProxy;
30  
31  /**
32   * The class ProbeInitializer.
33   */
34  public class ProbeInitializer implements WebApplicationInitializer {
35  
36    @Override
37    public void onStartup(ServletContext servletContext) throws ServletException {
38  
39      // Set spring config location
40      try (AnnotationConfigWebApplicationContext rootContext =
41          new AnnotationConfigWebApplicationContext()) {
42        rootContext.register(ProbeConfig.class);
43  
44        // Set context loader listener
45        servletContext.addListener(new ContextLoaderListener(rootContext));
46      }
47  
48      // Set probe servlet
49      ServletRegistration.Dynamic probe = servletContext.addServlet("probe", ProbeServlet.class);
50  
51      // Set Role that can view session attribute values
52      servletContext.setInitParameter("attribute.value.roles", "ROLE_MANAGER,ROLE_MANAGER-GUI");
53  
54      // Set Initial Parameters
55      Map<String, String> initParameters = new HashMap<>();
56      initParameters.put("contextConfigLocation", "");
57      probe.setInitParameters(initParameters);
58  
59      // Set load on startup
60      probe.setLoadOnStartup(0);
61  
62      // Add Mapping
63      probe.addMapping("*.htm", "*.ajax", "/logs/*", "/chart.png");
64  
65      // Set sitemesh filter
66      FilterRegistration.Dynamic sitemesh =
67          servletContext.addFilter("sitemesh", SiteMeshFilter.class);
68      sitemesh.addMappingForUrlPatterns(
69          EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ERROR), false,
70          "/*");
71  
72      // Set security filter
73      FilterRegistration.Dynamic security =
74          servletContext.addFilter("filterChainProxy", DelegatingFilterProxy.class);
75      security.addMappingForUrlPatterns(
76          EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ERROR), false,
77          "/*");
78  
79      // Set session cookie config
80      servletContext.getSessionCookieConfig().setHttpOnly(true);
81      servletContext.getSessionCookieConfig().setSecure(true);
82  
83      // Set session tracking mode
84      EnumSet<SessionTrackingMode> trackingMode = EnumSet.of(SessionTrackingMode.COOKIE);
85      servletContext.setSessionTrackingModes(trackingMode);
86    }
87  
88  }