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