4cd371f08a726e812b5552411a5e5c94908fa500
[platform/upstream/iotivity.git] / include / OCResourceResponse.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 OCResourceResponse.h
22
23 /// @brief  This file contains the declaration of classes and its members related to
24 ///         ResourceResponse.
25
26 #ifndef __OCRESOURCERESPONSE_H
27 #define __OCRESOURCERESPONSE_H
28
29 #include "OCApi.h"
30 #include <IServerWrapper.h>
31 #include <ocstack.h>
32 #include <OCRepresentation.h>
33
34 using namespace std;
35
36 namespace OC
37 {
38     /**
39     *   @brief  OCResourceResponse provides APIs to set the response details
40     */
41     class OCResourceResponse
42     {
43     public:
44         typedef std::shared_ptr<OCResourceResponse> Ptr;
45
46         /**
47         *  Default destructor
48         */
49         OCResourceResponse() {}
50
51         /**
52         *  Virtual destructor
53         */
54         virtual ~OCResourceResponse(void) {}
55
56         /**
57         *  This API sets the error code for this response
58         *  @param eCode error code to set
59         */
60         void setErrorCode(const int eCode) { m_errorCode = eCode; }
61
62         /**
63         *  gets new resource uri
64         *  @return std::string new resource uri
65         */
66         std::string getNewResourceUri(void)
67         {
68             return m_newResourceUri;
69         }
70
71         /**
72         *  sets new resource uri
73         *  @param newResourceUri specifies the resource uri of the resource created
74         */
75         void setNewResourceUri(const std::string newResourceUri)
76         {
77             m_newResourceUri = newResourceUri;
78         }
79
80         /**
81         * This API allows to set headerOptions in the response
82         * @param headerOptions HeaderOptions vector consisting of OCHeaderOption objects
83         */
84         void setHeaderOptions(const HeaderOptions& headerOptions)
85         {
86             m_headerOptions = headerOptions;
87         }
88
89         /**
90         *  API to set the entire resource attribute representation
91         *  @param attributeMap reference containing the name value pairs representing
92         *         the resource's attributes
93         *  @param interface specifies the interface
94         */
95         void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
96             if(!interface.compare(LINK_INTERFACE))
97             {
98                 setResourceRepresentationLL(rep);
99             }
100             else if(!interface.compare(BATCH_INTERFACE))
101             {
102                 setResourceRepresentationBatch(rep);
103             }
104             else
105             {
106                 setResourceRepresentationDefault(rep);
107             }
108             // TODO other interfaces
109         }
110
111         /**
112         *  API to set the entire resource attribute representation
113         *  @param attributeMap rvalue reference containing the name value pairs representing
114         *         the resource's attributes
115         *  @param interface specifies the interface
116         */
117         void setResourceRepresentation(OCRepresentation&& rep, std::string interface) {
118             setResourceRepresentation(rep, interface);
119         }
120
121         /**
122         *  API to set the entire resource attribute representation
123         *  @param attributeMap reference containing the name value pairs representing the resource's
124         *  attributes
125         */
126         void setResourceRepresentation(OCRepresentation& rep) {
127             // Call the default
128             setResourceRepresentationDefault(rep);
129         }
130
131         /**
132         *  API to set the entire resource attribute representation
133         *  @param attributeMap rvalue reference containing the name value pairs representing the
134         *  resource's attributes
135         */
136         void setResourceRepresentation(OCRepresentation&& rep) {
137             // Call the above function
138             setResourceRepresentation(rep);
139         }
140
141         /**
142         *  API to set the entire resource attribute representation (Linked List Interface))
143         *  @param attributeMap reference containing the name value pairs representing the resource's
144         *  attributes
145         */
146         void setResourceRepresentationLL(OCRepresentation& rep) {
147
148             // Default Set
149
150             ostringstream payload;
151
152             // Parent
153             payload << "{";
154             payload << "\"href\":";
155             payload << "\"" ;
156             payload << rep.getUri();
157             payload << "\"" ;
158             payload << "}";
159
160             // Children stuff
161             std::vector<OCRepresentation> children = rep.getChildren();
162
163             for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
164             {
165                 payload << ",{\"href\":";
166
167                 payload << "\"" ;
168                 payload << oitr->getUri();
169                 payload << "\"" ;
170
171                 payload << ",\"prop\":{";
172
173                 payload << "\"rt\":[";
174                 std::vector<std::string> types = oitr->getResourceTypes();
175                 for(auto itr = types.begin(); itr != types.end(); ++itr)
176                 {
177                     if(itr != types.begin())
178                     {
179                         payload << ',';
180                     }
181
182                     payload << *itr;
183                 }
184                 payload << "],";
185
186                 payload << "\"if\":[";
187                 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
188                 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
189                 {
190                     if(itr != interfaces.begin())
191                     {
192                         payload << ',';
193                     }
194
195                     payload << "\"" << *itr << "\"";
196                 }
197                 payload << "]";
198
199                 payload << "}}";
200             }
201
202             m_payload = payload.str();
203         }
204
205         /**
206         *  API to set the entire resource attribute representation (Default))
207         *  @param attributeMap reference containing the name value pairs representing the resource's
208         *  attributes
209         */
210         void setResourceRepresentationDefault(OCRepresentation& rep) {
211
212             // Default Set
213
214             ostringstream payload;
215
216             // Parent
217             payload << "{";
218             payload << "\"href\":";
219             payload << "\"" ;
220             payload << rep.getUri();
221             payload << "\"" ;
222
223             payload << ",\"rep\":";
224
225             payload << rep.getJSONRepresentation();
226
227             payload << "}";
228
229             // Children stuff
230             std::vector<OCRepresentation> children = rep.getChildren();
231
232             for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
233             {
234                 payload << ",{\"href\":";
235
236                 payload << "\"" ;
237                 payload << oitr->getUri();
238                 payload << "\"" ;
239
240                 payload << ",\"prop\":{";
241
242                 payload << "\"rt\":[";
243                 std::vector<std::string> types = oitr->getResourceTypes();
244                 for(auto itr = types.begin(); itr != types.end(); ++itr)
245                 {
246                     if(itr != types.begin())
247                     {
248                         payload << ',';
249                     }
250
251                     payload << "\"" << *itr << "\"";
252                 }
253                 payload << "],";
254
255                 payload << "\"if\":[";
256                 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
257                 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
258                 {
259                     if(itr != interfaces.begin())
260                     {
261                         payload << ',';
262                     }
263
264                     payload << "\"" << *itr << "\"";
265                 }
266                 payload << "]";
267
268                 payload << "}}";
269             }
270
271             m_payload = payload.str();
272         }
273
274         /**
275         *  API to set the entire resource attribute representation (BATCH)
276         *  @param attributeMap reference containing the name value pairs representing the resource's
277         *  attributes
278         */
279         void setResourceRepresentationBatch(OCRepresentation& rep) {
280             ostringstream payload;
281
282             // Parent
283             payload << "{";
284             payload << "\"href\":";
285             payload << "\"" ;
286             payload << rep.getUri();
287             payload << "\"" ;
288             payload << "}";
289
290             std::vector<OCRepresentation> children = rep.getChildren();
291
292             for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
293             {
294                 payload << ',';
295
296                 payload << "{";
297
298                 payload << "\"href\":";
299
300                 payload << "\"" ;
301                 payload << oitr->getUri();
302                 payload << "\"" ;
303
304                 payload << ",\"rep\":";
305
306                 payload << oitr->getJSONRepresentation();
307
308                 payload << "}";
309             }
310
311             m_payload = payload.str();
312         }
313
314     private:
315         std::string m_newResourceUri;
316         std::string m_payload;
317         int m_errorCode;
318         HeaderOptions m_headerOptions;
319
320     // TODO only stack should have visibility and apps should not
321     public:
322
323         /**
324         * Get error code
325         */
326         int getErrorCode() const;
327
328         /**
329         * This API allows to retrieve headerOptions from a response
330         */
331         const HeaderOptions& getHeaderOptions() const
332         {
333             return m_headerOptions;
334         }
335
336         /**
337         * Get the resource attribute representation
338         */
339         AttributeMap& getResourceRepresentation() const;
340
341         // TODO This should go away & just use getResourceRepresentation
342         std::string getPayload()
343         {
344             return m_payload;
345         }
346     };
347
348 } // namespace OC
349
350 #endif //__OCRESOURCERESPONSE_H