diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..f5c99a7
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1 @@
+language: java
\ No newline at end of file
diff --git a/README.md b/README.md
index d904797..6f1ba37 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ Java client stub for a [PASE](https://github.com/aminfa/Pase) server.
## Code Example
Take a look at the code example section of the [PASE repository](https://github.com/aminfa/Pase).
-The same operations can execute using a `PaseInstance`:
+The same operations can be executed using a `PaseInstance`:
```java
PaseInstance instance = new PaseInstance("localhost:5000"); // specify host
diff --git a/pom.xml b/pom.xml
index 9b08727..a5eecc6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -12,6 +12,8 @@
UTF-8
+ 1.8
+ 1.8
diff --git a/src/main/java/de/upb/pasestub/PaseInstance.java b/src/main/java/de/upb/pasestub/PaseInstance.java
index 78595b6..9b984a4 100644
--- a/src/main/java/de/upb/pasestub/PaseInstance.java
+++ b/src/main/java/de/upb/pasestub/PaseInstance.java
@@ -1,13 +1,13 @@
package de.upb.pasestub;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@@ -17,7 +17,7 @@
/**
* Mutable PaseInterface Implementation.
*/
-public final class PaseInstance implements PaseInterface{
+public final class PaseInstance implements PaseInterface {
/**
* Flag that indicated that the create function has beed called successfully.
*/
@@ -39,86 +39,96 @@ public final class PaseInstance implements PaseInterface{
/**
* PaseInstance defined with the given http host. (Don't include http:// in host)
*/
- public PaseInstance(String host){
+ public PaseInstance(String host) {
this.host = host;
}
/**
* PaseInstance defined to access the Pase with standard port running on the same machine.
*/
- public PaseInstance(){
+ public PaseInstance() {
this("localhost:5000");
}
+ /**
+ * Constructor is used by 'copy' method to initialize a 'created' PaseInstance object.
+ */
+ private PaseInstance(String host, String className, String id){
+ this(host);
+ this.className = className;
+ this.id = id;
+ this.creationFlag = true;
+ }
+
// GETTERS:
/**
* Returns the pase instance id.
*/
- public String getId(){
+ public String getId() {
checkCreated(); // may throw Exception.
return id;
}
+
/**
* Returns class Name that was assigned
*/
- public String getClassName(){
+ public String getClassName() {
checkCreated(); // may throw Exception.
return className;
}
+
/**
* Returns the host url that this object was assigned to use.
*/
- public String getHost(){
+ public String getHost() {
return host;
}
/**
* Returns the instance url that is used to access this instance on the pase server.
*/
- String getInstanceUrl(){
+ String getInstanceUrl() {
checkCreated(); // may throw Exception.
return getHost() + "/" + getClassName() + "/" + getId();
}
// INTERFACE:
@Override
- public boolean create(String constructor, Map parameters)
- throws JsonProcessingException, IOException {
- if(isCreated()){
+ public boolean create(String constructor, Map parameters)
+ throws JsonProcessingException, IOException {
+ if (isCreated()) {
// create was already called. Stop create
- throw createAlreadyCalled();
+ throw createAlreadyCalled();
}
- if(constructor == null || constructor.trim().isEmpty() || parameters == null){
+ if (constructor == null || constructor.trim().isEmpty() || parameters == null) {
throw new NullPointerException();
}
String jsonString = serialize(parameters);
Response serverResponse = httpPost(host + "/" + constructor, jsonString);
- if(serverResponse.code() != 200){
+ if (serverResponse.code() != 200) {
return false;
}
Map returnValues = deserializeMap(serverResponse.body().string());
- if(returnValues.containsKey("id") && returnValues.containsKey("class")){
+ if (returnValues.containsKey("id") && returnValues.containsKey("class")) {
id = returnValues.get("id").toString();
className = returnValues.get("class").toString();
creationFlag = true;
return true;
- }
- else{
+ } else {
return false;
}
}
-
+
@Override
- public Object getAttribute(String attributeName)
- throws IOException, JsonProcessingException{
- checkCreated();
- if(attributeName == null || attributeName.trim().isEmpty()){
+ public Object getAttribute(String attributeName) throws IOException, JsonProcessingException {
+ checkCreated();
+ if (attributeName == null || attributeName.trim().isEmpty()) {
throw new NullPointerException();
}
Response serverResponse = httpGet(getInstanceUrl() + "/" + attributeName);
- if(serverResponse.code() != 200){
+ if (serverResponse.code() != 200) {
throw responseErrorCode(serverResponse);
}
Object pojo = deserializeObject(serverResponse.body().string());
@@ -126,36 +136,44 @@ public Object getAttribute(String attributeName)
}
@Override
- public Object callFunction(String functionName, Map parameters)
- throws JsonProcessingException, IOException{
- checkCreated();
- if(functionName == null || functionName.trim().isEmpty() || parameters == null){
+ public Object callFunction(String functionName, Map parameters)
+ throws JsonProcessingException, IOException {
+ checkCreated();
+ if (functionName == null || functionName.trim().isEmpty() || parameters == null) {
throw new NullPointerException();
}
String jsonString = serialize(parameters);
Response serverResponse = httpPost(getInstanceUrl() + "/" + functionName, jsonString);
- if(serverResponse.code() != 200){
+ if (serverResponse.code() != 200) {
throw responseErrorCode(serverResponse);
}
Object pojo = deserializeObject(serverResponse.body().string());
return pojo;
}
-
+ @Override
+ public PaseInterface cloneObject() throws JsonProcessingException, IOException {
+ checkCreated();
+ Response serverResponse = httpGet(host + "/" + getClassName() + "/copy/" + getId());
+ if (serverResponse.code() != 200) {
+ throw responseErrorCode(serverResponse);
+ }
+ Map returnMap = deserializeMap(serverResponse.body().string());
+ String newClassName = returnMap.get("class").toString();
+ String newId = returnMap.get("id").toString();
+ return new PaseInstance(host, newClassName, newId);
+ }
// HELPER FUNCTIONS:
/**
* Handles basic http post using OkHttp.
*/
- private Response httpPost(String url, String bodyString) throws IOException{
+ private Response httpPost(String url, String bodyString) throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, bodyString);
- Request request = new Request.Builder()
- .url("http://" + url)
- .post(body)
- .addHeader("content-type", "application/json")
- .build();
+ Request request = new Request.Builder().url("http://" + url).post(body)
+ .addHeader("content-type", "application/json").build();
Response response = client.newCall(request).execute();
return response;
}
@@ -163,27 +181,23 @@ private Response httpPost(String url, String bodyString) throws IOException{
/**
* Handles basic http get using OkHttp.
*/
- private Response httpGet(String url) throws IOException{
+ private Response httpGet(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
- Request request = new Request.Builder()
- .url("http://" + url)
- .addHeader("content-type", "application/json")
- .build();
+ Request request = new Request.Builder().url("http://" + url).addHeader("content-type", "application/json")
+ .build();
Response response = client.newCall(request).execute();
return response;
}
-
//TODO: custom json parser if there are any problems parsing objects.
/**
* JSON-Serializes the given map.
*/
- private String serialize(Map map) throws JsonProcessingException{
+ private String serialize(Map map) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
- String jsonResult = mapper.writerWithDefaultPrettyPrinter()
- .writeValueAsString(map);
+ String jsonResult = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
return jsonResult;
}
@@ -192,18 +206,19 @@ private String serialize(Map map) throws JsonProcessingException
*/
private Map deserializeMap(String jsonString) throws IOException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
- TypeReference> typeRef
- = new TypeReference>() {};
+ TypeReference> typeRef = new TypeReference>() {
+ };
Map map = mapper.readValue(jsonString, typeRef);
return map;
}
+
/**
* JSON-Deserializes the given json string to a pojo.
*/
private Object deserializeObject(String jsonString) throws IOException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
- TypeReference