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 javax.servlet.jsp.*;
021    import javax.servlet.jsp.tagext.*;
022    
023    import java.io.*;
024    
025    /**
026     * Display the sources of the JSP file.
027     */
028    public class ShowSource
029        extends TagSupport
030    {
031        String jspFile;
032        
033        public void setJspFile(String jspFile) {
034            this.jspFile = jspFile;
035        }
036    
037        public int doEndTag() throws JspException {
038            if ((jspFile.indexOf( ".." ) >= 0) ||
039                (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0) ||
040                (jspFile.toUpperCase().indexOf("/META-INF/") != 0))
041                throw new JspTagException("Invalid JSP file " + jspFile);
042    
043            InputStream in
044                = pageContext.getServletContext().getResourceAsStream(jspFile);
045    
046            if (in == null)
047                throw new JspTagException("Unable to find JSP file: "+jspFile);
048    
049            InputStreamReader reader = new InputStreamReader(in);
050            JspWriter out = pageContext.getOut();
051    
052    
053            try {
054                out.println("<body>");
055                out.println("<pre>");
056                for(int ch = in.read(); ch != -1; ch = in.read())
057                    if (ch == '<')
058                        out.print("&lt;");
059                    else
060                        out.print((char) ch);
061                out.println("</pre>");
062                out.println("</body>");
063            } catch (IOException ex) {
064                throw new JspTagException("IOException: "+ex.toString());
065            }
066            return super.doEndTag();
067        }
068    }
069    
070        
071            
072