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