com.jaspersoft.jasperserver.war.control.interceptor.RenderViewExceptionInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.jaspersoft.jasperserver.war.control.interceptor.RenderViewExceptionInterceptor.java

Source

/*
 * Copyright (C) 2005 - 2014 TIBCO Software Inc. All rights reserved.
 * http://www.jaspersoft.com.
 *
 * Unless you have purchased  a commercial license agreement from Jaspersoft,
 * the following license terms  apply:
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License  as
 * published by the Free Software Foundation, either version 3 of  the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero  General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public  License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.jaspersoft.jasperserver.war.control.interceptor;

import com.jaspersoft.jasperserver.api.JSException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Handles exceptions which occurred during view rendering.
 *
 * @author Yuriy Plakosh
 */
public class RenderViewExceptionInterceptor extends HandlerInterceptorAdapter {
    protected final Log log = LogFactory.getLog(this.getClass());

    private MessageSource messageSource;

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) throws Exception {
        if (ex != null) {
            String message;

            Locale locale = LocaleContextHolder.getLocale();
            if (ex instanceof JSException) {
                JSException jsException = (JSException) ex;
                message = messageSource.getMessage(ex.getMessage(), jsException.getArgs(), locale);
            } else {
                message = messageSource.getMessage("error.500.message", null, locale);
            }

            log.error(message, ex);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
        }
    }

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public MessageSource getMessageSource() {
        return messageSource;
    }
}