01: /*
02: * Copyright 2005 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.myfaces.shared_impl.util.servlet;
18:
19: import javax.servlet.*;
20: import javax.servlet.http.*;
21: import java.io.*;
22:
23: public class SourceCodeServlet extends HttpServlet {
24: public void doGet(HttpServletRequest req, HttpServletResponse res)
25: throws IOException, ServletException {
26: String webPage = req.getServletPath();
27:
28: // remove the '*.source' suffix that maps to this servlet
29: int chopPoint = webPage.indexOf(".source");
30:
31: webPage = webPage.substring(0, chopPoint - 1);
32: webPage += "p"; // replace jsf with jsp
33:
34: // get the actual file location of the requested resource
35: String realPath = getServletConfig().getServletContext()
36: .getRealPath(webPage);
37:
38: // output an HTML page
39: res.setContentType("text/plain");
40:
41: // print some html
42: ServletOutputStream out = res.getOutputStream();
43:
44: // print the file
45: InputStream in = null;
46: try {
47: in = new BufferedInputStream(new FileInputStream(realPath));
48: int ch;
49: while ((ch = in.read()) != -1) {
50: out.print((char) ch);
51: }
52: } finally {
53: if (in != null)
54: in.close(); // very important
55: }
56: }
57: }
|