From 21d1fd962fd4d902e31e42993054f0c77e0e3bea Mon Sep 17 00:00:00 2001 From: rivexe Date: Tue, 21 Feb 2023 14:42:20 +0300 Subject: [PATCH] java: fix - search for filename by path --- .../main/java/controllers/IndexServlet.java | 44 ++++++++++++------- .../java/src/main/webapp/editor.jsp | 1 - 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java b/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java index fff229386..422064cce 100755 --- a/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java +++ b/web/documentserver-example/java/src/main/java/controllers/IndexServlet.java @@ -645,31 +645,39 @@ private static void reference(final HttpServletRequest request, scanner.useDelimiter("\\A"); String bodyString = scanner.hasNext() ? scanner.next() : ""; scanner.close(); - Gson gson = new GsonBuilder().disableHtmlEscaping().create(); - - JSONParser parser = new JSONParser(); - JSONObject body = (JSONObject) parser.parse(bodyString); - JSONObject referenceDataObj = (JSONObject) body.get("referenceData"); - String instanceId = (String) referenceDataObj.get("instanceId"); String fileKeyValue = ""; String userAddress = ""; String fileName = ""; + boolean incorrectFileKey = false; + + JSONParser parser = new JSONParser(); + Gson gson = new GsonBuilder().disableHtmlEscaping().create(); + JSONObject body = (JSONObject) parser.parse(bodyString); - if (instanceId.equals(DocumentManager.getServerUrl(false))) { - JSONObject fileKey = (JSONObject) referenceDataObj.get("fileKey"); - fileKeyValue = gson.toJson(fileKey); - userAddress = (String) fileKey.get("userAddress"); - if (userAddress.equals(DocumentManager.curUserHostAddress(null))) { - fileName = (String) fileKey.get("fileName"); + if (body.containsKey("referenceData")) { + JSONObject referenceDataObj = (JSONObject) body.get("referenceData"); + String instanceId = (String) referenceDataObj.get("instanceId"); + + if (instanceId.equals(DocumentManager.getServerUrl(false))) { + try { + JSONObject fileKey = (JSONObject) parser.parse((String) referenceDataObj.get("fileKey")); + userAddress = (String) fileKey.get("userAddress"); + + if (userAddress.equals(DocumentManager.curUserHostAddress(null))) { + fileName = (String) fileKey.get("fileName"); + } + } catch (Exception e) { + incorrectFileKey = true; //data from DocEditor can give incorrect fileKey param in java Example + } } } - if (fileName.equals("") && !userAddress.equals("")) { + if (fileName.equals("")) { try { String path = (String) body.get("path"); path = FileUtility.getFileName(path); - File f = new File(DocumentManager.storagePath(path, userAddress)); + File f = new File(DocumentManager.storagePath(path, null)); if (f.exists()) { fileName = path; } @@ -684,16 +692,20 @@ private static void reference(final HttpServletRequest request, return; } + HashMap fileKey = new HashMap<>(); + fileKey.put("fileName", fileName); + fileKey.put("userAddress", DocumentManager.curUserHostAddress(null)); + HashMap referenceData = new HashMap<>(); referenceData.put("instanceId", DocumentManager.getServerUrl(false)); - referenceData.put("fileKey", fileKeyValue); + referenceData.put("fileKey", gson.toJson(fileKey)); HashMap data = new HashMap<>(); data.put("fileType", FileUtility.getFileExtension(fileName)); data.put("url", DocumentManager.getDownloadUrl(fileName, true)); data.put("directUrl", DocumentManager.getDownloadUrl(fileName, true)); data.put("referenceData", referenceData); - data.put("path", referenceData); + data.put("path", fileName); if (DocumentManager.tokenEnabled()) { String token = DocumentManager.createToken(data); diff --git a/web/documentserver-example/java/src/main/webapp/editor.jsp b/web/documentserver-example/java/src/main/webapp/editor.jsp index 813220c02..331f9430b 100644 --- a/web/documentserver-example/java/src/main/webapp/editor.jsp +++ b/web/documentserver-example/java/src/main/webapp/editor.jsp @@ -162,7 +162,6 @@ }; var onRequestReferenceData = function(event) { // user refresh external data source - event.data.directUrl = !!config.document.directUrl; let xhr = new XMLHttpRequest(); xhr.open("POST", "IndexServlet?type=reference");