Fix terminate sequence and observe option parser 03/6203/2
authorJee Hyeok Kim <jihyeok13.kim@samsung.com>
Wed, 23 Mar 2016 02:51:48 +0000 (11:51 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Fri, 25 Mar 2016 00:16:47 +0000 (00:16 +0000)
1. Fix Observe option parser at stack
2. Fix terminate logic at stack
3. Add validation check to client
4. Remove JSONUtil class which multiply exist.
5. Rename Const file to Constants

Change-Id: Ib6419574151e787046b9a7fa89c351237304792a
Signed-off-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/6203
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Glen Youngjin Kim <glen.kim@samsung.com>
24 files changed:
cloud/account/src/main/java/org/iotivity/cloud/accountserver/AccountServer.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/AccountServerManager.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java [moved from cloud/account/src/main/java/org/iotivity/cloud/accountserver/Const.java with 99% similarity]
cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/AccountDBManager.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/MongoDB.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/oauth/GitHub.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/AccountResource.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/AuthResource.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/util/JSONUtil.java [deleted file]
cloud/account/src/test/java/org/iotivity/cloud/testaccountserver/TestAccountServer.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/protocols/CoapAuthHandler.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/protocols/CoapRelayHandler.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/KeepAliveResource.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/testci/TestCloudInterface.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/JSONUtil.java [deleted file]
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/ResourceDirectoryServer.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/ResourceDirectoryResource.java
cloud/resourcedirectory/src/test/java/org/iotivity/cloud/testrdserver/RDServerTest.java
cloud/samples/client/sample_device.cpp
cloud/stack/src/main/java/org/iotivity/cloud/base/CoapClient.java
cloud/stack/src/main/java/org/iotivity/cloud/base/CoapServer.java
cloud/stack/src/main/java/org/iotivity/cloud/base/protocols/coap/CoapMessage.java
cloud/stack/src/main/java/org/iotivity/cloud/util/JSONUtil.java

index 8b49953..2fefffe 100644 (file)
@@ -22,6 +22,7 @@
 package org.iotivity.cloud.accountserver;
 
 import java.net.InetSocketAddress;
+import java.util.Scanner;
 
 import org.iotivity.cloud.accountserver.resources.AccountResource;
 import org.iotivity.cloud.accountserver.resources.AuthResource;
@@ -59,6 +60,19 @@ public class AccountServer {
 
         coapServer
                 .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
-    }
+        
+        Scanner in = new Scanner(System.in);
+        
+        System.out.println("press 'q' to terminate");
+        
+        while(!in.nextLine().equals("q"));
+        
+        in.close();
+        
+        System.out.println("Terminating...");
 
+        coapServer.stopServer();
+        
+        System.out.println("Terminated");
+    }
 }
index 8a62ba7..85ec5ed 100644 (file)
@@ -134,7 +134,7 @@ public class AccountServerManager {
 
         String accessToken = null;
 
-        if (authServer.equals(Const.GITHUB)) {
+        if (authServer.equals(Constants.GITHUB)) {
 
             GitHub gitHub = new GitHub();
             accessToken = gitHub.requestAccessToken(authCode);
@@ -151,7 +151,7 @@ public class AccountServerManager {
 
         String userId = null;
 
-        if (authServer.equals(Const.GITHUB)) {
+        if (authServer.equals(Constants.GITHUB)) {
 
             GitHub gitHub = new GitHub();
             userId = gitHub.requestGetUserInfo(accessToken);
@@ -21,7 +21,7 @@
  */
 package org.iotivity.cloud.accountserver;
 
-public class Const {
+public class Constants {
 
     // MongoDB
     public static final String DEVICE_TABLE          = "USER_DEVICE";
index 1142c75..b012a9a 100644 (file)
@@ -23,7 +23,7 @@ package org.iotivity.cloud.accountserver.db;
 
 import java.util.ArrayList;
 
-import org.iotivity.cloud.accountserver.Const;
+import org.iotivity.cloud.accountserver.Constants;
 
 /**
  *
@@ -39,10 +39,10 @@ public class AccountDBManager {
 
         try {
 
-            mongoDB = new MongoDB(Const.DB_NAME);
+            mongoDB = new MongoDB(Constants.DB_NAME);
 
-            mongoDB.createTable(Const.DEVICE_TABLE);
-            mongoDB.createTable(Const.SESSION_TABLE);
+            mongoDB.createTable(Constants.DEVICE_TABLE);
+            mongoDB.createTable(Constants.SESSION_TABLE);
 
             registerAdminAccount();
 
index 16b5d2b..049a3f3 100644 (file)
@@ -24,7 +24,7 @@ package org.iotivity.cloud.accountserver.db;
 import java.util.ArrayList;
 
 import org.bson.Document;
-import org.iotivity.cloud.accountserver.Const;
+import org.iotivity.cloud.accountserver.Constants;
 import org.iotivity.cloud.util.Logger;
 
 import com.mongodb.MongoClient;
@@ -98,11 +98,11 @@ public class MongoDB {
 
         Document doc = createDocument(userSession);
         MongoCollection<Document> collection = db
-                .getCollection(Const.SESSION_TABLE);
+                .getCollection(Constants.SESSION_TABLE);
 
         if (collection.findOneAndReplace(Filters.and(
-                Filters.eq(Const.USER_ID, doc.get(Const.USER_ID)),
-                Filters.eq(Const.SESSION_CODE, doc.get(Const.SESSION_CODE))),
+                Filters.eq(Constants.USER_ID, doc.get(Constants.USER_ID)),
+                Filters.eq(Constants.SESSION_CODE, doc.get(Constants.SESSION_CODE))),
                 doc) == null) {
 
             collection.insertOne(doc);
@@ -121,11 +121,11 @@ public class MongoDB {
 
         Document doc = createDocument(userDevice);
         MongoCollection<Document> collection = db
-                .getCollection(Const.DEVICE_TABLE);
+                .getCollection(Constants.DEVICE_TABLE);
 
         if (collection.findOneAndReplace(Filters.and(
-                Filters.eq(Const.USER_ID, doc.get(Const.USER_ID)),
-                Filters.eq(Const.DEVICE_ID, doc.get(Const.DEVICE_ID))), doc) == null) {
+                Filters.eq(Constants.USER_ID, doc.get(Constants.USER_ID)),
+                Filters.eq(Constants.DEVICE_ID, doc.get(Constants.DEVICE_ID))), doc) == null) {
 
             collection.insertOne(doc);
         }
@@ -146,10 +146,10 @@ public class MongoDB {
         String userId = null;
 
         MongoCollection<Document> collection = db
-                .getCollection(Const.SESSION_TABLE);
+                .getCollection(Constants.SESSION_TABLE);
 
         MongoCursor<Document> cursor = collection.find(
-                Filters.eq(Const.SESSION_CODE, sessionCode)).iterator();
+                Filters.eq(Constants.SESSION_CODE, sessionCode)).iterator();
 
         try {
 
@@ -181,10 +181,10 @@ public class MongoDB {
         ArrayList<String> deviceList = new ArrayList<String>();
 
         MongoCollection<Document> collection = db
-                .getCollection(Const.DEVICE_TABLE);
+                .getCollection(Constants.DEVICE_TABLE);
 
         MongoCursor<Document> cursor = collection.find(
-                Filters.eq(Const.USER_ID, userId)).iterator();
+                Filters.eq(Constants.USER_ID, userId)).iterator();
 
         try {
 
@@ -209,7 +209,7 @@ public class MongoDB {
         ArrayList<UserDevice> dlist = readDeviceResources();
         int size = dlist.size();
 
-        Logger.i("*Table: " + Const.DEVICE_TABLE);
+        Logger.i("*Table: " + Constants.DEVICE_TABLE);
         for (int i = 0; i < size; i++) {
 
             UserDevice item = dlist.get(i);
@@ -221,7 +221,7 @@ public class MongoDB {
         ArrayList<UserSession> slist = readSessionResources();
         size = slist.size();
 
-        Logger.i("*Table: " + Const.SESSION_TABLE);
+        Logger.i("*Table: " + Constants.SESSION_TABLE);
 
         for (int i = 0; i < size; i++) {
 
@@ -235,16 +235,16 @@ public class MongoDB {
 
     private Document createDocument(UserSession userSession) {
 
-        Document doc = new Document(Const.USER_ID, userSession.getUserId())
-                .append(Const.SESSION_CODE, userSession.getSessionCode());
+        Document doc = new Document(Constants.USER_ID, userSession.getUserId())
+                .append(Constants.SESSION_CODE, userSession.getSessionCode());
 
         return doc;
     }
 
     private Document createDocument(UserDevice userDevice) {
 
-        Document doc = new Document(Const.USER_ID, userDevice.getUserId())
-                .append(Const.DEVICE_ID, userDevice.getDeviceId());
+        Document doc = new Document(Constants.USER_ID, userDevice.getUserId())
+                .append(Constants.DEVICE_ID, userDevice.getDeviceId());
 
         return doc;
     }
@@ -253,8 +253,8 @@ public class MongoDB {
 
         UserSession userSession = new UserSession();
 
-        userSession.setUserId(doc.getString(Const.USER_ID));
-        userSession.setSessionCode(doc.getString(Const.SESSION_CODE));
+        userSession.setUserId(doc.getString(Constants.USER_ID));
+        userSession.setSessionCode(doc.getString(Constants.SESSION_CODE));
 
         return userSession;
     }
@@ -263,8 +263,8 @@ public class MongoDB {
 
         UserDevice userDevice = new UserDevice();
 
-        userDevice.setUserId(doc.getString(Const.USER_ID));
-        userDevice.setDeviceId(doc.getString(Const.DEVICE_ID));
+        userDevice.setUserId(doc.getString(Constants.USER_ID));
+        userDevice.setDeviceId(doc.getString(Constants.DEVICE_ID));
 
         return userDevice;
     }
@@ -274,7 +274,7 @@ public class MongoDB {
         ArrayList<UserSession> userSessionList = new ArrayList<UserSession>();
 
         MongoCollection<Document> collection = db
-                .getCollection(Const.SESSION_TABLE);
+                .getCollection(Constants.SESSION_TABLE);
         MongoCursor<Document> cursor = collection.find().iterator();
 
         while (cursor.hasNext()) {
@@ -293,7 +293,7 @@ public class MongoDB {
         ArrayList<UserDevice> userDeviceList = new ArrayList<UserDevice>();
 
         MongoCollection<Document> collection = db
-                .getCollection(Const.DEVICE_TABLE);
+                .getCollection(Constants.DEVICE_TABLE);
         MongoCursor<Document> cursor = collection.find().iterator();
 
         while (cursor.hasNext()) {
index 19b5ab6..d44a6dc 100644 (file)
@@ -32,7 +32,7 @@ import org.apache.oltu.oauth2.common.OAuthProviderType;
 import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
 import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
 import org.apache.oltu.oauth2.common.message.types.GrantType;
-import org.iotivity.cloud.accountserver.util.JSONUtil;
+import org.iotivity.cloud.util.JSONUtil;
 import org.iotivity.cloud.util.Logger;
 
 /**
@@ -96,9 +96,8 @@ public class GitHub extends OAuthServer {
             e.printStackTrace();
         }
 
-        JSONUtil util = new JSONUtil();
         String userIdKey = "login";
-        String userId = util.parseJSON(userInfo, userIdKey);
+        String userId = JSONUtil.parseJSON(userInfo, userIdKey);
 
         return userId;
     }
index c0be4b8..4618df9 100644 (file)
@@ -26,14 +26,14 @@ import java.util.HashMap;
 import java.util.List;
 
 import org.iotivity.cloud.accountserver.AccountServerManager;
-import org.iotivity.cloud.accountserver.Const;
+import org.iotivity.cloud.accountserver.Constants;
 import org.iotivity.cloud.accountserver.util.CoapMessageBuilder;
-import org.iotivity.cloud.accountserver.util.JSONUtil;
 import org.iotivity.cloud.base.Resource;
 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapStatus;
+import org.iotivity.cloud.util.JSONUtil;
 import org.iotivity.cloud.util.Logger;
 
 import io.netty.channel.ChannelHandlerContext;
@@ -47,7 +47,7 @@ import io.netty.channel.ChannelHandlerContext;
 public class AccountResource extends Resource {
 
     public AccountResource() {
-        setUri(Const.ACCOUNT_URI);
+        setUri(Constants.ACCOUNT_URI);
     }
 
     @Override
@@ -89,7 +89,7 @@ public class AccountResource extends Resource {
     private void handleGetRequest(ChannelHandlerContext ctx, CoapRequest request)
             throws Exception {
 
-        String reqType = extractQuery(request, Const.REQ_TYPE);
+        String reqType = extractQuery(request, Constants.REQ_TYPE);
 
         if (reqType == null)
             throw new IllegalArgumentException("request type is null in query!");
@@ -98,7 +98,7 @@ public class AccountResource extends Resource {
 
         switch (reqType) {
 
-            case Const.TYPE_FIND:
+            case Constants.TYPE_FIND:
                 response = handleFindRequest(request);
                 break;
             default:
@@ -112,7 +112,7 @@ public class AccountResource extends Resource {
     private void handlePostRequest(ChannelHandlerContext ctx,
             CoapRequest request) throws Exception {
 
-        String reqType = extractQuery(request, Const.REQ_TYPE);
+        String reqType = extractQuery(request, Constants.REQ_TYPE);
 
         if (reqType == null)
             throw new IllegalArgumentException("request type is null in query!");
@@ -120,7 +120,7 @@ public class AccountResource extends Resource {
         CoapResponse response = null;
 
         switch (reqType) {
-            case Const.TYPE_PUBLISH:
+            case Constants.TYPE_PUBLISH:
                 response = handlePublishRequest(request);
                 break;
             default:
@@ -144,9 +144,8 @@ public class AccountResource extends Resource {
 
         String payload = request.getPayloadString();
 
-        JSONUtil util = new JSONUtil();
-        String userId = util.parseJSON(payload, Const.REQUEST_USER_ID);
-        String deviceId = util.parseJSON(payload, Const.REQUEST_DEVICE_ID);
+        String userId = JSONUtil.parseJSON(payload, Constants.REQUEST_USER_ID);
+        String deviceId = JSONUtil.parseJSON(payload, Constants.REQUEST_DEVICE_ID);
 
         Logger.d("userId: " + userId + ", deviceId: " + deviceId);
 
@@ -185,7 +184,7 @@ public class AccountResource extends Resource {
         // String payload = getPayloadString(request.getPayload());
 
         JSONUtil util = new JSONUtil();
-        String userId = util.parseJSON(payload, Const.REQUEST_USER_ID);
+        String userId = util.parseJSON(payload, Constants.REQUEST_USER_ID);
 
         Logger.d("userId: " + userId);
 
@@ -211,7 +210,7 @@ public class AccountResource extends Resource {
         HashMap<Object, Object> responseMap = new HashMap<Object, Object>();
 
         ArrayList<String> deviceList = response.getDeviceList();
-        responseMap.put(Const.RESPONSE_DEVICES, deviceList);
+        responseMap.put(Constants.RESPONSE_DEVICES, deviceList);
 
         JSONUtil jsonUtil = new JSONUtil();
         String responseJson = jsonUtil.writeJSON(responseMap);
index 7a85141..50d2793 100644 (file)
@@ -25,14 +25,14 @@ import java.util.HashMap;
 import java.util.List;
 
 import org.iotivity.cloud.accountserver.AccountServerManager;
-import org.iotivity.cloud.accountserver.Const;
+import org.iotivity.cloud.accountserver.Constants;
 import org.iotivity.cloud.accountserver.util.CoapMessageBuilder;
-import org.iotivity.cloud.accountserver.util.JSONUtil;
 import org.iotivity.cloud.base.Resource;
 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapStatus;
+import org.iotivity.cloud.util.JSONUtil;
 import org.iotivity.cloud.util.Logger;
 
 import io.netty.channel.ChannelHandlerContext;
@@ -46,7 +46,7 @@ import io.netty.channel.ChannelHandlerContext;
 public class AuthResource extends Resource {
 
     public AuthResource() {
-        setUri(Const.AUTH_URI);
+        setUri(Constants.AUTH_URI);
     }
 
     @Override
@@ -81,7 +81,7 @@ public class AuthResource extends Resource {
     private void handlePostRequest(ChannelHandlerContext ctx,
             CoapRequest request) throws Exception {
 
-        String reqType = extractQuery(request, Const.REQ_TYPE);
+        String reqType = extractQuery(request, Constants.REQ_TYPE);
 
         if (reqType == null)
             throw new IllegalArgumentException(
@@ -90,10 +90,10 @@ public class AuthResource extends Resource {
         CoapResponse response = null;
 
         switch (reqType) {
-            case Const.TYPE_REGISTER:
+            case Constants.TYPE_REGISTER:
                 response = handleRegisterRequest(request);
                 break;
-            case Const.TYPE_LOGIN:
+            case Constants.TYPE_LOGIN:
                 response = handleLoginRequest(request);
                 break;
             default:
@@ -116,9 +116,8 @@ public class AuthResource extends Resource {
 
         String payload = request.getPayloadString();
 
-        JSONUtil util = new JSONUtil();
-        String sessionCode = util.parseJSON(payload,
-                Const.REQUEST_SESSION_CODE);
+        String sessionCode = JSONUtil.parseJSON(payload,
+                Constants.REQUEST_SESSION_CODE);
 
         Logger.d("sessionCode: " + sessionCode);
 
@@ -163,8 +162,8 @@ public class AuthResource extends Resource {
         String payload = request.getPayloadString();
 
         JSONUtil util = new JSONUtil();
-        String authCode = util.parseJSON(payload, Const.REQUEST_AUTH_CODE);
-        String authServer = util.parseJSON(payload, Const.REQUEST_AUTH_SERVER);
+        String authCode = util.parseJSON(payload, Constants.REQUEST_AUTH_CODE);
+        String authServer = util.parseJSON(payload, Constants.REQUEST_AUTH_SERVER);
 
         Logger.d("authCode: " + authCode + ", authServer: " + authServer);
 
@@ -208,10 +207,10 @@ public class AuthResource extends Resource {
         String userId = response.getUserId();
 
         if (userId != null)
-            responseMap.put(Const.RESPONSE_USER_ID, userId);
+            responseMap.put(Constants.RESPONSE_USER_ID, userId);
 
         if (sessionCode != null)
-            responseMap.put(Const.RESPONSE_SESSION_CODE, sessionCode);
+            responseMap.put(Constants.RESPONSE_SESSION_CODE, sessionCode);
 
         JSONUtil jsonUtil = new JSONUtil();
         String responseJson = jsonUtil.writeJSON(responseMap);
@@ -226,7 +225,7 @@ public class AuthResource extends Resource {
         String userId = response.getUserId();
 
         if (userId != null)
-            responseMap.put(Const.RESPONSE_USER_ID, userId);
+            responseMap.put(Constants.RESPONSE_USER_ID, userId);
 
         JSONUtil jsonUtil = new JSONUtil();
         String responseJson = jsonUtil.writeJSON(responseMap);
diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/util/JSONUtil.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/util/JSONUtil.java
deleted file mode 100644 (file)
index 8fb0816..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * //******************************************************************
- * //
- * // 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.accountserver.util;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-/**
- * 
- * This class provides a set of APIs to parse JSON object and convert data
- * object to JSON string.
- * 
- */
-public class JSONUtil {
-
-    private static ObjectMapper mapper = new ObjectMapper();
-
-    /**
-     * API for parsing json string and getting value corresponding with key.
-     * 
-     * @param jsonString
-     *            json string
-     * @return String - value corresponding with key
-     */
-    public String parseJSON(String jsonString, String key) {
-
-        if (jsonString == null || jsonString.equals(""))
-            return null;
-
-        String value = null;
-
-        try {
-            @SuppressWarnings("unchecked")
-            Map<String, String> jsonMap = mapper.readValue(jsonString,
-                    Map.class);
-            value = jsonMap.get(key);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-        }
-
-        return value;
-    }
-
-    /**
-     * API for converting data of HashMap-type to json string.
-     * 
-     * @param data
-     *            data of HashMap-type
-     * @return String - converted json string
-     */
-    public String writeJSON(HashMap<Object, Object> data) {
-        if (data == null)
-            return null;
-
-        String json = null;
-        try {
-            json = mapper.writeValueAsString(data);
-        } catch (JsonProcessingException e) {
-            e.printStackTrace();
-        }
-
-        if (json == null)
-            json = "{}";
-
-        return json;
-    }
-}
index 0d39ee1..aebada9 100644 (file)
@@ -7,16 +7,16 @@ import java.net.InetSocketAddress;
 import java.nio.charset.StandardCharsets;
 
 import org.junit.Test;
-import org.iotivity.cloud.accountserver.Const;
+import org.iotivity.cloud.accountserver.Constants;
 import org.iotivity.cloud.accountserver.resources.AccountResource;
 import org.iotivity.cloud.accountserver.resources.AuthResource;
-import org.iotivity.cloud.accountserver.util.JSONUtil;
 import org.iotivity.cloud.base.CoapClient;
 import org.iotivity.cloud.base.CoapServer;
 import org.iotivity.cloud.base.ResourceManager;
 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
+import org.iotivity.cloud.util.JSONUtil;
 
 public class TestAccountServer {
 
@@ -44,9 +44,8 @@ public class TestAccountServer {
             if (arg1.getTokenString().equals("1111")) {
 
                 String json = arg1.getPayloadString();
-                JSONUtil util = new JSONUtil();
 
-                sessionCode = util.parseJSON(json, "session");
+                sessionCode = JSONUtil.parseJSON(json, "session");
             }
 
         }
@@ -91,7 +90,7 @@ public class TestAccountServer {
                 + authServer + "\"}";
 
         CoapRequest request = new CoapRequest(CoapMethod.POST);
-        request.setUriPath(Const.AUTH_URI);
+        request.setUriPath(Constants.AUTH_URI);
         request.setUriQuery("reqtype=register");
         request.setToken("1111".getBytes(StandardCharsets.UTF_8));
         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
@@ -110,7 +109,7 @@ public class TestAccountServer {
         String json = "{\"session\":\"" + sessionCode + "\"}";
 
         CoapRequest request = new CoapRequest(CoapMethod.POST);
-        request.setUriPath(Const.AUTH_URI);
+        request.setUriPath(Constants.AUTH_URI);
         request.setUriQuery("reqtype=login");
         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
@@ -134,7 +133,7 @@ public class TestAccountServer {
                 + deviceId + "\"}";
 
         CoapRequest request = new CoapRequest(CoapMethod.POST);
-        request.setUriPath(Const.ACCOUNT_URI);
+        request.setUriPath(Constants.ACCOUNT_URI);
         request.setUriQuery("reqtype=publish");
         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
@@ -154,7 +153,7 @@ public class TestAccountServer {
         String json = "{\"userid\":\"" + userId + "\"}";
 
         CoapRequest request = new CoapRequest(CoapMethod.GET);
-        request.setUriPath(Const.ACCOUNT_URI);
+        request.setUriPath(Constants.ACCOUNT_URI);
         request.setUriQuery("reqtype=find");
         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
index 972830f..7188ff5 100644 (file)
@@ -22,6 +22,7 @@
 package org.iotivity.cloud.ciserver;
 
 import java.net.InetSocketAddress;
+import java.util.Scanner;
 
 import org.iotivity.cloud.base.CoapServer;
 import org.iotivity.cloud.base.ResourceManager;
@@ -48,27 +49,64 @@ public class CloudInterfaceServer {
         ResourceManager resourceManager = null;
         SessionManager sessionManager = null;
         CoapServer coapServer = null;
+        
+        CoapRelayHandler relayHandler = null;
+        CoapAuthHandler  authHandler = null;
+        
+        KeepAliveResource keepAliveResource = null;
 
         coapServer = new CoapServer();
 
         sessionManager = new SessionManager();
 
         resourceManager = new ResourceManager();
+        
+        relayHandler = new CoapRelayHandler(sessionManager);
+        
+        authHandler = new CoapAuthHandler();
+        
+        keepAliveResource = new KeepAliveResource(sessionManager,
+                new int[] { 1, 2, 4, 8 });
+        
+        
+        coapServer.addHandler(new CoapLogHandler()); 
 
-        coapServer.addHandler(
-                new CoapAuthHandler(args[3], Integer.parseInt(args[4])));
+        coapServer.addHandler(authHandler);
 
-        coapServer.addHandler(new CoapLogHandler());
-
-        coapServer.addHandler(new CoapRelayHandler(sessionManager, args[1],
-                Integer.parseInt(args[2]), args[3], Integer.parseInt(args[4])));
+        coapServer.addHandler(relayHandler);
 
         coapServer.addHandler(resourceManager);
-
-        resourceManager.registerResource(new KeepAliveResource(sessionManager,
-                new int[] { 1, 2, 4, 8 }));
+        
+        resourceManager.registerResource(keepAliveResource);        
+        
+        authHandler.startHandler(args[3], Integer.parseInt(args[4]));
+        
+        relayHandler.startHandler(args[1],
+                Integer.parseInt(args[2]), args[3], Integer.parseInt(args[4]));
 
         coapServer
                 .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
+        
+        keepAliveResource.startSessionChecker();
+        
+        Scanner in = new Scanner(System.in);
+        
+        System.out.println("press 'q' to terminate");
+        
+        while(!in.nextLine().equals("q"));
+        
+        in.close();
+        
+        System.out.println("Terminating...");
+        
+        keepAliveResource.stopSessionChecker();
+
+        coapServer.stopServer();
+        
+        relayHandler.stopHandler();
+        
+        authHandler.stopHandler();
+        
+        System.out.println("Terminated");
     }
 }
index 9cfa4d5..d2f2186 100644 (file)
@@ -89,18 +89,23 @@ public class CoapAuthHandler extends ChannelDuplexHandler {
         }
     }
 
-    private CoapClient accountClient = new CoapClient();
+    private CoapClient asClient = new CoapClient();
 
-    public CoapAuthHandler(String accountAddress, int accountPort) {
+    public CoapAuthHandler() {
 
-        accountClient.addHandler(new AccountHandler());
-        try {
-            accountClient.startClient(
-                    new InetSocketAddress(accountAddress, accountPort));
-        } catch (InterruptedException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
+       asClient.addHandler(new AccountHandler());
+        
+    }
+    
+    public void startHandler(String acAddress, int acPort) throws Exception
+    {
+       asClient.startClient(
+                    new InetSocketAddress(acAddress, acPort));
+    }
+    
+    public void stopHandler() throws Exception
+    {
+       asClient.stopClient();
     }
 
     private Cbor<HashMap<Object, Object>> cbor = new Cbor<HashMap<Object, Object>>();
@@ -119,9 +124,9 @@ public class CoapAuthHandler extends ChannelDuplexHandler {
                                     HashMap.class);
                     request.setPayload(
                             JSONUtil.writeJSON(payloadData).getBytes(StandardCharsets.UTF_8));
-                    accountClient.getChannelFuture().channel()
+                    asClient.getChannelFuture().channel()
                             .attr(keyAuthClient).set(ctx);
-                    accountClient.sendRequest(request);
+                    asClient.sendRequest(request);
                     return;
 
                 case Constants.KEEP_ALIVE_URI:
index 60cac3a..e2eab57 100644 (file)
@@ -73,7 +73,7 @@ public class CoapRelayHandler extends ChannelDuplexHandler {
         }
     }
 
-    private CoapClient rdClient = new CoapClient();
+    private CoapClient rdClient = null;
     ///////////
 
     ////////// Handler for Account Server
@@ -121,33 +121,38 @@ public class CoapRelayHandler extends ChannelDuplexHandler {
         }
     }
 
-    private CoapClient asClient = new CoapClient();
+    private CoapClient asClient = null;
     //////////
 
     private SessionManager sessionManager = null;
 
-    public CoapRelayHandler(SessionManager sessionManager, String rdAddress,
-            int rdPort, String acAddress, int acPort) {
+    public CoapRelayHandler(SessionManager sessionManager) {
         this.sessionManager = sessionManager;
+        
+        rdClient = new CoapClient();
 
         rdClient.addHandler(new RDHandler());
+        
+        asClient = new CoapClient();
 
         asClient.addHandler(new AccountHandler());
-
-        try {
-            rdClient.startClient(new InetSocketAddress(rdAddress, rdPort));
-            asClient.startClient(new InetSocketAddress(acAddress, acPort));
-        } catch (InterruptedException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
+    }
+    
+    public void startHandler(String rdAddress, int rdPort, String acAddress, int acPort) throws Exception
+    {
+        rdClient.startClient(new InetSocketAddress(rdAddress, rdPort));
+        
+        asClient.startClient(new InetSocketAddress(acAddress, acPort));
 
         asClient.getChannelFuture().channel().attr(keyAccountClient)
                 .set(new ArrayList<CoapRequest>());
     }
-
-    public CoapRelayHandler(SessionManager sessionManager) {
-        this.sessionManager = sessionManager;
+    
+    public void stopHandler() throws Exception
+    {
+       asClient.stopClient();
+       
+       rdClient.stopClient();
     }
 
     private static final AttributeKey<ChannelHandlerContext> keyDevice = AttributeKey
index a2424a0..98d63ed 100644 (file)
@@ -69,9 +69,18 @@ public class KeepAliveResource extends Resource {
         this.sessionManager = sessionManager;
         connectPool = new HashMap<ChannelHandlerContext, Long>();
         timer = new Timer();
-        timer.schedule(new KeepAliveTask(), 30000, 60000);
         cbor = new Cbor<HashMap<String, Integer>>();
     }
+    
+    public void startSessionChecker()
+    {
+        timer.schedule(new KeepAliveTask(), 30000, 60000);
+    }
+    
+    public void stopSessionChecker()
+    {
+        timer.cancel();
+    }
 
     /**
      * API for receiving message(message to keepalive resource)
index 4d60239..311dec9 100644 (file)
@@ -74,7 +74,7 @@ public class TestCloudInterface {
         CoapClientHandler coapHandler = new CoapClientHandler();
         coapClient.addHandler(coapHandler);
 
-        coapClient.addHandler(new CoapAuthHandler("127.0.0.1", 5683));
+        coapClient.addHandler(new CoapAuthHandler());
 
         coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
 
@@ -139,7 +139,7 @@ public class TestCloudInterface {
         makeinterval(request);
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
@@ -158,7 +158,7 @@ public class TestCloudInterface {
         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
@@ -175,7 +175,7 @@ public class TestCloudInterface {
         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
@@ -193,7 +193,7 @@ public class TestCloudInterface {
         makePayload(request);
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
@@ -211,7 +211,7 @@ public class TestCloudInterface {
         makePayload(request);
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
@@ -229,7 +229,7 @@ public class TestCloudInterface {
         makePayload(request);
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
@@ -242,12 +242,12 @@ public class TestCloudInterface {
 
         CoapRequest request = new CoapRequest(CoapMethod.GET);
         request.setUriPath(
-                "/10.113.64.102/98f7483c-5a31-4161-ba7e-9c13e0d/a/light");
+                "/98f7483c-5a31-4161-ba7e-9c13e0d/a/light");
         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
         makePayload(request);
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
@@ -260,12 +260,12 @@ public class TestCloudInterface {
 
         CoapRequest request = new CoapRequest(CoapMethod.PUT);
         request.setUriPath(
-                "/10.113.64.98/98f7483c-5a31-4161-ba7e-9c13e0d/a/light");
+                "/98f7483c-5a31-4161-ba7e-9c13e0d/a/light");
         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
         makePayload(request);
 
         startServer();
-        ChannelHandlerContext ctx = startClient();
+        startClient();
 
         coapClient.sendRequest(request);
 
diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/JSONUtil.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/JSONUtil.java
deleted file mode 100644 (file)
index 8907a44..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-package org.iotivity.cloud.rdserver;
-
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Map;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-public class JSONUtil {
-
-    private static ObjectMapper mapper = new ObjectMapper();
-
-    public ArrayList<String> parseJSON(byte[] payload, String key) {
-
-        if (payload == null)
-            return null;
-
-        ArrayList<String> value = null;
-
-        try {
-            @SuppressWarnings("unchecked")
-            Map<String, ArrayList<String>> jsonMap = mapper.readValue(payload,
-                    Map.class);
-            value = jsonMap.get(key);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-        }
-
-        return value;
-    }
-
-    public byte[] writeJSON(HashMap<Object, Object> data) throws Exception {
-        if (data == null)
-            return null;
-
-        byte[] json = null;
-        try {
-            json = mapper.writeValueAsBytes(data);
-        } catch (JsonProcessingException e) {
-            e.printStackTrace();
-        }
-
-        if (json == null)
-            json = "{}".getBytes(StandardCharsets.UTF_8);
-
-        return json;
-    }
-}
index f39e048..e7eff9d 100644 (file)
@@ -22,6 +22,7 @@
 package org.iotivity.cloud.rdserver;
 
 import java.net.InetSocketAddress;
+import java.util.Scanner;
 
 import org.iotivity.cloud.base.CoapServer;
 import org.iotivity.cloud.base.ResourceManager;
@@ -53,6 +54,19 @@ public class ResourceDirectoryServer {
 
         coapServer
                 .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
-    }
 
+        Scanner in = new Scanner(System.in);
+        
+        System.out.println("press 'q' to terminate");
+        
+        while(!in.nextLine().equals("q"));
+        
+        in.close();
+        
+        System.out.println("Terminating...");
+
+        coapServer.stopServer();
+        
+        System.out.println("Terminated");
+    }
 }
index 2276568..b3f63e9 100644 (file)
@@ -35,9 +35,9 @@ import org.iotivity.cloud.base.protocols.coap.CoapResponse;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapOption;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapStatus;
 import org.iotivity.cloud.rdserver.Constants;
-import org.iotivity.cloud.rdserver.JSONUtil;
 import org.iotivity.cloud.rdserver.MongoDB;
 import org.iotivity.cloud.util.Cbor;
+import org.iotivity.cloud.util.JSONUtil;
 import org.iotivity.cloud.util.Logger;
 
 import io.netty.channel.ChannelHandlerContext;
@@ -263,9 +263,7 @@ public class ResourceDirectoryResource extends Resource {
                         "st is not null, so this is the get msg about private devices");
                 // parse payload
                 byte[] payload = request.getPayload();
-                JSONUtil util = new JSONUtil();
-                ArrayList<String> deviceList = util.parseJSON(payload,
-                        Constants.RS_DEVICE_LIST_KEY);
+                ArrayList<String> deviceList = JSONUtil.parseJSON(payload, Constants.RS_DEVICE_LIST_KEY);
                 if (deviceList == null) {
                     throw new IllegalArgumentException("deviceList is null");
                 }
index 72929d0..7608f24 100644 (file)
@@ -33,9 +33,9 @@ import org.iotivity.cloud.base.protocols.coap.CoapRequest;
 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
 import org.iotivity.cloud.rdserver.Constants;
-import org.iotivity.cloud.rdserver.JSONUtil;
 import org.iotivity.cloud.rdserver.resources.ResourceDirectoryResource;
 import org.iotivity.cloud.util.Cbor;
+import org.iotivity.cloud.util.JSONUtil;
 import org.junit.Test;
 
 import io.netty.channel.ChannelHandlerContext;
@@ -185,9 +185,8 @@ public class RDServerTest {
         ArrayList<String> didList = new ArrayList<String>();
         didList.add("98f7483c-5a31-4161-ba7e-9c13e0d");
         data.put("devices", didList);
-        JSONUtil util = new JSONUtil();
-        byte[] payload = util.writeJSON(data);
-        request.setPayload(payload);
+        String payload = JSONUtil.writeJSON(data);
+        request.setPayload(payload.getBytes());
 
         startServer();
         ChannelHandlerContext ctx = startClient();
index 4d38b3d..b7f60cb 100644 (file)
@@ -775,15 +775,25 @@ int main(int argc, char *argv[])
 
         case 4:
             session = argv[2];
-            if (argv[3][0] == 's')
+            if (strlen(argv[3]) != 1)
+            {
+                std::cout << "OCStack init error" << std::endl;
+                return 0;
+            }
+            if (strcmp(argv[3], "s") == 0)
             {
                 stackMode = OC_CLIENT_SERVER;
                 g_runningMode = 1;
             }
-            else if (argv[3][0] == 'c')
+            else if (strcmp(argv[3], "c") == 0)
             {
                 g_runningMode = 2;
             }
+            else
+            {
+                std::cout << "Invalid <mode>, 's' or 'c' required" << std::endl;
+                return 0;
+            }
             break;
 
         default:
index ddd5d37..d038565 100644 (file)
@@ -28,6 +28,7 @@ import java.util.List;
 import org.iotivity.cloud.base.protocols.coap.CoapDecoder;
 import org.iotivity.cloud.base.protocols.coap.CoapEncoder;
 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
+import org.iotivity.cloud.util.Logger;
 
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.ChannelFuture;
@@ -69,7 +70,9 @@ public class CoapClient {
         }
     }
 
-    private ChannelFuture channelFuture;
+    ChannelFuture channelFuture;
+
+    EventLoopGroup connectorGroup = new NioEventLoopGroup();
 
     CoAPClientInitializer initializer = new CoAPClientInitializer();
 
@@ -79,15 +82,10 @@ public class CoapClient {
 
     public void startClient(final InetSocketAddress inetSocketAddress)
             throws InterruptedException {
-        // Create bootstrap
-
-        EventLoopGroup bossGroup = new NioEventLoopGroup();
-        // bossGroup = new
-        // EpollEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2);
-
+                
         try {
             Bootstrap b = new Bootstrap();
-            b.group(bossGroup);
+            b.group(connectorGroup);
             b.channel(NioSocketChannel.class);
             b.option(ChannelOption.TCP_NODELAY, true);
             b.option(ChannelOption.SO_KEEPALIVE, true);
@@ -102,7 +100,7 @@ public class CoapClient {
                         @Override
                         public void operationComplete(ChannelFuture future)
                                 throws Exception {
-                            System.out.println(
+                            Logger.d(
                                     "Connection status of TCP CoAP CLIENT  : "
                                             + future.isSuccess());
                         }
@@ -119,26 +117,7 @@ public class CoapClient {
         channelFuture.channel().writeAndFlush(request);
     }
 
-    /**
-     * stop connection
-     */
-    public void stopClient() {
-
-        try {
-            if (channelFuture != null) {
-                channelFuture.channel().disconnect().sync().addListener(
-                        new GenericFutureListener<ChannelFuture>() {
-
-                            public void operationComplete(ChannelFuture future)
-                                    throws Exception {
-                                System.out.println(
-                                        "DisConnection status of TCP CoAP CLIENT : "
-                                                + future.isSuccess());
-                            }
-                        });
-            }
-        } catch (InterruptedException e1) {
-            e1.printStackTrace();
-        }
+    public void stopClient() throws Exception {
+        connectorGroup.shutdownGracefully().await();
     }
 }
index 741c8ce..9274f1e 100644 (file)
@@ -30,6 +30,7 @@ import javax.net.ssl.SSLException;
 
 import org.iotivity.cloud.base.protocols.coap.CoapDecoder;
 import org.iotivity.cloud.base.protocols.coap.CoapEncoder;
+import org.iotivity.cloud.util.Logger;
 
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.channel.ChannelFuture;
@@ -73,12 +74,12 @@ public class CoapServer {
         }
     }
 
-    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
+    EventLoopGroup acceptorGroup = new NioEventLoopGroup(1);
 
     EventLoopGroup workerGroup = new NioEventLoopGroup();
 
     CoAPServerInitializer initializer = new CoAPServerInitializer();
-
+    
     public void addHandler(ChannelHandler handler) {
         initializer.addHandler(handler);
     }
@@ -88,7 +89,7 @@ public class CoapServer {
 
         try {
             ServerBootstrap b = new ServerBootstrap();
-            b.group(bossGroup, workerGroup);
+            b.group(acceptorGroup, workerGroup);
             b.channel(NioServerSocketChannel.class);
             b.option(ChannelOption.TCP_NODELAY, true);
             b.option(ChannelOption.SO_KEEPALIVE, true);
@@ -96,47 +97,23 @@ public class CoapServer {
 
             b.childHandler(initializer);
 
-            ChannelFuture ch = b.bind(inetSocketAddress).sync();
-            ch.addListener(new GenericFutureListener<ChannelFuture>() {
+            ChannelFuture channelFuture = b.bind(inetSocketAddress).sync();
 
+            channelFuture.addListener(new GenericFutureListener<ChannelFuture>() {
                 @Override
                 public void operationComplete(ChannelFuture future)
                         throws Exception {
                     // TODO Auto-generated method stub
-                    System.out
-                            .println("Connection status of TCP CoAP SERVER  : "
+                    Logger.d("Connection status of TCP CoAP SERVER  : "
                                     + future.isSuccess());
                 }
-
             });
         } finally {
         }
-
     }
 
-    public void stopServer() {
-        // shut down all event loops
-        if (bossGroup != null) {
-            bossGroup.shutdownGracefully();
-
-            try {
-                bossGroup.terminationFuture().sync();
-            } catch (InterruptedException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-            }
-        }
-
-        if (workerGroup != null) {
-            workerGroup.shutdownGracefully();
-
-            try {
-                workerGroup.terminationFuture().sync();
-            } catch (InterruptedException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-            }
-        }
+    public void stopServer() throws Exception {
+       acceptorGroup.shutdownGracefully().await();
+       workerGroup.shutdownGracefully().await();
     }
-
 }
index e7c42e4..d22bd80 100644 (file)
@@ -55,7 +55,7 @@ public class CoapMessage {
     protected byte[]       proxy_uri      = null;
     protected byte[]       proxy_scheme   = null;
     protected byte[]       size1          = null;
-    protected boolean      observe        = false;
+    protected byte[]       observe        = null;
 
     public CoapMessage() {
     }
@@ -186,7 +186,7 @@ public class CoapMessage {
 
             // OBSERVE
             case 6:
-                observe = true;
+                observe = value;
                 break;
         }
     }
@@ -257,7 +257,7 @@ public class CoapMessage {
 
             // OBSERVE
             case 6:
-                return observe == true ? new ArrayList<byte[]>() : null;
+                return observe != null ? Arrays.asList(observe) : null;
         }
 
         return null;
index 72e4719..3f7e061 100644 (file)
@@ -22,6 +22,7 @@
 package org.iotivity.cloud.util;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -58,6 +59,25 @@ public class JSONUtil {
 
         return value;
     }
+    
+    public static ArrayList<String> parseJSON(byte[] payload, String key) {
+
+        if (payload == null)
+            return null;
+
+        ArrayList<String> value = null;
+
+        try {
+            @SuppressWarnings("unchecked")
+            Map<String, ArrayList<String>> jsonMap = mapper.readValue(payload,
+                    Map.class);
+            value = jsonMap.get(key);
+        } catch (IOException ioe) {
+            ioe.printStackTrace();
+        }
+
+        return value;
+    }
 
     public static Map<String, String> parseJSON(String jsonString)
             throws JsonParseException, JsonMappingException, IOException {