All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.appslandia.plum.defaults.DefaultProdErrorServlet Maven / Gradle / Ivy

// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.plum.defaults;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
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 com.appslandia.common.utils.AssertUtils;
import com.appslandia.plum.base.ExceptionHandler;
import com.appslandia.plum.base.Problem;
import com.appslandia.plum.base.RequestContext;
import com.appslandia.plum.base.RequestContextParser;
import com.appslandia.plum.base.ResponseSafeWriter;
import com.appslandia.plum.results.JspResult;
import com.appslandia.plum.utils.ServletUtils;

/**
 *
 * @author Loc Ha
 *
 */
@WebServlet(name = "DefaultProdErrorServlet", urlPatterns = "/prod/error")
public class DefaultProdErrorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Inject
	protected ExceptionHandler exceptionHandler;

	@Inject
	protected RequestContextParser requestContextParser;

	protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getDispatcherType() == DispatcherType.REQUEST) {
			response.getWriter().print("/prod/error");
			return;
		}
		AssertUtils.assertTrue(request.getDispatcherType() == DispatcherType.ERROR);

		RequestContext requestContext = this.requestContextParser.parse(request, response);
		Problem problem = (Problem) request.getAttribute(Problem.class.getName());

		if (problem == null) {
			Throwable exception = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
			problem = this.exceptionHandler.getProblem(request, requestContext, exception);
		}

		AssertUtils.assertNotNull(problem.getStatus());
		AssertUtils.assertNotNull(problem.getTitle());

		request.setAttribute(Problem.class.getName(), problem);
		final ResponseSafeWriter responseWrapper = new ResponseSafeWriter(response);

		try {
			ServletUtils.forward(request, responseWrapper, JspResult.getJspLocation(request, "/error.jsp"));
			responseWrapper.flushBuffer();

		} catch (Exception ex) {
			if (!response.isCommitted()) {
				response.resetBuffer();
				this.exceptionHandler.writeSimpleHtml(request, response, problem.getStatus(), problem.getTitle());
			}
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doRequest(req, resp);
	}

	@Override
	protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doRequest(req, resp);
	}

	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doRequest(req, resp);
	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doRequest(req, resp);
	}

	@Override
	protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		throw new UnsupportedOperationException();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy