From da1b0b2d5dd2df696b38c4673d9f3cf8c16e4867 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 5 Jun 2017 22:59:16 +0200 Subject: [PATCH] Ignore file Upload for the moment, work further with WebSockets --- .../logic/sharelatex/ShareLatexUtil.java | 12 +++ .../sharelatex/SharelatexClientEndpoint.java | 23 +++++ .../logic/sharelatex/SharelatexConnector.java | 99 +++++++++++++++---- .../sharelatex/WebSocketClientWrapper.java | 27 +++-- .../sharelatex/SharelatexConnectorTest.java | 15 ++- 5 files changed, 144 insertions(+), 32 deletions(-) create mode 100644 src/main/java/org/jabref/logic/sharelatex/ShareLatexUtil.java create mode 100644 src/main/java/org/jabref/logic/sharelatex/SharelatexClientEndpoint.java diff --git a/src/main/java/org/jabref/logic/sharelatex/ShareLatexUtil.java b/src/main/java/org/jabref/logic/sharelatex/ShareLatexUtil.java new file mode 100644 index 00000000000..5dd747b6fcc --- /dev/null +++ b/src/main/java/org/jabref/logic/sharelatex/ShareLatexUtil.java @@ -0,0 +1,12 @@ +package org.jabref.logic.sharelatex; + + +public class ShareLatexUtil { + + + public String processBibDocId() { + return ""; + + } + +} diff --git a/src/main/java/org/jabref/logic/sharelatex/SharelatexClientEndpoint.java b/src/main/java/org/jabref/logic/sharelatex/SharelatexClientEndpoint.java new file mode 100644 index 00000000000..fa8eb835d40 --- /dev/null +++ b/src/main/java/org/jabref/logic/sharelatex/SharelatexClientEndpoint.java @@ -0,0 +1,23 @@ +package org.jabref.logic.sharelatex; + +import javax.websocket.ClientEndpoint; +import javax.websocket.EndpointConfig; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; + +@ClientEndpoint +public class SharelatexClientEndpoint { + + @OnMessage + public void processMessageFromServer(String message, Session session) { + System.out.println("Message came from the server ! " + message); + } + + @OnOpen + public void onOpen(Session session, EndpointConfig config) { + System.out.println("Session opened"); + + } + +} diff --git a/src/main/java/org/jabref/logic/sharelatex/SharelatexConnector.java b/src/main/java/org/jabref/logic/sharelatex/SharelatexConnector.java index 59707f98ba7..ab4d44f4eed 100644 --- a/src/main/java/org/jabref/logic/sharelatex/SharelatexConnector.java +++ b/src/main/java/org/jabref/logic/sharelatex/SharelatexConnector.java @@ -8,6 +8,13 @@ import java.util.Map; import java.util.Optional; +import com.gargoylesoftware.htmlunit.BrowserVersion; +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlAnchor; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.util.Cookie; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -101,6 +108,78 @@ public Optional getProjects() throws IOException { return Optional.empty(); } + public void StartWebsocketListener() { + long millis = System.currentTimeMillis(); + System.out.println(millis); + try { + Connection.Response webSocketresponse = Jsoup.connect("http://192.168.1.248/socket.io/1") + .cookies(loginCookies) + .data("t", String.valueOf(millis)).method(Method.GET).execute(); + + System.out.println(webSocketresponse.body()); + + String resp = webSocketresponse.body(); + String channel = resp.substring(0, resp.indexOf(":")); + System.out.println("Channel " + channel); + + WebSocketClientWrapper.createAndConnect(channel); + // MqttPublishSample.connect(channel); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void uploadFileWithWebClient(String projectId, Path path) + { + String activeProject = projectUrl + "/" + projectId; + String uploadUrl = activeProject + "/upload"; + + WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52); + webClient.getOptions().setThrowExceptionOnScriptError(false); + webClient.getOptions().setCssEnabled(true); + webClient.waitForBackgroundJavaScriptStartingBefore(100); + webClient.setAjaxController(new NicelyResynchronizingAjaxController()); + + webClient.getCookieManager() + .addCookie(new Cookie("192.168.1.248", "sharelatex.sid", loginCookies.get("sharelatex.sid"))); + + webClient.addRequestHeader("Referer", loginUrl); + HtmlPage p; + try { + p = webClient.getPage(activeProject); + System.out.println(p.getWebResponse().getContentAsString()); + + + Optional anchor = p.getAnchors().stream() + .filter(a -> a.getAttribute("ng-click").equals("openUploadFileModal()")).findFirst(); + + anchor.ifPresent(x -> { + HtmlPage uploadPage; + try { + uploadPage = x.click(); + webClient.waitForBackgroundJavaScript(1000); + System.out.println("Clickedd!\r\n"); + System.out.println(uploadPage.getWebResponse().getContentAsString()); + // System.out.println(uploadPage.getElementByName("file")); + + } catch (IOException e) { + e.printStackTrace(); + } + + }); + + webClient.close(); + } catch (FailingHttpStatusCodeException | IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + + + public void uploadFile(String projectId, Path path) { String activeProject = projectUrl + "/" + projectId; String uploadUrl = activeProject + "/upload"; @@ -229,27 +308,7 @@ public void uploadFile(String projectId, Path path) { } - long millis = System.currentTimeMillis(); - System.out.println(millis); - try { - Connection.Response webSocketresponse = Jsoup.connect("http://192.168.1.248/socket.io/1") - .cookies(loginCookies) - .data("t", String.valueOf(millis)).method(Method.GET).execute(); - - System.out.println(webSocketresponse.body()); - String resp = webSocketresponse.body(); - String channel = resp.substring(0, resp.indexOf(":")); - System.out.println("Channel " + channel); - - - WebSocketClientWrapper.createAndConnect(channel); - // MqttPublishSample.connect(channel); - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } }); diff --git a/src/main/java/org/jabref/logic/sharelatex/WebSocketClientWrapper.java b/src/main/java/org/jabref/logic/sharelatex/WebSocketClientWrapper.java index 7e946bb1871..a14e1006703 100644 --- a/src/main/java/org/jabref/logic/sharelatex/WebSocketClientWrapper.java +++ b/src/main/java/org/jabref/logic/sharelatex/WebSocketClientWrapper.java @@ -1,15 +1,9 @@ package org.jabref.logic.sharelatex; -import java.io.IOException; import java.net.URI; import java.util.Arrays; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - import javax.websocket.ClientEndpointConfig; -import javax.websocket.Endpoint; -import javax.websocket.EndpointConfig; -import javax.websocket.MessageHandler.Whole; import javax.websocket.Session; import org.glassfish.tyrus.client.ClientManager; @@ -24,7 +18,22 @@ public static void createAndConnect(String channel) { final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create() .preferredSubprotocols(Arrays.asList("mqttt")).build(); ClientManager client = ClientManager.createClient(); - client.connectToServer(new Endpoint() { + + SharelatexClientEndpoint endpoint = new SharelatexClientEndpoint(); + Session session = client.connectToServer(endpoint, + new URI("ws://192.168.1.248/socket.io/1/websocket/" + channel)); + + Thread.sleep(100); + + session.getBasicRemote().sendText( + "5:1+::{\"name\":\"joinProject\",\"args\":[{\"project_id\":\"5909edaff31ff96200ef58dd\"}]}"); + + + + session.getBasicRemote().sendText("5:2+::{\"name\":\"joinDoc\",\"args\":[\"5909edb0f31ff96200ef58df\"]}"); + + + /* client.connectToServer(new Endpoint() { @Override public void onOpen(Session session, EndpointConfig config) { @@ -43,9 +52,9 @@ public void onOpen(Session session, EndpointConfig config) { e.printStackTrace(); } System.out.println("Sent"); + } - }, cec, new URI("ws://192.168.1.248/socket.io/1/websocket/" + channel)); - messageLatch.await(100, TimeUnit.SECONDS); + }, cec, new URI("ws://192.168.1.248/socket.io/1/websocket/" + channel)); */ } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/org/jabref/logic/sharelatex/SharelatexConnectorTest.java b/src/test/java/org/jabref/logic/sharelatex/SharelatexConnectorTest.java index 203f1e37240..f46e3b2e42e 100644 --- a/src/test/java/org/jabref/logic/sharelatex/SharelatexConnectorTest.java +++ b/src/test/java/org/jabref/logic/sharelatex/SharelatexConnectorTest.java @@ -1,7 +1,8 @@ package org.jabref.logic.sharelatex; import java.io.IOException; -import java.nio.file.Paths; + +import org.jabref.JabRefExecutorService; import org.junit.Test; @@ -12,8 +13,16 @@ public void test() throws IOException { SharelatexConnector connector = new SharelatexConnector(); connector.connectToServer("http://192.168.1.248", "joe@example.com", "test"); connector.getProjects(); - connector.uploadFile("591188ed98ba55690073c29e", - Paths.get("X:\\Users\\CS\\Documents\\_JABREFTEMP\\aaaaaaaaaaaaaa.bib")); + // connector.uploadFile("591188ed98ba55690073c29e",Paths.get("X:\\Users\\CS\\Documents\\_JABREFTEMP\\aaaaaaaaaaaaaa.bib")); + // connector.uploadFileWithWebClient("591188ed98ba55690073c29e", + // Paths.get("X:\\Users\\CS\\Documents\\_JABREFTEMP\\aaaaaaaaaaaaaa.bib")); + + + JabRefExecutorService.INSTANCE.executeAndWait(()-> { + + connector.StartWebsocketListener(); + + }); } }