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.nio.file.Path;
19  
20  import org.apache.catalina.Context;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import psiprobe.Utils;
26  import psiprobe.controllers.AbstractContextHandlerController;
27  
28  /**
29   * Downloads a deployment descriptor (web.xml) or a context descriptor (context.xml) of a web
30   * application.
31   */
32  public class BaseDownloadXmlConfController extends AbstractContextHandlerController {
33  
34    /** The Constant logger. */
35    private static final Logger logger = LoggerFactory.getLogger(BaseDownloadXmlConfController.class);
36  
37    /** The Constant TARGET_WEB_XML. */
38    private static final String TARGET_WEB_XML = "web.xml";
39  
40    /** The Constant TARGET_CONTEXT_XML. */
41    private static final String TARGET_CONTEXT_XML = "context.xml";
42  
43    /** Type of a configuration file to be downloaded. */
44    private String downloadTarget;
45  
46    /**
47     * Gets the download target.
48     *
49     * @return the download target
50     */
51    public String getDownloadTarget() {
52      return downloadTarget;
53    }
54  
55    /**
56     * Sets the download target.
57     *
58     * @param downloadTarget the new download target
59     */
60    public void setDownloadTarget(String downloadTarget) {
61      this.downloadTarget = downloadTarget;
62    }
63  
64    @Override
65    protected ModelAndView handleContext(String contextName, Context context,
66        HttpServletRequest request, HttpServletResponse response) throws Exception {
67  
68      if (downloadTarget == null) {
69        throw new RuntimeException("Download target is not set for " + getClass().getName());
70      }
71  
72      String xmlPath;
73  
74      if (TARGET_WEB_XML.equals(downloadTarget)) {
75        ServletContext sctx = context.getServletContext();
76        xmlPath = sctx.getRealPath("/WEB-INF/web.xml");
77      } else if (TARGET_CONTEXT_XML.equals(downloadTarget)) {
78        xmlPath = this.getContainerWrapper().getTomcatContainer().getConfigFile(context).getPath();
79      } else {
80        throw new RuntimeException("Unknown download target " + getDownloadTarget());
81      }
82  
83      if (xmlPath != null) {
84        File xmlFile = Path.of(xmlPath).toFile();
85        if (xmlFile.exists()) {
86          Utils.sendFile(request, response, xmlFile);
87        } else {
88          logger.debug("File {} of {} application does not exists.", xmlPath, contextName);
89        }
90      } else {
91        logger.debug("Cannot determine path to {} file of {} application.", getDownloadTarget(),
92            contextName);
93      }
94      return null;
95    }
96  
97  }