1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file OCResourceResponse.h
23 /// @brief This file contains the declaration of classes and its members related to
26 #ifndef __OCRESOURCERESPONSE_H
27 #define __OCRESOURCERESPONSE_H
30 #include <IServerWrapper.h>
32 #include <OCRepresentation.h>
39 * @brief OCResourceResponse provides APIs to set the response details
41 class OCResourceResponse
44 typedef std::shared_ptr<OCResourceResponse> Ptr;
49 OCResourceResponse() {}
54 virtual ~OCResourceResponse(void) {}
57 * This API sets the error code for this response
58 * @param eCode error code to set
60 void setErrorCode(const int eCode) { m_errorCode = eCode; }
63 * API to set the entire resource attribute representation (BATCH)
64 * @param attributeMap reference containing the name value pairs representing the resource's attributes
66 void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
67 if(!interface.compare(LINK_INTERFACE))
69 setResourceRepresentationLL(rep);
71 else if(!interface.compare(BATCH_INTERFACE))
73 setResourceRepresentationBatch(rep);
77 setResourceRepresentationDefault(rep);
79 // TODO other interfaces
83 * API to set the entire resource attribute representation (Linked List Interface))
84 * @param attributeMap reference containing the name value pairs representing the resource's attributes
86 void setResourceRepresentationLL(OCRepresentation& rep) {
90 ostringstream payload;
94 payload << "\"href\":";
96 payload << rep.getUri();
101 std::vector<OCRepresentation> children = rep.getChildren();
103 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
105 payload << ",{\"href\":";
108 payload << oitr->getUri();
111 payload << ",\"prop\":{";
113 payload << "\"rt\":[";
114 std::vector<std::string> types = oitr->getResourceTypes();
115 for(auto itr = types.begin(); itr != types.end(); ++itr)
117 if(itr != types.begin())
126 payload << "\"if\":[";
127 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
128 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
130 if(itr != interfaces.begin())
135 payload << "\"" << *itr << "\"";
142 m_payload = payload.str();
146 * API to set the entire resource attribute representation (Default))
147 * @param attributeMap reference containing the name value pairs representing the resource's attributes
149 void setResourceRepresentationDefault(OCRepresentation& rep) {
153 ostringstream payload;
157 payload << "\"href\":";
159 payload << rep.getUri();
162 payload << ",\"rep\":{";
164 AttributeMap attributes = rep.getAttributeMap();
166 for(auto itr = attributes.begin(); itr!= attributes.end(); ++ itr)
168 if(itr != attributes.begin())
172 payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
178 std::vector<OCRepresentation> children = rep.getChildren();
180 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
182 payload << ",{\"href\":";
185 payload << oitr->getUri();
188 payload << ",\"prop\":{";
190 payload << "\"rt\":[";
191 std::vector<std::string> types = oitr->getResourceTypes();
192 for(auto itr = types.begin(); itr != types.end(); ++itr)
194 if(itr != types.begin())
199 payload << "\"" << *itr << "\"";
203 payload << "\"if\":[";
204 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
205 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
207 if(itr != interfaces.begin())
212 payload << "\"" << *itr << "\"";
219 m_payload = payload.str();
223 * API to set the entire resource attribute representation (BATCH)
224 * @param attributeMap reference containing the name value pairs representing the resource's attributes
226 void setResourceRepresentationBatch(OCRepresentation& rep) {
227 ostringstream payload;
231 payload << "\"href\":";
233 payload << rep.getUri();
237 std::vector<OCRepresentation> children = rep.getChildren();
239 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
245 payload << "\"href\":";
248 payload << oitr->getUri();
251 payload << ",\"rep\":{";
253 AttributeMap attributes = oitr->getAttributeMap();
255 for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
257 if(itr != attributes.begin())
261 payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
267 m_payload = payload.str();
271 /** TODO remove this once after above function stabilize.
272 * API to set the entire resource attribute representation
273 * @param attributeMap reference containing the name value pairs representing the resource's attributes
275 void setResourceRepresentation(AttributeMap& attributes) {
277 // TODO To be refactored
278 ostringstream payload;
282 // TODO fix this (do this programmatically)
283 payload << "\"href\":\"/a/room\"";
285 payload << ",\"rep\":{";
287 for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
289 if(itr != attributes.begin())
293 // cout << itr->first << ":" <, itr->second.front() << endl;
294 payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
300 m_payload = payload.str();
304 std::string m_payload;
307 // TODO only stack should have visibility and apps should not
313 int getErrorCode() const;
316 * Get the resource attribute representation
318 AttributeMap& getResourceRepresentation() const;
320 // TODO This should go away & just use getResourceRepresentation
321 std::string getPayload()
329 #endif //__OCRESOURCERESPONSE_H