From: spurthi.segu Date: Wed, 18 Nov 2015 09:56:45 +0000 (+0530) Subject: Fix for issue of server updates its model on both PUT and POST requests even X-Git-Tag: 1.2.0+RC1~764^2~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d0686ee241a3102399a7135dbe3eb2930219aab9;p=platform%2Fupstream%2Fiotivity.git Fix for issue of server updates its model on both PUT and POST requests even its respective raml file doest not support either PUT or POST. Providing the request and response information from raml to simulated resource, so it can validate the requests it can support whenever it receives one from client. Change-Id: I42ea78d963dd2c11e7ab945cb6a75bb8e3cd0864 Signed-off-by: spurthi.segu Reviewed-on: https://gerrit.iotivity.org/gerrit/4259 Reviewed-by: RadhaBhavani Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- diff --git a/service/simulator/examples/client/simulator_client.cpp b/service/simulator/examples/client/simulator_client.cpp index f0f3148..db7fca2 100644 --- a/service/simulator/examples/client/simulator_client.cpp +++ b/service/simulator/examples/client/simulator_client.cpp @@ -315,9 +315,9 @@ class ClientController try { SimulatorResourceModelSP rep = std::make_shared(); - std::string value = "off"; + bool value = false; rep->add("power", value); - rep->add("intensity", 5); + rep->add("intensity", 15); resource->put(std::map (), rep, callback); std::cout << "PUT is successful!" << std::endl; @@ -358,9 +358,9 @@ class ClientController try { SimulatorResourceModelSP rep = std::make_shared(); - std::string value = "on"; + bool value = true; rep->add("power", value); - rep->add("intensity", 7); + rep->add("intensity", 17); resource->post(std::map (), rep, callback); std::cout << "POST is successful!" << std::endl; diff --git a/service/simulator/src/server/simulator_collection_resource_impl.cpp b/service/simulator/src/server/simulator_collection_resource_impl.cpp index ff673fd..b519c07 100755 --- a/service/simulator/src/server/simulator_collection_resource_impl.cpp +++ b/service/simulator/src/server/simulator_collection_resource_impl.cpp @@ -229,6 +229,11 @@ void SimulatorCollectionResourceImpl::setResourceModel(const SimulatorResourceMo m_resModel = resModel; } +void SimulatorCollectionResourceImpl::setActionType(std::map &actionType) +{ + m_actionTypes = actionType; +} + std::vector SimulatorCollectionResourceImpl::getObserversList() { return m_observersList; @@ -426,6 +431,15 @@ std::shared_ptr SimulatorCollectionResourceImpl::request std::shared_ptr request) { std::shared_ptr response; + + RAML::ActionType type = getActionType(request->getRequestType()); + + if (!m_actionTypes.empty()) + { + if (m_actionTypes.end() == m_actionTypes.find(type)) + return response; + } + if ("GET" == request->getRequestType()) { // Construct the representation @@ -455,6 +469,15 @@ std::shared_ptr SimulatorCollectionResourceImpl::request { std::lock_guard lock(m_childResourcesLock); std::shared_ptr response; + + RAML::ActionType type = getActionType(request->getRequestType()); + + if (!m_actionTypes.empty()) + { + if (m_actionTypes.end() == m_actionTypes.find(type)) + return response; + } + if ("GET" == request->getRequestType()) { // Construct the representation @@ -562,3 +585,20 @@ void SimulatorCollectionResourceImpl::removeLink(std::string uri) } } } + +RAML::ActionType SimulatorCollectionResourceImpl::getActionType(std::string requestType) +{ + if (!requestType.compare("GET")) + return RAML::ActionType::GET; + + if (!requestType.compare("PUT")) + return RAML::ActionType::PUT; + + if (!requestType.compare("POST")) + return RAML::ActionType::POST; + + if (!requestType.compare("DELETE")) + return RAML::ActionType::DELETE; + + return RAML::ActionType::NONE; +} \ No newline at end of file diff --git a/service/simulator/src/server/simulator_collection_resource_impl.h b/service/simulator/src/server/simulator_collection_resource_impl.h index 0b11440..ea31ac6 100755 --- a/service/simulator/src/server/simulator_collection_resource_impl.h +++ b/service/simulator/src/server/simulator_collection_resource_impl.h @@ -22,6 +22,7 @@ #define SIMULATOR_COLLECTION_RESOURCE_IMPL_H_ #include "simulator_collection_resource.h" +#include "RamlParser.h" class SimulatorResourceFactory; class SimulatorCollectionResourceImpl : public SimulatorCollectionResource @@ -59,6 +60,10 @@ class SimulatorCollectionResourceImpl : public SimulatorCollectionResource void setResourceModel(const SimulatorResourceModel &resModel); + void setActionType(std::map &actionType); + + RAML::ActionType getActionType(std::string requestType); + private: SimulatorCollectionResourceImpl(); @@ -88,6 +93,7 @@ class SimulatorCollectionResourceImpl : public SimulatorCollectionResource std::vector m_observersList; ObserverCallback m_observeCallback; ResourceModelChangedCallback m_modelCallback; + std::map m_actionTypes; OCResourceProperty m_property; OCResourceHandle m_resourceHandle; diff --git a/service/simulator/src/server/simulator_resource_factory.cpp b/service/simulator/src/server/simulator_resource_factory.cpp index 2c862ed..321d425 100644 --- a/service/simulator/src/server/simulator_resource_factory.cpp +++ b/service/simulator/src/server/simulator_resource_factory.cpp @@ -331,6 +331,7 @@ std::shared_ptr SimulatorResourceFactory::buildResource( name = ramlResource->getDisplayName(); uri = ramlResource->getResourceUri(); + std::map actionType = ramlResource->getActions(); RAML::RequestResponseBodyPtr successResponseBody = getRAMLResponseBody( ramlResource, RAML::ActionType::GET, "200"); @@ -359,6 +360,7 @@ std::shared_ptr SimulatorResourceFactory::buildResource( collectionRes->setResourceType(resourceType); collectionRes->setInterface(interfaceType); collectionRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri)); + collectionRes->setActionType(actionType); collectionRes->setResourceModel(successResponseModel); simResource = std::dynamic_pointer_cast(collectionRes); @@ -376,6 +378,7 @@ std::shared_ptr SimulatorResourceFactory::buildResource( singleRes->setResourceType(resourceType); singleRes->setInterface(interfaceType); singleRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri)); + singleRes->setActionType(actionType); singleRes->setResourceModel(successResponseModel); singleRes->setPutErrorResponseModel(putErrorResponseModel); diff --git a/service/simulator/src/server/simulator_single_resource_impl.cpp b/service/simulator/src/server/simulator_single_resource_impl.cpp index 16d6a65..9490372 100755 --- a/service/simulator/src/server/simulator_single_resource_impl.cpp +++ b/service/simulator/src/server/simulator_single_resource_impl.cpp @@ -426,6 +426,11 @@ void SimulatorSingleResourceImpl::setPostErrorResponseModel(const SimulatorResou m_postErrorResModel = resModel; } +void SimulatorSingleResourceImpl::setActionType(std::map &actionType) +{ + m_actionTypes = actionType; +} + void SimulatorSingleResourceImpl::notifyApp(SimulatorResourceModel &resModel) { if (m_modelCallback) @@ -467,17 +472,10 @@ OCEntityHandlerResult SimulatorSingleResourceImpl::handleRequests( << " request received. \n**Payload details**\n" << payload) } - // Handover the request to appropriate interface handler - std::string interfaceType(OC::DEFAULT_INTERFACE); - OC::QueryParamsMap queryParams = request->getQueryParameters(); - if (queryParams.end() != queryParams.find("if")) - interfaceType = queryParams["if"]; + // TODO: Handover the request to appropriate interface handler std::shared_ptr response; - if (interfaceType == OC::DEFAULT_INTERFACE) - { - response = requestOnBaseLineInterface(request); - } + response = requestOnBaseLineInterface(request); // Send response if the request handled by resource if (response) @@ -522,6 +520,15 @@ std::shared_ptr SimulatorSingleResourceImpl::requestOnBa std::shared_ptr request) { std::shared_ptr response; + + RAML::ActionType type = getActionType(request->getRequestType()); + + if (!m_actionTypes.empty()) + { + if (m_actionTypes.end() == m_actionTypes.find(type)) + return response; + } + if ("GET" == request->getRequestType()) { OC::OCRepresentation ocRep = m_resModel.getOCRepresentation(); @@ -621,4 +628,21 @@ void SimulatorSingleResourceImpl::removeAllObservers() if (m_observeCallback) m_observeCallback(m_uri, ObservationStatus::UNREGISTER, observerList[index]); } -} \ No newline at end of file +} + +RAML::ActionType SimulatorSingleResourceImpl::getActionType(std::string requestType) +{ + if (!requestType.compare("GET")) + return RAML::ActionType::GET; + + if (!requestType.compare("PUT")) + return RAML::ActionType::PUT; + + if (!requestType.compare("POST")) + return RAML::ActionType::POST; + + if (!requestType.compare("DELETE")) + return RAML::ActionType::DELETE; + + return RAML::ActionType::NONE; +} diff --git a/service/simulator/src/server/simulator_single_resource_impl.h b/service/simulator/src/server/simulator_single_resource_impl.h index d25c4b5..645507e 100755 --- a/service/simulator/src/server/simulator_single_resource_impl.h +++ b/service/simulator/src/server/simulator_single_resource_impl.h @@ -23,6 +23,7 @@ #include "simulator_single_resource.h" #include "resource_update_automation_mngr.h" +#include "RamlParser.h" class SimulatorResourceFactory; class SimulatorSingleResourceImpl : public SimulatorSingleResource @@ -74,6 +75,8 @@ class SimulatorSingleResourceImpl : public SimulatorSingleResource void setResourceModel(const SimulatorResourceModel &resModel); void setPutErrorResponseModel(const SimulatorResourceModel &resModel); void setPostErrorResponseModel(const SimulatorResourceModel &resModel); + void setActionType(std::map &actionType); + RAML::ActionType getActionType(std::string requestType); void notifyApp(); void notifyApp(SimulatorResourceModel &resModel); @@ -103,6 +106,7 @@ class SimulatorSingleResourceImpl : public SimulatorSingleResource ObserverCallback m_observeCallback; UpdateAutomationMngr m_updateAutomationMgr; std::vector m_observersList; + std::map m_actionTypes; OCResourceProperty m_property; OCResourceHandle m_resourceHandle;