Fix error response and error code
authorJee Hyeok Kim <jihyeok13.kim@samsung.com>
Mon, 8 Aug 2016 07:07:32 +0000 (16:07 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Tue, 9 Aug 2016 01:37:36 +0000 (01:37 +0000)
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 <jihyeok13.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10119
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: jung seungho <shonest.jung@samsung.com>
cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/ResourceFind.java
cloud/stack/src/main/java/org/iotivity/cloud/base/ServerSystem.java

index aa5e024..9fe076a 100644 (file)
@@ -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";
 
index 3218993..48e37b4 100644 (file)
@@ -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));
     }
index 4228b80..5e41656 100644 (file)
@@ -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));
     }
index c6e9d4b..de33e16 100644 (file)
@@ -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<String, Object> 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<String, Object> 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<String, Object> authPayload = mCbor
index 7ec2ad0..ac131e9 100644 (file)
@@ -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);
index 5ee5a06..91a5a32 100644 (file)
@@ -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();
                 }
             }
         }