Skip to content

Commit

Permalink
Ignore file Upload for the moment, work further with WebSockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Jun 5, 2017
1 parent 6124873 commit da1b0b2
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 32 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/logic/sharelatex/ShareLatexUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jabref.logic.sharelatex;


public class ShareLatexUtil {


public String processBibDocId() {
return "";

}

}
Original file line number Diff line number Diff line change
@@ -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");

}

}
99 changes: 79 additions & 20 deletions src/main/java/org/jabref/logic/sharelatex/SharelatexConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,6 +108,78 @@ public Optional<JsonObject> 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<HtmlAnchor> 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";
Expand Down Expand Up @@ -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();
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,8 +13,16 @@ public void test() throws IOException {
SharelatexConnector connector = new SharelatexConnector();
connector.connectToServer("http://192.168.1.248", "[email protected]", "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();

});
}

}

0 comments on commit da1b0b2

Please sign in to comment.