com.baron.bm.controller.ErrorController.java Source code

Java tutorial

Introduction

Here is the source code for com.baron.bm.controller.ErrorController.java

Source

/*
 * @(#)ErrorController.java $version 2014. 11. 23.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.baron.bm.controller;

import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author pokbab
 */
@Controller
public class ErrorController {

    /**
     *  ?  web.xml? ?? ?
     */
    @RequestMapping("/error")
    public String throwException(HttpServletRequest request, HttpServletResponse response, Model model) {
        int statusCode = (int) request.getAttribute("javax.servlet.error.status_code");

        String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
        if (requestUri == null) {
            requestUri = "   "; //Unknown
        }

        String message;

        if (statusCode == 400) {
            message = MessageFormat.format(ErrorType.ERROR_400.getMessage(), requestUri);
        } else if (statusCode == 403) {
            message = MessageFormat.format(ErrorType.ERROR_403.getMessage(), requestUri);
        } else if (statusCode == 404) {
            message = MessageFormat.format(ErrorType.ERROR_404.getMessage(), requestUri);
        } else if (statusCode == 500) {
            message = MessageFormat.format(ErrorType.ERROR_500.getMessage(), requestUri);
        } else {
            message = MessageFormat.format(ErrorType.ERROR_ELSE.getMessage(), requestUri);
        }

        model.addAttribute("errorMessage", message);
        return "/common/error";
    }

    enum ErrorType {
        ERROR_VALIDATION(" ? ? ?.  "), ERROR_400(
                "?? : {0}<br/>? . ?   ?!"), ERROR_403(
                        "?? : {0}<br/>? ? ?"), ERROR_404(
                                "?? : {0}<br/> ?  .   ?!"), ERROR_500(
                                        "?? : {0}<br/>? ?  . ?? ?."), ERROR_ELSE(
                                                "?? : {0}<br/>??? ? ? ?.  ? ?  ?? ? !");

        private String message;

        private ErrorType(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }
    }
}