modify cbor method logic.
authorJung Seungho <shonest.jung@samsung.com>
Mon, 22 Aug 2016 01:16:26 +0000 (10:16 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Tue, 23 Aug 2016 06:38:33 +0000 (06:38 +0000)
1. remove unnecessary method (parser devodeDeviceId)
2. Add Exception, when payload is null.

Change-Id: Idfffe24425c2c1f9c9f98f7efe1837bfc080b554
Signed-off-by: Jung Seungho <shonest.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10697
Reviewed-by: Yeonghun Nam <yeonghun.nam@samsung.com>
Reviewed-by: Glen Youngjin Kim <glen.kim@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
cloud/stack/src/main/java/org/iotivity/cloud/util/Cbor.java

index 6957f78..a332324 100644 (file)
 package org.iotivity.cloud.util;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
+
+import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
+import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
-import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
 
 public class Cbor<T> {
     private CBORFactory  f;
@@ -43,10 +43,12 @@ public class Cbor<T> {
     public T parsePayloadFromCbor(byte[] cborPayload,
             Class<? extends Object> class1) {
         T payload = null;
+        if (cborPayload == null) {
+            throw new BadRequestException("cborPayload is null");
+        }
         try {
             payload = (T) mapper.readValue(cborPayload, class1);
         } catch (IOException e) {
-            // TODO Auto-generated catch block
             e.printStackTrace();
         }
 
@@ -55,39 +57,14 @@ public class Cbor<T> {
 
     public byte[] encodingPayloadToCbor(Object payload) {
         byte[] cborData = null;
+        if (payload == null) {
+            throw new InternalServerErrorException("payload must be initialized");
+        }
         try {
             cborData = mapper.writeValueAsBytes(payload);
         } catch (JsonProcessingException e) {
-            // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return cborData;
     }
-
-    public String decodeDeviceId(byte[] payload) {
-        Cbor<ArrayList<Object>> cbor = new Cbor<ArrayList<Object>>();
-        ArrayList<Object> decodedPayload = null;
-
-        if (payload == null) {
-            throw new PreconditionFailedException("payload is null");
-        }
-
-        else {
-            decodedPayload = cbor.parsePayloadFromCbor(payload,
-                    ArrayList.class);
-
-            HashMap<Object, Object> tags = (HashMap<Object, Object>) decodedPayload
-                    .get(0);
-
-            String deviceId = tags.get("di").toString();
-
-            if (deviceId == null) {
-                throw new PreconditionFailedException("deviceId is null");
-            }
-
-            Log.i("deviceId : " + deviceId);
-
-            return deviceId;
-        }
-    }
 }
\ No newline at end of file