1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.util.servlet;
20
21 import java.io.BufferedInputStream;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletOutputStream;
28 import javax.servlet.http.HttpServlet;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 public class SourceCodeServlet extends HttpServlet
33 {
34 public void doGet(HttpServletRequest req, HttpServletResponse res)
35 throws IOException, ServletException
36 {
37 String webPage = req.getServletPath();
38
39
40 int chopPoint = webPage.indexOf(".source");
41
42 webPage = webPage.substring(0, chopPoint - 1);
43 webPage += "p";
44
45
46 String realPath = getServletConfig().getServletContext().getRealPath(webPage);
47
48
49 res.setContentType("text/plain");
50
51
52 ServletOutputStream out = res.getOutputStream();
53
54
55 InputStream in = null;
56 try
57 {
58 in = new BufferedInputStream(new FileInputStream(realPath));
59 int ch;
60 while ((ch = in.read()) !=-1)
61 {
62 out.print((char)ch);
63 }
64 }
65 finally
66 {
67 if (in != null)
68 {
69 in.close();
70 }
71 }
72 }
73 }