1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file OCResourceRequest.h
23 /// @brief This file contains the declaration of classes and its members related to
26 #ifndef __OCRESOURCEREQUEST_H
27 #define __OCRESOURCEREQUEST_H
29 #include <boost/property_tree/ptree.hpp>
30 #include <boost/property_tree/json_parser.hpp>
37 * @brief OCResourceRequest provides APIs to extract details from a request URI
39 class OCResourceRequest
42 typedef std::shared_ptr<OCResourceRequest> Ptr;
47 virtual ~OCResourceRequest(void)
52 * Retrieves the type of request string for the entity handler function to operate
53 * @return std::string request type. This could be 'GET'/'PUT'/'POST'/'DELETE'
55 std::string getRequestType() const {return m_requestType;}
58 * Retrieves the query parameters from the request
59 * @return std::string query parameters in the request
61 const QueryParamsMap& getQueryParameters() const {return m_queryParameters;}
64 * Retrieves the request handler flag type. This can be either INIT flag or REQUEST flag or OBSERVE flag.
66 * INIT indicates that the vendor's entity handler should go and perform initialization operations
67 * REQUEST indicates that it is a request of certain type (GET/PUT/POST/DELETE) and entity handler needs to perform
68 * corresponding operations
69 * OBSERVE indicates that the request is of type Observe and entity handler needs to perform corresponding operations
70 * @return std::string type of request flag
72 RequestHandlerFlag getRequestHandlerFlag() const {return m_requestHandlerFlag;}
75 * Provides the entire resource attribute representation
76 * @return std::map AttributeMap reference containing the name value pairs representing the resource's attributes
78 const AttributeMap& getResourceRepresentation() const {return m_attributeMap;}
81 std::string m_requestType;
82 QueryParamsMap m_queryParameters;
83 RequestHandlerFlag m_requestHandlerFlag;
84 AttributeMap m_attributeMap;
87 // TODO: This is not a public API for app developers.
88 // This function will not be exposed in future
89 void setRequestType(const std::string& requestType)
91 m_requestType = requestType;
94 // TODO: This is not a public API for app developers.
95 // This function will not be exposed in future
96 void setPayload(const std::string& requestPayload)
98 // TODO: The following JSON Parse implementation should be seperated into utitilites
99 // and used wherever required.
100 // e.g. parse(std::string& payload, Attributemap& attributeMap)
102 std::stringstream requestStream;
103 requestStream << requestPayload;
104 boost::property_tree::ptree root;
105 boost::property_tree::read_json(requestStream, root);
106 boost::property_tree::ptree payload = root.get_child("oc.payload", boost::property_tree::ptree());
108 for(auto& item : payload)
110 std::string name = item.first.data();
111 std::string value = item.second.data();
113 AttributeValues values;
114 values.push_back(value);
116 m_attributeMap[name] = values;
120 // TODO: This is not a public API for app developers.
121 // This function will not be exposed in future
122 void setQueryParams(QueryParamsMap& queryParams)
124 m_queryParameters = queryParams;
127 // TODO: This is not a public API for app developers.
128 // This function will not be exposed in future
129 void setRequestHandlerFlag(RequestHandlerFlag requestHandlerFlag)
131 m_requestHandlerFlag = requestHandlerFlag;
137 #endif //__OCRESOURCEREQUEST_H