Commit 45903a3d authored by Jung Seungho's avatar Jung Seungho Committed by Jee Hyeok Kim

statistical defects fixed

Change-Id: Id600550ead3fc240e6d266c945650792754dba21
Signed-off-by: default avatarJung Seungho <shonest.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/19047Tested-by: default avatarjenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: default avatarJee Hyeok Kim <jihyeok13.kim@samsung.com>
parent 3f184aa8
......@@ -58,6 +58,8 @@ public class Constants extends OICConstants {
// Database table key
public static final String RESP_GROUP_PERMISSION = "gp";
public static final String KEYFIELD_SN = "serialNumber";
public static final String KEYFIELD_NA = "notAfter";
......
......@@ -21,10 +21,10 @@
*/
package org.iotivity.cloud.accountserver.db;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
......@@ -276,7 +276,7 @@ public class MongoDB {
Document doc) {
if (tableName == null || doc == null)
return null;
return new ArrayList<>();
MongoCollection<Document> collection = db.getCollection(tableName);
MongoCursor<Document> cursor = collection.find(doc).iterator();
......@@ -306,7 +306,8 @@ public class MongoDB {
while (entryIter.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryIter.next();
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryIter
.next();
String entryKey = entry.getKey();
......@@ -314,13 +315,15 @@ public class MongoDB {
if (entry.getValue() != null && !entryKey.equals("_id")) {
// if value is Array
if (entry.getValue() instanceof List && !((List) entry.getValue()).isEmpty()
if (entry.getValue() instanceof List
&& !((List) entry.getValue()).isEmpty()
&& ((List) entry.getValue()).get(0) instanceof Document)
{
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
for (Document document : (List<Document>) entry.getValue()) {
for (Document document : (List<Document>) entry
.getValue()) {
list.add(convertDocumentToHashMap(document));
}
resourceMap.put(entry.getKey(), list);
......
......@@ -125,10 +125,6 @@ public class AccountResource extends Resource {
private IResponse handleGetSearch(IRequest request) {
if (!request.getUriPath().equals(Constants.ACCOUNT_SEARCH_FULL_URI)) {
throw new BadRequestException("invalid request uri");
}
HashMap<String, List<String>> queryData = request.getUriQueryMap();
if (queryData == null) {
......@@ -137,9 +133,11 @@ public class AccountResource extends Resource {
HashMap<String, Object> responsePayload = null;
String uriQuery = request.getUriQuery();
// AND or OR operation to find users
if (request.getUriQuery().contains(",")) {
queryData = mAsManager.getQueryMap(request.getUriQuery(), ",");
if (uriQuery != null && uriQuery.contains(",")) {
queryData = mAsManager.getQueryMap(uriQuery, ",");
responsePayload = (mAsManager.searchUserUsingCriteria(queryData,
SearchOperation.AND));
} else {
......
......@@ -274,9 +274,6 @@ public class GroupAclManager {
ArrayList<AceTable> getAceTableList = new ArrayList<>();
ArrayList<HashMap<String, Object>> getAceList = AccountDBManager
.getInstance().selectRecord(Constants.ACE_TABLE, condition);
if (getAceList == null) {
return null;
}
for (HashMap<String, Object> getAce : getAceList) {
AceTable getAceTable = new AceTable();
getAceTable = mTypeAceTable.convertMaptoObject(getAce, getAceTable);
......
......@@ -51,7 +51,7 @@ public class GroupBrokerManager {
return mGroupBrokerMgr;
}
private class GroupSubscriber {
private static class GroupSubscriber {
GroupSubscriber(Device subscriber, IRequest request) {
mSubscriber = subscriber;
mRequest = request;
......
......@@ -22,14 +22,11 @@
package org.iotivity.cloud.accountserver.resources.acl.id;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.ListIterator;
import java.util.UUID;
import org.iotivity.cloud.accountserver.Constants;
import org.iotivity.cloud.accountserver.db.AccountDBManager;
......@@ -48,11 +45,11 @@ import org.iotivity.cloud.util.Log;
public class Acl {
private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
private String mAclid = null;
private String mOid = null;
private String mDi = null;
private TypeCastingManager<AclTable> mTypeAcl = new TypeCastingManager<>();
private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
private String mAclid = null;
private String mOid = null;
private String mDi = null;
private TypeCastingManager<AclTable> mTypeAcl = new TypeCastingManager<>();
public Acl(String aclid) {
mAclid = aclid;
......@@ -64,138 +61,140 @@ public class Acl {
mDi = di;
}
private class AclSubscriber {
private static class AclSubscriber {
AclSubscriber(Device subscriber, IRequest request) {
mSubscriber = subscriber;
mRequest = request;
}
public Device mSubscriber;
public Device mSubscriber;
public IRequest mRequest;
}
private HashMap<String, AclSubscriber> mSubscribers = new HashMap<>();
private HashMap<String, AclSubscriber> mSubscribers = new HashMap<>();
public static String valueOf(Object object) {
return (object == null) ? "" : object.toString();
}
@SuppressWarnings("unchecked")
public static AclTable convertMaptoAclObject(HashMap<String, Object> aclMap) {
AclTable aclTable = new AclTable();
try {
aclTable.setAclid(valueOf(aclMap.get(Constants.KEYFIELD_ACLID)));
aclTable.setDi(valueOf(aclMap.get(Constants.KEYFIELD_DI)));
aclTable.setOid(valueOf(aclMap.get(Constants.REQ_OWNER_ID)));
aclTable.setRowneruuid(valueOf(aclMap.get(Constants.REQ_ROWNER_ID)));
List<Ace> aceList = new ArrayList<Ace>();
List<HashMap<String, Object>> aclist = (List<HashMap<String, Object>>) aclMap
public static AclTable convertMaptoAclObject(
HashMap<String, Object> aclMap) {
AclTable aclTable = new AclTable();
try {
aclTable.setAclid(valueOf(aclMap.get(Constants.KEYFIELD_ACLID)));
aclTable.setDi(valueOf(aclMap.get(Constants.KEYFIELD_DI)));
aclTable.setOid(valueOf(aclMap.get(Constants.REQ_OWNER_ID)));
aclTable.setRowneruuid(
valueOf(aclMap.get(Constants.REQ_ROWNER_ID)));
List<Ace> aceList = new ArrayList<Ace>();
List<HashMap<String, Object>> aclist = (List<HashMap<String, Object>>) aclMap
.get(Constants.REQ_ACL_LIST);
if (aclist == null) {
return aclTable;
}
if (aclist == null) {
return aclTable;
}
for (HashMap<String, Object> eachAce : aclist) {
for (HashMap<String, Object> eachAce : aclist) {
Ace ace = new Ace();
Ace ace = new Ace();
ace.setAceid(valueOf(eachAce.get(Constants.REQ_ACE_ID)));
ace.setSubjectuuid(valueOf(eachAce
.get(Constants.KEYFIELD_ACE_SUBJECT_ID)));
ace.setStype(Integer.valueOf(eachAce.get(
Constants.KEYFIELD_ACE_SUBJECT_TYPE).toString()));
ace.setPermission(Integer.valueOf(eachAce.get(
Constants.KEYFIELD_ACE_PERMISSION).toString()));
ace.setAceid(valueOf(eachAce.get(Constants.REQ_ACE_ID)));
ace.setSubjectuuid(valueOf(
eachAce.get(Constants.KEYFIELD_ACE_SUBJECT_ID)));
ace.setStype(Integer.valueOf(eachAce
.get(Constants.KEYFIELD_ACE_SUBJECT_TYPE).toString()));
ace.setPermission(Integer.valueOf(eachAce
.get(Constants.KEYFIELD_ACE_PERMISSION).toString()));
Object validity = eachAce.get(Constants.KEYFIELD_ACE_VALIDITY);
if (validity != null) {
ace.setValidity((List<String>) validity);
}
Object validity = eachAce.get(Constants.KEYFIELD_ACE_VALIDITY);
if (validity != null) {
ace.setValidity((List<String>) validity);
}
List<AceResource> resourceLst = new ArrayList<AceResource>();
List<HashMap<String, Object>> resourceList = (List<HashMap<String, Object>>) eachAce
List<AceResource> resourceLst = new ArrayList<AceResource>();
List<HashMap<String, Object>> resourceList = (List<HashMap<String, Object>>) eachAce
.get(Constants.KEYFIELD_ACE_RESOURCE);
for (HashMap<String, Object> resrouce : resourceList) {
for (HashMap<String, Object> resrouce : resourceList) {
AceResource aceResource = new AceResource();
aceResource.setHref(valueOf(resrouce
.get(Constants.KEYFIELD_ACE_RESOURCE_HREF)));
List<String> rtList = (List<String>) resrouce
AceResource aceResource = new AceResource();
aceResource.setHref(valueOf(resrouce
.get(Constants.KEYFIELD_ACE_RESOURCE_HREF)));
List<String> rtList = (List<String>) resrouce
.get(Constants.KEYFIELD_ACE_RESOURCE_RT);
aceResource.setRt(rtList);
List<String> ifList = (List<String>) resrouce
aceResource.setRt(rtList);
List<String> ifList = (List<String>) resrouce
.get(Constants.KEYFIELD_ACE_RESOURCE_IF);
aceResource.setOicif(ifList);
aceResource.setOicif(ifList);
resourceLst.add(aceResource);
}
ace.setResources(resourceLst);
resourceLst.add(aceResource);
}
ace.setResources(resourceLst);
aceList.add(ace);
aceList.add(ace);
}
aclTable.setAclist(aceList);
} catch (Exception e) {
throw new InternalServerErrorException(
"Map to Acl Object casting error " + e.getMessage());
}
return aclTable;
aclTable.setAclist(aceList);
} catch (Exception e) {
throw new InternalServerErrorException(
"Map to Acl Object casting error " + e.getMessage());
}
return aclTable;
}
@SuppressWarnings("unchecked")
public List<HashMap<String, Object>> addACE(List<HashMap<String, Object>> aclist) {
Log.v("IN addACE");
HashMap<String, Object> hashmap = AccountDBManager.getInstance()
public List<HashMap<String, Object>> addACE(
List<HashMap<String, Object>> aclist) {
Log.v("IN addACE");
HashMap<String, Object> hashmap = AccountDBManager.getInstance()
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
if (hashmap == null) {
throw new BadRequestException("aclid is invalid");
}
List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
if (hashmap == null) {
throw new BadRequestException("aclid is invalid");
}
List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
.get(Constants.REQ_ACL_LIST);
ListIterator<HashMap<String, Object>> iterator = aclist.listIterator();
while (iterator.hasNext()) {
HashMap<String, Object> aceMap = iterator.next();
if (aceMap.get(Constants.KEYFIELD_ACE_SUBJECT_ID)
.equals(hashmap.get(Constants.REQ_OWNER_ID))) {
// remove current iterator
iterator.remove();
continue;
}
aceMap.put(Constants.REQ_ACE_ID, UUID.randomUUID().toString());
ListIterator<HashMap<String, Object>> iterator = aclist.listIterator();
while (iterator.hasNext()) {
HashMap<String, Object> aceMap = iterator.next();
if (aceMap.get(Constants.KEYFIELD_ACE_SUBJECT_ID)
.equals(hashmap.get(Constants.REQ_OWNER_ID))) {
// remove current iterator
iterator.remove();
continue;
}
aceMap.put(Constants.REQ_ACE_ID, UUID.randomUUID().toString());
}
List<HashMap<String, Object>> newAcList = new ArrayList<HashMap<String, Object>>(
aclist);
List<HashMap<String, Object>> newAcList = new ArrayList<HashMap<String, Object>>(
aclist);
if (aclDbList != null) {
newAcList.addAll(aclDbList);
}
hashmap.put(Constants.REQ_ACL_LIST, newAcList);
AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
hashmap);
notifyToSubscriber(getResponsePayload(true));
Log.v("OUT addACE");
return aclist;
if (aclDbList != null) {
newAcList.addAll(aclDbList);
}
hashmap.put(Constants.REQ_ACL_LIST, newAcList);
AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
hashmap);
notifyToSubscriber(getResponsePayload(true));
Log.v("OUT addACE");
return aclist;
}
public HashMap<String, Object> getACE(String aceid) {
HashMap<String, Object> hashmap = AccountDBManager.getInstance()
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
if (hashmap == null) {
throw new BadRequestException("aclid is invalid");
}
List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
.get(Constants.REQ_ACL_LIST);
.get(Constants.REQ_ACL_LIST);
ListIterator<HashMap<String, Object>> iterator = aclDbList
.listIterator();
.listIterator();
while (iterator.hasNext()) {
HashMap<String, Object> aceMap = iterator.next();
if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
......@@ -205,17 +204,18 @@ public class Acl {
}
throw new BadRequestException("aceid is invalid");
}
public boolean isValidAceId(String aceid) {
HashMap<String, Object> hashmap = AccountDBManager.getInstance()
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
if (hashmap == null) {
return false;
}
List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
.get(Constants.REQ_ACL_LIST);
.get(Constants.REQ_ACL_LIST);
ListIterator<HashMap<String, Object>> iterator = aclDbList
.listIterator();
.listIterator();
while (iterator.hasNext()) {
HashMap<String, Object> aceMap = iterator.next();
if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
......@@ -224,17 +224,20 @@ public class Acl {
}
return false;
}
public void updateACE(String aceid, HashMap<String, Object> ace) {
Log.v("IN updateACE");
HashMap<String, Object> hashmap = AccountDBManager.getInstance()
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap.get(Constants.REQ_ACL_LIST);
List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
.get(Constants.REQ_ACL_LIST);
ace.put(Constants.REQ_ACE_ID, aceid);
ListIterator<HashMap<String, Object>> iterator = aclDbList.listIterator();
ListIterator<HashMap<String, Object>> iterator = aclDbList
.listIterator();
while (iterator.hasNext()) {
HashMap<String, Object> aceMap = iterator.next();
if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
......@@ -245,7 +248,8 @@ public class Acl {
}
hashmap.put(Constants.REQ_ACL_LIST, aclDbList);
AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE, hashmap);
AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
hashmap);
notifyToSubscriber(getResponsePayload(true));
Log.v("OUT updateACE");
......@@ -254,13 +258,13 @@ public class Acl {
public void deleteACE(String aceid) {
HashMap<String, Object> hashmap = AccountDBManager.getInstance()
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
.get(Constants.REQ_ACL_LIST);
.get(Constants.REQ_ACL_LIST);
ListIterator<HashMap<String, Object>> iterator = aclDbList.listIterator();
ListIterator<HashMap<String, Object>> iterator = aclDbList
.listIterator();
while (iterator.hasNext()) {
HashMap<String, Object> aceMap = iterator.next();
if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
......@@ -280,7 +284,7 @@ public class Acl {
AclTable aclTable = getAclTable();
aclTable.setAclist(null);
AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
mTypeAcl.convertObjectToMap(aclTable));
mTypeAcl.convertObjectToMap(aclTable));
notifyToSubscriber(getResponsePayload(true));
}
......@@ -314,7 +318,7 @@ public class Acl {
String mDi = aclTable.getDi();
if (!mDi.equals(di)) {
throw new UnAuthorizedException(
di + "is not Device ID of this ACL");
di + "is not Device ID of this ACL");
}
}
......@@ -323,31 +327,30 @@ public class Acl {
synchronized (mSubscribers) {
Iterator<String> iterator = mSubscribers.keySet().iterator();
while(iterator.hasNext()) {
while (iterator.hasNext()) {
String key = iterator.next();
AclSubscriber aclSubscriber = mSubscribers.get(key);
aclSubscriber.mSubscriber.sendResponse(
MessageBuilder.createResponse(aclSubscriber.mRequest,
ResponseStatus.CONTENT,
ContentFormat.APPLICATION_CBOR,
mCbor.encodingPayloadToCbor(
notifyBytePayloadData)));
MessageBuilder.createResponse(aclSubscriber.mRequest,
ResponseStatus.CONTENT,
ContentFormat.APPLICATION_CBOR,
mCbor.encodingPayloadToCbor(
notifyBytePayloadData)));
}
}
}
private AclTable getAclTable() {
AclTable getAclTable = new AclTable();
getAclTable = convertMaptoAclObject(
AccountDBManager.getInstance().selectRecord(
Constants.ACL_TABLE, getCondition()).get(0));
getAclTable = convertMaptoAclObject(AccountDBManager.getInstance()
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0));
return getAclTable;
}
private HashMap<String, Object> getAclTablePayLoad() {
HashMap<String, Object> aclPayload = new HashMap<>();
aclPayload = AccountDBManager.getInstance()
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
.selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
return aclPayload;
}
......@@ -361,5 +364,4 @@ public class Acl {
return condition;
}
}
......@@ -88,10 +88,14 @@ public class AclResource extends Resource {
throws ServerException {
if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
String oid = request.getUriQueryMap().get(Constants.REQ_OWNER_ID)
.get(0);
String di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
.get(0);
HashMap<String, List<String>> queryMap = request.getUriQueryMap();
checkQueryException(Arrays.asList(Constants.REQ_OWNER_ID,
Constants.REQ_DEVICE_ID), queryMap);
String oid = queryMap.get(Constants.REQ_OWNER_ID).get(0);
String di = queryMap.get(Constants.REQ_DEVICE_ID).get(0);
if (mAclManager.getAclid(di) == null) {
return MessageBuilder.createResponse(request,
ResponseStatus.CREATED, ContentFormat.APPLICATION_CBOR,
......@@ -116,13 +120,26 @@ public class AclResource extends Resource {
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
if (null != request.getUriQueryMap()) {
HashMap<String, List<String>> queryMap = request.getUriQueryMap();
if (null != queryMap) {
String aclid = request.getUriPathSegments()
.get(getUriPathSegments().size());
String aceid = request.getUriQueryMap().get(Constants.REQ_ACE_ID)
.get(0);
checkQueryException(Constants.REQ_ACE_ID, queryMap);
String aceid = queryMap.get(Constants.REQ_ACE_ID).get(0);
checkPayloadException(Constants.REQ_ACL_LIST, payloadData);
List<HashMap<String, Object>> aclist = (List<HashMap<String, Object>>) payloadData
.get(Constants.REQ_ACL_LIST);
if (aclist == null) {
throw new BadRequestException(
Constants.REQ_ACL_LIST + " payload value is null");
}
mAclManager.updateACE(aclid, aceid, aclist.get(0));
return MessageBuilder.createResponse(request,
ResponseStatus.CHANGED);
......@@ -153,8 +170,10 @@ public class AclResource extends Resource {
HashMap<String, Object> responsePayload = null;
String di = null;
HashMap<String, List<String>> queryMap = request.getUriQueryMap();
if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
checkQueryException(Arrays.asList(Constants.REQ_DEVICE_ID),
queryMap);
di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID).get(0);
if (di == null) {
throw new PreconditionFailedException("di is invalid");
......@@ -168,14 +187,16 @@ public class AclResource extends Resource {
responsePayload = mAclManager.getAclInfo(aclid);
break;
case SUBSCRIBE:
di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
.get(0);
checkQueryException(Arrays.asList(Constants.REQ_DEVICE_ID),
queryMap);
di = queryMap.get(Constants.REQ_DEVICE_ID).get(0);
responsePayload = mAclManager.addAclSubscriber(aclid, di,
srcDevice, request);
break;
case UNSUBSCRIBE:
di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)