Use HttpStatus in ErrorPage
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.bootstrap.context.embedded;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
@@ -31,14 +32,14 @@ public class ErrorPage {
|
||||
|
||||
private Class<? extends Throwable> exception = null;
|
||||
|
||||
private int status = 0;
|
||||
private HttpStatus status = null;
|
||||
|
||||
public ErrorPage(String path) {
|
||||
super();
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public ErrorPage(int status, String path) {
|
||||
public ErrorPage(HttpStatus status, String path) {
|
||||
super();
|
||||
this.status = status;
|
||||
this.path = path;
|
||||
@@ -68,13 +69,22 @@ public class ErrorPage {
|
||||
return this.exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* The HTTP status value that this error page matches.
|
||||
*
|
||||
* @return the status
|
||||
*/
|
||||
public HttpStatus getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
/**
|
||||
* The HTTP status value that this error page matches.
|
||||
*
|
||||
* @return the status value (or 0 for a page that matches any status)
|
||||
*/
|
||||
public int getStatus() {
|
||||
return this.status;
|
||||
public int getStatusCode() {
|
||||
return this.status == null ? 0 : this.status.value();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +101,7 @@ public class ErrorPage {
|
||||
* types)?
|
||||
*/
|
||||
public boolean isGlobal() {
|
||||
return this.status == 0 && this.exception == null;
|
||||
return this.status == null && this.exception == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,7 +110,7 @@ public class ErrorPage {
|
||||
int result = 1;
|
||||
result = prime * result + ObjectUtils.nullSafeHashCode(getExceptionName());
|
||||
result = prime * result + ObjectUtils.nullSafeHashCode(this.path);
|
||||
result = prime * result + this.status;
|
||||
result = prime * result + this.getStatusCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -67,6 +67,8 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
private WebAppContext context = new WebAppContext();
|
||||
|
||||
/**
|
||||
* Create a new {@link JettyEmbeddedServletContainerFactory} instance.
|
||||
*/
|
||||
@@ -101,29 +103,29 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||
}
|
||||
Server server = new Server(new InetSocketAddress(getAddress(), getPort()));
|
||||
|
||||
WebAppContext context = new WebAppContext();
|
||||
if (this.resourceLoader != null) {
|
||||
context.setClassLoader(this.resourceLoader.getClassLoader());
|
||||
this.context.setClassLoader(this.resourceLoader.getClassLoader());
|
||||
}
|
||||
String contextPath = getContextPath();
|
||||
context.setContextPath(StringUtils.hasLength(contextPath) ? contextPath : "/");
|
||||
configureDocumentRoot(context);
|
||||
this.context.setContextPath(StringUtils.hasLength(contextPath) ? contextPath
|
||||
: "/");
|
||||
configureDocumentRoot(this.context);
|
||||
if (getRegisterDefaultServlet()) {
|
||||
addDefaultServlet(context);
|
||||
addDefaultServlet(this.context);
|
||||
}
|
||||
if (getRegisterJspServlet()
|
||||
&& ClassUtils.isPresent(getJspServletClassName(), getClass()
|
||||
.getClassLoader())) {
|
||||
addJspServlet(context);
|
||||
addJspServlet(this.context);
|
||||
}
|
||||
|
||||
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
|
||||
Configuration[] configurations = getWebAppContextConfigurations(context,
|
||||
Configuration[] configurations = getWebAppContextConfigurations(this.context,
|
||||
initializersToUse);
|
||||
context.setConfigurations(configurations);
|
||||
postProcessWebAppContext(context);
|
||||
this.context.setConfigurations(configurations);
|
||||
postProcessWebAppContext(this.context);
|
||||
|
||||
server.setHandler(context);
|
||||
server.setHandler(this.context);
|
||||
return getJettyEmbeddedServletContainer(server);
|
||||
}
|
||||
|
||||
@@ -187,23 +189,7 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||
@Override
|
||||
public void configure(WebAppContext context) throws Exception {
|
||||
ErrorHandler errorHandler = context.getErrorHandler();
|
||||
if (errorHandler instanceof ErrorPageErrorHandler) {
|
||||
ErrorPageErrorHandler handler = (ErrorPageErrorHandler) errorHandler;
|
||||
for (ErrorPage errorPage : getErrorPages()) {
|
||||
if (errorPage.isGlobal()) {
|
||||
handler.addErrorPage(ErrorPageErrorHandler.GLOBAL_ERROR_PAGE,
|
||||
errorPage.getPath());
|
||||
} else {
|
||||
if (errorPage.getExceptionName() != null) {
|
||||
handler.addErrorPage(errorPage.getExceptionName(),
|
||||
errorPage.getPath());
|
||||
} else {
|
||||
handler.addErrorPage(errorPage.getStatus(),
|
||||
errorPage.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
addJettyErrorPages(errorHandler, getErrorPages());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -269,11 +255,33 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||
|
||||
/**
|
||||
* Add {@link Configuration}s that will be applied to the {@link WebAppContext} before
|
||||
* the server is create.
|
||||
* the server is started.
|
||||
*
|
||||
* @param configurations the configurations to add
|
||||
*/
|
||||
public void addConfigurations(Configuration... configurations) {
|
||||
Assert.notNull(configurations, "Configurations must not be null");
|
||||
this.configurations.addAll(Arrays.asList(configurations));
|
||||
}
|
||||
|
||||
private void addJettyErrorPages(ErrorHandler errorHandler,
|
||||
Collection<ErrorPage> errorPages) {
|
||||
if (errorHandler instanceof ErrorPageErrorHandler) {
|
||||
ErrorPageErrorHandler handler = (ErrorPageErrorHandler) errorHandler;
|
||||
for (ErrorPage errorPage : errorPages) {
|
||||
if (errorPage.isGlobal()) {
|
||||
handler.addErrorPage(ErrorPageErrorHandler.GLOBAL_ERROR_PAGE,
|
||||
errorPage.getPath());
|
||||
} else {
|
||||
if (errorPage.getExceptionName() != null) {
|
||||
handler.addErrorPage(errorPage.getExceptionName(),
|
||||
errorPage.getPath());
|
||||
} else {
|
||||
handler.addErrorPage(errorPage.getStatusCode(),
|
||||
errorPage.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ public class TomcatEmbeddedServletContainerFactory extends
|
||||
org.apache.catalina.deploy.ErrorPage tomcatPage = new org.apache.catalina.deploy.ErrorPage();
|
||||
tomcatPage.setLocation(errorPage.getPath());
|
||||
tomcatPage.setExceptionType(errorPage.getExceptionName());
|
||||
tomcatPage.setErrorCode(errorPage.getStatus());
|
||||
tomcatPage.setErrorCode(errorPage.getStatusCode());
|
||||
context.addErrorPage(tomcatPage);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user