All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver Maven / Gradle / Ivy

There is a newer version: 3.2.5
Show newest version
/*
 * Copyright 2012-2020 the original author or authors.
 *
 * 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
 *
 *      https://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 org.springframework.boot.autoconfigure.web.servlet.error;

import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;

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

import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
import org.springframework.boot.autoconfigure.web.WebProperties.Resources;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatus.Series;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

/**
 * Default {@link ErrorViewResolver} implementation that attempts to resolve error views
 * using well known conventions. Will search for templates and static assets under
 * {@code '/error'} using the {@link HttpStatus status code} and the
 * {@link HttpStatus#series() status series}.
 * 

* For example, an {@code HTTP 404} will search (in the specific order): *

    *
  • {@code '//error/404.'}
  • *
  • {@code '//error/404.html'}
  • *
  • {@code '//error/4xx.'}
  • *
  • {@code '//error/4xx.html'}
  • *
* * @author Phillip Webb * @author Andy Wilkinson * @since 1.4.0 */ public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered { private static final Map SERIES_VIEWS; static { Map views = new EnumMap<>(Series.class); views.put(Series.CLIENT_ERROR, "4xx"); views.put(Series.SERVER_ERROR, "5xx"); SERIES_VIEWS = Collections.unmodifiableMap(views); } private ApplicationContext applicationContext; private final Resources resources; private final TemplateAvailabilityProviders templateAvailabilityProviders; private int order = Ordered.LOWEST_PRECEDENCE; /** * Create a new {@link DefaultErrorViewResolver} instance. * @param applicationContext the source application context * @param resourceProperties resource properties * @deprecated since 2.4.0 in favour of * {@link #DefaultErrorViewResolver(ApplicationContext, Resources)} */ @Deprecated public DefaultErrorViewResolver(ApplicationContext applicationContext, org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties) { this(applicationContext, (Resources) resourceProperties); } /** * Create a new {@link DefaultErrorViewResolver} instance. * @param applicationContext the source application context * @param resources resource properties * @since 2.4.0 */ public DefaultErrorViewResolver(ApplicationContext applicationContext, Resources resources) { Assert.notNull(applicationContext, "ApplicationContext must not be null"); Assert.notNull(resources, "Resources must not be null"); this.applicationContext = applicationContext; this.resources = resources; this.templateAvailabilityProviders = new TemplateAvailabilityProviders(applicationContext); } DefaultErrorViewResolver(ApplicationContext applicationContext, Resources resourceProperties, TemplateAvailabilityProviders templateAvailabilityProviders) { Assert.notNull(applicationContext, "ApplicationContext must not be null"); Assert.notNull(resourceProperties, "Resources must not be null"); this.applicationContext = applicationContext; this.resources = resourceProperties; this.templateAvailabilityProviders = templateAvailabilityProviders; } @Override public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map model) { ModelAndView modelAndView = resolve(String.valueOf(status.value()), model); if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) { modelAndView = resolve(SERIES_VIEWS.get(status.series()), model); } return modelAndView; } private ModelAndView resolve(String viewName, Map model) { String errorViewName = "error/" + viewName; TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName, this.applicationContext); if (provider != null) { return new ModelAndView(errorViewName, model); } return resolveResource(errorViewName, model); } private ModelAndView resolveResource(String viewName, Map model) { for (String location : this.resources.getStaticLocations()) { try { Resource resource = this.applicationContext.getResource(location); resource = resource.createRelative(viewName + ".html"); if (resource.exists()) { return new ModelAndView(new HtmlResourceView(resource), model); } } catch (Exception ex) { } } return null; } @Override public int getOrder() { return this.order; } public void setOrder(int order) { this.order = order; } /** * {@link View} backed by an HTML resource. */ private static class HtmlResourceView implements View { private Resource resource; HtmlResourceView(Resource resource) { this.resource = resource; } @Override public String getContentType() { return MediaType.TEXT_HTML_VALUE; } @Override public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType(getContentType()); FileCopyUtils.copy(this.resource.getInputStream(), response.getOutputStream()); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy