Partial Implementation of US1574:
[platform/upstream/iotivity.git] / include / OCResourceRequest.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file OCResourceRequest.h
22
23 /// @brief  This file contains the declaration of classes and its members related to
24 ///         ResourceRequest.
25
26 #ifndef __OCRESOURCEREQUEST_H
27 #define __OCRESOURCEREQUEST_H
28
29 #include <boost/property_tree/ptree.hpp>
30 #include <boost/property_tree/json_parser.hpp>
31
32 #include "OCApi.h"
33 #include "OCRepresentation.h"
34
35 namespace OC
36 {
37     /**
38     *   @brief  OCResourceRequest provides APIs to extract details from a request URI
39     */
40     class OCResourceRequest
41     {
42     public:
43         typedef std::shared_ptr<OCResourceRequest> Ptr;
44
45         /**
46         *  Virtual destructor
47         */
48         virtual ~OCResourceRequest(void)
49         {
50         }
51
52         /**
53         *  Retrieves the type of request string for the entity handler function to operate
54         *  @return std::string request type. This could be 'GET'/'PUT'/'POST'/'DELETE'
55         */
56         std::string getRequestType() const {return m_requestType;}
57
58         /**
59         *  Retrieves the query parameters from the request
60         *  @return std::string query parameters in the request
61         */
62         const QueryParamsMap& getQueryParameters() const {return m_queryParameters;}
63
64         /**
65         *  Retrieves the request handler flag type. This can be either INIT flag or REQUEST flag or OBSERVE flag.
66         *  NOTE:
67         *  INIT indicates that the vendor's entity handler should go and perform initialization operations
68         *  REQUEST indicates that it is a request of certain type (GET/PUT/POST/DELETE) and entity handler needs to perform
69         *  corresponding operations
70         *  OBSERVE indicates that the request is of type Observe and entity handler needs to perform corresponding operations
71         *  @return std::string type of request flag
72         */
73         RequestHandlerFlag getRequestHandlerFlag() const {return m_requestHandlerFlag;}
74
75         /**
76         *  Provides the entire resource attribute representation
77         *  @return OCRepresentation reference containing the name value pairs representing the resource's attributes
78         */
79         const OCRepresentation& getResourceRepresentation() const {return m_representation;}
80
81     private:
82         std::string m_requestType;
83         QueryParamsMap m_queryParameters;
84         RequestHandlerFlag m_requestHandlerFlag;
85         OCRepresentation m_representation;
86
87     public:
88         // TODO: This is not a public API for app developers.
89         // This function will not be exposed in future
90         void setRequestType(const std::string& requestType)
91         {
92             m_requestType = requestType;
93         }
94
95         // TODO: This is not a public API for app developers.
96         // This function will not be exposed in future
97         void setPayload(const std::string& requestPayload)
98         {
99             AttributeMap attributeMap;
100             // TODO: The following JSON Parse implementation should be seperated into utitilites
101             // and used wherever required.
102             // e.g. parse(std::string& payload, Attributemap& attributeMap)
103
104             std::stringstream requestStream;
105             requestStream << requestPayload;
106             boost::property_tree::ptree root;
107             try
108             {
109                 boost::property_tree::read_json(requestStream, root);
110             }
111             catch(boost::property_tree::json_parser::json_parser_error &e)
112             {
113                 //TOD: log this
114                 return;
115             }
116
117             // TODO this expects the representation oc:{} and not oc:[{}]
118             //      this representation is fine when setting for simple resource.
119             boost::property_tree::ptree payload = root.get_child("oc", boost::property_tree::ptree());
120
121             for(auto& item: payload)
122             {
123                 std::string name = item.first.data();
124                 std::string value = item.second.data();
125
126                 attributeMap[name] = value;
127             }
128
129             m_representation.setAttributeMap(attributeMap);
130         }
131
132         // TODO: This is not a public API for app developers.
133         // This function will not be exposed in future
134         void setQueryParams(QueryParamsMap& queryParams)
135         {
136             m_queryParameters = queryParams;
137         }
138
139         // TODO: This is not a public API for app developers.
140         // This function will not be exposed in future
141         void setRequestHandlerFlag(RequestHandlerFlag requestHandlerFlag)
142         {
143             m_requestHandlerFlag = requestHandlerFlag;
144         }
145     };
146
147 } // namespace OC
148
149 #endif //__OCRESOURCEREQUEST_H