Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SourceCodeServlet |
|
| 3.0;3 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
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 | 0 | public class SourceCodeServlet extends HttpServlet |
33 | { | |
34 | public void doGet(HttpServletRequest req, HttpServletResponse res) | |
35 | throws IOException, ServletException | |
36 | { | |
37 | 0 | String webPage = req.getServletPath(); |
38 | ||
39 | // remove the '*.source' suffix that maps to this servlet | |
40 | 0 | int chopPoint = webPage.indexOf(".source"); |
41 | ||
42 | 0 | webPage = webPage.substring(0, chopPoint - 1); |
43 | 0 | webPage += "p"; // replace jsf with jsp |
44 | ||
45 | // get the actual file location of the requested resource | |
46 | 0 | String realPath = getServletConfig().getServletContext().getRealPath(webPage); |
47 | ||
48 | // output an HTML page | |
49 | 0 | res.setContentType("text/plain"); |
50 | ||
51 | // print some html | |
52 | 0 | ServletOutputStream out = res.getOutputStream(); |
53 | ||
54 | // print the file | |
55 | 0 | InputStream in = null; |
56 | try | |
57 | { | |
58 | 0 | in = new BufferedInputStream(new FileInputStream(realPath)); |
59 | int ch; | |
60 | 0 | while ((ch = in.read()) !=-1) |
61 | { | |
62 | 0 | out.print((char)ch); |
63 | } | |
64 | } | |
65 | finally | |
66 | { | |
67 | 0 | if (in != null) |
68 | { | |
69 | 0 | in.close(); // very important |
70 | } | |
71 | } | |
72 | 0 | } |
73 | } |