001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package core.tut.pori.context;
017
018import java.io.IOException;
019
020import javax.servlet.RequestDispatcher;
021import javax.servlet.ServletException;
022import javax.servlet.http.HttpServlet;
023import javax.servlet.http.HttpServletRequest;
024import javax.servlet.http.HttpServletResponse;
025
026import org.apache.log4j.Logger;
027
028import core.tut.pori.http.Response;
029import core.tut.pori.http.Response.Status;
030
031/**
032 * The generic fall-back error handler.
033 * 
034 * Called when a critical internal error (such as null pointer exception) happens during servlet execution.
035 * 
036 * This class will convert all errors to a 500 INTERNAL SERVER ERROR HTTP response hiding the actual error
037 * from the user, and prints the exception stacktrace (if available) to the system log.
038 * 
039 */
040public class ErrorHandler extends HttpServlet {
041  /** serial id */
042  private static final long serialVersionUID = -2642062282485268614L;
043  private static final String ATTRIBUTE_EXCEPTION = "javax.servlet.error.exception";
044  private static final Logger LOGGER = Logger.getLogger(ErrorHandler.class);
045
046  @Override
047  protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
048    processGeneric(req, resp);
049  }
050
051  @Override
052  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
053    processGeneric(req, resp);
054  }
055
056  @Override
057  protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
058    processGeneric(req, resp);
059  }
060
061  @Override
062  protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
063    processGeneric(req, resp);
064  }
065
066  @Override
067  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
068    processGeneric(req, resp);
069  }
070
071  @Override
072  protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
073    processGeneric(req, resp);
074  }
075
076  @Override
077  protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
078    processGeneric(req, resp);
079  }
080
081  /**
082   * 
083   * @param req
084   * @param resp
085   */
086  private void processGeneric(HttpServletRequest req, HttpServletResponse resp){
087    String requestUri = (String) req.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
088    int status = resp.getStatus();
089    LOGGER.error(req.getMethod()+" "+requestUri+", status: "+status, (Throwable) req.getAttribute(ATTRIBUTE_EXCEPTION));
090    
091    Response r = new Response(Status.fromStatusCode(status));
092    r.setMessage(requestUri);
093    r.writeTo(resp);
094  }
095}