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
64 * @param attributeMap reference containing the name value pairs representing the resource's attributes
65 * @param interface specifies the interface
67 void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
68 if(!interface.compare(LINK_INTERFACE))
70 setResourceRepresentationLL(rep);
72 else if(!interface.compare(BATCH_INTERFACE))
74 setResourceRepresentationBatch(rep);
78 setResourceRepresentationDefault(rep);
80 // TODO other interfaces
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
88 void setResourceRepresentation(OCRepresentation&& rep, std::string interface) {
89 setResourceRepresentation(rep, interface);
93 * API to set the entire resource attribute representation
94 * @param attributeMap reference containing the name value pairs representing the resource's attributes
96 void setResourceRepresentation(OCRepresentation& rep) {
98 setResourceRepresentationDefault(rep);
102 * API to set the entire resource attribute representation
103 * @param attributeMap rvalue reference containing the name value pairs representing the resource's attributes
105 void setResourceRepresentation(OCRepresentation&& rep) {
106 // Call the above function
107 setResourceRepresentation(rep);
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
114 void setResourceRepresentationLL(OCRepresentation& rep) {
118 ostringstream payload;
122 payload << "\"href\":";
124 payload << rep.getUri();
129 std::vector<OCRepresentation> children = rep.getChildren();
131 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
133 payload << ",{\"href\":";
136 payload << oitr->getUri();
139 payload << ",\"prop\":{";
141 payload << "\"rt\":[";
142 std::vector<std::string> types = oitr->getResourceTypes();
143 for(auto itr = types.begin(); itr != types.end(); ++itr)
145 if(itr != types.begin())
154 payload << "\"if\":[";
155 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
156 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
158 if(itr != interfaces.begin())
163 payload << "\"" << *itr << "\"";
170 m_payload = payload.str();
174 * API to set the entire resource attribute representation (Default))
175 * @param attributeMap reference containing the name value pairs representing the resource's attributes
177 void setResourceRepresentationDefault(OCRepresentation& rep) {
181 ostringstream payload;
185 payload << "\"href\":";
187 payload << rep.getUri();
190 payload << ",\"rep\":{";
192 AttributeMap attributes = rep.getAttributeMap();
194 for(auto itr = attributes.begin(); itr!= attributes.end(); ++ itr)
196 if(itr != attributes.begin())
200 payload << "\""<<itr->first<<"\":\""<< itr->second <<"\"";
206 std::vector<OCRepresentation> children = rep.getChildren();
208 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
210 payload << ",{\"href\":";
213 payload << oitr->getUri();
216 payload << ",\"prop\":{";
218 payload << "\"rt\":[";
219 std::vector<std::string> types = oitr->getResourceTypes();
220 for(auto itr = types.begin(); itr != types.end(); ++itr)
222 if(itr != types.begin())
227 payload << "\"" << *itr << "\"";
231 payload << "\"if\":[";
232 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
233 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
235 if(itr != interfaces.begin())
240 payload << "\"" << *itr << "\"";
247 m_payload = payload.str();
251 * API to set the entire resource attribute representation (BATCH)
252 * @param attributeMap reference containing the name value pairs representing the resource's attributes
254 void setResourceRepresentationBatch(OCRepresentation& rep) {
255 ostringstream payload;
259 payload << "\"href\":";
261 payload << rep.getUri();
265 std::vector<OCRepresentation> children = rep.getChildren();
267 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
273 payload << "\"href\":";
276 payload << oitr->getUri();
279 payload << ",\"rep\":{";
281 AttributeMap attributes = oitr->getAttributeMap();
283 for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
285 if(itr != attributes.begin())
289 payload << "\""<<itr->first<<"\":\""<< itr->second<<"\"";
295 m_payload = payload.str();
299 std::string m_payload;
302 // TODO only stack should have visibility and apps should not
308 int getErrorCode() const;
310 // TODO This should go away & just use getResourceRepresentation
311 std::string getPayload()
319 #endif //__OCRESOURCERESPONSE_H