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