Partial Implementation of US1574:
[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         *  API to set the entire resource attribute representation
64         *  @param attributeMap reference containing the name value pairs representing the resource's attributes
65         *  @param interface specifies the interface
66         */
67         void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
68             if(!interface.compare(LINK_INTERFACE))
69             {
70                 setResourceRepresentationLL(rep);
71             }
72             else if(!interface.compare(BATCH_INTERFACE))
73             {
74                 setResourceRepresentationBatch(rep);
75             }
76             else
77             {
78                 setResourceRepresentationDefault(rep);
79             }
80             // TODO other interfaces
81         }
82
83         /**
84         *  API to set the entire resource attribute representation
85         *  @param attributeMap rvalue reference containing the name value pairs representing the resource's attributes
86         *  @param interface specifies the interface
87         */
88         void setResourceRepresentation(OCRepresentation&& rep, std::string interface) {
89             setResourceRepresentation(rep, interface);
90         }
91
92         /**
93         *  API to set the entire resource attribute representation
94         *  @param attributeMap reference containing the name value pairs representing the resource's attributes
95         */
96         void setResourceRepresentation(OCRepresentation& rep) {
97             // Call the default
98             setResourceRepresentationDefault(rep);
99         }
100
101         /**
102         *  API to set the entire resource attribute representation
103         *  @param attributeMap rvalue reference containing the name value pairs representing the resource's attributes
104         */
105         void setResourceRepresentation(OCRepresentation&& rep) {
106             // Call the above function
107             setResourceRepresentation(rep);
108         }
109
110         /**
111         *  API to set the entire resource attribute representation (Linked List Interface))
112         *  @param attributeMap reference containing the name value pairs representing the resource's attributes
113         */
114         void setResourceRepresentationLL(OCRepresentation& rep) {
115
116             // Default Set
117
118             ostringstream payload;
119
120             // Parent
121             payload << "{";
122             payload << "\"href\":";
123             payload << "\"" ;
124             payload << rep.getUri();
125             payload << "\"" ;
126             payload << "}";
127
128             // Children stuff
129             std::vector<OCRepresentation> children = rep.getChildren();
130
131             for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
132             {
133                 payload << ",{\"href\":";
134
135                 payload << "\"" ;
136                 payload << oitr->getUri();
137                 payload << "\"" ;
138
139                 payload << ",\"prop\":{";
140
141                 payload << "\"rt\":[";
142                 std::vector<std::string> types = oitr->getResourceTypes();
143                 for(auto itr = types.begin(); itr != types.end(); ++itr)
144                 {
145                     if(itr != types.begin())
146                     {
147                         payload << ',';
148                     }
149
150                     payload << *itr;
151                 }
152                 payload << "],";
153
154                 payload << "\"if\":[";
155                 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
156                 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
157                 {
158                     if(itr != interfaces.begin())
159                     {
160                         payload << ',';
161                     }
162
163                     payload << "\"" << *itr << "\"";
164                 }
165                 payload << "]";
166
167                 payload << "}}";
168             }
169
170             m_payload = payload.str();
171         }
172
173         /**
174         *  API to set the entire resource attribute representation (Default))
175         *  @param attributeMap reference containing the name value pairs representing the resource's attributes
176         */
177         void setResourceRepresentationDefault(OCRepresentation& rep) {
178
179             // Default Set
180
181             ostringstream payload;
182
183             // Parent
184             payload << "{";
185             payload << "\"href\":";
186             payload << "\"" ;
187             payload << rep.getUri();
188             payload << "\"" ;
189
190             payload << ",\"rep\":{";
191
192             AttributeMap attributes = rep.getAttributeMap();
193
194             for(auto itr = attributes.begin(); itr!= attributes.end(); ++ itr)
195             {
196                 if(itr != attributes.begin())
197                 {
198                     payload << ',';
199                 }
200                 payload << "\""<<itr->first<<"\":\""<< itr->second <<"\"";
201             }
202
203             payload << "}}";
204
205             // Children stuff
206             std::vector<OCRepresentation> children = rep.getChildren();
207
208             for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
209             {
210                 payload << ",{\"href\":";
211
212                 payload << "\"" ;
213                 payload << oitr->getUri();
214                 payload << "\"" ;
215
216                 payload << ",\"prop\":{";
217
218                 payload << "\"rt\":[";
219                 std::vector<std::string> types = oitr->getResourceTypes();
220                 for(auto itr = types.begin(); itr != types.end(); ++itr)
221                 {
222                     if(itr != types.begin())
223                     {
224                         payload << ',';
225                     }
226
227                     payload << "\"" << *itr << "\"";
228                 }
229                 payload << "],";
230
231                 payload << "\"if\":[";
232                 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
233                 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
234                 {
235                     if(itr != interfaces.begin())
236                     {
237                         payload << ',';
238                     }
239
240                     payload << "\"" << *itr << "\"";
241                 }
242                 payload << "]";
243
244                 payload << "}}";
245             }
246
247             m_payload = payload.str();
248         }
249
250         /**
251         *  API to set the entire resource attribute representation (BATCH)
252         *  @param attributeMap reference containing the name value pairs representing the resource's attributes
253         */
254         void setResourceRepresentationBatch(OCRepresentation& rep) {
255             ostringstream payload;
256
257             // Parent
258             payload << "{";
259             payload << "\"href\":";
260             payload << "\"" ;
261             payload << rep.getUri();
262             payload << "\"" ;
263             payload << "}";
264
265             std::vector<OCRepresentation> children = rep.getChildren();
266
267             for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
268             {
269                 payload << ',';
270
271                 payload << "{";
272
273                 payload << "\"href\":";
274
275                 payload << "\"" ;
276                 payload << oitr->getUri();
277                 payload << "\"" ;
278
279                 payload << ",\"rep\":{";
280
281                 AttributeMap attributes = oitr->getAttributeMap();
282
283                 for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
284                 {
285                     if(itr != attributes.begin())
286                     {
287                         payload << ',';
288                     }
289                     payload << "\""<<itr->first<<"\":\""<< itr->second<<"\"";
290                 }
291
292                 payload << "}}";
293             }
294
295             m_payload = payload.str();
296         }
297
298     private:
299         std::string m_payload;
300         int m_errorCode;
301
302     // TODO only stack should have visibility and apps should not
303     public:
304
305         /**
306         * Get error code
307         */
308         int getErrorCode() const;
309
310         // TODO This should go away & just use getResourceRepresentation
311         std::string getPayload()
312         {
313             return m_payload;
314         }
315     };
316
317 } // namespace OC
318
319 #endif //__OCRESOURCERESPONSE_H