de.otto.mongodb.profiler.web.DefaultController.java Source code

Java tutorial

Introduction

Here is the source code for de.otto.mongodb.profiler.web.DefaultController.java

Source

/*
 *  Copyright 2013 Robert Gacki <robert.gacki@cgi.com>
 *
 *  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.
 */
package de.otto.mongodb.profiler.web;

import de.otto.mongodb.profiler.ProfilerService;
import de.otto.mongodb.profiler.util.Logger;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(DefaultController.CONTROLLER_RESOURCE)
public class DefaultController extends AbstractController {

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

    public static final String CONTROLLER_RESOURCE = "/";

    private static void logError(HttpServletRequest request) {

        final Throwable t = throwableFrom(request);
        if (t != null) {
            logger.error(t, "Unhandled exception caught!");
        }
    }

    private static Throwable throwableFrom(HttpServletRequest request) {

        final Object obj = request.getAttribute("javax.servlet.error.exception");
        return obj != null && obj instanceof Throwable ? ((Throwable) obj) : null;
    }

    public DefaultController(ProfilerService profilerService) {
        super(profilerService);
    }

    @Page
    @RequestMapping(method = RequestMethod.GET)
    public String showWelcomePage() {
        return "page.welcome";
    }

    @Page
    @RequestMapping(value = "404", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView showPageNotFound() {

        return new ModelAndView("page.404");
    }

    @Page
    @RequestMapping(value = "500", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView showInternalServerError(HttpServletRequest request) {

        logError(request);
        return new ModelAndView("page.500");
    }

}