/*
 * //******************************************************************
 * //
 * // Copyright 2016 Samsung Electronics All Rights Reserved.
 * //
 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 * //
 * // Licensed under the Apache License, Version 2.0 (the "License");
 * // you may not use this file except in compliance with the License.
 * // You may obtain a copy of the License at
 * //
 * //      http://www.apache.org/licenses/LICENSE-2.0
 * //
 * // Unless required by applicable law or agreed to in writing, software
 * // distributed under the License is distributed on an "AS IS" BASIS,
 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * // See the License for the specific language governing permissions and
 * // limitations under the License.
 * //
 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 */
package org.iotivity.cloud.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

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;
    private ObjectMapper mapper;

    public Cbor() {
        f = new CBORFactory();
        mapper = new ObjectMapper(f);
    }

    @SuppressWarnings("unchecked")
    public T parsePayloadFromCbor(byte[] cborPayload,
            Class<? extends Object> class1) {
        T payload = null;
        try {
            payload = (T) mapper.readValue(cborPayload, class1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return payload;
    }

    public byte[] encodingPayloadToCbor(Object payload) {
        byte[] cborData = null;
        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;
        }
    }
}
@@ -953,60 +953,172 @@
 on;%0A
+%0A
 import 
-java.util.ArrayList;%0Aimport java.util.HashMap
+org.iotivity.cloud.base.exception.ServerException.BadRequestException;%0Aimport org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException
 ;%0A%0Ai
@@ -1288,94 +1288,8 @@
 ory;
-%0Aimport org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
 %0A%0Apu
@@ -1620,32 +1620,143 @@
 payload = null;%0A
+        if (cborPayload == null) %7B%0A            throw new BadRequestException(%22cborPayload is null%22);%0A        %7D%0A
         try %7B%0A  
@@ -1856,55 +1856,8 @@
 ) %7B%0A
-            // TODO Auto-generated catch block%0A
     
@@ -2029,508 +2029,110 @@
     
-try %7B%0A            cborData = mapper.writeValueAsBytes(payload);%0A        %7D catch (JsonProcessingException e) %7B%0A            // TODO Auto-generated catch block%0A            e.printStackTrace();%0A        %7D%0A        return cborData;%0A    %7D%0A%0A    public String decodeDeviceId(byte%5B%5D payload) %7B%0A        Cbor%3CArrayList%3CObject%3E%3E cbor = new Cbor%3CArrayList%3CObject%3E%3E();%0A        ArrayList%3CObject%3E decodedPayload = null;%0A%0A        if (payload == null) %7B%0A            throw new PreconditionFailedException(%22payload is null
+if (payload == null) %7B%0A            throw new InternalServerErrorException(%22payload must be initialized
 %22);%0A
@@ -2141,25 +2141,24 @@
       %7D%0A
-%0A
         
 else %7B%0A 
@@ -2153,12 +2153,11 @@
     
-else
+try
  %7B%0A 
@@ -2171,107 +2171,62 @@
     
-decodedPayload = cbor.parsePayloadFromCbor(payload,%0A                    ArrayList.class
+cborData = mapper.writeValueAsBytes(payload
 );%0A
-%0A
         
     
@@ -2225,280 +2225,76 @@
     
-    HashMap%3CObject, Object%3E tags = (HashMap%3CObject, Object%3E) decodedPayload%0A                    .get(0);%0A%0A            String deviceId = tags.get(%22di%22).toString();%0A%0A            if (deviceId == null) %7B%0A                throw new PreconditionFailedException(%22deviceId is null%22
+%7D catch (JsonProcessingException e) %7B%0A            e.printStackTrace(
 );%0A 
@@ -2296,30 +2296,25 @@
 ();%0A        
-    %7D%0A
+%7D
 %0A           
@@ -2314,84 +2314,24 @@
     
-    Log.i(%22deviceId : %22 + deviceId);%0A%0A            return deviceId;%0A        %7D
+return cborData;
 %0A   

modify cbor method logic.