import org.iotivity.cloud.accountserver.resources.account.session.SessionResource;
import org.iotivity.cloud.accountserver.resources.account.tokenrefresh.TokenRefreshResource;
import org.iotivity.cloud.accountserver.resources.acl.group.GroupResource;
+import org.iotivity.cloud.accountserver.resources.acl.invite.InviteResource;
import org.iotivity.cloud.accountserver.resources.certificate.CertificateResource;
import org.iotivity.cloud.base.ServerSystem;
import org.iotivity.cloud.base.server.CoapServer;
serverSystem.addResource(new CertificateResource());
- serverSystem.addServer(new CoapServer(
- new InetSocketAddress(Integer.parseInt(args[0]))));
+ serverSystem.addResource(new InviteResource());
+
+ serverSystem.addServer(new CoapServer(new InetSocketAddress(Integer
+ .parseInt(args[0]))));
boolean tlsMode = Integer.parseInt(args[1]) == 1;
public static final String GROUP_TABLE = "GROUP_TABLE";
+ public static final String INVITE_TABLE = "INVITE_TABLE";
+
public static final String DEVICE_TABLE = "DEVICE_TABLE";
public static final String ACL_TABLE = "ACL_TABLE";
public static final String KEYFIELD_EXPIRED_TIME = "expiredtime";
+ public static final String KEYFIELD_INVITE_USER = "inviteUser";
+
+ public static final String KEYFIELD_INVITED_USER = "invitedUser";
+
// Request payload key
public static final String REQ_DEVICE_ID = "di";
public static final String REQ_GTYPE_PUBLIC = "Public";
- public static final String REQ_CSR = "csr";
+ public static final String REQ_CSR = "csr";
+
+ public static final String REQ_INVITE = "invite";
// Response payload key
public static final String RESP_USER_LIST = "ulist";
- public static final String RESP_DEVICE_ID = "di";
+ public static final String RESP_DEVICE_ID = "di";
+
+ public static final String RESP_CERT = "cert";
+
+ public static final String RESP_CACERT = "cacert";
+
+ public static final String RESP_INVITE = "invite";
- public static final String RESP_CERT = "cert";
-
- public static final String RESP_CACERT = "cacert";
+ public static final String RESP_INVITED = "invited";
// static token type
mongoDB.createTable(Constants.USER_TABLE);
mongoDB.createTable(Constants.TOKEN_TABLE);
mongoDB.createTable(Constants.GROUP_TABLE);
+ mongoDB.createTable(Constants.INVITE_TABLE);
mongoDB.createTable(Constants.DEVICE_TABLE);
mongoDB.createTable(Constants.ACL_TABLE);
mongoDB.createTable(Constants.ACLTEMPLATE_TABLE);
mongoDB.createIndex(Constants.GROUP_TABLE, keys);
keyField.put(Constants.GROUP_TABLE, keys);
+ keys = new ArrayList<>();
+ keys.add(Constants.KEYFIELD_INVITE_USER);
+ keys.add(Constants.KEYFIELD_GID);
+ keys.add(Constants.KEYFIELD_INVITED_USER);
+
+ mongoDB.createIndex(Constants.INVITE_TABLE, keys);
+ keyField.put(Constants.INVITE_TABLE, keys);
+
keys = new ArrayList<>();
keys.add(Constants.KEYFIELD_UUID);
keys.add(Constants.KEYFIELD_DID);
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // 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.db;
+
+public class InviteTable {
+
+ String gid;
+ String inviteUser;
+ String invitedUser;
+
+ public InviteTable() {
+ }
+
+ public InviteTable(String inviteUser, String gid, String invitedUser) {
+ this.inviteUser = inviteUser;
+ this.gid = gid;
+ this.invitedUser = invitedUser;
+ }
+
+ public String getGid() {
+ return gid;
+ }
+
+ public void setGid(String gid) {
+ this.gid = gid;
+ }
+
+ public String getInviteUser() {
+ return inviteUser;
+ }
+
+ public void setInviteUser(String inviteUser) {
+ this.inviteUser = inviteUser;
+ }
+
+ public String getInvitedUser() {
+ return invitedUser;
+ }
+
+ public void setInvitedUser(String invitedUser) {
+ this.invitedUser = invitedUser;
+ }
+
+}
\ No newline at end of file
"uid and search query param are null");
}
-
- return MessageBuilder.createResponse(request, ResponseStatus.CHANGED,
+ return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
ContentFormat.APPLICATION_CBOR,
mCbor.encodingPayloadToCbor(responsePayload));
}
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // 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.resources.acl.invite;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.iotivity.cloud.accountserver.Constants;
+import org.iotivity.cloud.accountserver.db.AccountDBManager;
+import org.iotivity.cloud.accountserver.db.InviteTable;
+import org.iotivity.cloud.accountserver.util.TypeCastingManager;
+import org.iotivity.cloud.base.device.Device;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.enums.ContentFormat;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
+import org.iotivity.cloud.util.Cbor;
+
+public class InviteManager {
+
+ private TypeCastingManager<InviteTable> mTypeInvite = new TypeCastingManager<>();
+
+ private class InviteSubscriber {
+ InviteSubscriber(Device subscriber, IRequest request) {
+ mSubscriber = subscriber;
+ mRequest = request;
+ }
+
+ public Device mSubscriber;
+ public IRequest mRequest;
+ }
+
+ private HashMap<String, InviteSubscriber> mSubscribers = new HashMap<>();
+
+ public void addInvitation(String uid, String gid, String mid) {
+
+ InviteTable newInviteTable = new InviteTable(uid, gid, mid);
+
+ HashMap<String, Object> condition = new HashMap<>();
+ condition.put(Constants.KEYFIELD_INVITE_USER, uid);
+ condition.put(Constants.KEYFIELD_GID, gid);
+ condition.put(Constants.KEYFIELD_INVITED_USER, mid);
+
+ if (AccountDBManager.getInstance()
+ .selectRecord(Constants.INVITE_TABLE, condition).isEmpty()) {
+ AccountDBManager.getInstance().insertRecord(Constants.INVITE_TABLE,
+ mTypeInvite.convertObjectToMap(newInviteTable));
+ notifyToSubscriber(uid);
+ notifyToSubscriber(mid);
+ }
+ }
+
+ public void deleteInvitation(String mid, String gid) {
+ HashMap<String, Object> condition = new HashMap<>();
+ condition.put(Constants.REQ_GROUP_ID, gid);
+ condition.put(Constants.KEYFIELD_INVITED_USER, mid);
+
+ InviteTable getInviteTable = new InviteTable();
+
+ List<HashMap<String, Object>> getInviteList = AccountDBManager
+ .getInstance().selectRecord(Constants.INVITE_TABLE, condition);
+
+ ArrayList<String> uidList = new ArrayList<>();
+ for (HashMap<String, Object> getInvite : getInviteList) {
+ getInviteTable = mTypeInvite.convertMaptoObject(getInvite,
+ getInviteTable);
+ uidList.add(getInviteTable.getInviteUser());
+ }
+
+ AccountDBManager.getInstance().deleteRecord(Constants.INVITE_TABLE,
+ condition);
+
+ notifyToSubscriber(mid);
+ for (String uid : uidList) {
+ notifyToSubscriber(uid);
+ }
+ }
+
+ public void cancelInvitation(String uid, String gid, String mid) {
+
+ HashMap<String, Object> condition = new HashMap<>();
+
+ condition.put(Constants.REQ_GROUP_ID, gid);
+ condition.put(Constants.KEYFIELD_INVITED_USER, mid);
+ condition.put(Constants.KEYFIELD_INVITE_USER, uid);
+
+ AccountDBManager.getInstance().deleteRecord(Constants.INVITE_TABLE,
+ condition);
+
+ notifyToSubscriber(uid);
+ notifyToSubscriber(mid);
+ }
+
+ public HashMap<String, Object> getInvitationInfo(String uid) {
+ HashMap<String, Object> responsePayload = new HashMap<>();
+
+ ArrayList<Object> invitePayloadData = null;
+ ArrayList<Object> invitedPayloadData = null;
+
+ List<InviteTable> inviteList = getInviteTableList(
+ Constants.KEYFIELD_INVITE_USER, uid);
+ if (inviteList != null) {
+ invitePayloadData = new ArrayList<>();
+ for (InviteTable invite : inviteList) {
+ HashMap<String, String> inviteElement = new HashMap<>();
+ inviteElement.put(Constants.REQ_GROUP_ID, invite.getGid());
+ inviteElement.put(Constants.REQ_MEMBER,
+ invite.getInvitedUser());
+ invitePayloadData.add(inviteElement);
+ }
+ }
+
+ List<InviteTable> invitedList = getInviteTableList(
+ Constants.KEYFIELD_INVITED_USER, uid);
+ if (invitedList != null) {
+ invitedPayloadData = new ArrayList<>();
+ for (InviteTable invited : invitedList) {
+ HashMap<String, String> invitedElement = new HashMap<>();
+ invitedElement.put(Constants.REQ_GROUP_ID, invited.getGid());
+ invitedElement.put(Constants.REQ_MEMBER,
+ invited.getInviteUser());
+ invitedPayloadData.add(invitedElement);
+ }
+ }
+
+ responsePayload.put(Constants.RESP_INVITE, invitePayloadData);
+ responsePayload.put(Constants.RESP_INVITED, invitedPayloadData);
+
+ return responsePayload;
+ }
+
+ public HashMap<String, Object> addSubscriber(String uid, Device subscriber,
+ IRequest request) {
+
+ InviteSubscriber newSubscriber = new InviteSubscriber(subscriber,
+ request);
+ mSubscribers.put(uid, newSubscriber);
+
+ return getInvitationInfo(uid);
+ }
+
+ public HashMap<String, Object> removeSubscriber(String uid) {
+
+ if (mSubscribers.containsKey(uid)) {
+ mSubscribers.remove(uid);
+ }
+
+ return getInvitationInfo(uid);
+ }
+
+ private void notifyToSubscriber(String id) {
+
+ synchronized (mSubscribers) {
+ if (!mSubscribers.containsKey(id)) {
+ return;
+ }
+ Cbor<HashMap<String, Object>> cbor = new Cbor<>();
+ mSubscribers.get(id).mSubscriber.sendResponse(
+ MessageBuilder.createResponse(mSubscribers.get(id).mRequest,
+ ResponseStatus.CONTENT,
+ ContentFormat.APPLICATION_CBOR,
+ cbor.encodingPayloadToCbor(getInvitationInfo(id))));
+ }
+ }
+
+ private List<InviteTable> getInviteTableList(String property, String uid) {
+
+ InviteTable getInviteTable = new InviteTable();
+ ArrayList<InviteTable> inviteList = null;
+
+ HashMap<String, Object> condition = new HashMap<>();
+ condition.put(property, uid);
+ ArrayList<HashMap<String, Object>> mapInviteList = AccountDBManager
+ .getInstance().selectRecord(Constants.INVITE_TABLE, condition);
+ if (!mapInviteList.isEmpty()) {
+ inviteList = new ArrayList<>();
+ }
+
+ for (HashMap<String, Object> mapInviteTable : mapInviteList) {
+
+ getInviteTable = mTypeInvite.convertMaptoObject(mapInviteTable,
+ getInviteTable);
+ inviteList.add(getInviteTable);
+ }
+ return inviteList;
+
+ }
+
+}
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // 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.resources.acl.invite;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+
+import org.iotivity.cloud.accountserver.Constants;
+import org.iotivity.cloud.base.device.Device;
+import org.iotivity.cloud.base.exception.ServerException;
+import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
+import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.IResponse;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.enums.ContentFormat;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
+import org.iotivity.cloud.base.resource.Resource;
+import org.iotivity.cloud.util.Cbor;
+
+public class InviteResource extends Resource {
+
+ private InviteManager mInviteManager = new InviteManager();
+
+ private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
+
+ public InviteResource() {
+ super(Arrays.asList(Constants.PREFIX_WELL_KNOWN, Constants.PREFIX_OCF,
+ Constants.ACL_URI, Constants.INVITE_URI));
+ }
+
+ @Override
+ public void onDefaultRequestReceived(Device srcDevice, IRequest request)
+ throws ServerException {
+
+ IResponse response = null;
+
+ switch (request.getMethod()) {
+ case GET:
+ response = handleGetRequest(srcDevice, request);
+ break;
+ case POST:
+ response = handlePostRequest(request);
+ break;
+ case DELETE:
+ response = handleDeleteRequest(request);
+ break;
+ default:
+ throw new BadRequestException(request.getMethod()
+ + " request type is not supported");
+ }
+
+ srcDevice.sendResponse(response);
+ }
+
+ private IResponse handleGetRequest(Device srcDevice, IRequest request)
+ throws ServerException {
+
+ checkQueryException(Constants.REQ_UUID_ID, request.getUriQueryMap());
+
+ String uid = request.getUriQueryMap().get(Constants.REQ_UUID_ID).get(0);
+ HashMap<String, Object> responsePayload = null;
+
+ switch (request.getObserve()) {
+ case NOTHING:
+ responsePayload = mInviteManager.getInvitationInfo(uid);
+ break;
+ case SUBSCRIBE:
+ responsePayload = mInviteManager.addSubscriber(uid, srcDevice,
+ request);
+ break;
+ case UNSUBSCRIBE:
+ responsePayload = mInviteManager.removeSubscriber(uid);
+ break;
+ default:
+ break;
+ }
+
+ return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
+ ContentFormat.APPLICATION_CBOR,
+ mCbor.encodingPayloadToCbor(responsePayload));
+ }
+
+ @SuppressWarnings("unchecked")
+ private IResponse handlePostRequest(IRequest request)
+ throws ServerException {
+
+ HashMap<String, Object> payload = mCbor.parsePayloadFromCbor(
+ request.getPayload(), HashMap.class);
+
+ checkPayloadException(
+ Arrays.asList(Constants.REQ_UUID_ID, Constants.REQ_INVITE),
+ payload);
+
+ String uid = (String) payload.get(Constants.REQ_UUID_ID);
+ ArrayList<HashMap<String, String>> inviteList = (ArrayList<HashMap<String, String>>) payload
+ .get(Constants.REQ_INVITE);
+
+ for (HashMap<String, String> invite : inviteList) {
+
+ String gid = invite.get(Constants.REQ_GROUP_ID);
+ String mid = invite.get(Constants.REQ_MEMBER);
+
+ if (gid == null || mid == null) {
+
+ throw new PreconditionFailedException(
+ "value of invite property is invalid");
+ }
+
+ mInviteManager.addInvitation(uid, gid, mid);
+ }
+
+ return MessageBuilder.createResponse(request, ResponseStatus.CHANGED);
+ }
+
+ private IResponse handleDeleteRequest(IRequest request)
+ throws ServerException {
+
+ HashMap<String, List<String>> queryParams = request.getUriQueryMap();
+
+ checkQueryException(Constants.REQ_GROUP_ID, queryParams);
+
+ String gid = queryParams.get(Constants.REQ_GROUP_ID).get(0);
+ String uid = queryParams.get(Constants.REQ_UUID_ID).get(0);
+
+ if (queryParams.get(Constants.REQ_MEMBER) == null) {
+ mInviteManager.deleteInvitation(uid, gid);
+ } else {
+ String mid = queryParams.get(Constants.REQ_MEMBER).get(0);
+ mInviteManager.cancelInvitation(uid, gid, mid);
+ }
+
+ return MessageBuilder.createResponse(request, ResponseStatus.DELETED);
+ }
+
+}
\ No newline at end of file
import org.iotivity.cloud.ciserver.resources.proxy.account.AccountSession;
import org.iotivity.cloud.ciserver.resources.proxy.account.Acl;
import org.iotivity.cloud.ciserver.resources.proxy.account.AclGroup;
+import org.iotivity.cloud.ciserver.resources.proxy.account.AclInvite;
+import org.iotivity.cloud.ciserver.resources.proxy.account.Certificate;
import org.iotivity.cloud.ciserver.resources.proxy.mq.MessageQueue;
import org.iotivity.cloud.ciserver.resources.proxy.rd.DevicePresence;
import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceDirectory;
import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceFind;
import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourcePresence;
-import org.iotivity.cloud.ciserver.resources.proxy.account.Certificate;
import org.iotivity.cloud.util.Log;
public class CloudInterfaceServer {
boolean tlsMode = Integer.parseInt(args[7]) == 1;
- ConnectorPool.addConnection("rd",
- new InetSocketAddress(args[1], Integer.parseInt(args[2])),
- tlsMode);
- ConnectorPool.addConnection("account",
- new InetSocketAddress(args[3], Integer.parseInt(args[4])),
- tlsMode);
- ConnectorPool.addConnection("mq",
- new InetSocketAddress(args[5], Integer.parseInt(args[6])),
- tlsMode);
+ ConnectorPool.addConnection("rd", new InetSocketAddress(args[1],
+ Integer.parseInt(args[2])), tlsMode);
+ ConnectorPool.addConnection("account", new InetSocketAddress(args[3],
+ Integer.parseInt(args[4])), tlsMode);
+ ConnectorPool.addConnection("mq", new InetSocketAddress(args[5],
+ Integer.parseInt(args[6])), tlsMode);
DeviceServerSystem deviceServer = new DeviceServerSystem();
Acl aclHandler = new Acl();
AclGroup aclGroupHandler = new AclGroup();
Certificate certHandler = new Certificate();
+ AclInvite aclInviteHandler = new AclInvite();
CoapDevicePool devicePool = deviceServer.getDevicePool();
deviceServer.addResource(certHandler);
- KeepAliveResource resKeepAlive = new KeepAliveResource(
- new int[] { 1, 2, 4, 8 });
+ deviceServer.addResource(aclInviteHandler);
+
+ KeepAliveResource resKeepAlive = new KeepAliveResource(new int[] { 1,
+ 2, 4, 8 });
deviceServer.addResource(resKeepAlive);
deviceServer.addResource(new DiResource(devicePool));
- deviceServer.addServer(new CoapServer(
- new InetSocketAddress(Integer.parseInt(args[0]))));
+ deviceServer.addServer(new CoapServer(new InetSocketAddress(Integer
+ .parseInt(args[0]))));
// deviceServer.addServer(new HttpServer(new InetSocketAddress(8080)));
public void onDefaultRequestReceived(Device srcDevice, IRequest request)
throws ServerException {
if (request.getMethod().equals(RequestMethod.DELETE)) {
- String di = request.getUriQueryMap().get(Constants.REQ_DEVICE_ID)
- .get(0);
- StringBuffer uriQuery = new StringBuffer();
- uriQuery.append(Constants.REQ_DEVICE_ID + "=" + di);
- uriQuery.append(";");
- uriQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId());
-
- request = MessageBuilder.modifyRequest(request, null,
- uriQuery.toString(), null, null);
+ StringBuffer additionalQuery = new StringBuffer();
+ additionalQuery
+ .append(Constants.USER_ID + "=" + srcDevice.getUserId());
+ String uriQuery = request.getUriQuery() + ";"
+ + additionalQuery.toString();
+ request = MessageBuilder.modifyRequest(request, null, uriQuery,
+ null, null);
}
mAuthServer.sendRequest(request, srcDevice);
}
payloadData.put(Constants.REQ_GROUP_MASTER_ID,
srcDevice.getUserId());
} else {
- if (!payloadData.containsKey(Constants.REQ_DEVICE_LIST)
- && !payloadData
- .containsKey(Constants.REQ_MEMBER_LIST)) {
+ if (payloadData.isEmpty()) {
payloadData.put(Constants.REQ_MEMBER_LIST,
Arrays.asList(srcDevice.getUserId()));
}
uriGetQuery.toString(), null, null);
break;
case DELETE:
- String uriDeleteQuery;
StringBuffer additionalQuery = new StringBuffer();
if (getUriPathSegments()
.containsAll(request.getUriPathSegments())) {
additionalQuery.append(Constants.REQ_GROUP_MASTER_ID + "="
+ srcDevice.getUserId());
- uriDeleteQuery = request.getUriQuery() + ";"
+ String uriDeleteQuery = request.getUriQuery() + ";"
+ additionalQuery.toString();
request = MessageBuilder.modifyRequest(request, null,
uriDeleteQuery, null, null);
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // 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.ciserver.resources.proxy.account;
+
+import java.util.Arrays;
+import java.util.HashMap;
+
+import org.iotivity.cloud.base.connector.ConnectorPool;
+import org.iotivity.cloud.base.device.Device;
+import org.iotivity.cloud.base.device.IRequestChannel;
+import org.iotivity.cloud.base.exception.ServerException;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.enums.ContentFormat;
+import org.iotivity.cloud.base.resource.Resource;
+import org.iotivity.cloud.ciserver.Constants;
+import org.iotivity.cloud.util.Cbor;
+
+public class AclInvite extends Resource {
+
+ IRequestChannel mAuthServer = null;
+
+ public AclInvite() {
+ super(Arrays.asList(Constants.PREFIX_WELL_KNOWN, Constants.PREFIX_OCF,
+ Constants.ACL_URI, Constants.INVITE_URI));
+
+ mAuthServer = ConnectorPool.getConnection("account");
+
+ }
+
+ @Override
+ public void onDefaultRequestReceived(Device srcDevice, IRequest request)
+ throws ServerException {
+
+ switch (request.getMethod()) {
+
+ case POST:
+
+ Cbor<HashMap<String, Object>> cbor = new Cbor<>();
+ HashMap<String, Object> payload = cbor.parsePayloadFromCbor(
+ request.getPayload(), HashMap.class);
+
+ payload.put(Constants.USER_ID, srcDevice.getUserId());
+
+ request = MessageBuilder.modifyRequest(request, null, null,
+ ContentFormat.APPLICATION_CBOR,
+ cbor.encodingPayloadToCbor(payload));
+ break;
+
+ default:
+
+ StringBuffer additionalQuery = new StringBuffer();
+ additionalQuery.append(
+ Constants.USER_ID + "=" + srcDevice.getUserId());
+
+ String uriQuery = additionalQuery.toString()
+ + (request.getUriQuery() != null
+ ? (";" + request.getUriQuery()) : "");
+ request = MessageBuilder.modifyRequest(request, null, uriQuery,
+ null, null);
+
+ break;
+ }
+
+ mAuthServer.sendRequest(request, srcDevice);
+ }
+
+}
\ No newline at end of file
import org.iotivity.cloud.base.device.IRequestChannel;
import org.iotivity.cloud.base.device.IResponseEventHandler;
import org.iotivity.cloud.base.exception.ClientException;
-import org.iotivity.cloud.base.exception.ClientException.BadResponseException;
import org.iotivity.cloud.base.exception.ServerException;
-import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
import org.iotivity.cloud.base.protocols.IRequest;
import org.iotivity.cloud.base.protocols.IResponse;
import org.iotivity.cloud.base.protocols.MessageBuilder;
import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
import org.iotivity.cloud.base.resource.Resource;
import org.iotivity.cloud.ciserver.Constants;
import org.iotivity.cloud.util.Cbor;
throws ClientException {
switch (response.getStatus()) {
case CONTENT:
- StringBuilder additionalQuery = new StringBuilder();
-
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(response.getPayload(),
HashMap.class);
- List<String> deviceList = (List<String>) payloadData
- .get(Constants.REQ_DEVICE_LIST);
- if (deviceList != null) {
- int index = deviceList.size();
- if (mRequest.getUriQuery() == null) {
- for (String device : deviceList) {
- if (!device.equals(mSrcDevice.getDeviceId())) {
- additionalQuery
- .append(Constants.REQ_DEVICE_ID
- + "=" + device);
- if (--index > 0) {
- additionalQuery.append(";");
- }
- }
- }
- String uriQuery = (mRequest.getUriQuery() == null
- ? "" : mRequest.getUriQuery()) + ";"
- + additionalQuery.toString();
- mRequest = MessageBuilder.modifyRequest(mRequest,
- null, uriQuery, null, null);
- } else {
- List<String> originDeviceList = mRequest
- .getUriQueryMap()
- .get(Constants.REQ_DEVICE_ID);
- if (originDeviceList == null) {
- throw new UnAuthorizedException(
- "device is not include in group");
- }
- for (String device : originDeviceList) {
- if (!deviceList.contains(device)) {
- throw new UnAuthorizedException(device
- + " device is not include in group");
- }
- }
+
+ if (mRequest.getUriQuery() != null
+ && mRequest.getUriQueryMap()
+ .containsKey(Constants.REQ_DEVICE_ID)) {
+ if (!getResponseDeviceList(payloadData)
+ .containsAll(mRequest.getUriQueryMap()
+ .get(Constants.REQ_DEVICE_ID))) {
+ mSrcDevice.sendResponse(
+ MessageBuilder.createResponse(mRequest,
+ ResponseStatus.BAD_REQUEST));
}
- mRDServer.sendRequest(mRequest, mSrcDevice);
- } else
- throw new UnAuthorizedException(
- "There is not device in group");
+ } else {
+ String additionalQuery = makeAdditionalQuery(
+ payloadData, mSrcDevice.getDeviceId());
+ if (additionalQuery == null) {
+ mSrcDevice.sendResponse(
+ MessageBuilder.createResponse(mRequest,
+ ResponseStatus.BAD_REQUEST));
+ return;
+ }
+ mRequest = MessageBuilder.modifyRequest(mRequest, null,
+ additionalQuery
+ + (mRequest.getUriQuery() != null
+ ? ";" + mRequest.getUriQuery()
+ : ""),
+ null, null);
+ }
+
+ mRDServer.sendRequest(mRequest, mSrcDevice);
break;
default:
- throw new BadResponseException(
- response.getStatus().toString()
- + " response type is not supported");
+ mSrcDevice.sendResponse(MessageBuilder.createResponse(
+ mRequest, ResponseStatus.BAD_REQUEST));
}
}
+
+ private String makeAdditionalQuery(HashMap<String, Object> payloadData,
+ String did) {
+
+ StringBuilder additionalQuery = new StringBuilder();
+
+ List<String> deviceList = getResponseDeviceList(payloadData);
+
+ if (deviceList.isEmpty()) {
+ return null;
+ }
+
+ int index = deviceList.size();
+ for (String device : deviceList) {
+ additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
+ if (--index > 0) {
+ additionalQuery.append(";");
+ }
+ }
+ return additionalQuery.toString();
+ }
+
+ @SuppressWarnings("unchecked")
+ private List<String> getResponseDeviceList(
+ HashMap<String, Object> payloadData) {
+ List<String> deviceList = (List<String>) payloadData
+ .get(Constants.REQ_DEVICE_LIST);
+
+ return deviceList;
+ }
}
@Override
import org.iotivity.cloud.base.device.IRequestChannel;
import org.iotivity.cloud.base.device.IResponseEventHandler;
import org.iotivity.cloud.base.exception.ClientException;
-import org.iotivity.cloud.base.exception.ClientException.BadResponseException;
import org.iotivity.cloud.base.exception.ServerException;
-import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
import org.iotivity.cloud.base.protocols.IRequest;
import org.iotivity.cloud.base.protocols.IResponse;
import org.iotivity.cloud.base.protocols.MessageBuilder;
import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
import org.iotivity.cloud.base.resource.Resource;
import org.iotivity.cloud.ciserver.Constants;
import org.iotivity.cloud.util.Cbor;
throws ClientException {
switch (response.getStatus()) {
case CONTENT:
- StringBuilder additionalQuery = new StringBuilder();
-
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(response.getPayload(),
HashMap.class);
- List<String> deviceList = (List<String>) payloadData
- .get(Constants.REQ_DEVICE_LIST);
- if (deviceList != null) {
- int index = deviceList.size();
- if (mRequest.getUriQuery() == null) {
- for (String device : deviceList) {
- if (!device.equals(mSrcDevice.getDeviceId())) {
- additionalQuery
- .append(Constants.REQ_DEVICE_ID
- + "=" + device);
- if (--index > 0) {
- additionalQuery.append(";");
- }
- }
- }
- String uriQuery = (mRequest.getUriQuery() == null
- ? "" : mRequest.getUriQuery()) + ";"
- + additionalQuery.toString();
- mRequest = MessageBuilder.modifyRequest(mRequest,
- null, uriQuery, null, null);
- } else {
- List<String> originDeviceList = mRequest
- .getUriQueryMap()
- .get(Constants.REQ_DEVICE_ID);
- if (originDeviceList == null) {
- throw new UnAuthorizedException(
- "device is not include in group");
- }
- for (String device : originDeviceList) {
- if (!deviceList.contains(device)) {
- throw new UnAuthorizedException(device
- + " device is not include in group");
- }
- }
+
+ if (mRequest.getUriQuery() != null
+ && mRequest.getUriQueryMap()
+ .containsKey(Constants.REQ_DEVICE_ID)) {
+ if (!getResponseDeviceList(payloadData)
+ .containsAll(mRequest.getUriQueryMap()
+ .get(Constants.REQ_DEVICE_ID))) {
+ mSrcDevice.sendResponse(
+ MessageBuilder.createResponse(mRequest,
+ ResponseStatus.BAD_REQUEST));
}
- mRDServer.sendRequest(mRequest, mSrcDevice);
- } else
- throw new UnAuthorizedException(
- "There is not device in group");
+ } else {
+ String additionalQuery = makeAdditionalQuery(
+ payloadData, mSrcDevice.getDeviceId());
+ if (additionalQuery == null) {
+ mSrcDevice.sendResponse(
+ MessageBuilder.createResponse(mRequest,
+ ResponseStatus.BAD_REQUEST));
+ return;
+ }
+ mRequest = MessageBuilder.modifyRequest(mRequest, null,
+ additionalQuery
+ + (mRequest.getUriQuery() != null
+ ? ";" + mRequest.getUriQuery()
+ : ""),
+ null, null);
+ }
+
+ mRDServer.sendRequest(mRequest, mSrcDevice);
break;
default:
- throw new BadResponseException(
- response.getStatus().toString()
- + " response type is not supported");
+ mSrcDevice.sendResponse(MessageBuilder.createResponse(
+ mRequest, ResponseStatus.BAD_REQUEST));
}
}
+
+ private String makeAdditionalQuery(HashMap<String, Object> payloadData,
+ String did) {
+
+ StringBuilder additionalQuery = new StringBuilder();
+
+ List<String> deviceList = getResponseDeviceList(payloadData);
+
+ if (deviceList.isEmpty()) {
+ return null;
+ }
+
+ int index = deviceList.size();
+ for (String device : deviceList) {
+ additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
+ if (--index > 0) {
+ additionalQuery.append(";");
+ }
+ }
+ return additionalQuery.toString();
+ }
+
+ @SuppressWarnings("unchecked")
+ private List<String> getResponseDeviceList(
+ HashMap<String, Object> payloadData) {
+ List<String> deviceList = (List<String>) payloadData
+ .get(Constants.REQ_DEVICE_LIST);
+
+ return deviceList;
+ }
}
@Override
import org.iotivity.cloud.base.device.IRequestChannel;
import org.iotivity.cloud.base.device.IResponseEventHandler;
import org.iotivity.cloud.base.exception.ClientException;
-import org.iotivity.cloud.base.exception.ClientException.BadResponseException;
import org.iotivity.cloud.base.exception.ServerException;
-import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
import org.iotivity.cloud.base.protocols.IRequest;
import org.iotivity.cloud.base.protocols.IResponse;
import org.iotivity.cloud.base.protocols.MessageBuilder;
import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
import org.iotivity.cloud.base.resource.Resource;
import org.iotivity.cloud.ciserver.Constants;
import org.iotivity.cloud.util.Cbor;
throws ClientException {
switch (response.getStatus()) {
case CONTENT:
- StringBuilder additionalQuery = new StringBuilder();
-
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(response.getPayload(),
HashMap.class);
- List<String> deviceList = (List<String>) payloadData
- .get(Constants.REQ_DEVICE_LIST);
- if (deviceList != null) {
- int index = deviceList.size();
- if (mRequest.getUriQuery() == null) {
- for (String device : deviceList) {
- if (!device.equals(mSrcDevice.getDeviceId())) {
- additionalQuery
- .append(Constants.REQ_DEVICE_ID
- + "=" + device);
- if (--index > 0) {
- additionalQuery.append(";");
- }
- }
- }
- String uriQuery = (mRequest.getUriQuery() == null
- ? "" : mRequest.getUriQuery()) + ";"
- + additionalQuery.toString();
- mRequest = MessageBuilder.modifyRequest(mRequest,
- null, uriQuery, null, null);
- } else {
- List<String> originDeviceList = mRequest
- .getUriQueryMap()
- .get(Constants.REQ_DEVICE_ID);
- if (originDeviceList == null) {
- throw new UnAuthorizedException(
- "device is not include in group");
- }
- for (String device : originDeviceList) {
- if (!deviceList.contains(device)) {
- throw new UnAuthorizedException(device
- + " device is not include in group");
- }
- }
+
+ if (mRequest.getUriQuery() != null
+ && mRequest.getUriQueryMap()
+ .containsKey(Constants.REQ_DEVICE_ID)) {
+ if (!getResponseDeviceList(payloadData)
+ .containsAll(mRequest.getUriQueryMap()
+ .get(Constants.REQ_DEVICE_ID))) {
+ mSrcDevice.sendResponse(
+ MessageBuilder.createResponse(mRequest,
+ ResponseStatus.BAD_REQUEST));
}
- mRDServer.sendRequest(mRequest, mSrcDevice);
- } else
- throw new UnAuthorizedException(
- "There is not device in group");
+ } else {
+ String additionalQuery = makeAdditionalQuery(
+ payloadData, mSrcDevice.getDeviceId());
+ if (additionalQuery == null) {
+ mSrcDevice.sendResponse(
+ MessageBuilder.createResponse(mRequest,
+ ResponseStatus.BAD_REQUEST));
+ return;
+ }
+ mRequest = MessageBuilder.modifyRequest(mRequest, null,
+ additionalQuery
+ + (mRequest.getUriQuery() != null
+ ? ";" + mRequest.getUriQuery()
+ : ""),
+ null, null);
+ }
+
+ mRDServer.sendRequest(mRequest, mSrcDevice);
break;
default:
- throw new BadResponseException(
- response.getStatus().toString()
- + " response type is not supported");
+ mSrcDevice.sendResponse(MessageBuilder.createResponse(
+ mRequest, ResponseStatus.BAD_REQUEST));
}
}
+
+ private String makeAdditionalQuery(HashMap<String, Object> payloadData,
+ String did) {
+
+ StringBuilder additionalQuery = new StringBuilder();
+
+ List<String> deviceList = getResponseDeviceList(payloadData);
+
+ if (deviceList.isEmpty()) {
+ return null;
+ }
+
+ int index = deviceList.size();
+ for (String device : deviceList) {
+ additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
+ if (--index > 0) {
+ additionalQuery.append(";");
+ }
+ }
+ return additionalQuery.toString();
+ }
+
+ @SuppressWarnings("unchecked")
+ private List<String> getResponseDeviceList(
+ HashMap<String, Object> payloadData) {
+ List<String> deviceList = (List<String>) payloadData
+ .get(Constants.REQ_DEVICE_LIST);
+
+ return deviceList;
+ }
}
@Override
public class OCFConstants {
/* resource Interface for keep-alive */
- public static final String DEFAULT_INTERFACE = "oic.if.baseline";
+ public static final String DEFAULT_INTERFACE = "oic.if.baseline";
- public static final String LINK_INTERFACE = "oic.if.ll";
+ public static final String LINK_INTERFACE = "oic.if.ll";
- public static final String PREFIX_WELL_KNOWN = ".well-known";
+ public static final String PREFIX_WELL_KNOWN = ".well-known";
- public static final String PREFIX_OCF = "ocf";
+ public static final String PREFIX_OCF = "ocf";
- public static final String PREFIX_OIC = "oic";
+ public static final String PREFIX_OIC = "oic";
- public static final String RS_INTERFACE = "if";
+ public static final String RS_INTERFACE = "if";
/* resource url for account */
- public static final String ACCOUNT_URI = "account";
+ public static final String ACCOUNT_URI = "account";
- public static final String SESSION_URI = "session";
+ public static final String SESSION_URI = "session";
- public static final String DEVICE_URI = "device";
+ public static final String DEVICE_URI = "device";
- public static final String TOKEN_REFRESH_URI = "tokenrefresh";
+ public static final String TOKEN_REFRESH_URI = "tokenrefresh";
/* resource url for acl */
- public static final String CREDPROV_URI = "credprov";
+ public static final String CREDPROV_URI = "credprov";
- public static final String CERT_URI = "cert";
+ public static final String CERT_URI = "cert";
/* resource url for acl */
public static final String ACL_URI = "acl";
public static final String ID_URI = "id";
+ public static final String INVITE_URI = "invite";
+
/* resource uri to publish, update, delete resource info */
- public static final String RD_URI = "rd";
+ public static final String RD_URI = "rd";
/* resource uri for keep-alive */
- public static final String KEEP_ALIVE_URI = "ping";
+ public static final String KEEP_ALIVE_URI = "ping";
/* resource uri to discover resource info */
- public static final String WELL_KNOWN_URI = "res";
+ public static final String WELL_KNOWN_URI = "res";
/* resource uri for resource presence */
- public static final String RES_PRESENCE_URI = "ad";
+ public static final String RES_PRESENCE_URI = "ad";
/* resource uri for device presence */
public static final String DEVICE_PRESENCE_URI = "prs";
/* resource uri for message queue */
- public static final String MQ_BROKER_URI = "ps";
- public static final String MQ_BROKER_RT = "oic.wk.ps";
+ public static final String MQ_BROKER_URI = "ps";
/* file path for tls communication - Modify filePath to yours */
- public static final String ROOT_CERT_FILE = "./rootca.crt";
+ public static final String ROOT_CERT_FILE = "./rootca.crt";
- public static final String CLOUD_CERT_FILE = "./iotivitycloud.crt";
+ public static final String CLOUD_CERT_FILE = "./iotivitycloud.crt";
- public static final String CLOUD_KEY_FILE = "./iotivitycloud.key";
+ public static final String CLOUD_KEY_FILE = "./iotivitycloud.key";
/* cloud uuid */
public static final String CLOUD_UUID = "2a6085d1-815d-4277-baba-4e4e4df91308";
for (byte[] querySegment : uri_query) {
uriQuery.append(new String(querySegment, StandardCharsets.UTF_8));
if (--nItem > 0) {
- uriQuery.append('&');
+ uriQuery.append(';');
}
}
return uriQuery.toString();