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