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.io.File;
14  import java.io.InputStream;
15  import java.nio.charset.Charset;
16  import java.nio.charset.StandardCharsets;
17  import java.nio.file.Files;
18  
19  import javax.servlet.ServletContext;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.catalina.Context;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import psiprobe.Utils;
29  import psiprobe.controllers.AbstractContextHandlerController;
30  
31  /**
32   * Displays a deployment descriptor (web.xml) or a context descriptor (context.xml) of a web
33   * application
34   */
35  public class BaseViewXmlConfController extends AbstractContextHandlerController {
36  
37    /** The Constant logger. */
38    private static final Logger logger = LoggerFactory.getLogger(BaseViewXmlConfController.class);
39  
40    /** The Constant TARGET_WEB_XML. */
41    private static final String TARGET_WEB_XML = "web.xml";
42  
43    /** The Constant TARGET_CONTEXT_XML. */
44    private static final String TARGET_CONTEXT_XML = "context.xml";
45  
46    /** Type of a file to be displayed. */
47    private String displayTarget;
48  
49    /** Url that will be used in the view to download the file. */
50    private String downloadUrl;
51  
52    /**
53     * Gets the display target.
54     *
55     * @return the display target
56     */
57    public String getDisplayTarget() {
58      return displayTarget;
59    }
60  
61    /**
62     * Sets the display target.
63     *
64     * @param displayTarget the new display target
65     */
66    public void setDisplayTarget(String displayTarget) {
67      this.displayTarget = displayTarget;
68    }
69  
70    /**
71     * Gets the download url.
72     *
73     * @return the download url
74     */
75    public String getDownloadUrl() {
76      return downloadUrl;
77    }
78  
79    /**
80     * Sets the download url.
81     *
82     * @param downloadUrl the new download url
83     */
84    public void setDownloadUrl(String downloadUrl) {
85      this.downloadUrl = downloadUrl;
86    }
87  
88    @Override
89    protected ModelAndView handleContext(String contextName, Context context,
90        HttpServletRequest request, HttpServletResponse response) throws Exception {
91  
92      if (displayTarget == null) {
93        throw new RuntimeException("Display target is not set for " + getClass().getName());
94      }
95  
96      String xmlPath;
97      File xmlFile = null;
98      ModelAndView mv = new ModelAndView(getViewName());
99  
100     if (TARGET_WEB_XML.equals(displayTarget)) {
101       ServletContext sctx = context.getServletContext();
102       xmlPath = sctx.getRealPath("/WEB-INF/web.xml");
103       xmlFile = new File(xmlPath);
104       mv.addObject("fileDesc",
105           getMessageSourceAccessor().getMessage("probe.src.app.viewxmlconf.webxml.desc"));
106     } else if (TARGET_CONTEXT_XML.equals(displayTarget)) {
107       xmlFile = getContainerWrapper().getTomcatContainer().getConfigFile(context);
108       if (xmlFile != null) {
109         xmlPath = xmlFile.getPath();
110       } else {
111         xmlPath = null;
112       }
113       mv.addObject("fileDesc",
114           getMessageSourceAccessor().getMessage("probe.src.app.viewxmlconf.contextxml.desc"));
115     } else {
116       throw new RuntimeException("Unknown display target " + getDisplayTarget());
117     }
118 
119     mv.addObject("displayTarget", displayTarget);
120     mv.addObject("downloadUrl", downloadUrl);
121 
122     if (xmlFile != null) {
123       mv.addObject("fileName", xmlFile.getName());
124       if (xmlFile.exists()) {
125         try (InputStream fis = Files.newInputStream(xmlFile.toPath())) {
126           String encoding = Charset.defaultCharset().displayName();
127           mv.addObject("content", Utils.highlightStream(TARGET_WEB_XML, fis, "xml",
128               encoding == null ? StandardCharsets.UTF_8.name() : encoding));
129         }
130       } else {
131         logger.debug("File {} of {} application does not exists.", xmlPath, contextName);
132       }
133     } else {
134       logger.debug("Cannot determine path to {} file of {} application.", getDisplayTarget(),
135           contextName);
136     }
137 
138     return mv;
139   }
140 
141 }