fr.hhdev.ocelot.web.JSServlet Maven / Gradle / Ivy
The newest version!
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package fr.hhdev.ocelot.web;
import fr.hhdev.ocelot.Constants;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Servlet to serve ocelot.js
*
* @author hhfrancois
*/
@WebServlet(urlPatterns = {Constants.SLASH_OCELOT_JS})
public class JSServlet extends HttpServlet {
private static final long serialVersionUID = 1973549844535787671L;
private final static Logger logger = LoggerFactory.getLogger(JSServlet.class);
/**
* Processes requests for both HTTP GET
and POST
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 {
response.setContentType(Constants.JSTYPE);
String minify = request.getParameter(Constants.MINIFY_PARAMETER);
String filename = request.getServletContext().getInitParameter(Constants.OCELOT_MIN);
if (Constants.FALSE.equalsIgnoreCase(minify)) {
filename = request.getServletContext().getInitParameter(Constants.OCELOT);
}
long lg = new File(filename).length();
logger.debug("Get resource {}({})", filename, lg);
response.setContentLength((int) lg);
try (Writer out = response.getWriter()) {
try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.write(inputLine);
out.write(Constants.BACKSLASH_N);
}
}
}
}
//
/**
* Handles the HTTP GET
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 POST
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 "Short description";
}//
}