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 * gets new resource uri
64 * @return std::string new resource uri
66 std::string getNewResourceUri(void)
68 return m_newResourceUri;
72 * sets new resource uri
73 * @param newResourceUri specifies the resource uri of the resource created
75 void setNewResourceUri(const std::string newResourceUri)
77 m_newResourceUri = newResourceUri;
81 * This API allows to set headerOptions in the response
82 * @param headerOptions HeaderOptions vector consisting of OCHeaderOption objects
84 void setHeaderOptions(const HeaderOptions& headerOptions)
86 m_headerOptions = headerOptions;
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
95 void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
96 if(!interface.compare(LINK_INTERFACE))
98 setResourceRepresentationLL(rep);
100 else if(!interface.compare(BATCH_INTERFACE))
102 setResourceRepresentationBatch(rep);
106 setResourceRepresentationDefault(rep);
108 // TODO other interfaces
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
117 void setResourceRepresentation(OCRepresentation&& rep, std::string interface) {
118 setResourceRepresentation(rep, interface);
122 * API to set the entire resource attribute representation
123 * @param attributeMap reference containing the name value pairs representing the resource's
126 void setResourceRepresentation(OCRepresentation& rep) {
128 setResourceRepresentationDefault(rep);
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
136 void setResourceRepresentation(OCRepresentation&& rep) {
137 // Call the above function
138 setResourceRepresentation(rep);
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
146 void setResourceRepresentationLL(OCRepresentation& rep) {
150 ostringstream payload;
154 payload << "\"href\":";
156 payload << rep.getUri();
161 std::vector<OCRepresentation> children = rep.getChildren();
163 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
165 payload << ",{\"href\":";
168 payload << oitr->getUri();
171 payload << ",\"prop\":{";
173 payload << "\"rt\":[";
174 std::vector<std::string> types = oitr->getResourceTypes();
175 for(auto itr = types.begin(); itr != types.end(); ++itr)
177 if(itr != types.begin())
186 payload << "\"if\":[";
187 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
188 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
190 if(itr != interfaces.begin())
195 payload << "\"" << *itr << "\"";
202 m_payload = payload.str();
206 * API to set the entire resource attribute representation (Default))
207 * @param attributeMap reference containing the name value pairs representing the resource's
210 void setResourceRepresentationDefault(OCRepresentation& rep) {
214 ostringstream payload;
218 payload << "\"href\":";
220 payload << rep.getUri();
223 payload << ",\"rep\":";
225 payload << rep.getJSONRepresentation();
230 std::vector<OCRepresentation> children = rep.getChildren();
232 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
234 payload << ",{\"href\":";
237 payload << oitr->getUri();
240 payload << ",\"prop\":{";
242 payload << "\"rt\":[";
243 std::vector<std::string> types = oitr->getResourceTypes();
244 for(auto itr = types.begin(); itr != types.end(); ++itr)
246 if(itr != types.begin())
251 payload << "\"" << *itr << "\"";
255 payload << "\"if\":[";
256 std::vector<std::string> interfaces = oitr->getResourceInterfaces();
257 for(auto itr = interfaces.begin(); itr != interfaces.end(); ++itr)
259 if(itr != interfaces.begin())
264 payload << "\"" << *itr << "\"";
271 m_payload = payload.str();
275 * API to set the entire resource attribute representation (BATCH)
276 * @param attributeMap reference containing the name value pairs representing the resource's
279 void setResourceRepresentationBatch(OCRepresentation& rep) {
280 ostringstream payload;
284 payload << "\"href\":";
286 payload << rep.getUri();
290 std::vector<OCRepresentation> children = rep.getChildren();
292 for(auto oitr = children.begin(); oitr != children.end(); ++oitr)
298 payload << "\"href\":";
301 payload << oitr->getUri();
304 payload << ",\"rep\":";
306 payload << oitr->getJSONRepresentation();
311 m_payload = payload.str();
315 std::string m_newResourceUri;
316 std::string m_payload;
318 HeaderOptions m_headerOptions;
320 // TODO only stack should have visibility and apps should not
326 int getErrorCode() const;
329 * This API allows to retrieve headerOptions from a response
331 const HeaderOptions& getHeaderOptions() const
333 return m_headerOptions;
337 * Get the resource attribute representation
339 AttributeMap& getResourceRepresentation() const;
341 // TODO This should go away & just use getResourceRepresentation
342 std::string getPayload()
350 #endif //__OCRESOURCERESPONSE_H