Updated Makefiles and CMakeLists.txt to point to resource, not oic-resource
[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
66         *  REQUEST flag or OBSERVE flag.
67         *  NOTE:
68         *  INIT indicates that the vendor's entity handler should go and perform
69         *  initialization operations
70         *  REQUEST indicates that it is a request of certain type (GET/PUT/POST/DELETE)
71         *  and entity handler needs to perform corresponding operations
72         *  OBSERVE indicates that the request is of type Observe and entity handler
73         *  needs to perform corresponding operations
74         *  @return int type of request flag
75         */
76         int getRequestHandlerFlag() const {return m_requestHandlerFlag;}
77
78         /**
79         *  Provides the entire resource attribute representation
80         *  @return OCRepresentation reference containing the name value pairs representing the resource's attributes
81         */
82         const OCRepresentation& getResourceRepresentation() const {return m_representation;}
83
84         /**
85         *  @return ObservationInfo reference provides observation information
86         */
87         const ObservationInfo& getObservationInfo() const {return m_observationInfo;}
88
89         /**
90         *  sets resource uri
91         *  @param resourceUri specifies the resource uri
92         */
93         void setResourceUri(const std::string resourceUri)
94         {
95             m_resourceUri = resourceUri;
96         }
97
98         /**
99         *  gets resource uri
100         *  @return std::string resource uri
101         */
102         std::string getResourceUri(void)
103         {
104             return m_resourceUri;
105         }
106
107         /** This API retrieves headerOptions which was sent from a client
108         * @return std::map HeaderOptions with the header options
109         */
110         const HeaderOptions& getHeaderOptions() const {return m_headerOptions;}
111
112     private:
113         std::string m_requestType;
114         std::string m_resourceUri;
115         QueryParamsMap m_queryParameters;
116         int m_requestHandlerFlag;
117         OCRepresentation m_representation;
118         ObservationInfo m_observationInfo;
119         HeaderOptions m_headerOptions;
120
121     public:
122         // TODO: This is not a public API for app developers.
123         // This function will not be exposed in future
124         void setRequestType(const std::string& requestType)
125         {
126             m_requestType = requestType;
127         }
128
129         // TODO: This is not a public API for app developers.
130         // This function will not be exposed in future
131         void setPayload(const std::string& requestPayload)
132         {
133             AttributeMap attributeMap;
134             // TODO: The following JSON Parse implementation should be seperated into utitilites
135             // and used wherever required.
136             // e.g. parse(std::string& payload, Attributemap& attributeMap)
137
138             std::stringstream requestStream;
139             requestStream << requestPayload;
140             boost::property_tree::ptree root;
141             try
142             {
143                 boost::property_tree::read_json(requestStream, root);
144             }
145             catch(boost::property_tree::json_parser::json_parser_error &e)
146             {
147                 //TOD: log this
148                 return;
149             }
150
151             // TODO this expects the representation oc:{} and not oc:[{}]
152             //      this representation is fine when setting for simple resource.
153             boost::property_tree::ptree payload = root.get_child(OC::Key::OCKEY, boost::property_tree::ptree());
154
155             for(auto& item: payload)
156             {
157                 std::string name = item.first.data();
158                 std::string value = item.second.data();
159
160                 attributeMap[name] = value;
161             }
162
163             m_representation.setAttributeMap(attributeMap);
164         }
165
166         // TODO: This is not a public API for app developers.
167         // This function will not be exposed in future
168         void setQueryParams(QueryParamsMap& queryParams)
169         {
170             m_queryParameters = queryParams;
171         }
172
173         // TODO: This is not a public API for app developers.
174         // This function will not be exposed in future
175         void setRequestHandlerFlag(int requestHandlerFlag)
176         {
177             m_requestHandlerFlag = requestHandlerFlag;
178         }
179
180         // TODO: This is not a public API for app developers.
181         // This function will not be exposed in future
182         void setObservationInfo(const ObservationInfo& observationInfo)
183         {
184             m_observationInfo = observationInfo;
185         }
186
187         // TODO: This is not a public API for app developers.
188         // This function will not be exposed in future
189         void setHeaderOptions(const HeaderOptions& headerOptions)
190         {
191             m_headerOptions = headerOptions;
192         }
193     };
194  }// namespace OC
195
196 #endif //__OCRESOURCEREQUEST_H