1
2
3
4
5
6
7
8
9
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
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
29
30
31 public class BaseDownloadXmlConfController extends AbstractContextHandlerController {
32
33
34 private static final Logger logger = LoggerFactory.getLogger(BaseDownloadXmlConfController.class);
35
36
37 private static final String TARGET_WEB_XML = "web.xml";
38
39
40 private static final String TARGET_CONTEXT_XML = "context.xml";
41
42
43 private String downloadTarget;
44
45
46
47
48
49
50 public String getDownloadTarget() {
51 return downloadTarget;
52 }
53
54
55
56
57
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 }