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 java.io.File;
14  import java.io.InputStream;
15  import java.nio.file.Files;
16  
17  import javax.servlet.ServletConfig;
18  import javax.servlet.ServletContext;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
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  }