Skip to content

Instantly share code, notes, and snippets.

@jyeary
Created August 7, 2017 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyeary/b20bb3d520626f1e8611d21c21ab042c to your computer and use it in GitHub Desktop.
Save jyeary/b20bb3d520626f1e8611d21c21ab042c to your computer and use it in GitHub Desktop.
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