Merge "Enable building C Samples using scons"
[platform/upstream/iotivity.git] / resource / 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 "OCApi.h"
30 #include "OCRepresentation.h"
31
32 void formResourceRequest(OCEntityHandlerFlag,
33                          OCEntityHandlerRequest*,
34                          std::shared_ptr<OC::OCResourceRequest>);
35
36
37 namespace OC
38 {
39     /**
40     *   @brief  OCResourceRequest provides APIs to extract details from a request URI
41     */
42     class OCResourceRequest
43     {
44     public:
45         typedef std::shared_ptr<OCResourceRequest> Ptr;
46
47         OCResourceRequest():
48             m_requestType{},
49             m_resourceUri{},
50             m_queryParameters{},
51             m_requestHandlerFlag{},
52             m_representation{},
53             m_observationInfo{},
54             m_headerOptions{},
55             m_requestHandle{nullptr},
56             m_resourceHandle{nullptr}
57         {
58         }
59
60         /**
61         *  Virtual destructor
62         */
63         virtual ~OCResourceRequest(void)
64         {
65         }
66
67         /**
68         *  Retrieves the type of request string for the entity handler function to operate
69         *  @return std::string request type. This could be 'GET'/'PUT'/'POST'/'DELETE'
70         */
71         std::string getRequestType() const {return m_requestType;}
72
73         /**
74         *  Retrieves the query parameters from the request
75         *  @return std::string query parameters in the request
76         */
77         const QueryParamsMap& getQueryParameters() const {return m_queryParameters;}
78
79         /**
80         *  Retrieves the request handler flag type. This can be either INIT flag or
81         *  REQUEST flag or OBSERVE flag.
82         *  NOTE:
83         *  INIT indicates that the vendor's entity handler should go and perform
84         *  initialization operations
85         *  REQUEST indicates that it is a request of certain type (GET/PUT/POST/DELETE)
86         *  and entity handler needs to perform corresponding operations
87         *  OBSERVE indicates that the request is of type Observe and entity handler
88         *  needs to perform corresponding operations
89         *  @return int type of request flag
90         */
91         int getRequestHandlerFlag() const {return m_requestHandlerFlag;}
92
93         /**
94         *  Provides the entire resource attribute representation
95         *  @return OCRepresentation reference containing the name value pairs
96         *   representing the resource's attributes
97         */
98         const OCRepresentation& getResourceRepresentation() const {return m_representation;}
99
100         /**
101         *  @return ObservationInfo reference provides observation information
102         */
103         const ObservationInfo& getObservationInfo() const {return m_observationInfo;}
104
105         /**
106         *  sets resource uri
107         *  @param resourceUri specifies the resource uri
108         */
109         void setResourceUri(const std::string resourceUri)
110         {
111             m_resourceUri = resourceUri;
112         }
113
114         /**
115         *  gets resource uri
116         *  @return std::string resource uri
117         */
118         std::string getResourceUri(void)
119         {
120             return m_resourceUri;
121         }
122
123         /**
124         * This API retrieves headerOptions which was sent from a client
125         *
126         * @return std::map HeaderOptions with the header options
127         */
128         const HeaderOptions& getHeaderOptions() const
129         {
130             return m_headerOptions;
131         }
132
133         /**
134         * This API retrieves the request handle
135         *
136         * @return OCRequestHandle
137         */
138         const OCRequestHandle& getRequestHandle() const
139         {
140             return m_requestHandle;
141         }
142
143         /**
144         * This API retrieves the resource handle
145         *
146         * return OCResourceHandle
147         */
148         const OCResourceHandle& getResourceHandle() const
149         {
150             return m_resourceHandle;
151         }
152
153     private:
154         std::string m_requestType;
155         std::string m_resourceUri;
156         QueryParamsMap m_queryParameters;
157         int m_requestHandlerFlag;
158         OCRepresentation m_representation;
159         ObservationInfo m_observationInfo;
160         HeaderOptions m_headerOptions;
161         OCRequestHandle m_requestHandle;
162         OCResourceHandle m_resourceHandle;
163
164
165     private:
166         friend void (::formResourceRequest)(OCEntityHandlerFlag, OCEntityHandlerRequest*,
167             std::shared_ptr<OC::OCResourceRequest>);
168         void setRequestType(const std::string& requestType)
169         {
170             m_requestType = requestType;
171         }
172
173         void setPayload(const std::string& requestPayload)
174         {
175             if(requestPayload.empty())
176             {
177                 return;
178             }
179
180             MessageContainer info;
181             info.setJSONRepresentation(requestPayload);
182
183             const std::vector<OCRepresentation>& reps = info.representations();
184             if(reps.size() >0)
185             {
186                 std::vector<OCRepresentation>::const_iterator itr = reps.begin();
187                 std::vector<OCRepresentation>::const_iterator back = reps.end();
188                 m_representation = *itr;
189                 ++itr;
190
191                 for(;itr != back; ++itr)
192                 {
193                     m_representation.addChild(*itr);
194                 }
195             }
196             else
197             {
198                 throw OCException(OC::Exception::INVALID_REPRESENTATION);
199             }
200         }
201
202         void setQueryParams(QueryParamsMap& queryParams)
203         {
204             m_queryParameters = queryParams;
205         }
206
207         void setRequestHandlerFlag(int requestHandlerFlag)
208         {
209             m_requestHandlerFlag = requestHandlerFlag;
210         }
211
212         void setObservationInfo(const ObservationInfo& observationInfo)
213         {
214             m_observationInfo = observationInfo;
215         }
216
217         void setHeaderOptions(const HeaderOptions& headerOptions)
218         {
219             m_headerOptions = headerOptions;
220         }
221
222         /**
223         * This API allows to set request handle
224         * @param requestHandle - OCRequestHandle type used to set the
225         * request handle
226         */
227         void setRequestHandle(const OCRequestHandle& requestHandle)
228         {
229             m_requestHandle = requestHandle;
230         }
231
232         /**
233         * This API allows to set the resource handle
234         * @param resourceHandle - OCResourceHandle type used to set the
235         * resource handle
236         */
237         void setResourceHandle(const OCResourceHandle& resourceHandle)
238         {
239             m_resourceHandle = resourceHandle;
240         }
241
242     };
243  }// namespace OC
244
245 #endif //__OCRESOURCEREQUEST_H