Skip to content

Commit bf5b8fb

Browse files
committed
java: search for fileName by path added
1 parent 63b7641 commit bf5b8fb

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@
5757
import java.nio.file.Files;
5858
import java.nio.file.Path;
5959
import java.nio.file.Paths;
60-
import java.util.ArrayList;
61-
import java.util.HashMap;
62-
import java.util.Map;
63-
import java.util.Scanner;
60+
import java.util.*;
6461

6562
import static utils.Constants.KILOBYTE_SIZE;
6663

@@ -650,15 +647,45 @@ private static void reference(final HttpServletRequest request,
650647

651648
JSONParser parser = new JSONParser();
652649
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);
650+
JSONObject referenceDataObj = (JSONObject) body.get("referenceData");
651+
String instanceId = (String) referenceDataObj.get("instanceId");
656652

657-
String fileName = (String) fileKey.get("fileName");
658-
String userAddress = (String) fileKey.get("userAddress");
653+
String fileKeyValue = "";
654+
String userAddress = "";
655+
String fileName = "";
656+
657+
if (Objects.equals(instanceId, DocumentManager.getServerUrl(false))) {
658+
JSONObject fileKey = (JSONObject) referenceDataObj.get("fileKey");
659+
fileKeyValue = gson.toJson(fileKey);
660+
userAddress = (String) fileKey.get("userAddress");
661+
if (Objects.equals(userAddress, DocumentManager.curUserHostAddress(null))) {
662+
fileName = (String) fileKey.get("fileName");
663+
}
664+
}
665+
666+
if (Objects.equals(fileName, "") && !Objects.equals(userAddress, ""))
667+
{
668+
try {
669+
String path = (String) body.get("path");
670+
path = FileUtility.getFileName(path);
671+
File f = new File(DocumentManager.storagePath(path, userAddress));
672+
if (f.exists())
673+
{
674+
fileName = path;
675+
}
676+
} catch (Exception e) {
677+
e.printStackTrace();
678+
writer.write("{ \"error\" : 1, \"message\" : \"" + e.getMessage() + "\"}");
679+
}
680+
}
681+
682+
if (Objects.equals(fileName, "")) {
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)