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.apps;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
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.ServletRequestUtils;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import psiprobe.controllers.AbstractTomcatContainerController;
27  import psiprobe.model.Application;
28  import psiprobe.tools.ApplicationUtils;
29  import psiprobe.tools.SecurityUtils;
30  
31  /**
32   * Creates the list of web application installed in the same "host" as the Probe.
33   */
34  @Controller
35  public class ListWebappsController extends AbstractTomcatContainerController {
36  
37    @RequestMapping(path = "/index.htm")
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 handleRequestInternal(HttpServletRequest request,
46        HttpServletResponse response) throws Exception {
47  
48      boolean calcSize = ServletRequestUtils.getBooleanParameter(request, "size", false)
49          && SecurityUtils.hasAttributeValueRole(getServletContext());
50  
51      List<Context> apps;
52      try {
53        apps = getContainerWrapper().getTomcatContainer().findContexts();
54      } catch (NullPointerException ex) {
55        throw new IllegalStateException(
56            "No container found for your server: " + getServletContext().getServerInfo(), ex);
57      }
58      List<Application> applications = new ArrayList<>(apps.size());
59      boolean showResources = getContainerWrapper().getResourceResolver().supportsPrivateResources();
60      for (Context appContext : apps) {
61        // check if this is not the ROOT webapp
62        if (appContext.getName() != null) {
63          applications.add(ApplicationUtils.getApplication(appContext,
64              getContainerWrapper().getResourceResolver(), calcSize, getContainerWrapper()));
65        }
66      }
67      if (!applications.isEmpty() && !showResources) {
68        request.setAttribute("no_resources", Boolean.TRUE);
69      }
70      return new ModelAndView(getViewName(), "apps", applications);
71    }
72  
73    @Value("applications")
74    @Override
75    public void setViewName(String viewName) {
76      super.setViewName(viewName);
77    }
78  }