01: /*
02: * Copyright 2004 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: package examples;
17:
18: import javax.servlet.*;
19: import javax.servlet.jsp.*;
20: import javax.servlet.jsp.tagext.*;
21:
22: import java.io.*;
23:
24: /**
25: * Display the sources of the JSP file.
26: */
27: public class ShowSource extends TagSupport {
28: String jspFile;
29:
30: public void setJspFile(String jspFile) {
31: this .jspFile = jspFile;
32: }
33:
34: public int doEndTag() throws JspException {
35: if ((jspFile.indexOf("..") >= 0)
36: || (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0)
37: || (jspFile.toUpperCase().indexOf("/META-INF/") != 0))
38: throw new JspTagException("Invalid JSP file " + jspFile);
39:
40: InputStream in = pageContext.getServletContext()
41: .getResourceAsStream(jspFile);
42:
43: if (in == null)
44: throw new JspTagException("Unable to find JSP file: "
45: + jspFile);
46:
47: InputStreamReader reader = new InputStreamReader(in);
48: JspWriter out = pageContext.getOut();
49:
50: try {
51: out.println("<body>");
52: out.println("<pre>");
53: for (int ch = in.read(); ch != -1; ch = in.read())
54: if (ch == '<')
55: out.print("<");
56: else
57: out.print((char) ch);
58: out.println("</pre>");
59: out.println("</body>");
60: } catch (IOException ex) {
61: throw new JspTagException("IOException: " + ex.toString());
62: }
63: return super.doEndTag();
64: }
65: }
|