diff --git a/client/pom.xml b/client/pom.xml
index c6fa45c6..785da8b0 100644
--- a/client/pom.xml
+++ b/client/pom.xml
@@ -8,11 +8,11 @@
com.github.devicehive
devicehive-java
- 3.0.3
+ 3.0.4
../pom.xml
devicehive-java
- 3.0.3
+ 3.0.4
2.2.0
diff --git a/client/src/main/java/com/github/devicehive/client/api/MainDeviceHive.java b/client/src/main/java/com/github/devicehive/client/api/MainDeviceHive.java
index 8dd2aae3..0d053cc3 100644
--- a/client/src/main/java/com/github/devicehive/client/api/MainDeviceHive.java
+++ b/client/src/main/java/com/github/devicehive/client/api/MainDeviceHive.java
@@ -65,7 +65,7 @@ public interface MainDeviceHive {
DHResponse removeDevice(String id);
- Device getDevice(String id) ;
+ DHResponse getDevice(String id) ;
DHResponse putDevice(String id, String name) ;
}
diff --git a/client/src/main/java/com/github/devicehive/client/service/BaseService.java b/client/src/main/java/com/github/devicehive/client/service/BaseService.java
index d277cdc1..77b69294 100644
--- a/client/src/main/java/com/github/devicehive/client/service/BaseService.java
+++ b/client/src/main/java/com/github/devicehive/client/service/BaseService.java
@@ -33,6 +33,7 @@
import com.github.devicehive.rest.model.JsonStringWrapper;
import com.github.devicehive.rest.model.JwtAccessToken;
import com.github.devicehive.rest.model.JwtRefreshToken;
+import com.github.devicehive.websocket.model.repsonse.ErrorResponse;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
@@ -45,6 +46,7 @@
public class BaseService {
public static final String ERROR_MESSAGE_KEY = "message";
+ public static final String FAILED_CONNECT_TO_SERVER = "Failed connect to server";
ApiClient apiClient;
BaseService() {
@@ -102,6 +104,9 @@ final DHResponse execute(Call call) {
} catch (IOException e) {
FailureData failureData = createFailData(FailureData.NO_CODE, e.getMessage());
return new DHResponse(null, failureData);
+ } catch (NullPointerException e) {
+ return new DHResponse(null, FailureData.create(
+ ErrorResponse.create(FAILED_CONNECT_TO_SERVER)));
}
}
diff --git a/client/src/main/java/com/github/devicehive/client/service/DeviceHive.java b/client/src/main/java/com/github/devicehive/client/service/DeviceHive.java
index 78649082..52e45f0e 100644
--- a/client/src/main/java/com/github/devicehive/client/service/DeviceHive.java
+++ b/client/src/main/java/com/github/devicehive/client/service/DeviceHive.java
@@ -24,22 +24,45 @@
import com.github.devicehive.client.api.MainDeviceHive;
import com.github.devicehive.client.callback.ResponseCallback;
import com.github.devicehive.client.exceptions.IncorrectUrlException;
-import com.github.devicehive.client.model.*;
+import com.github.devicehive.client.model.CommandFilter;
+import com.github.devicehive.client.model.DHResponse;
+import com.github.devicehive.client.model.DeviceCommandsCallback;
+import com.github.devicehive.client.model.DeviceFilter;
import com.github.devicehive.client.model.DeviceNotification;
+import com.github.devicehive.client.model.DeviceNotificationsCallback;
+import com.github.devicehive.client.model.FailureData;
+import com.github.devicehive.client.model.NetworkFilter;
+import com.github.devicehive.client.model.NotificationFilter;
+import com.github.devicehive.client.model.TokenAuth;
+import com.github.devicehive.client.model.UserFilter;
import com.github.devicehive.rest.api.AuthApi;
-import com.github.devicehive.rest.model.*;
+import com.github.devicehive.rest.model.ApiInfo;
+import com.github.devicehive.rest.model.ClusterConfig;
+import com.github.devicehive.rest.model.Configuration;
+import com.github.devicehive.rest.model.JsonStringWrapper;
+import com.github.devicehive.rest.model.JwtAccessToken;
+import com.github.devicehive.rest.model.JwtRequest;
+import com.github.devicehive.rest.model.JwtToken;
+import com.github.devicehive.rest.model.NetworkVO;
+import com.github.devicehive.rest.model.RoleEnum;
+import com.github.devicehive.rest.model.StatusEnum;
import com.github.devicehive.websocket.api.CommandWS;
import com.github.devicehive.websocket.api.NotificationWS;
import com.github.devicehive.websocket.api.WebSocketClient;
import com.github.devicehive.websocket.listener.CommandListener;
import com.github.devicehive.websocket.listener.NotificationListener;
-import com.github.devicehive.websocket.model.repsonse.*;
-import org.joda.time.DateTime;
+import com.github.devicehive.websocket.model.repsonse.CommandGetResponse;
+import com.github.devicehive.websocket.model.repsonse.CommandInsertResponse;
+import com.github.devicehive.websocket.model.repsonse.CommandListResponse;
+import com.github.devicehive.websocket.model.repsonse.CommandSubscribeResponse;
+import com.github.devicehive.websocket.model.repsonse.ErrorResponse;
+import com.github.devicehive.websocket.model.repsonse.NotificationGetResponse;
+import com.github.devicehive.websocket.model.repsonse.NotificationInsertResponse;
+import com.github.devicehive.websocket.model.repsonse.NotificationListResponse;
+import com.github.devicehive.websocket.model.repsonse.NotificationSubscribeResponse;
+import com.github.devicehive.websocket.model.repsonse.ResponseAction;
-import okhttp3.logging.HttpLoggingInterceptor;
-import retrofit2.Call;
-import retrofit2.Callback;
-import retrofit2.Response;
+import org.joda.time.DateTime;
import java.io.IOException;
import java.net.URI;
@@ -48,6 +71,11 @@
import java.util.Collections;
import java.util.List;
+import okhttp3.logging.HttpLoggingInterceptor;
+import retrofit2.Call;
+import retrofit2.Callback;
+import retrofit2.Response;
+
public class DeviceHive implements MainDeviceHive {
private static final String WEBSOCKET_PATH = "/api/websocket";
@@ -408,7 +436,7 @@ public DHResponse removeDevice(String id) {
return deviceService.removeDevice(id);
}
- public Device getDevice(String id) {
+ public DHResponse getDevice(String id) {
return deviceService.getDevice(id);
}
diff --git a/client/src/main/java/com/github/devicehive/client/service/DeviceService.java b/client/src/main/java/com/github/devicehive/client/service/DeviceService.java
index 20a1a945..0fb04720 100644
--- a/client/src/main/java/com/github/devicehive/client/service/DeviceService.java
+++ b/client/src/main/java/com/github/devicehive/client/service/DeviceService.java
@@ -48,7 +48,7 @@ DHResponse createDevice(String deviceId, String name) {
}
}
- Device getDevice(String deviceId) {
+ DHResponse getDevice(String deviceId) {
deviceApi = createService(DeviceApi.class);
DHResponse response;
DHResponse result;
@@ -56,23 +56,23 @@ Device getDevice(String deviceId) {
result = execute(deviceApi.get(deviceId));
response = new DHResponse(Device.create(result.getData()), result.getFailureData());
if (response.isSuccessful()) {
- return response.getData();
+ return response;
} else if (response.getFailureData().getCode() == 401) {
authorize();
deviceApi = createService(DeviceApi.class);
result = execute(deviceApi.get(deviceId));
response = new DHResponse(Device.create(result.getData()), result.getFailureData());
if (response.isSuccessful()) {
- return response.getData();
+ return response;
} else if (response.getFailureData().getCode() == 404 || response.getFailureData().getCode() == 403) {
return createAndGetDevice(deviceId);
} else {
- return null;
+ return response;
}
} else if (response.getFailureData().getCode() == 404 || response.getFailureData().getCode() == 403) {
return createAndGetDevice(deviceId);
} else {
- return null;
+ return response;
}
}
@@ -114,11 +114,10 @@ DHResponse removeDevice(String deviceId) {
}
}
- private Device createAndGetDevice(String id) {
+ private DHResponse createAndGetDevice(String id) {
createDevice(id, id);
DHResponse result = execute(deviceApi.get(id));
- DHResponse response = new DHResponse(Device.create(result.getData()), result.getFailureData());
- return response.isSuccessful() ? response.getData() : null;
+ return new DHResponse<>(Device.create(result.getData()), result.getFailureData());
}
}
diff --git a/client/src/main/java/example/Echo.java b/client/src/main/java/example/Echo.java
index 88037b2e..71a6d1ff 100644
--- a/client/src/main/java/example/Echo.java
+++ b/client/src/main/java/example/Echo.java
@@ -22,12 +22,14 @@
package example;
import com.github.devicehive.client.model.CommandFilter;
+import com.github.devicehive.client.model.DHResponse;
import com.github.devicehive.client.model.DeviceCommandsCallback;
import com.github.devicehive.client.model.FailureData;
import com.github.devicehive.client.model.TokenAuth;
import com.github.devicehive.client.service.Device;
import com.github.devicehive.client.service.DeviceCommand;
import com.github.devicehive.client.service.DeviceHive;
+
import org.joda.time.DateTime;
import java.util.Collections;
@@ -46,7 +48,12 @@ public class Echo {
public static void main(String[] args) {
final DeviceHive deviceHive = DeviceHive.getInstance().init(URL, new TokenAuth(refreshToken, accessToken));
final String deviceId = UUID.randomUUID().toString();
- final Device device = deviceHive.getDevice(deviceId);
+ DHResponse deviceResponse = deviceHive.getDevice(deviceId);
+ if (!deviceResponse.isSuccessful()) {
+ System.out.println(deviceResponse);
+ return;
+ }
+ Device device = deviceResponse.getData();
CommandFilter filter = getFilter();
deviceHive.subscribeCommands(Collections.singletonList(deviceId), filter, new DeviceCommandsCallback() {
@Override
diff --git a/client/src/main/java/example/Main.java b/client/src/main/java/example/Main.java
index 9cb05b4f..6456fae3 100644
--- a/client/src/main/java/example/Main.java
+++ b/client/src/main/java/example/Main.java
@@ -1,6 +1,7 @@
package example;
import com.github.devicehive.client.model.CommandFilter;
+import com.github.devicehive.client.model.DHResponse;
import com.github.devicehive.client.model.DeviceCommandsCallback;
import com.github.devicehive.client.model.DeviceNotification;
import com.github.devicehive.client.model.DeviceNotificationsCallback;
@@ -47,7 +48,12 @@ public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
String DEVICE_ID = "123456-example";
//Device Initiating
- final Device device = deviceHive.getDevice(DEVICE_ID);
+ DHResponse deviceResponse = deviceHive.getDevice(DEVICE_ID);
+ if (!deviceResponse.isSuccessful()) {
+ System.out.println(deviceResponse);
+ return;
+ }
+ final Device device = deviceResponse.getData();
//Creating filter to listen commands from the server
CommandFilter commandFilter = new CommandFilter();
commandFilter.setCommandNames(PING);
@@ -65,8 +71,8 @@ public void onSuccess(List commands) {
if (params != null) {
//Getting param value
- Gson gson=new Gson();
- JsonObject jsonObject =gson.fromJson(params.getJsonString(),JsonObject.class);
+ Gson gson = new Gson();
+ JsonObject jsonObject = gson.fromJson(params.getJsonString(), JsonObject.class);
boolean needToSend = false;
if (jsonObject.has(PRODUCE_NOTIFICATION)) {
diff --git a/client/src/test/java/com/github/devicehive/client/CommandTest.java b/client/src/test/java/com/github/devicehive/client/CommandTest.java
index dd6c72ee..ebb60abb 100644
--- a/client/src/test/java/com/github/devicehive/client/CommandTest.java
+++ b/client/src/test/java/com/github/devicehive/client/CommandTest.java
@@ -27,6 +27,7 @@
import com.github.devicehive.client.service.DeviceCommand;
import com.github.devicehive.client.service.DeviceHive;
import com.github.devicehive.rest.model.JsonStringWrapper;
+
import org.junit.Assert;
import org.junit.Test;
@@ -41,11 +42,12 @@ public class CommandTest {
private DeviceHive deviceHive = DeviceHive.getInstance().init(URL, WS_URL, new TokenAuth(refreshToken, accessToken));
- private Device device = deviceHive.getDevice(DEVICE_ID);
+ private DHResponse deviceResponse = deviceHive.getDevice(DEVICE_ID);
@Test
public void createAndUpdate() throws InterruptedException {
- Assert.assertNotNull(device);
+ Assert.assertTrue(deviceResponse.isSuccessful());
+ Device device = deviceResponse.getData();
DHResponse response = device.sendCommand(COM_A, null);
Assert.assertTrue(response.isSuccessful());
DeviceCommand command = response.getData();
diff --git a/client/src/test/java/com/github/devicehive/client/DeviceTest.java b/client/src/test/java/com/github/devicehive/client/DeviceTest.java
index e5e0969b..bb045c83 100644
--- a/client/src/test/java/com/github/devicehive/client/DeviceTest.java
+++ b/client/src/test/java/com/github/devicehive/client/DeviceTest.java
@@ -21,13 +21,21 @@
package com.github.devicehive.client;
-import com.github.devicehive.client.model.*;
+import com.github.devicehive.client.model.CommandFilter;
+import com.github.devicehive.client.model.DHResponse;
+import com.github.devicehive.client.model.DeviceCommandsCallback;
+import com.github.devicehive.client.model.DeviceNotification;
+import com.github.devicehive.client.model.DeviceNotificationsCallback;
+import com.github.devicehive.client.model.FailureData;
+import com.github.devicehive.client.model.NotificationFilter;
+import com.github.devicehive.client.model.Parameter;
+import com.github.devicehive.client.model.TokenAuth;
import com.github.devicehive.client.service.Device;
import com.github.devicehive.client.service.DeviceCommand;
import com.github.devicehive.client.service.DeviceHive;
+
import org.joda.time.DateTime;
import org.junit.Assert;
-import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
@@ -61,8 +69,9 @@ public class DeviceTest {
@Test
public void createDevice() throws IOException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
- Device device = deviceHive.getDevice(deviceId);
- Assert.assertNotNull(device);
+ DHResponse deviceResponse = deviceHive.getDevice(deviceId);
+ Assert.assertTrue(deviceResponse.isSuccessful());
+ final Device device = deviceResponse.getData();
Assert.assertTrue(device != null);
Assert.assertTrue(deviceHive.removeDevice(deviceId).isSuccessful());
}
@@ -70,8 +79,9 @@ public void createDevice() throws IOException {
@Test
public void getCommands() throws IOException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
- final Device device = deviceHive.getDevice(deviceId);
- Assert.assertNotNull(device);
+ DHResponse deviceResponse = deviceHive.getDevice(deviceId);
+ Assert.assertTrue(deviceResponse.isSuccessful());
+ final Device device = deviceResponse.getData();
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.schedule(new Thread(new Runnable() {
public void run() {
@@ -94,8 +104,9 @@ public void run() {
@Test
public void subscribeCommands() throws InterruptedException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
- final Device device = deviceHive.getDevice(deviceId);
- Assert.assertNotNull(device);
+ DHResponse deviceResponse = deviceHive.getDevice(deviceId);
+ Assert.assertTrue(deviceResponse.isSuccessful());
+ final Device device = deviceResponse.getData();
final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
final String commandName1 = COM_A + new Random().nextInt();
final String commandName2 = COM_B + new Random().nextInt();
@@ -169,8 +180,9 @@ public void onFail(FailureData failureData) {
public void subscribeNotifications() throws IOException, InterruptedException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
- final Device device = deviceHive.getDevice(deviceId);
- Assert.assertNotNull(device);
+ DHResponse deviceResponse = deviceHive.getDevice(deviceId);
+ Assert.assertTrue(deviceResponse.isSuccessful());
+ final Device device = deviceResponse.getData();
final CountDownLatch latch = new CountDownLatch(3);
final String notificationName1 = NOTIFICATION_A + new Random().nextInt();
final String notificationName2 = NOTIFICATION_B + new Random().nextInt();
@@ -244,8 +256,9 @@ public void run() {
@Test
public void getNotification() throws IOException {
String deviceId = UUID.randomUUID().toString();
- final Device device = deviceHive.getDevice(deviceId);
- Assert.assertNotNull(device);
+ DHResponse deviceResponse = deviceHive.getDevice(deviceId);
+ Assert.assertTrue(deviceResponse.isSuccessful());
+ final Device device = deviceResponse.getData();
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.schedule(new Thread(new Runnable() {
public void run() {
@@ -267,7 +280,9 @@ public void run() {
@Test
public void sendNotification() throws IOException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
- Device device = deviceHive.getDevice(deviceId);
+ DHResponse deviceResponse = deviceHive.getDevice(deviceId);
+ Assert.assertTrue(deviceResponse.isSuccessful());
+ final Device device = deviceResponse.getData();
Assert.assertNotNull(device);
List parameters = new ArrayList();
diff --git a/pom.xml b/pom.xml
index 124e89b7..3a1f9768 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
devicehive-java
pom
devicehive-java
- 3.0.3
+ 3.0.4
2.2.0
diff --git a/rest/pom.xml b/rest/pom.xml
index ab930168..d0ff1e8f 100644
--- a/rest/pom.xml
+++ b/rest/pom.xml
@@ -7,7 +7,7 @@
com.github.devicehive
devicehive-java
- 3.0.3
+ 3.0.4
../pom.xml
devicehive-rest
diff --git a/websocket/pom.xml b/websocket/pom.xml
index 84a5d6a5..fb55f79c 100644
--- a/websocket/pom.xml
+++ b/websocket/pom.xml
@@ -7,7 +7,7 @@
com.github.devicehive
devicehive-java
- 3.0.3
+ 3.0.4
../pom.xml
devicehive-websocket
diff --git a/websocket/src/main/java/com/github/devicehive/websocket/model/repsonse/ErrorResponse.java b/websocket/src/main/java/com/github/devicehive/websocket/model/repsonse/ErrorResponse.java
index 381d291f..607a014d 100644
--- a/websocket/src/main/java/com/github/devicehive/websocket/model/repsonse/ErrorResponse.java
+++ b/websocket/src/main/java/com/github/devicehive/websocket/model/repsonse/ErrorResponse.java
@@ -25,7 +25,7 @@
public class ErrorResponse extends ResponseAction {
public static final String ERROR = "error";
-
+ public static final int DEFAULT_CODE = -1;
@SerializedName("code")
int code;
@SerializedName("error")
@@ -64,4 +64,11 @@ public static ErrorResponse create(int code, String message) {
response.setError(message);
return response;
}
+
+ public static ErrorResponse create(String message) {
+ ErrorResponse response = new ErrorResponse();
+ response.setCode(DEFAULT_CODE);
+ response.setError(message);
+ return response;
+ }
}
\ No newline at end of file