/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.ui.core.controller;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UncaughtExceptionController {

    private static final Logger logger = LoggerFactory.getLogger(UncaughtExceptionController.class);

    @RequestMapping("/uncaughtException.ftl")
    public ModelAndView handleException(HttpServletRequest request) {
        // http://java.sun.com/developer/technicalArticles/Servlets/servletapi2.3
        Object obj = request.getAttribute("javax.servlet.error.exception");
        Throwable cause = null;
        if (obj != null) {
            cause = (Throwable) obj;
        }

        String requestUri = request.getRequestURI();
        logger.error("Uncaught exception while accessing '" + requestUri + "'", cause);

        ModelAndView mm = new ModelAndView();
        mm.setViewName("uncaughtException");
        mm.addObject("uncaughtException", cause);
        mm.addObject("requestUri", requestUri);
        if (isDevCookieAvailable(request) && cause != null) {
            Writer result = new StringWriter();
            cause.printStackTrace(new PrintWriter(result));
            mm.addObject("stackString", result.toString());
        }
        return mm;
    }

    private boolean isDevCookieAvailable(HttpServletRequest request) {
        boolean cookieExists = false;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("MifosDevCookie".equals(cookie.getName())) {
                    cookieExists = true;
                    break;
                }
            }
        }
        return cookieExists;
    }
}

@@ -871,25 +871,24 @@
 .io.Writer;%0A
-%0A
 import javax
@@ -924,42 +924,8 @@
 est;
-%0Aimport javax.servlet.http.Cookie;
 %0A%0Aim
@@ -2032,41 +2032,8 @@
 if (
-isDevCookieAvailable(request) && 
 caus
@@ -2251,451 +2251,7 @@
    %7D
-%0A%0A    private boolean isDevCookieAvailable(HttpServletRequest request) %7B%0A        boolean cookieExists = false;%0A        Cookie%5B%5D cookies = request.getCookies();%0A        if (cookies != null) %7B%0A            for (Cookie cookie : cookies) %7B%0A                if (%22MifosDevCookie%22.equals(cookie.getName())) %7B%0A                    cookieExists = true;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        return cookieExists;%0A    %7D
 %0A%7D%0A

Enable Stack trace in the UI