From 2f027cdce0ffe82efe760f6e5661c8ef8b38e1f0 Mon Sep 17 00:00:00 2001 From: Jee Hyeok Kim Date: Mon, 8 Aug 2016 16:07:32 +0900 Subject: [PATCH] Fix error response and error code 1. Fix packet forawrding to client correctly when resource find receives error from account server, 2. Change response code to use CHANGED when POST method succeed. Change-Id: Ibf7b1ba6996c2bcef086f3c4361f38036f4a8c71 Signed-off-by: Jee Hyeok Kim Reviewed-on: https://gerrit.iotivity.org/gerrit/10119 Tested-by: jenkins-iotivity Reviewed-by: jung seungho --- .../cloud/accountserver/Constants.java | 2 +- .../resources/account/AccountResource.java | 2 +- .../account/session/SessionResource.java | 4 +- .../cloud/ciserver/DeviceServerSystem.java | 81 ++++++++++--------- .../resources/proxy/ResourceFind.java | 8 +- .../org/iotivity/cloud/base/ServerSystem.java | 1 - 6 files changed, 48 insertions(+), 50 deletions(-) diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java index aa5e0246c..9fe076ac1 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java @@ -55,7 +55,7 @@ public class Constants extends OCFConstants { public static final String REQ_ACCESS_TOKEN = "accesstoken"; - public static final String REQ_STATUS = "status"; + public static final String REQ_LOGIN = "login"; public static final String REQ_REFRESH_TOKEN = "refreshtoken"; diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java index 32189930d..48e37b4a5 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java @@ -157,7 +157,7 @@ public class AccountResource extends Resource { throw new InternalServerErrorException("MongoDB is not operating"); } - return MessageBuilder.createResponse(request, ResponseStatus.CREATED, + return MessageBuilder.createResponse(request, ResponseStatus.CHANGED, ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(responsePayload)); } diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java index 4228b8061..5e416562c 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java @@ -98,7 +98,7 @@ public class SessionResource extends Resource { String accessToken = payloadData.get(Constants.REQ_ACCESS_TOKEN) .toString(); - boolean signinRequest = (boolean) payloadData.get(Constants.REQ_STATUS); + boolean signinRequest = (boolean) payloadData.get(Constants.REQ_LOGIN); Boolean res = false; @@ -119,7 +119,7 @@ public class SessionResource extends Resource { responsePayload.put(Constants.RESP_EXPIRES_IN, mTokenManager.getRemainExpiredTime(accessToken)); - return MessageBuilder.createResponse(request, ResponseStatus.VALID, + return MessageBuilder.createResponse(request, ResponseStatus.CHANGED, ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(responsePayload)); } 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 c6e9d4be1..de33e1644 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 @@ -23,6 +23,7 @@ package org.iotivity.cloud.ciserver; import java.util.HashMap; +import org.iotivity.cloud.base.OCFConstants; import org.iotivity.cloud.base.ServerSystem; import org.iotivity.cloud.base.connector.ConnectorPool; import org.iotivity.cloud.base.device.CoapDevice; @@ -30,7 +31,6 @@ 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.exception.ServerException.BadRequestException; -import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException; import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException; import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException; import org.iotivity.cloud.base.protocols.MessageBuilder; @@ -104,6 +104,7 @@ public class DeviceServerSystem extends ServerSystem { ctx.channel().writeAndFlush(MessageBuilder .createResponse((CoapRequest) msg, responseStatus)); Log.f(ctx.channel(), t); + ctx.channel().close(); } } @@ -176,33 +177,30 @@ public class DeviceServerSystem extends ServerSystem { // Once the response is valid, add this to deviceList CoapResponse response = (CoapResponse) msg; - switch (response.getStatus()) { - // TODO: below section is exceptional case for ping from - // clients - case CREATED: - case CONTENT: - break; + if (response.getUriPath() + .equals("/" + OCFConstants.PREFIX_WELL_KNOWN + "/" + + OCFConstants.PREFIX_OCF + "/" + + OCFConstants.ACCOUNT_URI + "/" + + OCFConstants.SESSION_URI)) { - case VALID: - if (response.getPayload() != null) { - HashMap payloadData = mCbor - .parsePayloadFromCbor(response.getPayload(), - HashMap.class); - int remainTime = (int) payloadData - .get(Constants.EXPIRES_IN); + if (response.getStatus() != ResponseStatus.CHANGED) { + throw new UnAuthorizedException(); + } - Device device = ctx.channel().attr(keyDevice).get(); - ((CoapDevice) device).setExpiredPolicy(remainTime); + HashMap payloadData = mCbor + .parsePayloadFromCbor(response.getPayload(), + HashMap.class); + int remainTime = (int) payloadData + .get(Constants.EXPIRES_IN); - // Remove current auth handler - ctx.channel().pipeline().remove(this); + Device device = ctx.channel().attr(keyDevice).get(); + ((CoapDevice) device).setExpiredPolicy(remainTime); - // Raise event that we have Authenticated device - ctx.fireChannelActive(); - } - break; + // Remove current auth handler + ctx.channel().pipeline().remove(this); - default: + // Raise event that we have Authenticated device + ctx.fireChannelActive(); } ctx.writeAndFlush(msg); @@ -225,25 +223,28 @@ public class DeviceServerSystem extends ServerSystem { // And check first response is VALID then add or cut CoapRequest request = (CoapRequest) msg; - // Check whether first request is about account - if (request.getUriPathSegments().size() < 2) { - throw new UnAuthorizedException( - "first request must be about account or keepAlive"); - } - // TODO: device sends ping whether not authorized - if (request.getUriPathSegments().get(1) - .equals(Constants.KEEP_ALIVE_URI)) { - // Go upperlayer - ctx.fireChannelRead(msg); - return; - } + switch (request.getUriPath()) { + // Check whether first request is about account + case "/" + OCFConstants.PREFIX_WELL_KNOWN + "/" + + OCFConstants.PREFIX_OCF + "/" + + OCFConstants.ACCOUNT_URI: + + case "/" + OCFConstants.PREFIX_WELL_KNOWN + "/" + + OCFConstants.PREFIX_OCF + "/" + + OCFConstants.ACCOUNT_URI + "/" + + OCFConstants.SESSION_URI: + break; - if (request.getUriPathSegments().size() < 3 - || request.getUriPathSegments().get(2) - .equals(Constants.ACCOUNT_URI) == false) { - throw new UnAuthorizedException( - "authentication required first"); + case "/" + OCFConstants.PREFIX_OIC + "/" + + OCFConstants.KEEP_ALIVE_URI: + // TODO: Pass ping request to upper layer + ctx.fireChannelRead(msg); + return; + + default: + throw new UnAuthorizedException( + "authentication required first"); } HashMap authPayload = mCbor diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/ResourceFind.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/ResourceFind.java index 7ec2ad0ca..ac131e968 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/ResourceFind.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/ResourceFind.java @@ -31,7 +31,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.ClientException; -import org.iotivity.cloud.base.exception.ClientException.BadResponseException; import org.iotivity.cloud.base.exception.ServerException; import org.iotivity.cloud.base.protocols.IRequest; import org.iotivity.cloud.base.protocols.IResponse; @@ -102,9 +101,8 @@ public class ResourceFind extends Resource { break; default: - throw new BadResponseException( - response.getStatus().toString() - + " response type is not supported"); + mSrcDevice.sendResponse(MessageBuilder + .createResponse(mRequest, response.getStatus())); } } } @@ -119,7 +117,7 @@ public class ResourceFind extends Resource { uriQuery.append(coapDevice.getAccessToken()); if (request.getUriQueryMap().get("di") != null) { - for(String di : request.getUriQueryMap().get("di")){ + for (String di : request.getUriQueryMap().get("di")) { uriQuery.append("&"); uriQuery.append("di" + "="); uriQuery.append(di); diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/ServerSystem.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/ServerSystem.java index 5ee5a06bf..91a5a32f7 100644 --- a/cloud/stack/src/main/java/org/iotivity/cloud/base/ServerSystem.java +++ b/cloud/stack/src/main/java/org/iotivity/cloud/base/ServerSystem.java @@ -103,7 +103,6 @@ public class ServerSystem extends ResourceManager { if (msg instanceof CoapRequest) { ctx.channel().writeAndFlush(MessageBuilder.createResponse( msg, ResponseStatus.INTERNAL_SERVER_ERROR)); - ctx.channel().close(); } } } -- 2.34.1