View Javadoc

    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
    20  package org.apache.myfaces.shared_orchestra.util.servlet;
    21
    22  import javax.servlet.*;
    23  import javax.servlet.http.*;
    24  import java.io.*;
    25
    26  public class SourceCodeServlet extends HttpServlet
    27  {
    28      public void doGet(HttpServletRequest req, HttpServletResponse res)
    29          throws IOException, ServletException
    30      {
    31          String webPage = req.getServletPath();
    32
    33          // remove the '*.source' suffix that maps to this servlet
    34          int chopPoint = webPage.lastIndexOf(".source");
    35
    36          webPage = webPage.substring(0, chopPoint);
    37
    38          if(webPage.endsWith(".jsf"))
    39          {
    40              int jsfChopPoint = webPage.lastIndexOf(".jsf");
    41
    42              webPage = webPage.substring(0, jsfChopPoint);
    43
    44              webPage += ".jsp"; // replace jsf with jsp
    45
    46              // get the actual file location of the requested resource
    47              String realPath = getServletConfig().getServletContext().getRealPath(webPage);
    48
    49              outputFile(res, realPath);
    50          }
    51          else if(webPage.endsWith(".jsp"))
    52          {
    53              // get the actual file location of the requested resource
    54              String realPath = getServletConfig().getServletContext().getRealPath(webPage);
    55
    56              outputFile(res, realPath);
    57          }
    58          else if(webPage.endsWith(".jspx"))
    59          {
    60              // get the actual file location of the requested resource
    61              String realPath = getServletConfig().getServletContext().getRealPath(webPage);
    62
    63              outputFile(res, realPath);
    64          }
    65          else if(webPage.endsWith(".xhtml"))
    66          {
    67              // get the actual file location of the requested resource
    68              String realPath = getServletConfig().getServletContext().getRealPath(webPage);
    69
    70              outputFile(res, realPath);
    71          }
    72          else
    73          {
    74              int beginChopPoint = webPage.lastIndexOf("/");
    75              int extensionChopPoint = webPage.lastIndexOf(".java");
    76
    77              webPage = webPage.substring(beginChopPoint+1,extensionChopPoint);
    78
    79              try
    80              {
    81
    82                  // get the actual file location of the requested resource; try it in classes first
    83                  String realPath = getServletConfig().getServletContext().getRealPath("/WEB-INF/classes/"+webPage.replace('.','/')+".java");
    84
    85                  outputFile(res, realPath);
    86              }
    87              catch(Exception e)
    88              {
    89                  //classes hasn't worked. How about src?
    90                  String realPath = getServletConfig().getServletContext().getRealPath("/WEB-INF/src/"+webPage.replace('.','/')+".java");
    91
    92                  outputFile(res, realPath);
    93              }
    94
    95          }
    96      }
    97
    98      private void outputFile(HttpServletResponse res, String realPath)
    99              throws IOException
    100     {
    101         // output an HTML page
    102         res.setContentType("text/plain");
    103
    104         // print some html
    105         ServletOutputStream out = res.getOutputStream();
    106
    107         // print the file
    108         InputStream in = null;
    109         try
    110         {
    111             in = new BufferedInputStream(new FileInputStream(realPath));
    112             int ch;
    113             while ((ch = in.read()) !=-1)
    114             {
    115                 out.print((char)ch);
    116             }
    117         }
    118         finally {
    119             if (in != null) in.close();  // very important
    120         }
    121     }
    122 }