From 049acb9e9270554bb38987a69ae71fb9340258a2 Mon Sep 17 00:00:00 2001 From: Zheng Dong <57553691+XDZhelheim@users.noreply.github.com> Date: Fri, 19 Mar 2021 20:26:47 +0800 Subject: [PATCH] Fix for issue 6487: Opening BibTex file (doubleclick) from Folder with spaces not working (#7551) * Fix #7416 * sync with origin * backtracking file path argument who invoked the method sendMessage(type=SEND_COMMAND_LINE_ARGUMENTS) when double-clicking * fix #6487 (draft) * fix * moved encoding to sendMessage created a new test in RemoteCommunicationTest * move clone to sendMessage --- CHANGELOG.md | 1 + .../jabref/logic/remote/shared/Protocol.java | 24 ++++++++++++++++++- .../logic/remote/RemoteCommunicationTest.java | 10 ++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 261646e2b47..865bfdb6a2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where columns shrink in width when we try to enlarge JabRef window. [#6818](https://github.com/JabRef/jabref/issues/6818) - We fixed an issue where Content selector does not seem to work for custom fields. [#6819](https://github.com/JabRef/jabref/issues/6819) - We fixed an issue in which a linked online file consisting of a web page was saved as an invalid pdf file upon being downloaded. The user is now notified when downloading a linked file results in an HTML file. [#7452](https://github.com/JabRef/jabref/issues/7452) +- We fixed an issue where opening BibTex file (doubleclick) from Folder with spaces not working. [#6487](https://github.com/JabRef/jabref/issues/6487) ### Removed diff --git a/src/main/java/org/jabref/logic/remote/shared/Protocol.java b/src/main/java/org/jabref/logic/remote/shared/Protocol.java index 27442ab571c..6d93122fbe7 100644 --- a/src/main/java/org/jabref/logic/remote/shared/Protocol.java +++ b/src/main/java/org/jabref/logic/remote/shared/Protocol.java @@ -4,6 +4,9 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import javafx.util.Pair; @@ -39,7 +42,19 @@ public void sendMessage(RemoteMessage type) throws IOException { public void sendMessage(RemoteMessage type, Object argument) throws IOException { out.writeObject(type); - out.writeObject(argument); + + // encode the commandline arguments to handle special characters (eg. spaces and Chinese characters) + // related to issue #6487 + if (type == RemoteMessage.SEND_COMMAND_LINE_ARGUMENTS) { + String[] encodedArgs = ((String[]) argument).clone(); + for (int i = 0; i < encodedArgs.length; i++) { + encodedArgs[i] = URLEncoder.encode(encodedArgs[i], StandardCharsets.UTF_8); + } + out.writeObject(encodedArgs); + } else { + out.writeObject(argument); + } + out.write('\0'); out.flush(); } @@ -50,6 +65,13 @@ public Pair receiveMessage() throws IOException { Object argument = in.readObject(); int endOfMessage = in.read(); + // decode the received commandline arguments + if (type == RemoteMessage.SEND_COMMAND_LINE_ARGUMENTS) { + for (int i = 0; i < ((String[]) argument).length; i++) { + ((String[]) argument)[i] = URLDecoder.decode(((String[]) argument)[i], StandardCharsets.UTF_8); + } + } + if (endOfMessage != '\0') { throw new IOException("Message didn't end on correct end of message identifier. Got " + endOfMessage); } diff --git a/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java b/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java index 9f5a34ed95f..02a2b527f6f 100644 --- a/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java +++ b/src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java @@ -73,4 +73,14 @@ void commandLineArgumentMultiLinePassedToServer() { verify(server).handleCommandLineArguments(message); } + + @Test + void commandLineArgumentEncodingAndDecoding() { + final String[] message = new String[]{"D:\\T EST\\测试te st.bib"}; + + // will be encoded as "D%3A%5CT+EST%5C%E6%B5%8B%E8%AF%95te+st.bib" + client.sendCommandLineArguments(message); + + verify(server).handleCommandLineArguments(message); + } }