Skip to content

Commit

Permalink
json offsets are dynamic
Browse files Browse the repository at this point in the history
Add custom configurator for adding headers
  • Loading branch information
Siedlerchr committed Jul 4, 2017
1 parent d366e3d commit f9cec01
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jabref.logic.sharelatex;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.websocket.ClientEndpointConfig;

public class MyCustomClientEndpointConfigurator extends ClientEndpointConfig.Configurator {

private final String userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0";
private final String serverOrigin;

public MyCustomClientEndpointConfigurator(String serverOrigin) {
super();
this.serverOrigin = serverOrigin;
}

@Override
public void beforeRequest(Map<String, List<String>> headers) {

headers.put("User-Agent", Arrays.asList(userAgent));
headers.put("origin", Arrays.asList(serverOrigin));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

public class ShareLatexParser {

private static final int JSON_ERROR_OBJECT_OFFSET = 4;
public static final int JSON_START_OFFSET = 6;
private final JsonParser parser = new JsonParser();

public int getVersionFromBibTexJsonString(String content) {
Expand Down Expand Up @@ -117,13 +115,13 @@ private List<BibEntry> parseBibEntryFromJsonArray(JsonArray arr, ImportFormatPre
}

private JsonArray parseFirstPartOfMessageAsArray(String documentToParse) {
String jsonToRead = documentToParse.substring(JSON_START_OFFSET, documentToParse.length());
String jsonToRead = documentToParse.substring(documentToParse.indexOf("+") + 1, documentToParse.length());
JsonArray arr = parser.parse(jsonToRead).getAsJsonArray();
return arr;
}

private JsonObject parseFirstPartOfMessageAsObject(String documentToParse) {
String jsonToRead = documentToParse.substring(JSON_ERROR_OBJECT_OFFSET, documentToParse.length());
String jsonToRead = documentToParse.substring(documentToParse.indexOf("{"), documentToParse.length());
return parser.parse(jsonToRead).getAsJsonObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void startWebsocketListener(String projectId, BibDatabaseContext database
URI webSocketchannelUri = new URIBuilder(socketioUrl + "/websocket/" + channel).setScheme(scheme).build();
System.out.println("WebSocketChannelUrl " + webSocketchannelUri);
client.setImportFormatPrefs(prefs);
client.setServerNameOrigin(server);
client.createAndConnect(webSocketchannelUri, projectId, database);

setDatabaseName(database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class WebSocketClientWrapper {
private final BlockingQueue<String> queue = new LinkedBlockingQueue<>();

private final ShareLatexParser parser = new ShareLatexParser();
private String serverOrigin;

public WebSocketClientWrapper() {
this.eventBus.register(this);
Expand All @@ -63,8 +64,9 @@ public void createAndConnect(URI webSocketchannelUri, String projectId, BibDatab
try {
this.projectId = projectId;

ClientEndpointConfig.Configurator configurator = new MyCustomClientEndpointConfigurator(serverOrigin);
final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().extensions(Arrays.asList(new PerMessageDeflateExtension()))
.preferredSubprotocols(Arrays.asList("mqttt")).build();
.configurator(configurator).build();
final CountDownLatch messageLatch = new CountDownLatch(1);

ClientManager client = ClientManager.createClient();
Expand Down Expand Up @@ -284,6 +286,11 @@ public void leaveDocAndCloseConn() throws IOException {

}

public void setServerNameOrigin(String serverOrigin) {
this.serverOrigin = serverOrigin;

}

public void registerListener(Object listener) {
eventBus.register(listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void testFixWrongUTF8IsoEncoded() {
@Test
public void parseBibTexString() {

String message = "6:::7+[null,[\"@book{adams1995hitchhiker, \",\" title={The Hitchhiker's Guide to the Galaxy},\",\" author={Adams, D.},\",\" isbn={9781417642595},\",\" url={http://books.google.com/books?id=W-xMPgAACAAJ},\",\" year={199},\",\" publisher={San Val}\",\"}\",\"\"],74,[],{}]";
String message = "6:::78988+[null,[\"@book{adams1995hitchhiker, \",\" title={The Hitchhiker's Guide to the Galaxy},\",\" author={Adams, D.},\",\" isbn={9781417642595},\",\" url={http://books.google.com/books?id=W-xMPgAACAAJ},\",\" year={199},\",\" publisher={San Val}\",\"}\",\"\"],74,[],{}]";
String expected = "@book{adams1995hitchhiker, \n" +
" title={The Hitchhiker's Guide to the Galaxy},\n" +
" author={Adams, D.},\n" +
Expand Down

0 comments on commit f9cec01

Please sign in to comment.