From e52568a8ae4e2c6d86457ebf3ddf095a1482579d Mon Sep 17 00:00:00 2001 From: zhouxin Date: Sun, 23 May 2021 20:15:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=B1=E8=B4=A5=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webdavteambition/filter/ErrorFilter.java | 4 ++- .../store/AliYunDriverClientService.java | 9 +++-- .../store/AliYunDriverFileSystemStore.java | 33 +++++++++++------ src/main/java/net/sf/webdav/ITransaction.java | 5 +++ src/main/java/net/sf/webdav/IWebdavStore.java | 14 ++++---- .../net/sf/webdav/LocalFileSystemStore.java | 18 ++++------ src/main/java/net/sf/webdav/Transaction.java | 32 +++++++++++++++++ .../java/net/sf/webdav/WebDavServletBean.java | 35 ++++++------------- 8 files changed, 92 insertions(+), 58 deletions(-) create mode 100644 src/main/java/net/sf/webdav/Transaction.java diff --git a/src/main/java/com/github/zxbu/webdavteambition/filter/ErrorFilter.java b/src/main/java/com/github/zxbu/webdavteambition/filter/ErrorFilter.java index 40f6a1e..9ed6132 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/filter/ErrorFilter.java +++ b/src/main/java/com/github/zxbu/webdavteambition/filter/ErrorFilter.java @@ -22,7 +22,9 @@ public class ErrorFilter extends OncePerRequestFilter { filterChain.doFilter(httpServletRequest, wrapperResponse); if (wrapperResponse.hasErrorToSend()) { httpServletResponse.setStatus(wrapperResponse.getStatus()); - httpServletResponse.getWriter().write(wrapperResponse.getMessage()); + if (wrapperResponse.getMessage() != null) { + httpServletResponse.getWriter().write(wrapperResponse.getMessage()); + } } httpServletResponse.flushBuffer(); } catch (Throwable t) { diff --git a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java index 246df06..cdfeda9 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java +++ b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverClientService.java @@ -4,9 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.zxbu.webdavteambition.client.AliYunDriverClient; import com.github.zxbu.webdavteambition.model.*; -import com.github.zxbu.webdavteambition.model.result.CreateFileResult; -import com.github.zxbu.webdavteambition.model.result.TFileListResult; import com.github.zxbu.webdavteambition.model.result.TFile; +import com.github.zxbu.webdavteambition.model.result.TFileListResult; import com.github.zxbu.webdavteambition.model.result.UploadPreResult; import com.github.zxbu.webdavteambition.util.JsonUtil; import net.sf.webdav.exceptions.WebdavException; @@ -86,10 +85,10 @@ public class AliYunDriverClientService { } } - public void uploadPre(String path, int size, InputStream inputStream) { + public void uploadPre(String path, long size, InputStream inputStream) { path = normalizingPath(path); PathInfo pathInfo = getPathInfo(path); - TFile parent = getTFileByPath(pathInfo.getParentPath()); + TFile parent = getTFileByPath(pathInfo.getParentPath()); if (parent == null) { return; } @@ -111,7 +110,7 @@ public class AliYunDriverClientService { uploadPreRequest.setDrive_id(client.getDriveId()); uploadPreRequest.setName(pathInfo.getName()); uploadPreRequest.setParent_file_id(parent.getFile_id()); - uploadPreRequest.setSize((long) size); + uploadPreRequest.setSize(size); List part_info_list = new ArrayList<>(); for (int i = 0; i < chunkCount; i++) { UploadPreRequest.PartInfo partInfo = new UploadPreRequest.PartInfo(); diff --git a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java index 69cfc6e..6d8c113 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java +++ b/src/main/java/com/github/zxbu/webdavteambition/store/AliYunDriverFileSystemStore.java @@ -6,14 +6,15 @@ import com.github.zxbu.webdavteambition.model.result.TFile; import net.sf.webdav.ITransaction; import net.sf.webdav.IWebdavStore; import net.sf.webdav.StoredObject; +import net.sf.webdav.Transaction; import net.sf.webdav.exceptions.WebdavException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.security.Principal; import java.util.Set; @@ -40,16 +41,19 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { } @Override - public ITransaction begin(Principal principal) { + public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) { LOGGER.debug("begin"); + aliYunDriverClientService.clearCache(); - return null; + return new Transaction(principal, req, resp); } @Override public void checkAuthentication(ITransaction transaction) { LOGGER.debug("checkAuthentication"); - +// if (transaction.getPrincipal() == null) { +// throw new UnauthenticatedException(WebdavStatus.SC_UNAUTHORIZED); +// } } @Override @@ -86,21 +90,28 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @Override public long setResourceContent(ITransaction transaction, String resourceUri, InputStream content, String contentType, String characterEncoding) { LOGGER.info("setResourceContent {}", resourceUri); - ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - HttpServletRequest request = requestAttributes.getRequest(); - int contentLength = request.getContentLength(); + HttpServletRequest request = transaction.getRequest(); + HttpServletResponse response = transaction.getResponse(); + + long contentLength = request.getContentLength(); if (contentLength < 0) { - contentLength = 0; + contentLength = Long.parseLong(request.getHeader("content-length")); } + aliYunDriverClientService.uploadPre(resourceUri, contentLength, content); + if (contentLength == 0) { String expect = request.getHeader("Expect"); // 支持大文件上传 if ("100-continue".equalsIgnoreCase(expect)) { + try { + response.sendError(100, "Continue"); + } catch (IOException e) { + e.printStackTrace(); + } return 0; } } - aliYunDriverClientService.uploadPre(resourceUri, contentLength, content); return contentLength; } @@ -155,6 +166,8 @@ public class AliYunDriverFileSystemStore implements IWebdavStore { @Override public StoredObject getStoredObject(ITransaction transaction, String uri) { + + LOGGER.debug("getStoredObject: {}", uri); TFile tFile = aliYunDriverClientService.getTFileByPath(uri); if (tFile != null) { diff --git a/src/main/java/net/sf/webdav/ITransaction.java b/src/main/java/net/sf/webdav/ITransaction.java index 6b505c3..78f9a21 100644 --- a/src/main/java/net/sf/webdav/ITransaction.java +++ b/src/main/java/net/sf/webdav/ITransaction.java @@ -1,9 +1,14 @@ package net.sf.webdav; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.security.Principal; public interface ITransaction { Principal getPrincipal(); + HttpServletRequest getRequest(); + + HttpServletResponse getResponse(); } diff --git a/src/main/java/net/sf/webdav/IWebdavStore.java b/src/main/java/net/sf/webdav/IWebdavStore.java index 334d195..28dada0 100644 --- a/src/main/java/net/sf/webdav/IWebdavStore.java +++ b/src/main/java/net/sf/webdav/IWebdavStore.java @@ -23,11 +23,13 @@ package net.sf.webdav; +import net.sf.webdav.exceptions.WebdavException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.security.Principal; -import net.sf.webdav.exceptions.WebdavException; - /** * Interface for simple implementation of any store for the WebdavServlet *

@@ -48,15 +50,15 @@ public interface IWebdavStore { * or {@link #rollback()}. If only non-read methods have been called, the * request will be terminated by a {@link #commit()}. This method will be * called by (@link WebdavStoreAdapter} at the beginning of each request. - * - * + * + * * @param principal * the principal that started this request or null if * there is non available - * + * * @throws WebdavException */ - ITransaction begin(Principal principal); + ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp); /** * Checks if authentication information passed in is valid. If not throws an diff --git a/src/main/java/net/sf/webdav/LocalFileSystemStore.java b/src/main/java/net/sf/webdav/LocalFileSystemStore.java index 2422ff9..0bb2813 100644 --- a/src/main/java/net/sf/webdav/LocalFileSystemStore.java +++ b/src/main/java/net/sf/webdav/LocalFileSystemStore.java @@ -15,22 +15,16 @@ */ package net.sf.webdav; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import net.sf.webdav.exceptions.WebdavException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; import java.security.Principal; import java.util.ArrayList; import java.util.Date; import java.util.List; -import net.sf.webdav.exceptions.UnauthenticatedException; -import net.sf.webdav.exceptions.WebdavException; - /** * Reference Implementation of WebdavStore * @@ -54,7 +48,7 @@ public class LocalFileSystemStore implements IWebdavStore { ; } - public ITransaction begin(Principal principal) throws WebdavException { + public ITransaction begin(Principal principal, HttpServletRequest req, HttpServletResponse resp) throws WebdavException { LOG.info("LocalFileSystemStore.begin()"); if (!_root.exists()) { if (!_root.mkdirs()) { diff --git a/src/main/java/net/sf/webdav/Transaction.java b/src/main/java/net/sf/webdav/Transaction.java new file mode 100644 index 0000000..51a4835 --- /dev/null +++ b/src/main/java/net/sf/webdav/Transaction.java @@ -0,0 +1,32 @@ +package net.sf.webdav; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.security.Principal; + +public class Transaction implements ITransaction { + private final Principal principal; + private final HttpServletRequest request; + private final HttpServletResponse response; + + public Transaction(Principal principal, HttpServletRequest request, HttpServletResponse response) { + this.principal = principal; + this.request = request; + this.response = response; + } + + @Override + public Principal getPrincipal() { + return principal; + } + + @Override + public HttpServletRequest getRequest() { + return request; + } + + @Override + public HttpServletResponse getResponse() { + return response; + } +} diff --git a/src/main/java/net/sf/webdav/WebDavServletBean.java b/src/main/java/net/sf/webdav/WebDavServletBean.java index 146710f..dacfaaa 100644 --- a/src/main/java/net/sf/webdav/WebDavServletBean.java +++ b/src/main/java/net/sf/webdav/WebDavServletBean.java @@ -1,5 +1,15 @@ package net.sf.webdav; +import net.sf.webdav.exceptions.UnauthenticatedException; +import net.sf.webdav.exceptions.WebdavException; +import net.sf.webdav.fromcatalina.MD5Encoder; +import net.sf.webdav.locking.ResourceLocks; +import net.sf.webdav.methods.*; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -7,29 +17,6 @@ import java.security.Principal; import java.util.Enumeration; import java.util.HashMap; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import net.sf.webdav.exceptions.UnauthenticatedException; -import net.sf.webdav.exceptions.WebdavException; -import net.sf.webdav.fromcatalina.MD5Encoder; -import net.sf.webdav.locking.ResourceLocks; -import net.sf.webdav.methods.DoCopy; -import net.sf.webdav.methods.DoDelete; -import net.sf.webdav.methods.DoGet; -import net.sf.webdav.methods.DoHead; -import net.sf.webdav.methods.DoLock; -import net.sf.webdav.methods.DoMkcol; -import net.sf.webdav.methods.DoMove; -import net.sf.webdav.methods.DoNotImplemented; -import net.sf.webdav.methods.DoOptions; -import net.sf.webdav.methods.DoPropfind; -import net.sf.webdav.methods.DoProppatch; -import net.sf.webdav.methods.DoPut; -import net.sf.webdav.methods.DoUnlock; - public class WebDavServletBean extends HttpServlet { private static org.slf4j.Logger LOG = org.slf4j.LoggerFactory @@ -124,7 +111,7 @@ public class WebDavServletBean extends HttpServlet { try { Principal userPrincipal = getUserPrincipal(req); - transaction = _store.begin(userPrincipal); + transaction = _store.begin(userPrincipal, req, resp); needRollback = true; _store.checkAuthentication(transaction); resp.setStatus(WebdavStatus.SC_OK);