1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.shared_orchestra.util.servlet;
21
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24 import java.io.*;
25
26 public class SourceCodeServlet extends HttpServlet
27 {
28 public void doGet(HttpServletRequest req, HttpServletResponse res)
29 throws IOException, ServletException
30 {
31 String webPage = req.getServletPath();
32
33
34 int chopPoint = webPage.lastIndexOf(".source");
35
36 webPage = webPage.substring(0, chopPoint);
37
38 if(webPage.endsWith(".jsf"))
39 {
40 int jsfChopPoint = webPage.lastIndexOf(".jsf");
41
42 webPage = webPage.substring(0, jsfChopPoint);
43
44 webPage += ".jsp";
45
46
47 String realPath = getServletConfig().getServletContext().getRealPath(webPage);
48
49 outputFile(res, realPath);
50 }
51 else if(webPage.endsWith(".jsp"))
52 {
53
54 String realPath = getServletConfig().getServletContext().getRealPath(webPage);
55
56 outputFile(res, realPath);
57 }
58 else if(webPage.endsWith(".jspx"))
59 {
60
61 String realPath = getServletConfig().getServletContext().getRealPath(webPage);
62
63 outputFile(res, realPath);
64 }
65 else if(webPage.endsWith(".xhtml"))
66 {
67
68 String realPath = getServletConfig().getServletContext().getRealPath(webPage);
69
70 outputFile(res, realPath);
71 }
72 else
73 {
74 int beginChopPoint = webPage.lastIndexOf("/");
75 int extensionChopPoint = webPage.lastIndexOf(".java");
76
77 webPage = webPage.substring(beginChopPoint+1,extensionChopPoint);
78
79 try
80 {
81
82
83 String realPath = getServletConfig().getServletContext().getRealPath("/WEB-INF/classes/"+webPage.replace('.','/')+".java");
84
85 outputFile(res, realPath);
86 }
87 catch(Exception e)
88 {
89
90 String realPath = getServletConfig().getServletContext().getRealPath("/WEB-INF/src/"+webPage.replace('.','/')+".java");
91
92 outputFile(res, realPath);
93 }
94
95 }
96 }
97
98 private void outputFile(HttpServletResponse res, String realPath)
99 throws IOException
100 {
101
102 res.setContentType("text/plain");
103
104
105 ServletOutputStream out = res.getOutputStream();
106
107
108 InputStream in = null;
109 try
110 {
111 in = new BufferedInputStream(new FileInputStream(realPath));
112 int ch;
113 while ((ch = in.read()) !=-1)
114 {
115 out.print((char)ch);
116 }
117 }
118 finally {
119 if (in != null) in.close();
120 }
121 }
122 }