修复发生404等错误码时,重定向到error目录的bug
This commit is contained in:
parent
71e1266d02
commit
46d1dfb71f
|
@ -1,12 +1,12 @@
|
|||
package com.github.zxbu.webdavteambition;
|
||||
|
||||
import com.github.zxbu.webdavteambition.filter.ErrorFilter;
|
||||
import com.github.zxbu.webdavteambition.store.AliYunDriverFileSystemStore;
|
||||
import net.sf.webdav.WebdavServlet;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.support.ErrorPageFilter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
@ -31,16 +31,12 @@ public class WebdavTeambitionApplication {
|
|||
return servletRegistrationBean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorPageFilter errorPageFilter() {
|
||||
return new ErrorPageFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
|
||||
public FilterRegistrationBean disableSpringBootErrorFilter() {
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
|
||||
filterRegistrationBean.setFilter(filter);
|
||||
filterRegistrationBean.setEnabled(false);
|
||||
filterRegistrationBean.setFilter(new ErrorFilter());
|
||||
filterRegistrationBean.setEnabled(true);
|
||||
return filterRegistrationBean;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import okhttp3.*;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -17,8 +16,8 @@ import java.nio.file.LinkOption;
|
|||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class AliYunDriverClient {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AliYunDriverClient.class);
|
||||
|
@ -57,7 +56,11 @@ public class AliYunDriverClient {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
}).build();
|
||||
})
|
||||
.readTimeout(1, TimeUnit.MINUTES)
|
||||
.writeTimeout(1, TimeUnit.MINUTES)
|
||||
.connectTimeout(1, TimeUnit.MINUTES)
|
||||
.build();
|
||||
this.okHttpClient = okHttpClient;
|
||||
this.aliYunDriveProperties = aliYunDriveProperties;
|
||||
init();
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
package com.github.zxbu.webdavteambition.filter;
|
||||
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpServletResponseWrapper;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class ErrorFilter extends OncePerRequestFilter {
|
||||
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
|
||||
ErrorWrapperResponse wrapperResponse = new ErrorWrapperResponse(httpServletResponse);
|
||||
|
||||
try {
|
||||
filterChain.doFilter(httpServletRequest, wrapperResponse);
|
||||
if (wrapperResponse.hasErrorToSend()) {
|
||||
httpServletResponse.setStatus(wrapperResponse.getStatus());
|
||||
httpServletResponse.getWriter().write(wrapperResponse.getMessage());
|
||||
}
|
||||
httpServletResponse.flushBuffer();
|
||||
} catch (Throwable t) {
|
||||
httpServletResponse.setStatus(500);
|
||||
httpServletResponse.getWriter().write(t.getMessage());
|
||||
httpServletResponse.flushBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
private static class ErrorWrapperResponse extends HttpServletResponseWrapper {
|
||||
private int status;
|
||||
private String message;
|
||||
private boolean hasErrorToSend = false;
|
||||
|
||||
ErrorWrapperResponse(HttpServletResponse response) {
|
||||
super(response);
|
||||
}
|
||||
|
||||
public void sendError(int status) throws IOException {
|
||||
this.sendError(status, (String) null);
|
||||
}
|
||||
|
||||
public void sendError(int status, String message) throws IOException {
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
this.hasErrorToSend = true;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return this.hasErrorToSend ? this.status : super.getStatus();
|
||||
}
|
||||
|
||||
public void flushBuffer() throws IOException {
|
||||
super.flushBuffer();
|
||||
}
|
||||
|
||||
|
||||
String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
boolean hasErrorToSend() {
|
||||
return this.hasErrorToSend;
|
||||
}
|
||||
|
||||
public PrintWriter getWriter() throws IOException {
|
||||
return super.getWriter();
|
||||
}
|
||||
|
||||
public ServletOutputStream getOutputStream() throws IOException {
|
||||
return super.getOutputStream();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue