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.controllers.servlets;
12  
13  import jakarta.servlet.http.HttpServletRequest;
14  import jakarta.servlet.http.HttpServletResponse;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.apache.catalina.Context;
20  import org.springframework.beans.factory.annotation.Value;
21  import org.springframework.stereotype.Controller;
22  import org.springframework.web.bind.annotation.RequestMapping;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import psiprobe.controllers.AbstractContextHandlerController;
26  import psiprobe.model.ServletMapping;
27  import psiprobe.tools.ApplicationUtils;
28  
29  /**
30   * Retrieves a list of servlet mappings for a particular web application or all web applications if
31   * an application name is not passed in a query string.
32   */
33  @Controller
34  public class ListServletMapsController extends AbstractContextHandlerController {
35  
36    @RequestMapping(path = "/servletmaps.htm")
37    @Override
38    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
39        throws Exception {
40      return super.handleRequest(request, response);
41    }
42  
43    @Override
44    protected ModelAndView handleContext(String contextName, Context context,
45        HttpServletRequest request, HttpServletResponse response) throws Exception {
46  
47      List<Context> ctxs;
48      if (context == null) {
49        ctxs = getContainerWrapper().getTomcatContainer().findContexts();
50      } else {
51        ctxs = new ArrayList<>();
52        ctxs.add(context);
53      }
54  
55      List<ServletMapping> servletMaps = new ArrayList<>();
56      for (Context ctx : ctxs) {
57        servletMaps.addAll(ApplicationUtils.getApplicationServletMaps(ctx));
58      }
59  
60      return new ModelAndView(getViewName(), "servletMaps", servletMaps);
61    }
62  
63    @Override
64    protected boolean isContextOptional() {
65      return true;
66    }
67  
68    @Value("servletmaps")
69    @Override
70    public void setViewName(String viewName) {
71      super.setViewName(viewName);
72    }
73  
74  }