Commit a986bf9f authored by Jee Hyeok Kim's avatar Jee Hyeok Kim

Fix terminate sequence and observe option parser

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: default avatarJee Hyeok Kim <jihyeok13.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/6203Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: default avatarGlen Youngjin Kim <glen.kim@samsung.com>
parent 0026f2b5
......@@ -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");
}
}
......@@ -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";
......
......@@ -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();
......
......@@ -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()) {
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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);
......
/*
* //******************************************************************
* //
* // 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;
}
}
......@@ -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));
......
......@@ -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();