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