Skip to content

Commit e88c2e9

Browse files
committed
java: search for fileName by path added + json-data fix
1 parent 63b7641 commit e88c2e9

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464

6565
import static utils.Constants.KILOBYTE_SIZE;
6666

67-
6867
@WebServlet(name = "IndexServlet", urlPatterns = {"/IndexServlet"})
6968
@MultipartConfig
7069
public class IndexServlet extends HttpServlet {
@@ -650,15 +649,43 @@ private static void reference(final HttpServletRequest request,
650649

651650
JSONParser parser = new JSONParser();
652651
JSONObject body = (JSONObject) parser.parse(bodyString);
653-
String instanceId = (String) body.get("instanceId");
654-
JSONObject fileKey = (JSONObject) body.get("fileKey");
655-
String fileKeyValue = gson.toJson(fileKey);
652+
JSONObject referenceDataObj = (JSONObject) body.get("referenceData");
653+
String instanceId = (String) referenceDataObj.get("instanceId");
654+
655+
String fileKeyValue = "";
656+
String userAddress = "";
657+
String fileName = "";
658+
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");
665+
}
666+
}
667+
668+
if (fileName.equals("") && !userAddress.equals("")) {
669+
try {
670+
String path = (String) body.get("path");
671+
path = FileUtility.getFileName(path);
672+
File f = new File(DocumentManager.storagePath(path, userAddress));
673+
if (f.exists()) {
674+
fileName = path;
675+
}
676+
} catch (Exception e) {
677+
e.printStackTrace();
678+
writer.write("{ \"error\" : 1, \"message\" : \"" + e.getMessage() + "\"}");
679+
}
680+
}
656681

657-
String fileName = (String) fileKey.get("fileName");
658-
String userAddress = (String) fileKey.get("userAddress");
682+
if (fileName.equals("")) {
683+
writer.write("{ \"error\": \"File not found\"}");
684+
return;
685+
}
659686

660687
HashMap<String, Object> referenceData = new HashMap<>();
661-
referenceData.put("instanceId", DocumentManager.getServerUrl(true));
688+
referenceData.put("instanceId", DocumentManager.getServerUrl(false));
662689
referenceData.put("fileKey", fileKeyValue);
663690

664691
HashMap<String, Object> data = new HashMap<>();

0 commit comments

Comments
 (0)