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;
deviceServer.addResource(resKeepAlive);
- deviceServer.addResource(new DiResource(devicePool));
+ deviceServer.addResource(new RouteResource(devicePool));
deviceServer.addServer(new CoapServer(
new InetSocketAddress(Integer.parseInt(args[0]))));
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";
Iterator<String> 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());
}
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()) {
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
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;
* device
*
*/
-public class DiResource extends Resource {
+public class RouteResource extends Resource {
private CoapDevicePool mDevicePool = null;
private IRequestChannel mASServer = null;
private Cbor<HashMap<String, Object>> 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<String> 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
List<String> uriPathSegment = request.getUriPathSegments();
// Remove prefix path
- uriPathSegment.remove(0);
+ for (String path : getUriPathSegments()) {
+ uriPathSegment.remove(path);
+ }
uriPathSegment.remove(0);
StringBuilder uriPath = new StringBuilder();
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
private Cbor<List<HashMap<String, Object>>> 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<HashMap<String, Object>> linkPayload) {
+
+ StringBuilder uriPath = new StringBuilder();
+ for (String path : getUriPathSegments()) {
+ uriPath.append("/" + path);
+ }
+
for (HashMap<String, Object> 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<HashMap<String, Object>> 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
.get(Constants.RESP_GRANT_POLICY);
verifyRequest(mSrcDevice, mRequest, gp);
break;
+
default:
- mSrcDevice.sendResponse(MessageBuilder.createResponse(
- mRequest, ResponseStatus.BAD_REQUEST));
+ mSrcDevice.sendResponse(response);
}
}
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));
}
}
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 + "="
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;
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;
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);
}
}
}
.parsePayloadFromCbor(response.getPayload(),
HashMap.class);
- if (payloadData == null) {
- mSrcDevice.sendResponse(MessageBuilder.createResponse(
- mRequest, ResponseStatus.BAD_REQUEST));
- return;
- }
-
ArrayList<String> devices = (ArrayList<String>) getResponseDeviceList(
payloadData);
} 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()) : "");
mRDServer.sendRequest(mRequest, mSrcDevice);
break;
+
default:
- mSrcDevice.sendResponse(MessageBuilder.createResponse(
- mRequest, ResponseStatus.BAD_REQUEST));
+ mSrcDevice.sendResponse(response);
}
}
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;
case DELETE:
mRDServer.sendRequest(request, srcDevice);
break;
+
default:
throw new BadRequestException(
request.getMethod() + " request type is not support");
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);
break;
default:
- mSrcDevice.sendResponse(MessageBuilder.createResponse(
- mRequest, ResponseStatus.BAD_REQUEST));
+ mSrcDevice.sendResponse(response);
}
}
HashMap<String, Object> payload = cbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
- if (verifyPublishPayload(payload) == false) {
-
- return null;
- }
-
ArrayList<HashMap<String, Object>> links = (ArrayList<HashMap<String, Object>>) payload
.get(Constants.REQ_LINKS);
for (HashMap<String, Object> 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);
}
return cbor.encodingPayloadToCbor(payload);
}
-
- @SuppressWarnings("unchecked")
- private boolean verifyPublishPayload(HashMap<String, Object> payload) {
-
- ArrayList<HashMap<String, Object>> links = (ArrayList<HashMap<String, Object>>) payload
- .get(Constants.REQ_LINKS);
-
- if (links == null || links.isEmpty()) {
- return false;
- }
-
- for (HashMap<String, Object> 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
switch (response.getStatus()) {
case CHANGED:
-
response = MessageBuilder.modifyResponse(response,
ContentFormat.APPLICATION_CBOR,
convertResponseHref(response));
default:
-
mSrcDevice.sendResponse(response);
}
}
// remove prefix
ArrayList<String> hrefSegments = new ArrayList<>(
Arrays.asList(href.split("/")));
- for (int i = 0; i < 3; i++) {
- hrefSegments.remove(0);
+
+ ArrayList<String> 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) {
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;
.parsePayloadFromCbor(response.getPayload(),
HashMap.class);
- if (payloadData == null) {
- mSrcDevice.sendResponse(MessageBuilder.createResponse(
- mRequest, ResponseStatus.BAD_REQUEST));
- return;
- }
-
ArrayList<String> devices = (ArrayList<String>) 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);
}
}
.containsKey(Constants.REQ_DEVICE_ID)) {
mRDServer.sendRequest(request, srcDevice);
+
} else {
StringBuffer additionalQuery = new StringBuffer();
additionalQuery
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;
.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);
}
}
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;
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";
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);
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";
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));
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
payload.put(Constants.DEVICE_ID, mDi);
ArrayList<HashMap<Object, Object>> publishLinks = new ArrayList<>();
HashMap<Object, Object> link = new HashMap<>();
- link.put("href", "/di/" + mDi + "/a/light");
+ link.put("href", "/oic/route/" + mDi + "/a/light");
ArrayList<String> rt = new ArrayList<String>();
rt.add("core.light");
ArrayList<String> itf = new ArrayList<String>();
private String getHrefInTestPublishPayload(byte[] payload) {
Cbor<HashMap<String, Object>> cbor = new Cbor<>();
- HashMap<String, Object> parsedPayload = cbor.parsePayloadFromCbor(
- payload, HashMap.class);
+ HashMap<String, Object> parsedPayload = cbor
+ .parsePayloadFromCbor(payload, HashMap.class);
@SuppressWarnings("unchecked")
ArrayList<HashMap<String, Object>> links = (ArrayList<HashMap<String, Object>>) parsedPayload
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;
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
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 */
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;
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;
}
coapRequest.setToken(Bytes.longTo8Bytes(newToken));
mTokenExchanger.put(newToken,
- new RequestInfo(token, responseEvent, observe));
+ new RequestInfo(token, request, responseEvent, observe));
mChannel.writeAndFlush(request);
+ 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))) {
}
}
- 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;
}
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<Long> 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);
Long token = iterator.next();
Long respToken = Bytes.bytesToLong(coapResponse.getToken());
if (respToken.equals(token)
- && coapResponse.getSequenceNumber() == -1) {
+ && coapResponse.getObserve() == Observe.NOTHING) {
iterator.remove();
}
}
@Override
public void onDisconnected() {
- for (IRequestChannel serverChannel : mObserveChannelList) {
- Iterator<Long> iterator = mObserveRequestList.keySet().iterator();
- while (iterator.hasNext()) {
- Long token = iterator.next();
+ Iterator<Long> 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);