Skip to content

Commit

Permalink
solve a bug that can't save word
Browse files Browse the repository at this point in the history
  • Loading branch information
ethendev committed Oct 28, 2019
1 parent f971e5a commit d9e51c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
17 changes: 10 additions & 7 deletions src/main/java/com/ethendev/wopihost/service/WopiHostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void getFile(String name, HttpServletResponse response) {

public ResponseEntity postFile(String name, byte[] content, HttpServletRequest request) {
ResponseEntity response;
String path = filePath + name;
File file = new File(path);
String requestLock = request.getHeader(WopiRequestHeader.LOCK);
File file = new File(filePath + name);
if (file.exists()) {
response = lockService.putFile(request, file, content);
if (response.getStatusCodeValue() != WopiStatus.OK.value()) {
Expand All @@ -73,6 +73,7 @@ public ResponseEntity postFile(String name, byte[] content, HttpServletRequest r
response = ResponseEntity.status(WopiStatus.NOT_FOUND.value()).build();
logger.error("postFile failed, file not found");
}
logger.info("postFile -- filename: {}, response: {} , requestLock: {}", name, response, requestLock);
return response;
}

Expand All @@ -95,10 +96,12 @@ public ResponseEntity<FileInfo> getFileInfo(String fileName) throws Exception {

public ResponseEntity handleLock(String fileName, HttpServletRequest request) {
ResponseEntity response = null;
String wopiOverride = request.getHeader("X-WOPI-Override");
String wopiOverride = request.getHeader(WopiRequestHeader.OVERRIDE);
String requestLock = request.getHeader(WopiRequestHeader.LOCK);
String oldLock = request.getHeader(WopiRequestHeader.OLD_LOCK);
switch (wopiOverride) {
case "LOCK":
if (request.getHeader(WopiRequestHeader.OLD_LOCK) != null) {
if (oldLock != null) {
wopiOverride = "UNLOCK_AND_RELOCK";
response = lockService.unlockAndRelock(request, fileName);
} else {
Expand All @@ -115,11 +118,11 @@ public ResponseEntity handleLock(String fileName, HttpServletRequest request) {
response = lockService.unlock(request, fileName);
break;
default:
response = ResponseEntity.status(WopiStatus.NOT_FOUND.value()).build();
response = ResponseEntity.status(WopiStatus.NOT_IMPLEMENTED.value()).build();
break;
}
logger.debug("handleLock status: {}, filename: {}, override: {}, response: {}", response.getStatusCodeValue(),
fileName, wopiOverride, response);
logger.info("handleLock -- filename: {}, override: {}, response: {}, requestLock: {}, oldLock: {}",
fileName, wopiOverride, response, requestLock, oldLock);
return response;
}

Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/ethendev/wopihost/service/WopiLockService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -166,7 +165,7 @@ public ResponseEntity unlockAndRelock(HttpServletRequest request, String fileNam
}

/**
* Processes a PutFile request
* Processes a update file request
*
* @param request
* @param file
Expand All @@ -190,13 +189,23 @@ public ResponseEntity putFile(HttpServletRequest request, File file, byte[] cont
lockRepository.delete(file.getName());
return setLockMismatch(EMPTY_STRING, "File isn't locked");
} else if (!lockInfo.getLockValue().equals(requestLock)) {
// TODO remove below 4 lines code when the lock for word works well
// There seems to be a bug in word's lock
// the locks in the header of unlock_and_relock and putfile request are not the same
// we need to deal with it, otherwise we won't be able to save the word document//
// refer https://social.msdn.microsoft.com/Forums/en-US/bb2f9118-8efd-463d-b4a2-54bb2cebf882/word-online-file-unlock-bug-office-online-server-201605?forum=os_office
String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
if ("docx".equals(suffix)) {
writeFile(file, content);
return ResponseEntity.ok().build();
}
return setLockMismatch(EMPTY_STRING, "Lock mismatch");
}
writeFile(file, content);
return ResponseEntity.ok().build();
}

public void writeFile(File file, byte[] content) {
private void writeFile(File file, byte[] content) {
try (FileOutputStream fop = new FileOutputStream(file)) {
fop.write(content);
fop.flush();
Expand All @@ -209,10 +218,7 @@ private ResponseEntity setLockMismatch(String existingLock, String failReason) {
HttpHeaders headers = new HttpHeaders();
headers.set(WopiResponseHeader.LOCK, existingLock);
headers.set(WopiResponseHeader.LOCK_FAILURE_REASON, failReason);
return ResponseEntity.status(HttpStatus.CONFLICT)
.headers(headers)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body("Lock mismatch/Locked by another interface");
return ResponseEntity.status(HttpStatus.CONFLICT).headers(headers).build();
}

}
4 changes: 2 additions & 2 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{35} - %msg%n</pattern>
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>
</layout>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{35} - %msg%n</pattern>
<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>

Expand Down

0 comments on commit d9e51c1

Please sign in to comment.