com.miserablemind.butter.apps.butterApp.controller.error.HTTPErrorController.java Source code

Java tutorial

Introduction

Here is the source code for com.miserablemind.butter.apps.butterApp.controller.error.HTTPErrorController.java

Source

package com.miserablemind.butter.apps.butterApp.controller.error;

import com.miserablemind.butter.apps.butterApp.exception.HTTPBadRequestException;
import com.miserablemind.butter.apps.butterApp.exception.HTTPInternalServerErrorException;
import com.miserablemind.butter.apps.butterApp.exception.HTTPNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

/*
 * Miserable Mind
 * http://www.butter.miserablemind.com
 * The MIT License (MIT)
 */

/**
 * HTTP Response Status Error pages.
 * <p>Used to override ugly servlet container errors. The errors to urls are mapped in web.xml.</p>
 * <p>In case there is a need to show an error message from a controller, it is better to throw an exception right there that would be handled by
 * {@link com.miserablemind.butter.apps.butterApp.controller.ControllerExceptionHandler}.</p>
 *
 * @author <a href="http://www.miserablemind.com" target="_blank">miserablemind</a>
 */
@Controller
@RequestMapping("/error")
public class HTTPErrorController {

    /**
     * @param request HTTP request to handle
     * @return never gets to return value. Always throws an exception.
     * @throws com.miserablemind.butter.apps.butterApp.exception.HTTPNotFoundException a 404 Exception that gets handled by {@link com.miserablemind.butter.apps.butterApp.controller.ControllerExceptionHandler}
     */
    @RequestMapping(method = RequestMethod.GET, value = "/404")
    public String handle404(HttpServletRequest request) throws HTTPNotFoundException {
        String originalUrl = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
        throw new HTTPNotFoundException("URL: " + originalUrl);
    }

    /**
     * @param request HTTP request to handle
     * @return never gets to return value. Always throws an exception.
     * @throws com.miserablemind.butter.apps.butterApp.exception.HTTPBadRequestException a 400 Exception that gets handled by {@link com.miserablemind.butter.apps.butterApp.controller.ControllerExceptionHandler}
     */
    @RequestMapping(method = RequestMethod.GET, value = "/400")
    public String handle400(HttpServletRequest request) throws HTTPBadRequestException {
        String originalUrl = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
        throw new HTTPBadRequestException("URL: " + originalUrl);
    }

    /**
     * @param request HTTP request to handle
     * @return never gets to return value. Always throws an exception.
     * @throws com.miserablemind.butter.apps.butterApp.exception.HTTPInternalServerErrorException a 500 Exception that gets handled by {@link com.miserablemind.butter.apps.butterApp.controller.ControllerExceptionHandler}
     */
    @RequestMapping(method = RequestMethod.GET, value = "/500")
    public String handle500(HttpServletRequest request) throws HTTPInternalServerErrorException {
        String originalUrl = (String) request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
        throw new HTTPInternalServerErrorException("URL: " + originalUrl);
    }

}