01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/example/src/java/example/ViewSourceServlet.java $
03: * $Id: ViewSourceServlet.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2003, 2004 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package example;
21:
22: import javax.servlet.*;
23: import javax.servlet.http.*;
24: import java.io.*;
25:
26: /**
27: * Serves up the source of a JSP/JSF page. Should be mapped to "*.source"
28: * in web.xml. Looks at the URL its mapping from, strips off ".source",
29: * and looks for a corresponding JSP/JSF file. Use like:
30: * http://localhost:8080/myservlet/thefile.jsp.source
31: */
32: public class ViewSourceServlet extends HttpServlet {
33: public void doGet(HttpServletRequest req, HttpServletResponse res)
34: throws IOException, ServletException {
35: String webPage = req.getServletPath();
36:
37: // remove the '*.source' suffix that maps to this servlet
38: int chopPoint = webPage.indexOf(".source");
39:
40: webPage = webPage.substring(0, chopPoint - 1);
41: webPage += "p"; // replace jsf with jsp
42:
43: // get the actual file location of the requested resource
44: String realPath = getServletConfig().getServletContext()
45: .getRealPath(webPage);
46: System.out.println("realPath: " + realPath);
47:
48: // output an HTML page
49: res.setContentType("text/plain");
50:
51: // print some html
52: ServletOutputStream out = res.getOutputStream();
53:
54: // print the file
55: InputStream in = null;
56: try {
57: in = new BufferedInputStream(new FileInputStream(realPath));
58: int ch;
59: while ((ch = in.read()) != -1) {
60: out.print((char) ch);
61: }
62: } finally {
63: if (in != null)
64: in.close(); // very important
65: }
66: }
67: }
|