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.jsp;
12  
13  import jakarta.servlet.ServletConfig;
14  import jakarta.servlet.ServletContext;
15  import jakarta.servlet.http.HttpServletRequest;
16  import jakarta.servlet.http.HttpServletResponse;
17  
18  import java.io.File;
19  import java.io.InputStream;
20  import java.nio.file.Files;
21  import java.nio.file.Path;
22  
23  import org.apache.catalina.Context;
24  import org.apache.jasper.EmbeddedServletOptions;
25  import org.apache.jasper.Options;
26  import org.springframework.beans.factory.annotation.Value;
27  import org.springframework.stereotype.Controller;
28  import org.springframework.web.bind.ServletRequestUtils;
29  import org.springframework.web.bind.annotation.RequestMapping;
30  import org.springframework.web.servlet.ModelAndView;
31  
32  import psiprobe.Utils;
33  import psiprobe.controllers.AbstractContextHandlerController;
34  
35  /**
36   * The Class ViewServletSourceController.
37   */
38  @Controller
39  public class ViewServletSourceController extends AbstractContextHandlerController {
40  
41    @RequestMapping(path = "/app/viewservlet.htm")
42    @Override
43    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
44        throws Exception {
45      return super.handleRequest(request, response);
46    }
47  
48    @Override
49    protected ModelAndView handleContext(String contextName, Context context,
50        HttpServletRequest request, HttpServletResponse response) throws Exception {
51  
52      String jspName = ServletRequestUtils.getStringParameter(request, "source");
53      ServletContext sctx = context.getServletContext();
54      ServletConfig scfg = (ServletConfig) context.findChild("jsp");
55      Options opt = new EmbeddedServletOptions(scfg, sctx);
56      String encoding = opt.getJavaEncoding();
57      String content = null;
58  
59      if (jspName != null) {
60        String servletName =
61            getContainerWrapper().getTomcatContainer().getServletFileNameForJsp(context, jspName);
62  
63        if (servletName != null) {
64          File servletFile = Path.of(servletName).toFile();
65          if (servletFile.exists()) {
66            try (InputStream fis = Files.newInputStream(servletFile.toPath())) {
67              content = Utils.highlightStream(jspName, fis, "java", encoding);
68            }
69          }
70        }
71      }
72      return new ModelAndView(getViewName(), "content", content);
73    }
74  
75    @Value("view_servlet_source")
76    @Override
77    public void setViewName(String viewName) {
78      super.setViewName(viewName);
79    }
80  
81  }