/*
 * //******************************************************************
 * //
 * // 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.base.resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.iotivity.cloud.base.device.Device;
import org.iotivity.cloud.base.device.IRequestEventHandler;
import org.iotivity.cloud.base.exception.ServerException;
import org.iotivity.cloud.base.exception.ServerException.NotFoundException;
import org.iotivity.cloud.base.protocols.IRequest;

public class Resource implements IRequestEventHandler {

    private List<String> mPathSegments;

    public interface Functional {
        void queryHandler(Device srcDevice, IRequest request)
                throws ServerException;
    }

    private HashMap<List<String>, Functional> mQuerySetList = new HashMap<>();

    public void addQueryHandler(List<String> querySet,
            Functional queryHandler) {
        mQuerySetList.put(querySet, queryHandler);
    }

    public List<String> getUriPathSegments() {
        return mPathSegments;
    }

    public Resource(List<String> pathSegments) {
        mPathSegments = pathSegments;
    }

    @Override
    final public void onRequestReceived(Device srcDevice, IRequest request)
            throws ServerException {
        // Handle pre-registered handlers
        List<String> reqQuerySet = request.getUriQuerySegments();

        if (reqQuerySet != null && mQuerySetList.size() > 0) {
            for (Entry<List<String>, Functional> entry : mQuerySetList
                    .entrySet()) {
                if (reqQuerySet.retainAll(entry.getKey()) == false) {
                    entry.getValue().queryHandler(srcDevice, request);
                    return;
                }
            }
        }

        // No packet handler found, pass to default handler
        onDefaultRequestReceived(srcDevice, request);
    }

    public void onDefaultRequestReceived(Device srcDevice, IRequest request)
            throws ServerException {
        // No default handlers registered, send error
        throw new NotFoundException("No handlers registered");
    }
}

@@ -930,16 +930,41 @@
 ource;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -1254,23 +1254,187 @@
 ception.
-NotFoun
+BadRequestException;%0Aimport org.iotivity.cloud.base.exception.ServerException.NotFoundException;%0Aimport org.iotivity.cloud.base.exception.ServerException.PreconditionFaile
 dExcepti
@@ -1487,16 +1487,53 @@
 Request;
+%0Aimport org.iotivity.cloud.util.Cbor;
 %0A%0Apublic
@@ -3151,11 +3151,1805 @@
 );%0A    %7D
+%0A%0A    public boolean checkQueryException(String property, IRequest request) %7B%0A        return checkQueryException(Arrays.asList(property), request);%0A    %7D%0A%0A    public boolean checkQueryException(List%3CString%3E propertyList,%0A            IRequest request) %7B%0A        HashMap%3CString, List%3CString%3E%3E queryData = request.getUriQueryMap();%0A        if (queryData == null)%0A            throw new BadRequestException(%22queryData is null%22);%0A%0A        for (String property : propertyList) %7B%0A            if (!queryData.containsKey(property))%0A                throw new PreconditionFailedException(%0A                        property + %22 property is not include%22);%0A            if (queryData.get(property) == null)%0A                throw new PreconditionFailedException(%0A                        property + %22 param is null%22);%0A        %7D%0A        return true;%0A    %7D%0A%0A    public boolean checkPayloadException(String property, IRequest request) %7B%0A        return checkPayloadException(Arrays.asList(property), request);%0A    %7D%0A%0A    public boolean checkPayloadException(List%3CString%3E propertyList,%0A            IRequest request) %7B%0A        Cbor%3CHashMap%3CString, Object%3E%3E cbor = new Cbor%3C%3E();%0A        HashMap%3CString, Object%3E payloadData = cbor%0A                .parsePayloadFromCbor(request.getPayload(), HashMap.class);%0A        if (payloadData == null)%0A            throw new BadRequestException(%22payloadData is null%22);%0A%0A        for (String property : propertyList) %7B%0A            if (!payloadData.containsKey(property))%0A                throw new PreconditionFailedException(%0A                        property + %22 property is not include%22);%0A            if (payloadData.get(property) == null)%0A                throw new PreconditionFailedException(%0A                        property + %22 param is null%22);%0A        %7D%0A        return true;%0A    %7D
 %0A%7D%0A

Add check exception for request message in stack