Created
August 7, 2017 00:45
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bluelotussoftware.example.richfaces; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.net.URLDecoder; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* A servlet that displays XHTML/Java Source code as plain text from the project | |
* in which the servlet is included. | |
* | |
* @author John Yeary | |
* @version 1.0 | |
*/ | |
@WebServlet(name = "SourceServlet", urlPatterns = {"/source/*"}) | |
public class SourceServlet extends HttpServlet { | |
private static final long serialVersionUID = 3859447979763694232L; | |
/** | |
* Processes requests for both HTTP | |
* <code>GET</code> and | |
* <code>POST</code> methods. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
String basePath = getServletContext().getRealPath("/"); | |
if (request.getPathInfo() == null || "/".equals(request.getPathInfo())) { | |
//NOI18N | |
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "An file name is required."); | |
return; | |
} | |
String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8"); | |
File file = new File(basePath, filename); | |
if (!file.exists()) { | |
//NOI18N | |
response.sendError(HttpServletResponse.SC_NOT_FOUND, file.getName() + " was not found."); | |
return; | |
} | |
response.setContentType("text/plain"); | |
response.setContentLength((int) file.length()); | |
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\""); | |
BufferedInputStream input = null; | |
BufferedOutputStream output = null; | |
try { | |
input = new BufferedInputStream(new FileInputStream(file)); | |
output = new BufferedOutputStream(response.getOutputStream()); | |
byte[] buffer = new byte[8192]; | |
int length; | |
while ((length = input.read(buffer)) > 0) { | |
output.write(buffer, 0, length); | |
} | |
} finally { | |
if (output != null) { | |
try { | |
output.close(); | |
} catch (IOException ignore) { | |
} | |
} | |
if (input != null) { | |
try { | |
input.close(); | |
} catch (IOException ignore) { | |
} | |
} | |
} | |
} | |
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> | |
/** | |
* Handles the HTTP | |
* <code>GET</code> method. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Handles the HTTP | |
* <code>POST</code> method. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Returns a short description of the servlet. | |
* | |
* @return a String containing servlet description | |
*/ | |
@Override | |
public String getServletInfo() { | |
return "This servlet will serve source code from a project as text/plain."; | |
}// </editor-fold> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment