From 37446f9d945fc5fbf84652df607ce0728004a0bf Mon Sep 17 00:00:00 2001 From: Jung Seungho Date: Fri, 25 Nov 2016 14:58:59 +0900 Subject: [PATCH] [IOT-1604] Fix cloud routing code - Fix on do GET request to discovered resource server. - changed DiResource to RouteResource - code clean up https://jira.iotivity.org/browse/IOT-1604 Change-Id: If1ccc98bb4d8e288d5264d1cadaf540ef0ca4794 Signed-off-by: Jee Hyeok Kim Signed-off-by: Jung Seungho Reviewed-on: https://gerrit.iotivity.org/gerrit/14781 Tested-by: jenkins-iotivity (cherry picked from commit f1e992e402ce526f3b2d9b4dd769d91a9f8888d8) Reviewed-on: https://gerrit.iotivity.org/gerrit/16087 Tested-by: jenkins-iotivity --- .../cloud/ciserver/CloudInterfaceServer.java | 4 +- .../org/iotivity/cloud/ciserver/Constants.java | 1 + .../cloud/ciserver/DeviceServerSystem.java | 56 ++++++------ .../{DiResource.java => RouteResource.java} | 100 +++++++++++---------- .../ciserver/resources/proxy/account/Account.java | 38 +------- .../resources/proxy/rd/DevicePresence.java | 17 +--- .../resources/proxy/rd/ResourceDirectory.java | 53 +++-------- .../ciserver/resources/proxy/rd/ResourceFind.java | 40 +++------ .../resources/proxy/rd/ResourcePresence.java | 44 +++------ ...{DiResourceTest.java => RouteResourceTest.java} | 18 ++-- .../resources/proxy/rd/ResourceDirectoryTest.java | 18 ++-- .../resources/proxy/rd/ResourceFindTest.java | 3 - .../java/org/iotivity/cloud/base/OICConstants.java | 5 ++ .../iotivity/cloud/base/connector/CoapClient.java | 21 +++-- .../org/iotivity/cloud/base/device/CoapDevice.java | 33 ++++--- 15 files changed, 172 insertions(+), 279 deletions(-) rename cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/{DiResource.java => RouteResource.java} (79%) rename cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/{DiResourceTest.java => RouteResourceTest.java} (96%) diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java index f948771..62f0164 100755 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java @@ -27,7 +27,7 @@ import java.util.Scanner; import org.iotivity.cloud.base.connector.ConnectorPool; import org.iotivity.cloud.base.server.CoapServer; import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool; -import org.iotivity.cloud.ciserver.resources.DiResource; +import org.iotivity.cloud.ciserver.resources.RouteResource; import org.iotivity.cloud.ciserver.resources.KeepAliveResource; import org.iotivity.cloud.ciserver.resources.proxy.account.Account; import org.iotivity.cloud.ciserver.resources.proxy.account.AccountSession; @@ -113,7 +113,7 @@ public class CloudInterfaceServer { deviceServer.addResource(resKeepAlive); - deviceServer.addResource(new DiResource(devicePool)); + deviceServer.addResource(new RouteResource(devicePool)); deviceServer.addServer(new CoapServer( new InetSocketAddress(Integer.parseInt(args[0])))); diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java index af91bcc..ac921ae 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java @@ -43,6 +43,7 @@ public class Constants extends OICConstants { public static final String REQ_MEMBER_ID = "mid"; public static final String REQ_MEMBER_LIST = "members"; public static final String REQ_DEVICE_ID = "di"; + public static final String REQ_DEVICE_ROUTE = "route"; public static final String REQ_DEVICE_LIST = "devices"; public static final String REQ_INVITE = "invite"; public static final String REQ_PING_ARRAY = "inarray"; diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java index 8aa5823..4571b80 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java @@ -109,8 +109,7 @@ public class DeviceServerSystem extends ServerSystem { Iterator iterator = mMapDevice.keySet().iterator(); while (iterator.hasNext()) { String deviceId = iterator.next(); - CoapDevice getDevice = (CoapDevice) mDevicePool - .queryDevice(deviceId); + CoapDevice getDevice = (CoapDevice) queryDevice(deviceId); getDevice.removeObserveChannel( ((CoapDevice) device).getRequestChannel()); } @@ -154,11 +153,14 @@ public class DeviceServerSystem extends ServerSystem { CoapRequest coapRequest = (CoapRequest) msg; IRequestChannel targetChannel = null; - if (coapRequest.getUriPathSegments() - .contains(Constants.REQ_DEVICE_ID)) { + if (coapRequest.getUriPath() + .contains(Constants.ROUTE_FULL_URI)) { + + int RouteResourcePathSize = Constants.ROUTE_FULL_URI + .split("/").length; CoapDevice targetDevice = (CoapDevice) mDevicePool .queryDevice(coapRequest.getUriPathSegments() - .get(1)); + .get(RouteResourcePathSize - 1)); targetChannel = targetDevice.getRequestChannel(); } switch (coapRequest.getObserve()) { @@ -194,34 +196,32 @@ public class DeviceServerSystem extends ServerSystem { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - if (!(msg instanceof CoapResponse)) { - throw new BadRequestException( - "this msg type is not CoapResponse"); - } - // This is CoapResponse - // Once the response is valid, add this to deviceList - CoapResponse response = (CoapResponse) msg; - - switch (response.getUriPath()) { - case OICConstants.ACCOUNT_SESSION_FULL_URI: - if (response.getStatus() != ResponseStatus.CHANGED) { - throw new UnAuthorizedException(); - } + boolean bCloseConnection = false; - if (response.getPayload() != null) { - break; - } + if (msg instanceof CoapResponse) { + // This is CoapResponse + // Once the response is valid, add this to deviceList + CoapResponse response = (CoapResponse) msg; - ctx.close(); - break; - case OICConstants.ACCOUNT_FULL_URI: - if (response.getStatus() != ResponseStatus.DELETED) { + switch (response.getUriPath()) { + case OICConstants.ACCOUNT_SESSION_FULL_URI: + if (response.getStatus() != ResponseStatus.CHANGED) { + bCloseConnection = true; + } break; - } - ctx.close(); - break; + case OICConstants.ACCOUNT_FULL_URI: + if (response.getStatus() == ResponseStatus.DELETED) { + bCloseConnection = true; + } + break; + } } + ctx.writeAndFlush(msg); + + if (bCloseConnection == true) { + ctx.close(); + } } @Override diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/DiResource.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/RouteResource.java similarity index 79% rename from cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/DiResource.java rename to cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/RouteResource.java index efb81f5..9ba78a8 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/DiResource.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/RouteResource.java @@ -33,9 +33,6 @@ import org.iotivity.cloud.base.device.Device; import org.iotivity.cloud.base.device.IRequestChannel; import org.iotivity.cloud.base.device.IResponseEventHandler; import org.iotivity.cloud.base.exception.ServerException; -import org.iotivity.cloud.base.exception.ServerException.BadRequestException; -import org.iotivity.cloud.base.exception.ServerException.NotFoundException; -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; @@ -54,32 +51,26 @@ import org.iotivity.cloud.util.Cbor; * device * */ -public class DiResource extends Resource { +public class RouteResource extends Resource { private CoapDevicePool mDevicePool = null; private IRequestChannel mASServer = null; private Cbor> mCbor = new Cbor<>(); - public DiResource(CoapDevicePool devicePool) { - super(Arrays.asList(Constants.REQ_DEVICE_ID)); + public RouteResource(CoapDevicePool devicePool) { + super(Arrays.asList(Constants.PREFIX_OIC, Constants.REQ_DEVICE_ROUTE)); mASServer = ConnectorPool.getConnection("account"); mDevicePool = devicePool; } private IRequestChannel getTargetDeviceChannel(IRequest request) throws ServerException { - List uriPathSegment = request.getUriPathSegments(); - - if (uriPathSegment.size() < 2) { - throw new PreconditionFailedException(); - } - String deviceId = uriPathSegment.get(1); CoapDevice targetDevice = (CoapDevice) mDevicePool - .queryDevice(deviceId); + .queryDevice(getDeviceId(request)); if (targetDevice == null) { - throw new NotFoundException(); + return null; } // Do request and receive response @@ -90,7 +81,9 @@ public class DiResource extends Resource { List uriPathSegment = request.getUriPathSegments(); // Remove prefix path - uriPathSegment.remove(0); + for (String path : getUriPathSegments()) { + uriPathSegment.remove(path); + } uriPathSegment.remove(0); StringBuilder uriPath = new StringBuilder(); @@ -107,14 +100,24 @@ public class DiResource extends Resource { CoapResponse coapResponse = (CoapResponse) response; + StringBuilder uriPath = new StringBuilder(); + for (String path : getUriPathSegments()) { + uriPath.append("/" + path); + } + if (coapResponse.getUriPath().isEmpty() == false) { - convertedUri = "/di/" + di + "/" + coapResponse.getUriPath(); + convertedUri = uriPath.toString() + "/" + di + + coapResponse.getUriPath(); } return MessageBuilder.modifyResponse(response, convertedUri, null, null); } + private String getDeviceId(IRequest request) { + return request.getUriPathSegments().get(getUriPathSegments().size()); + } + /** * * This class provides a set of APIs to handling message contains link @@ -125,30 +128,40 @@ public class DiResource extends Resource { private Cbor>> mCbor = new Cbor<>(); private String mTargetDI = null; private Device mSrcDevice = null; + private IRequest mRequest = null; - public LinkInterfaceHandler(String targetDI, Device srcDevice) { + public LinkInterfaceHandler(String targetDI, Device srcDevice, + IRequest request) { mTargetDI = targetDI; mSrcDevice = srcDevice; + mRequest = request; } private void convertHref(List> linkPayload) { + + StringBuilder uriPath = new StringBuilder(); + for (String path : getUriPathSegments()) { + uriPath.append("/" + path); + } + for (HashMap link : linkPayload) { - link.put("href", "/di/" + mTargetDI + link.get("href")); + link.put("href", uriPath.toString() + "/" + mTargetDI + + link.get("href")); } } @Override public void onResponseReceived(IResponse response) { List> linkPayload = null; - if (response.getStatus() == ResponseStatus.CONTENT) { + if (response.getStatus().equals(ResponseStatus.CONTENT)) { linkPayload = mCbor.parsePayloadFromCbor(response.getPayload(), ArrayList.class); if (linkPayload == null) { - throw new BadRequestException("payload is null"); + mSrcDevice.sendResponse(MessageBuilder.createResponse( + mRequest, ResponseStatus.NOT_FOUND)); } convertHref(linkPayload); } - mSrcDevice.sendResponse(MessageBuilder.modifyResponse( convertReponseUri(response, mTargetDI), ContentFormat.APPLICATION_CBOR, linkPayload != null @@ -194,9 +207,9 @@ public class DiResource extends Resource { .get(Constants.RESP_GRANT_POLICY); verifyRequest(mSrcDevice, mRequest, gp); break; + default: - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); + mSrcDevice.sendResponse(response); } } @@ -205,42 +218,35 @@ public class DiResource extends Resource { private void verifyRequest(Device srcDevice, IRequest request, String grantPermisson) { switch (grantPermisson) { + case Constants.RESP_ACL_DENIED: + srcDevice.sendResponse(MessageBuilder.createResponse(request, + ResponseStatus.UNAUTHORIZED)); + break; + case Constants.RESP_ACL_ALLOWED: IRequestChannel requestChannel = getTargetDeviceChannel( request); if (requestChannel == null) { - throw new NotFoundException(); + srcDevice.sendResponse(MessageBuilder + .createResponse(request, ResponseStatus.NOT_FOUND)); + break; } - String deviceId = request.getUriPathSegments().get(1); - - IResponseEventHandler responseHandler = null; - if (request.getUriQuery() != null && checkQueryException( - Constants.RS_INTERFACE, request.getUriQueryMap())) { - boolean hasLinkInterface = request.getUriQuery() - .contains(Constants.LINK_INTERFACE); - if (hasLinkInterface) { - responseHandler = new LinkInterfaceHandler(deviceId, - srcDevice); - } - } else { - responseHandler = new DefaultResponseHandler(deviceId, - srcDevice); + IResponseEventHandler responseHandler = new DefaultResponseHandler( + getDeviceId(request), srcDevice); + if (request.getUriQuery() != null && request.getUriQuery() + .contains(Constants.LINK_INTERFACE)) { + responseHandler = new LinkInterfaceHandler( + getDeviceId(request), srcDevice, request); } String uriPath = extractTargetUriPath(request); IRequest requestToResource = MessageBuilder .modifyRequest(request, uriPath, null, null, null); + requestChannel.sendRequest(requestToResource, responseHandler); break; - case Constants.RESP_ACL_DENIED: - srcDevice.sendResponse(MessageBuilder.createResponse(request, - ResponseStatus.UNAUTHORIZED)); - break; - default: - srcDevice.sendResponse(MessageBuilder.createResponse(request, - ResponseStatus.BAD_REQUEST)); } } @@ -251,8 +257,8 @@ public class DiResource extends Resource { StringBuffer uriQuery = new StringBuffer(); uriQuery.append(Constants.REQ_SEARCH_USER_ID + "=" + srcDevice.getUserId() + ";"); - uriQuery.append(Constants.REQ_DEVICE_ID + "=" - + request.getUriPathSegments().get(1) + ";"); + uriQuery.append( + Constants.REQ_DEVICE_ID + "=" + getDeviceId(request) + ";"); uriQuery.append( Constants.REQ_REQUEST_METHOD + "=" + request.getMethod() + ";"); uriQuery.append(Constants.REQ_REQUEST_URI + "=" diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java index a4e08ca..8cc0c37 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java @@ -33,7 +33,6 @@ 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; @@ -52,33 +51,6 @@ public class Account extends Resource { mASServer = ConnectorPool.getConnection("account"); } - class RDReceiveHandler implements IResponseEventHandler { - - private Device mSrcDevice; - private IResponse mResponse; - private IRequest mRequest; - - public RDReceiveHandler(IRequest request, IResponse response, - Device srcDevice) { - mSrcDevice = srcDevice; - mRequest = request; - mResponse = response; - } - - @Override - public void onResponseReceived(IResponse response) - throws ClientException { - switch (response.getStatus()) { - case DELETED: - mSrcDevice.sendResponse(mResponse); - break; - default: - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); - } - } - } - class AccountReceiveHandler implements IResponseEventHandler { IRequestChannel mRDServer = null; @@ -102,16 +74,10 @@ public class Account extends Resource { mRDServer.sendRequest( MessageBuilder.createRequest(RequestMethod.DELETE, uriPath.toString(), mRequest.getUriQuery()), - new RDReceiveHandler(mRequest, response, - mSrcDevice)); - break; - case CHANGED: - case CONTENT: - mSrcDevice.sendResponse(response); + mSrcDevice); break; default: - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); + mSrcDevice.sendResponse(response); } } } diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java index fe86968..80d22c0 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java @@ -80,12 +80,6 @@ public class DevicePresence extends Resource { .parsePayloadFromCbor(response.getPayload(), HashMap.class); - if (payloadData == null) { - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); - return; - } - ArrayList devices = (ArrayList) getResponseDeviceList( payloadData); @@ -101,12 +95,7 @@ public class DevicePresence extends Resource { } else { String additionalQuery = makeAdditionalQuery( payloadData, mSrcDevice.getDeviceId()); - if (additionalQuery == null) { - mSrcDevice.sendResponse( - MessageBuilder.createResponse(mRequest, - ResponseStatus.BAD_REQUEST)); - return; - } + String uriQuery = additionalQuery.toString() + (mRequest.getUriQuery() != null ? (";" + mRequest.getUriQuery()) : ""); @@ -116,9 +105,9 @@ public class DevicePresence extends Resource { mRDServer.sendRequest(mRequest, mSrcDevice); break; + default: - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); + mSrcDevice.sendResponse(response); } } diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java index 1be0ebf..b71df9e 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java @@ -37,7 +37,6 @@ 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.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; @@ -95,6 +94,7 @@ public class ResourceDirectory extends Resource { case DELETE: mRDServer.sendRequest(request, srcDevice); break; + default: throw new BadRequestException( request.getMethod() + " request type is not support"); @@ -116,16 +116,9 @@ public class ResourceDirectory extends Resource { switch (response.getStatus()) { case CHANGED: - byte[] convertedPayload = convertPublishHref(mRequest, mSrcDevice); - if (convertedPayload == null) { - - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.PRECONDITION_FAILED)); - } - mRequest = MessageBuilder.modifyRequest(mRequest, null, null, ContentFormat.APPLICATION_CBOR, convertedPayload); @@ -135,8 +128,7 @@ public class ResourceDirectory extends Resource { break; default: - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); + mSrcDevice.sendResponse(response); } } @@ -147,18 +139,14 @@ public class ResourceDirectory extends Resource { HashMap payload = cbor .parsePayloadFromCbor(request.getPayload(), HashMap.class); - if (verifyPublishPayload(payload) == false) { - - return null; - } - ArrayList> links = (ArrayList>) payload .get(Constants.REQ_LINKS); for (HashMap link : links) { String href = (String) link.get(Constants.REQ_HREF); - href = "/di/" + device.getDeviceId() + href; + href = Constants.ROUTE_FULL_URI + "/" + device.getDeviceId() + + href; link.put(Constants.REQ_HREF, href); } @@ -167,28 +155,6 @@ public class ResourceDirectory extends Resource { return cbor.encodingPayloadToCbor(payload); } - - @SuppressWarnings("unchecked") - private boolean verifyPublishPayload(HashMap payload) { - - ArrayList> links = (ArrayList>) payload - .get(Constants.REQ_LINKS); - - if (links == null || links.isEmpty()) { - return false; - } - - for (HashMap link : links) { - - String href = (String) link.get(Constants.REQ_HREF); - - if (href == null || href.isEmpty()) { - return false; - } - } - - return true; - } } // handles response to convert href in response payload into original href @@ -207,13 +173,11 @@ public class ResourceDirectory extends Resource { switch (response.getStatus()) { case CHANGED: - response = MessageBuilder.modifyResponse(response, ContentFormat.APPLICATION_CBOR, convertResponseHref(response)); default: - mSrcDevice.sendResponse(response); } } @@ -235,9 +199,14 @@ public class ResourceDirectory extends Resource { // remove prefix ArrayList hrefSegments = new ArrayList<>( Arrays.asList(href.split("/"))); - for (int i = 0; i < 3; i++) { - hrefSegments.remove(0); + + ArrayList RouteResourceSegment = new ArrayList<>( + Arrays.asList(Constants.ROUTE_FULL_URI.split("/"))); + + for (String path : RouteResourceSegment) { + hrefSegments.remove(path); } + hrefSegments.remove(0); StringBuilder newHref = new StringBuilder(); for (String path : hrefSegments) { diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java index 55da35e..e13382a 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java @@ -36,7 +36,6 @@ 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; @@ -80,39 +79,23 @@ public class ResourceFind extends Resource { .parsePayloadFromCbor(response.getPayload(), HashMap.class); - if (payloadData == null) { - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); - return; - } - ArrayList devices = (ArrayList) getResponseDeviceList( payloadData); - if (mRequest.getUriQuery() != null - && mRequest.getUriQueryMap() - .containsKey(Constants.REQ_DEVICE_ID)) { - if (!devices.containsAll(mRequest.getUriQueryMap() - .get(Constants.REQ_DEVICE_ID))) { - mSrcDevice.sendResponse( - MessageBuilder.createResponse(mRequest, - ResponseStatus.BAD_REQUEST)); - } - } else { - StringBuilder additionalQuery = makeAdditionalQuery( - devices); - String uriQuery = (additionalQuery != null - ? additionalQuery.toString() : "") - + (mRequest.getUriQuery() != null - ? (";" + mRequest.getUriQuery()) : ""); - mRequest = MessageBuilder.modifyRequest(mRequest, null, - uriQuery, null, null); - } + StringBuilder additionalQuery = makeAdditionalQuery( + devices); + String uriQuery = (additionalQuery != null + ? additionalQuery.toString() : "") + + (mRequest.getUriQuery() != null + ? (";" + mRequest.getUriQuery()) : ""); + mRequest = MessageBuilder.modifyRequest(mRequest, null, + uriQuery, null, null); + mRDServer.sendRequest(mRequest, mSrcDevice); break; + default: - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); + mSrcDevice.sendResponse(response); } } @@ -158,6 +141,7 @@ public class ResourceFind extends Resource { .containsKey(Constants.REQ_DEVICE_ID)) { mRDServer.sendRequest(request, srcDevice); + } else { StringBuffer additionalQuery = new StringBuffer(); additionalQuery diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java index 59c2981..a8aa880 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java @@ -35,7 +35,6 @@ 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; @@ -78,43 +77,20 @@ public class ResourcePresence extends Resource { .parsePayloadFromCbor(response.getPayload(), HashMap.class); - if (payloadData == null) { - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); - return; - } - - 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)); - } - } else { - String additionalQuery = makeAdditionalQuery( - payloadData, mSrcDevice.getDeviceId()); - if (additionalQuery == null) { - mSrcDevice.sendResponse( - MessageBuilder.createResponse(mRequest, - ResponseStatus.BAD_REQUEST)); - return; - } - String uriQuery = additionalQuery.toString() - + (mRequest.getUriQuery() != null - ? (";" + mRequest.getUriQuery()) : ""); - mRequest = MessageBuilder.modifyRequest(mRequest, null, - uriQuery, null, null); - } + String additionalQuery = makeAdditionalQuery(payloadData, + mSrcDevice.getDeviceId()); + + String uriQuery = additionalQuery.toString() + + (mRequest.getUriQuery() != null + ? (";" + mRequest.getUriQuery()) : ""); + mRequest = MessageBuilder.modifyRequest(mRequest, null, + uriQuery, null, null); mRDServer.sendRequest(mRequest, mSrcDevice); break; + default: - mSrcDevice.sendResponse(MessageBuilder.createResponse( - mRequest, ResponseStatus.BAD_REQUEST)); + mSrcDevice.sendResponse(response); } } diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/DiResourceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/RouteResourceTest.java similarity index 96% rename from cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/DiResourceTest.java rename to cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/RouteResourceTest.java index cb495a3..63c715c 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/DiResourceTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/RouteResourceTest.java @@ -44,9 +44,9 @@ import org.iotivity.cloud.base.protocols.enums.ResponseStatus; import org.iotivity.cloud.ciserver.Constants; import org.iotivity.cloud.ciserver.DeviceServerSystem; import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool; -import org.iotivity.cloud.ciserver.resources.DiResource.AccountReceiveHandler; -import org.iotivity.cloud.ciserver.resources.DiResource.DefaultResponseHandler; -import org.iotivity.cloud.ciserver.resources.DiResource.LinkInterfaceHandler; +import org.iotivity.cloud.ciserver.resources.RouteResource.AccountReceiveHandler; +import org.iotivity.cloud.ciserver.resources.RouteResource.DefaultResponseHandler; +import org.iotivity.cloud.ciserver.resources.RouteResource.LinkInterfaceHandler; import org.iotivity.cloud.util.Cbor; import org.junit.Before; import org.junit.Test; @@ -57,7 +57,7 @@ import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -public class DiResourceTest { +public class RouteResourceTest { private static final String RELAY_URI = "/di"; private static final String RESOURCE_URI = "/a/light/0"; private static final String VERIFY_URI = "/oic/acl/verify"; @@ -85,20 +85,20 @@ public class DiResourceTest { IRequestChannel requestChannel; @InjectMocks - DiResource diHandler = new DiResource( + RouteResource diHandler = new RouteResource( coapDevicePool); + IRequest requestDefault = makePutRequest(); + IRequest requestLinkInterface = makePutLinkInterfaceRequest(); + @InjectMocks LinkInterfaceHandler linkInterfaceHandler = diHandler.new LinkInterfaceHandler( - "targetDeviceId", mSourceDevice); + "targetDeviceId", mSourceDevice, requestLinkInterface); @InjectMocks DefaultResponseHandler defaultResponseHandler = diHandler.new DefaultResponseHandler( "targetDeviceId", mSourceDevice); - IRequest requestDefault = makePutRequest(); - IRequest requestLinkInterface = makePutLinkInterfaceRequest(); - @InjectMocks AccountReceiveHandler accountDefaultReceiveHandler = diHandler.new AccountReceiveHandler( mSourceDevice, requestDefault); diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java index 6d46ff0..763891f 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java @@ -55,9 +55,7 @@ import org.mockito.stubbing.Answer; public class ResourceDirectoryTest { private static final String TEST_RD_URI = Constants.RD_FULL_URI; - public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI; public static final String DEVICE_LIST_KEY = "devices"; - public static final String RES_PRS_URI = Constants.RESOURCE_PRESENCE_FULL_URI; private String mDi = "B371C481-38E6-4D47-8320-7688D8A5B58C"; String mUserId = "testuser"; @@ -153,8 +151,8 @@ public class ResourceDirectoryTest { rdPublishRequest, mMockDevice); IRequest request = makeResourcePublishRequest(); - accountReceiveHandler.onResponseReceived(MessageBuilder.createResponse( - request, ResponseStatus.CHANGED)); + accountReceiveHandler.onResponseReceived( + MessageBuilder.createResponse(request, ResponseStatus.CHANGED)); assertEquals(mReqRDServer, rdPublishRequest); assertTrue(mLatch.await(1L, SECONDS)); @@ -167,11 +165,11 @@ public class ResourceDirectoryTest { rdPublishRequest, mMockDevice); IRequest request = makeResourcePublishRequest(); - accountReceiveHandler.onResponseReceived(MessageBuilder.createResponse( - request, ResponseStatus.CHANGED)); + accountReceiveHandler.onResponseReceived( + MessageBuilder.createResponse(request, ResponseStatus.CHANGED)); assertEquals(getHrefInTestPublishPayload(mReqRDServer.getPayload()), - "/di/" + mDi + "/a/light"); + "/oic/route/" + mDi + "/a/light"); } @Test @@ -233,7 +231,7 @@ public class ResourceDirectoryTest { payload.put(Constants.DEVICE_ID, mDi); ArrayList> publishLinks = new ArrayList<>(); HashMap link = new HashMap<>(); - link.put("href", "/di/" + mDi + "/a/light"); + link.put("href", "/oic/route/" + mDi + "/a/light"); ArrayList rt = new ArrayList(); rt.add("core.light"); ArrayList itf = new ArrayList(); @@ -262,8 +260,8 @@ public class ResourceDirectoryTest { private String getHrefInTestPublishPayload(byte[] payload) { Cbor> cbor = new Cbor<>(); - HashMap parsedPayload = cbor.parsePayloadFromCbor( - payload, HashMap.class); + HashMap parsedPayload = cbor + .parsePayloadFromCbor(payload, HashMap.class); @SuppressWarnings("unchecked") ArrayList> links = (ArrayList>) parsedPayload diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java index 9173cdc..26a5efb 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java @@ -24,7 +24,6 @@ package org.iotivity.cloud.ciserver.resources.proxy.rd; import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -150,8 +149,6 @@ public class ResourceFindTest { assertTrue(mReq.getMethod() == RequestMethod.GET); assertTrue(queryMap.get("rt").contains("core.light")); assertTrue(queryMap.get("di").contains("device1")); - assertFalse(queryMap.get("di").contains("device2")); - assertFalse(queryMap.get("di").contains("device3")); } // @InjectMocks for testEntireDeviceonResponseReceived diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/OICConstants.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/OICConstants.java index 6e54131..6aa4b99 100755 --- a/cloud/stack/src/main/java/org/iotivity/cloud/base/OICConstants.java +++ b/cloud/stack/src/main/java/org/iotivity/cloud/base/OICConstants.java @@ -56,6 +56,8 @@ public class OICConstants { public static final String ID_URI = "id"; + public static final String ROUTE_URI = "route"; + public static final String INVITE_URI = "invite"; /* resource uri to publish, update, delete resource info */ @@ -120,6 +122,9 @@ public class OICConstants { public static final String KEEP_ALIVE_FULL_URI = "/" + PREFIX_OIC + "/" + OICConstants.KEEP_ALIVE_URI; + public static final String ROUTE_FULL_URI = "/" + PREFIX_OIC + + "/" + OICConstants.ROUTE_URI; + public static final String MQ_BROKER_FULL_URI = "/" + OICConstants.PREFIX_OIC + "/" + OICConstants.MQ_BROKER_URI; diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/connector/CoapClient.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/connector/CoapClient.java index d98b531..818914d 100644 --- a/cloud/stack/src/main/java/org/iotivity/cloud/base/connector/CoapClient.java +++ b/cloud/stack/src/main/java/org/iotivity/cloud/base/connector/CoapClient.java @@ -42,12 +42,14 @@ public class CoapClient implements IRequestChannel, IResponseEventHandler { private class RequestInfo { private byte[] originToken = null; + private IRequest originRequest = null; private IResponseEventHandler responseHandler = null; private Observe observe = Observe.NOTHING; - public RequestInfo(byte[] originToken, + public RequestInfo(byte[] originToken, IRequest originRequest, IResponseEventHandler responseHandler, Observe observe) { this.originToken = originToken; + this.originRequest = originRequest; this.responseHandler = responseHandler; this.observe = observe; } @@ -104,7 +106,7 @@ public class CoapClient implements IRequestChannel, IResponseEventHandler { coapRequest.setToken(Bytes.longTo8Bytes(newToken)); mTokenExchanger.put(newToken, - new RequestInfo(token, responseEvent, observe)); + new RequestInfo(token, request, responseEvent, observe)); mChannel.writeAndFlush(request); @@ -130,9 +132,10 @@ public class CoapClient implements IRequestChannel, IResponseEventHandler { + Bytes.bytesToLong(coapResponse.getToken())); } + ((CoapRequest) reqInfo.originRequest).setToken(reqInfo.originToken); + // Subscription response should stored - if (reqInfo.observe != Observe.SUBSCRIBE - || coapResponse.getSequenceNumber() == -1) { + if (reqInfo.observe != Observe.SUBSCRIBE) { mTokenExchanger.remove(Bytes.bytesToLong(coapResponse.getToken())); if (mSubscription .containsKey(Bytes.bytesToLong(reqInfo.originToken))) { @@ -140,16 +143,18 @@ public class CoapClient implements IRequestChannel, IResponseEventHandler { } } - coapResponse.setToken(reqInfo.originToken); - reqInfo.responseHandler.onResponseReceived(coapResponse); + if (reqInfo.responseHandler != null) { + coapResponse.setToken(reqInfo.originToken); + reqInfo.responseHandler.onResponseReceived(coapResponse); + } } - private void addObserve(long token, long newtoken) { + public void addObserve(long token, long newtoken) { mSubscription.put(token, newtoken); } - private Long removeObserve(long token) { + public Long removeObserve(long token) { Long getToken = mSubscription.remove(token); return getToken; diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/device/CoapDevice.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/device/CoapDevice.java index 7ab584e..05535d5 100644 --- a/cloud/stack/src/main/java/org/iotivity/cloud/base/device/CoapDevice.java +++ b/cloud/stack/src/main/java/org/iotivity/cloud/base/device/CoapDevice.java @@ -100,28 +100,25 @@ public class CoapDevice extends Device { } public void addObserveChannel(IRequestChannel channel) { - - if (channel != null) { - mObserveChannelList.add(channel); - } + mObserveChannelList.add(channel); } public void removeObserveChannel(IRequestChannel channel) throws ClientException { - if (channel != null && mObserveChannelList.contains(channel)) { + if (mObserveChannelList.contains(channel)) { Iterator iterator = mObserveRequestList.keySet().iterator(); while (iterator.hasNext()) { Long token = iterator.next(); CoapClient coapClient = (CoapClient) channel; if (coapClient.isObserveRequest(token) != null) { - IRequest getRequest = mObserveRequestList.get(token); - - CoapRequest coapRequest = (CoapRequest) getRequest; + coapClient.removeObserve(token); + CoapRequest coapRequest = (CoapRequest) mObserveRequestList + .get(token); coapRequest.setObserve(Observe.UNSUBSCRIBE); - - coapClient.onResponseReceived(MessageBuilder.createResponse( - coapRequest, ResponseStatus.CONTENT, null, null)); + IResponse response = MessageBuilder.createResponse( + coapRequest, ResponseStatus.CONTENT, null, null); + sendResponse(response); } } mObserveChannelList.remove(channel); @@ -149,7 +146,7 @@ public class CoapDevice extends Device { Long token = iterator.next(); Long respToken = Bytes.bytesToLong(coapResponse.getToken()); if (respToken.equals(token) - && coapResponse.getSequenceNumber() == -1) { + && coapResponse.getObserve() == Observe.NOTHING) { iterator.remove(); } } @@ -190,13 +187,13 @@ public class CoapDevice extends Device { @Override public void onDisconnected() { - for (IRequestChannel serverChannel : mObserveChannelList) { - Iterator iterator = mObserveRequestList.keySet().iterator(); - while (iterator.hasNext()) { - Long token = iterator.next(); + Iterator iterator = mObserveRequestList.keySet().iterator(); + while (iterator.hasNext()) { + Long token = iterator.next(); + for (IRequestChannel serverChannel : mObserveChannelList) { CoapClient coapClient = (CoapClient) serverChannel; - - if (coapClient.isObserveRequest(token) != null) { + if (coapClient != null + && coapClient.isObserveRequest(token) != null) { CoapRequest coapRequest = (CoapRequest) mObserveRequestList .get(token); coapRequest.setObserve(Observe.UNSUBSCRIBE); -- 2.7.4