forked from j62/ctbrec
Add automatic redirect and custom 404 page
This commit is contained in:
parent
d2486b2a63
commit
fcbe4a2e25
|
@ -138,6 +138,11 @@
|
|||
<artifactId>jetty-servlet</artifactId>
|
||||
<version>[9.4.19.v20190610,9.99.99)</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-rewrite</artifactId>
|
||||
<version>[9.4.19.v20190610,9.99.99)</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
|
@ -86,7 +86,11 @@
|
|||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-servlet</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-rewrite</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.activation</groupId>
|
||||
<artifactId>javax.activation</artifactId>
|
||||
<version>1.2.0</version>
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package ctbrec.recorder.server;
|
||||
|
||||
import static javax.servlet.http.HttpServletResponse.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Writer;
|
||||
import java.net.BindException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
|
@ -31,8 +34,10 @@ import org.eclipse.jetty.security.authentication.BasicAuthenticator;
|
|||
import org.eclipse.jetty.server.Handler;
|
||||
import org.eclipse.jetty.server.HttpConfiguration;
|
||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.server.handler.ErrorHandler;
|
||||
import org.eclipse.jetty.server.handler.HandlerList;
|
||||
import org.eclipse.jetty.server.handler.SecuredRedirectHandler;
|
||||
import org.eclipse.jetty.servlet.FilterHolder;
|
||||
|
@ -198,6 +203,7 @@ public class HttpServer {
|
|||
https.setIdleTimeout(this.config.getSettings().httpTimeout);
|
||||
|
||||
String contextPath = Config.getInstance().getContextPath();
|
||||
server.setErrorHandler(createErrorHandler(contextPath));
|
||||
ServletContextHandler basicAuthContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
|
||||
basicAuthContext.setContextPath(contextPath + "/secured");
|
||||
|
||||
|
@ -270,6 +276,30 @@ public class HttpServer {
|
|||
}
|
||||
}
|
||||
|
||||
private ErrorHandler createErrorHandler(String contextPath) {
|
||||
return new ErrorHandler() {
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
if (response.getStatus() == SC_NOT_FOUND && (request.getRequestURI().equals("/") || request.getRequestURI().equals("/index.html"))) {
|
||||
response.setStatus(SC_MOVED_PERMANENTLY);
|
||||
response.setHeader("Location", contextPath + "/static/index.html");
|
||||
} else {
|
||||
super.handle(target, baseRequest, request, response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException {
|
||||
if (code == 404) {
|
||||
writer.write("<html><head><title>404</title><style>* {font-family: sans-serif}</style></head><body><h1>404</h1><p>Looking for <a href=\""+contextPath+"/static/index.html\">CTB Recorder</a>?</p></body>");
|
||||
} else {
|
||||
super.handleErrorPage(request, writer, code, message);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void addHttpHeaderFilter(ServletContextHandler defaultContext) {
|
||||
FilterHolder httpHeaderFilter = new FilterHolder(new Filter() {
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue