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.deploy;
12  
13  import com.google.common.base.Strings;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.catalina.Context;
24  import org.springframework.beans.factory.annotation.Value;
25  import org.springframework.stereotype.Controller;
26  import org.springframework.web.bind.annotation.RequestMapping;
27  import org.springframework.web.servlet.ModelAndView;
28  
29  import psiprobe.controllers.AbstractTomcatContainerController;
30  
31  /**
32   * Precharges the list of contexts in the deploy page.
33   */
34  @Controller
35  public class DeployController extends AbstractTomcatContainerController {
36  
37    @RequestMapping(path = "/adm/deploy.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      List<Context> apps;
49      try {
50        apps = getContainerWrapper().getTomcatContainer().findContexts();
51      } catch (NullPointerException ex) {
52        throw new IllegalStateException(
53            "No container found for your server: " + getServletContext().getServerInfo(), ex);
54      }
55  
56      List<Map<String, String>> applications = new ArrayList<>();
57      for (Context appContext : apps) {
58        // check if this is not the ROOT webapp
59        if (!Strings.isNullOrEmpty(appContext.getName())) {
60          Map<String, String> app = new HashMap<>();
61          app.put("value", appContext.getName());
62          app.put("label", appContext.getName());
63          applications.add(app);
64        }
65      }
66      request.setAttribute("apps", applications);
67      return new ModelAndView(getViewName());
68    }
69  
70    @Value("deploy")
71    @Override
72    public void setViewName(String viewName) {
73      super.setViewName(viewName);
74    }
75  
76  }