Adding OCRep destructor, various move activities
[platform/upstream/iotivity.git] / resource / 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     class InProcServerWrapper;
39
40     /**
41     *   @brief  OCResourceResponse provides APIs to set the response details
42     */
43     class OCResourceResponse
44     {
45     public:
46         typedef std::shared_ptr<OCResourceResponse> Ptr;
47
48         OCResourceResponse():
49             m_newResourceUri{},
50             m_errorCode{},
51             m_headerOptions{},
52             m_interface{},
53             m_representation{},
54             m_requestHandle{nullptr},
55             m_resourceHandle{nullptr},
56             m_responseResult{}
57         {
58         }
59
60         OCResourceResponse(OCResourceResponse&&) = default;
61         OCResourceResponse& operator=(OCResourceResponse&&) = default;
62         virtual ~OCResourceResponse(void) {}
63
64         /**
65         *  This API sets the error code for this response
66         *  @param eCode error code to set
67         */
68         void setErrorCode(const int eCode) { m_errorCode = eCode; }
69
70         /**
71         *  gets new resource uri
72         *  @return std::string new resource uri
73         */
74         std::string getNewResourceUri(void)
75         {
76             return m_newResourceUri;
77         }
78
79         /**
80         *  sets new resource uri
81         *  @param newResourceUri specifies the resource uri of the resource created
82         */
83         void setNewResourceUri(const std::string newResourceUri)
84         {
85             m_newResourceUri = newResourceUri;
86         }
87
88         /**
89         * This API allows to set headerOptions in the response
90         * @param headerOptions HeaderOptions vector consisting of OCHeaderOption objects
91         */
92         void setHeaderOptions(const HeaderOptions& headerOptions)
93         {
94             m_headerOptions = headerOptions;
95         }
96
97         /**
98         * This API allows to set request handle
99         *
100         * @param requestHandle - OCRequestHandle type used to set the request handle
101         */
102         void setRequestHandle(const OCRequestHandle& requestHandle)
103         {
104             m_requestHandle = requestHandle;
105         }
106
107         /**
108         * This API allows to set the resource handle
109         *
110         * @param resourceHandle - OCResourceHandle type used to set the resource handle
111         */
112         void setResourceHandle(const OCResourceHandle& resourceHandle)
113         {
114             m_resourceHandle = resourceHandle;
115         }
116
117         /**
118         * This API allows to set the EntityHandler response result
119         *
120         * @param responseResult - OCEntityHandlerResult type to set the result value
121         */
122         void setResponseResult(const OCEntityHandlerResult& responseResult)
123         {
124             m_responseResult = responseResult;
125         }
126
127         /**
128         *  API to set the entire resource attribute representation
129         *  @param attributeMap reference containing the name value pairs representing
130         *         the resource's attributes
131         *  @param interface specifies the interface
132         */
133         void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
134             m_interface = interface;
135             m_representation = rep;
136         }
137
138         /**
139         *  API to set the entire resource attribute representation
140         *  @param attributeMap rvalue reference containing the name value pairs representing
141         *         the resource's attributes
142         *  @param interface specifies the interface
143         */
144         void setResourceRepresentation(OCRepresentation&& rep, std::string interface) {
145             setResourceRepresentation(rep, interface);
146         }
147
148         /**
149         *  API to set the entire resource attribute representation
150         *  @param attributeMap reference containing the name value pairs representing the resource's
151         *  attributes
152         */
153         void setResourceRepresentation(OCRepresentation& rep) {
154             // Call the default
155             m_interface = DEFAULT_INTERFACE;
156             m_representation = rep;
157         }
158
159         /**
160         *  API to set the entire resource attribute representation
161         *  @param attributeMap rvalue reference containing the name value pairs representing the
162         *  resource's attributes
163         */
164         void setResourceRepresentation(OCRepresentation&& rep) {
165             // Call the above function
166             setResourceRepresentation(rep);
167         }
168     private:
169         std::string m_newResourceUri;
170         int m_errorCode;
171         HeaderOptions m_headerOptions;
172         std::string m_interface;
173         OCRepresentation m_representation;
174         OCRequestHandle m_requestHandle;
175         OCResourceHandle m_resourceHandle;
176         OCEntityHandlerResult m_responseResult;
177
178     private:
179         friend class InProcServerWrapper;
180
181         std::string getPayload() const
182         {
183             MessageContainer inf;
184             OCRepresentation first(m_representation);
185
186             if(m_interface==LINK_INTERFACE)
187             {
188                 first.setInterfaceType(InterfaceType::LinkParent);
189             }
190             else if(m_interface==BATCH_INTERFACE)
191             {
192                 first.setInterfaceType(InterfaceType::BatchParent);
193             }
194             else
195             {
196                 first.setInterfaceType(InterfaceType::DefaultParent);
197             }
198
199             inf.addRepresentation(first);
200
201             for(const OCRepresentation& rep : m_representation.getChildren())
202             {
203                 OCRepresentation cur(rep);
204
205                 if(m_interface==LINK_INTERFACE)
206                 {
207                     cur.setInterfaceType(InterfaceType::LinkChild);
208                 }
209                 else if(m_interface==BATCH_INTERFACE)
210                 {
211                     cur.setInterfaceType(InterfaceType::BatchChild);
212                 }
213                 else
214                 {
215                     cur.setInterfaceType(InterfaceType::DefaultChild);
216                 }
217
218                 inf.addRepresentation(cur);
219
220             }
221
222             return inf.getJSONRepresentation(OCInfoFormat::ExcludeOC);
223         }
224     public:
225
226         /**
227         * Get error code
228         */
229         int getErrorCode() const;
230
231         /**
232          * Get the Response Representation
233          */
234         const OCRepresentation& getResourceRepresentation() const
235         {
236             return m_representation;
237         }
238         /**
239         * This API allows to retrieve headerOptions from a response
240         */
241         const HeaderOptions& getHeaderOptions() const
242         {
243             return m_headerOptions;
244         }
245
246         /**
247         * This API retrieves the request handle
248         *
249         * @return OCRequestHandle value
250         */
251         const OCRequestHandle& getRequestHandle() const
252         {
253             return m_requestHandle;
254         }
255
256         /**
257         * This API retrieves the resource handle
258         *
259         * @return OCResourceHandle value
260         */
261         const OCResourceHandle& getResourceHandle() const
262         {
263             return m_resourceHandle;
264         }
265
266         /**
267         * This API retrieves the entity handle response result
268         *
269         * @return OCEntityHandler result value
270         */
271         const OCEntityHandlerResult getResponseResult() const
272         {
273             return m_responseResult;
274         }
275     };
276
277 } // namespace OC
278
279 #endif //__OCRESOURCERESPONSE_H