Skip to content

Commit 21d1fd9

Browse files
committed
java: fix - search for filename by path
1 parent 4eff2e7 commit 21d1fd9

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

web/documentserver-example/java/src/main/java/controllers/IndexServlet.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -645,31 +645,39 @@ private static void reference(final HttpServletRequest request,
645645
scanner.useDelimiter("\\A");
646646
String bodyString = scanner.hasNext() ? scanner.next() : "";
647647
scanner.close();
648-
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
649-
650-
JSONParser parser = new JSONParser();
651-
JSONObject body = (JSONObject) parser.parse(bodyString);
652-
JSONObject referenceDataObj = (JSONObject) body.get("referenceData");
653-
String instanceId = (String) referenceDataObj.get("instanceId");
654648

655649
String fileKeyValue = "";
656650
String userAddress = "";
657651
String fileName = "";
652+
boolean incorrectFileKey = false;
653+
654+
JSONParser parser = new JSONParser();
655+
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
656+
JSONObject body = (JSONObject) parser.parse(bodyString);
658657

659-
if (instanceId.equals(DocumentManager.getServerUrl(false))) {
660-
JSONObject fileKey = (JSONObject) referenceDataObj.get("fileKey");
661-
fileKeyValue = gson.toJson(fileKey);
662-
userAddress = (String) fileKey.get("userAddress");
663-
if (userAddress.equals(DocumentManager.curUserHostAddress(null))) {
664-
fileName = (String) fileKey.get("fileName");
658+
if (body.containsKey("referenceData")) {
659+
JSONObject referenceDataObj = (JSONObject) body.get("referenceData");
660+
String instanceId = (String) referenceDataObj.get("instanceId");
661+
662+
if (instanceId.equals(DocumentManager.getServerUrl(false))) {
663+
try {
664+
JSONObject fileKey = (JSONObject) parser.parse((String) referenceDataObj.get("fileKey"));
665+
userAddress = (String) fileKey.get("userAddress");
666+
667+
if (userAddress.equals(DocumentManager.curUserHostAddress(null))) {
668+
fileName = (String) fileKey.get("fileName");
669+
}
670+
} catch (Exception e) {
671+
incorrectFileKey = true; //data from DocEditor can give incorrect fileKey param in java Example
672+
}
665673
}
666674
}
667675

668-
if (fileName.equals("") && !userAddress.equals("")) {
676+
if (fileName.equals("")) {
669677
try {
670678
String path = (String) body.get("path");
671679
path = FileUtility.getFileName(path);
672-
File f = new File(DocumentManager.storagePath(path, userAddress));
680+
File f = new File(DocumentManager.storagePath(path, null));
673681
if (f.exists()) {
674682
fileName = path;
675683
}
@@ -684,16 +692,20 @@ private static void reference(final HttpServletRequest request,
684692
return;
685693
}
686694

695+
HashMap<String, Object> fileKey = new HashMap<>();
696+
fileKey.put("fileName", fileName);
697+
fileKey.put("userAddress", DocumentManager.curUserHostAddress(null));
698+
687699
HashMap<String, Object> referenceData = new HashMap<>();
688700
referenceData.put("instanceId", DocumentManager.getServerUrl(false));
689-
referenceData.put("fileKey", fileKeyValue);
701+
referenceData.put("fileKey", gson.toJson(fileKey));
690702

691703
HashMap<String, Object> data = new HashMap<>();
692704
data.put("fileType", FileUtility.getFileExtension(fileName));
693705
data.put("url", DocumentManager.getDownloadUrl(fileName, true));
694706
data.put("directUrl", DocumentManager.getDownloadUrl(fileName, true));
695707
data.put("referenceData", referenceData);
696-
data.put("path", referenceData);
708+
data.put("path", fileName);
697709

698710
if (DocumentManager.tokenEnabled()) {
699711
String token = DocumentManager.createToken(data);

web/documentserver-example/java/src/main/webapp/editor.jsp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@
162162
};
163163
164164
var onRequestReferenceData = function(event) { // user refresh external data source
165-
166165
event.data.directUrl = !!config.document.directUrl;
167166
let xhr = new XMLHttpRequest();
168167
xhr.open("POST", "IndexServlet?type=reference");

0 commit comments

Comments
 (0)