001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package examples; 018 019 020 import java.io.IOException; 021 import java.io.InputStream; 022 import java.io.InputStreamReader; 023 024 import javax.servlet.jsp.JspException; 025 import javax.servlet.jsp.JspTagException; 026 import javax.servlet.jsp.JspWriter; 027 import javax.servlet.jsp.tagext.TagSupport; 028 029 /** 030 * Display the sources of the JSP file. 031 */ 032 public class ShowSource 033 extends TagSupport { 034 String jspFile; 035 036 public void setJspFile(String jspFile) { 037 this.jspFile = jspFile; 038 } 039 040 public int doEndTag() throws JspException { 041 if ((jspFile.indexOf("..") >= 0) || 042 (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0) || 043 (jspFile.toUpperCase().indexOf("/META-INF/") != 0)) 044 throw new JspTagException("Invalid JSP file " + jspFile); 045 046 InputStream in 047 = pageContext.getServletContext().getResourceAsStream(jspFile); 048 049 if (in == null) 050 throw new JspTagException("Unable to find JSP file: " + jspFile); 051 052 InputStreamReader reader = new InputStreamReader(in); 053 JspWriter out = pageContext.getOut(); 054 055 056 try { 057 out.println("<body>"); 058 out.println("<pre>"); 059 for (int ch = in.read(); ch != -1; ch = in.read()) 060 if (ch == '<') 061 out.print("<"); 062 else 063 out.print((char) ch); 064 out.println("</pre>"); 065 out.println("</body>"); 066 } catch (IOException ex) { 067 throw new JspTagException("IOException: " + ex.toString()); 068 } 069 return super.doEndTag(); 070 } 071 } 072 073 074 075