Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue 6487: Opening BibTex file (doubleclick) from Folder with spaces not working #7551

Merged
merged 14 commits into from
Mar 19, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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

Expand Down
24 changes: 23 additions & 1 deletion src/main/java/org/jabref/logic/remote/shared/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand All @@ -50,6 +65,13 @@ public Pair<RemoteMessage, Object> 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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/jabref/logic/remote/RemoteCommunicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether this test case is appropriate. Not so sure about how to write a test about socket communication.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's hard to test this. I think, your added test is fine.

}