Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.weaverplatform.sdk.Weaver Maven / Gradle / Ivy
package com.weaverplatform.sdk;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.weaverplatform.CliOptions;
import com.weaverplatform.util.ProgressInputStream;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
/**
* @author bastbijl, Sysunite 2017
*/
public class Weaver {
public static int MAX_OPERATIONS = 5000;
private static enum OP { get, post };
private CliOptions options;
public Weaver() {
this.options = new CliOptions(new String[0]);
}
public Weaver(CliOptions options) {
this.options = options;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
private String uri = "http://weaver-server:9487";
private String username = "admin";
private String password = "admin";
private String project = null;
private String authToken = null;
private Gson converter = new Gson();
private Client client;
private Client clientMultiPart;
private Client getClient() {
if(client == null) {
client = ClientBuilder.newClient();
}
return client;
}
private Client getClientMultiPart() {
if(clientMultiPart == null) {
clientMultiPart = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
}
return clientMultiPart;
}
public String login() {
if(authToken != null) {
return authToken;
} else {
// Sign in
JsonObject signIn = new JsonObject();
signIn.addProperty("username", username);
signIn.addProperty("password", password);
Response response = send(signIn, "/user/signInUsername", OP.post);
authToken = response.readEntity(String.class);
return authToken;
}
}
public void setAuthToken(String authToken) {
this.authToken = authToken;
}
public Response sendProject(JsonObject payload, String target, OP op) {
payload.addProperty("target", project);
return sendAuthenticated(payload, target, op);
}
public Response sendAuthenticated(JsonObject payload, String target, OP op) {
String authToken = login();
payload.addProperty("authToken", authToken);
return send(payload, target, op);
}
public Response send(JsonObject payload, String target, OP op) {
Client client = getClient();
WebTarget webTarget = client.target(uri + target);
Invocation.Builder builder = webTarget.request();
Response response;
switch (op) {
case get:
try {
String json = URLEncoder.encode(converter.toJson(payload), "UTF-8");
webTarget = webTarget.queryParam("payload", json);
builder = webTarget.request();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
try {
response = builder.get();
} catch(Exception e) {
throw new RuntimeException(String.format("The get call to %s failed with this reason: %s", webTarget.getUri(), e.getMessage()));
}
break;
case post:
default:
Entity entity = Entity.entity(converter.toJson(payload), MediaType.APPLICATION_JSON_TYPE);
try {
response = builder.post(entity);
} catch(Exception e) {
throw new RuntimeException(String.format("The post call to %s failed with this reason: %s", webTarget.getUri(), e.getMessage()));
}
}
// Send response
if (response.getStatus() != 200) {
String string = response.readEntity(String.class);
throw new RuntimeException("Message delivery failed on " + target + " response: " + string);
}
return response;
}
public Response sendMultipart(InputStream fileStream, String fileName, String target) {
Client client = getClientMultiPart();
String authToken = login();
WebTarget webTarget = client.target(uri + target);
StreamDataBodyPart fileDataBodyPart = new StreamDataBodyPart("file", fileStream, fileName, MediaType.APPLICATION_OCTET_STREAM_TYPE);
fileDataBodyPart.setContentDisposition(FormDataContentDisposition.name("file").fileName(fileName).build());
final MultiPart multiPart = new FormDataMultiPart()
.field("authToken", authToken, MediaType.TEXT_PLAIN_TYPE)
.field("target", project, MediaType.TEXT_PLAIN_TYPE)
.field("filename", fileName, MediaType.TEXT_PLAIN_TYPE)
.bodyPart(fileDataBodyPart);
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
Entity entity = Entity.entity(multiPart, multiPart.getMediaType());
return webTarget.request("multipart/form-data").post(entity);
}
public void createProject(String id) {
JsonObject payload = new JsonObject();
payload.addProperty("id", id);
payload.addProperty("name", id);
sendAuthenticated(payload, "/project/create", OP.post);
}
public String projectReady(String id) {
JsonObject payload = new JsonObject();
payload.addProperty("id", id);
return sendAuthenticated(payload, "/project/ready", OP.post).readEntity(String.class);
}
public String getVersion() {
JsonObject payload = new JsonObject();
return sendProject(payload, "/application/version", OP.get).readEntity(String.class);
}
public void wipe() {
JsonObject payload = new JsonObject();
sendProject(payload, "/project/wipe", OP.post);
}
public void sendCreate(JsonArray operations, boolean quick) {
JsonArray array = new JsonArray();
boolean stop = false;
while(array.size() < MAX_OPERATIONS && operations.size() > 0 && !stop) {
JsonObject peek = operations.get(0).getAsJsonObject();
if(peek.get("action").getAsString().equals("increment-attribute")) {
stop = true;
if(array.size() == 0) {
array.add(operations.remove(0));
}
} else {
array.add(operations.remove(0));
}
}
if(operations.size() > 0) {
System.out.println("will send " + array.size() + " (" + operations.size() + " to go)");
} else {
System.out.println("will send " + array.size());
}
reallySendCreate(array, quick);
if(operations.size() > 0) {
sendCreate(operations, quick);
}
}
public void reallySendCreate(JsonArray operations, boolean quick) {
JsonObject payload = new JsonObject();
payload.add("operations", operations);
final String target = "/write";
sendProject(payload, target, OP.post);
}
public JsonObject createNode(String id) {
JsonObject payload = new JsonObject();
payload.addProperty("timestamp", System.currentTimeMillis());
payload.addProperty("action", "create-node");
payload.addProperty("id", id);
return payload;
}
public JsonObject createNode(String id, boolean createIfNotExists) {
JsonObject payload = new JsonObject();
payload.addProperty("timestamp", System.currentTimeMillis());
payload.addProperty("action", "create-node");
payload.addProperty("id", id);
if (createIfNotExists) {
payload.addProperty("upsert", true);
}
return payload;
}
public JsonObject createAttribute(String id, String key, String value) {
JsonObject payload = new JsonObject();
payload.addProperty("timestamp", System.currentTimeMillis());
payload.addProperty("action", "create-attribute");
payload.addProperty("id", id);
payload.addProperty("key", key);
payload.addProperty("value", value);
return payload;
}
public JsonObject createRelation(String from, String key, String to, String id) {
JsonObject payload = new JsonObject();
payload.addProperty("timestamp", System.currentTimeMillis());
payload.addProperty("action", "create-relation");
payload.addProperty("from", from);
payload.addProperty("key", key);
payload.addProperty("to", to);
payload.addProperty("id", id);
return payload;
}
public Project[] getProjects() {
Response res = sendAuthenticated(new JsonObject(), "/project", OP.get);
String s = res.readEntity(String.class);
return converter.fromJson(s, Project[].class);
}
public WeaverFile snapshotToStore(boolean zipped) {
JsonObject payload = new JsonObject();
payload.addProperty("json", false);
payload.addProperty("zipped", zipped);
payload.addProperty("stored", true);
Response response = sendProject(payload, "/snapshot", OP.get);
return converter.fromJson(response.readEntity(String.class), WeaverFile.class);
}
public ProgressInputStream getSnapshot(boolean zipped) {
JsonObject payload = new JsonObject();
payload.addProperty("json", false);
payload.addProperty("zipped", zipped);
payload.addProperty("stored", false);
Response response = sendProject(payload, "/snapshot", OP.get);
return new ProgressInputStream((InputStream) response.getEntity(), response.getLength());
}
public WeaverFile snapshotGraphToStore(List graphs, List toGraphs, List fromGraphs, boolean zipped) {
JsonObject payload = new JsonObject();
if(graphs != null && !graphs.isEmpty()) {
JsonElement array = converter.toJsonTree(graphs, new TypeToken>() {}.getType());
payload.add("graphs", array);
}
if(toGraphs != null && !toGraphs.isEmpty()) {
JsonElement array = converter.toJsonTree(toGraphs, new TypeToken>() {}.getType());
payload.add("toGraphs", array);
}
if(fromGraphs != null && !fromGraphs.isEmpty()) {
JsonElement array = converter.toJsonTree(fromGraphs, new TypeToken>() {}.getType());
payload.add("fromGraphs", array);
}
payload.addProperty("json", false);
payload.addProperty("zipped", zipped);
payload.addProperty("stored", true);
Response response = sendProject(payload, "/snapshotGraph", OP.get);
return converter.fromJson(response.readEntity(String.class), WeaverFile.class);
}
public ProgressInputStream getSnapshotGraph(List graphs, List toGraphs, List fromGraphs, boolean zipped) {
JsonObject payload = new JsonObject();
if(graphs != null && !graphs.isEmpty()) {
JsonElement array = converter.toJsonTree(graphs, new TypeToken>() {}.getType());
payload.add("graphs", array);
}
if(toGraphs != null && !toGraphs.isEmpty()) {
JsonElement array = converter.toJsonTree(toGraphs, new TypeToken>() {}.getType());
payload.add("toGraphs", array);
}
if(fromGraphs != null && !fromGraphs.isEmpty()) {
JsonElement array = converter.toJsonTree(fromGraphs, new TypeToken>() {}.getType());
payload.add("fromGraphs", array);
}
payload.addProperty("json", false);
payload.addProperty("zipped", zipped);
payload.addProperty("stored", false);
Response response = sendProject(payload, "/snapshotGraph", OP.get);
return new ProgressInputStream((InputStream) response.getEntity(), response.getLength());
}
public String getHistory() {
Response response = sendProject(new JsonObject(), "/history", OP.get);
return response.readEntity(String.class);
}
public void restore(InputStream operationsStream) {
JsonArray ops = converter.fromJson(new InputStreamReader(operationsStream), JsonArray.class);
System.out.println("Going to write " + ops.size() + " operations.");
sendCreate(ops, false);
}
public MinioFile[] listFiles() {
JsonObject payload = new JsonObject();
Response response = sendProject(payload, "/file/list", OP.get);
String s = response.readEntity(String.class);
return converter.fromJson(s, MinioFile[].class);
}
public WeaverFile uploadFile(InputStream fileStream, String fileName) {
Response response = sendMultipart(fileStream, fileName, "/file/upload");
return converter.fromJson(response.readEntity(String.class), WeaverFile.class);
}
public ProgressInputStream downloadFile(String id) {
JsonObject payload = new JsonObject();
payload.addProperty("fileId", id);
Response response = sendProject(payload, "/file/download", OP.get);
return new ProgressInputStream((InputStream) response.getEntity(), response.getLength());
}
public String deleteFile(String id) {
JsonObject payload = new JsonObject();
payload.addProperty("fileId", id);
Response response = sendProject(payload, "/file/delete", OP.post);
return response.readEntity(String.class);
}
public String getModel(String name, String version) {
JsonObject payload = new JsonObject();
payload.addProperty("name", name);
payload.addProperty("version", version);
Response response = sendProject(payload, "/model/read", OP.post);
return response.readEntity(String.class);
}
}