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 import java.io.InputStream;
19 import java.nio.charset.Charset;
20 import java.nio.charset.StandardCharsets;
21 import java.nio.file.Files;
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
33
34
35 public class BaseViewXmlConfController extends AbstractContextHandlerController {
36
37
38 private static final Logger logger = LoggerFactory.getLogger(BaseViewXmlConfController.class);
39
40
41 private static final String TARGET_WEB_XML = "web.xml";
42
43
44 private static final String TARGET_CONTEXT_XML = "context.xml";
45
46
47 private String displayTarget;
48
49
50 private String downloadUrl;
51
52
53
54
55
56
57 public String getDisplayTarget() {
58 return displayTarget;
59 }
60
61
62
63
64
65
66 public void setDisplayTarget(String displayTarget) {
67 this.displayTarget = displayTarget;
68 }
69
70
71
72
73
74
75 public String getDownloadUrl() {
76 return downloadUrl;
77 }
78
79
80
81
82
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 }