Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit 73d6068

Browse files
committed
Add more client rest methods
1 parent abc5e3b commit 73d6068

File tree

6 files changed

+140
-5
lines changed

6 files changed

+140
-5
lines changed

src/main/java/client/Client.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public static void main(String[] args) throws UnirestException, FBaseEncryptionE
4545
c.getScenario(address, port);
4646
}
4747

48-
49-
5048
// public boolean keygroupConfig_create(String address, int port, KeygroupConfig keygroupConfig)
5149
// throws UnirestException, FBaseEncryptionException {
5250
//
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package client;
2+
3+
import org.apache.log4j.Logger;
4+
5+
import com.mashape.unirest.http.HttpResponse;
6+
import com.mashape.unirest.http.Unirest;
7+
import com.mashape.unirest.http.exceptions.UnirestException;
8+
9+
import model.JSONable;
10+
import model.config.KeygroupConfig;
11+
import model.config.ReplicaNodeConfig;
12+
import model.config.TriggerNodeConfig;
13+
import model.data.ClientID;
14+
import model.data.KeygroupID;
15+
16+
public class KeygroupRequest extends RessourceRequest {
17+
18+
private static final String PATH = "keygroups";
19+
private static Logger logger = Logger.getLogger(KeygroupRequest.class.getName());
20+
21+
public KeygroupRequest(String address, int port) {
22+
super(address, port, PATH);
23+
}
24+
25+
public boolean createKeygroup(KeygroupConfig keygroupConfig) throws UnirestException {
26+
String target = target();
27+
logger.info("Running post request targeting " + target);
28+
29+
HttpResponse<String> response =
30+
Unirest.post(target).header("Content-Type", "application/json")
31+
.body(JSONable.toJSON(keygroupConfig)).asString();
32+
33+
return handleBoolResponse(response, logger);
34+
}
35+
36+
public boolean addClient(KeygroupID keygroupID, ClientID clientID) throws UnirestException {
37+
String target = target(keygroupID.getApp(), keygroupID.getTenant(), keygroupID.getGroup(), "addClient");
38+
logger.info("Running put request targeting " + target);
39+
40+
HttpResponse<String> response =
41+
Unirest.put(target).header("Content-Type", "application/json")
42+
.body(JSONable.toJSON(clientID)).asString();
43+
44+
return handleBoolResponse(response, logger);
45+
}
46+
47+
public boolean addReplicaNode(KeygroupID keygroupID, ReplicaNodeConfig repNC) throws UnirestException {
48+
String target = target(keygroupID.getApp(), keygroupID.getTenant(), keygroupID.getGroup(), "addReplicaNode");
49+
logger.info("Running put request targeting " + target);
50+
51+
HttpResponse<String> response =
52+
Unirest.put(target).header("Content-Type", "application/json")
53+
.body(JSONable.toJSON(repNC)).asString();
54+
55+
return handleBoolResponse(response, logger);
56+
}
57+
58+
public boolean addTriggerNode(KeygroupID keygroupID, TriggerNodeConfig triggerNC) throws UnirestException {
59+
String target = target(keygroupID.getApp(), keygroupID.getTenant(), keygroupID.getGroup(), "addTriggerNode");
60+
logger.info("Running put request targeting " + target);
61+
62+
HttpResponse<String> response =
63+
Unirest.put(target).header("Content-Type", "application/json")
64+
.body(JSONable.toJSON(triggerNC)).asString();
65+
66+
return handleBoolResponse(response, logger);
67+
}
68+
69+
70+
}

src/main/java/client/NodeRequest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package client;
2+
3+
import org.apache.log4j.Logger;
4+
5+
import com.mashape.unirest.http.HttpResponse;
6+
import com.mashape.unirest.http.Unirest;
7+
import com.mashape.unirest.http.exceptions.UnirestException;
8+
9+
import model.JSONable;
10+
import model.config.NodeConfig;
11+
12+
public class NodeRequest extends RessourceRequest {
13+
14+
private static final String PATH = "nodes";
15+
private static Logger logger = Logger.getLogger(NodeRequest.class.getName());
16+
17+
public NodeRequest(String address, int port) {
18+
super(address, port, PATH);
19+
}
20+
21+
public NodeConfig getNodeConfig() throws UnirestException {
22+
String target = target();
23+
logger.info("Running get request targeting " + target);
24+
25+
HttpResponse<String> response = Unirest.get(target).asString();
26+
return handleObjectResponse(response, NodeConfig.class, logger);
27+
}
28+
29+
public boolean createNodeConfig(NodeConfig nodeConfig) throws UnirestException {
30+
String target = target();
31+
logger.info("Running post request targeting " + target);
32+
33+
HttpResponse<String> response =
34+
Unirest.post(target).header("Content-Type", "application/json")
35+
.body(JSONable.toJSON(nodeConfig)).asString();
36+
37+
return handleBoolResponse(response, logger);
38+
}
39+
40+
}

src/main/java/client/RessourceRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ protected String target(String a, String b) {
4040
protected String target(String a, String b, String c) {
4141
return target(a, b) + "/" + c;
4242
}
43+
44+
protected String target(String a, String b, String c, String d) {
45+
return target(a, b, c) + "/" + d;
46+
}
4347

4448
protected <T> T handleObjectResponse(HttpResponse<String> response, Class<T> targetClass,
4549
Logger logger) {

src/main/java/control/Starter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String[] args)
1919
fbase = new FBase("local.properties");
2020
}
2121
fbase.startup(true, true); // TODO 2: parse from args
22-
fbase.fillWithData();
22+
//fbase.fillWithData();
2323
}
2424

2525
}

src/main/java/de/hasenburg/fbase/rest/jersey/NodesResource.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package de.hasenburg.fbase.rest.jersey;
22

33
import javax.inject.Inject;
4+
import javax.ws.rs.Consumes;
45
import javax.ws.rs.GET;
6+
import javax.ws.rs.POST;
57
import javax.ws.rs.Path;
68
import javax.ws.rs.Produces;
79
import javax.ws.rs.core.MediaType;
@@ -18,9 +20,8 @@
1820
import model.messages.Message;
1921

2022
/**
21-
* The only supported node method is get. The client should not be able to run any other
22-
* operations.
2323
*
24+
* TODO C: add update and delete
2425
*
2526
* @author jonathanhasenburg
2627
*
@@ -54,5 +55,27 @@ public Response getNodeConfig() {
5455
m.setContent(JSONable.toJSON(config));
5556
return Response.ok(JSONable.toJSON(m)).build();
5657
}
58+
59+
@POST
60+
@Produces(MediaType.TEXT_PLAIN)
61+
@Consumes(MediaType.APPLICATION_JSON)
62+
public Response createNodeConfig(String json) {
63+
64+
NodeConfig nodeConfig = JSONable.fromJSON(json, NodeConfig.class);
65+
if (nodeConfig == null) {
66+
return Response.status(400, "Body is not a node config").build();
67+
}
68+
69+
try {
70+
fBase.namingServiceSender.sendNodeConfigCreate(nodeConfig);
71+
fBase.connector.nodeConfig_put(nodeConfig.getNodeID(), nodeConfig);
72+
} catch (FBaseCommunicationException | FBaseNamingServiceException
73+
| FBaseStorageConnectorException e) {
74+
logger.warn(e);
75+
return Response.status(500, e.getMessage()).build();
76+
}
77+
78+
return Response.ok().build();
79+
}
5780

5881
}

0 commit comments

Comments
 (0)