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;
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();
}
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