replace : iotivity -> iotivity-sec
[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 /**
22  * @file
23  *
24  * This file contains the declaration of classes and its members related to
25  * ResourceResponse.
26  */
27
28 #ifndef OC_RESOURCERESPONSE_H_
29 #define OC_RESOURCERESPONSE_H_
30
31 #include "OCApi.h"
32 #include <IServerWrapper.h>
33 #include <ocstack.h>
34 #include <OCRepresentation.h>
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{0},
55             m_resourceHandle{nullptr},
56             m_responseResult{}
57         {
58         }
59
60 #if defined(_MSC_VER) && (_MSC_VER < 1900)
61         OCResourceResponse(OCResourceResponse&& o):
62             m_newResourceUri(std::move(o.m_newResourceUri)),
63             m_errorCode(o.m_errorCode),
64             m_headerOptions(std::move(o.m_headerOptions)),
65             m_interface(std::move(o.m_interface)),
66             m_representation(std::move(o.m_representation)),
67             m_requestHandle(std::move(o.m_requestHandle)),
68             m_resourceHandle(std::move(o.m_resourceHandle)),
69             m_responseResult(std::move(o.m_responseResult))
70         {
71         }
72         OCResourceResponse& operator=(OCResourceResponse&& o)
73         {
74             m_newResourceUri = std::move(o.m_newResourceUri);
75             m_errorCode = o.m_errorCode;
76             m_headerOptions = std::move(o.m_headerOptions);
77             m_interface = std::move(o.m_interface);
78             m_representation = std::move(o.m_representation);
79             m_requestHandle = std::move(o.m_requestHandle);
80             m_resourceHandle = std::move(o.m_resourceHandle);
81             m_responseResult = std::move(o.m_responseResult);
82         }
83 #else
84         OCResourceResponse(OCResourceResponse&&) = default;
85         OCResourceResponse& operator=(OCResourceResponse&&) = default;
86 #endif
87         virtual ~OCResourceResponse(void) {}
88
89         /**
90         *  This API sets the error code for this response
91         *  @param eCode error code to set
92         */
93         void setErrorCode(const int eCode) { m_errorCode = eCode; }
94
95         /**
96         *  gets new resource uri
97         *  @return std::string new resource uri
98         */
99         std::string getNewResourceUri(void)
100         {
101             return m_newResourceUri;
102         }
103
104         /**
105         *  sets new resource uri
106         *  @param newResourceUri specifies the resource uri of the resource created
107         */
108         void setNewResourceUri(const std::string newResourceUri)
109         {
110             m_newResourceUri = newResourceUri;
111         }
112
113         /**
114         * This API allows to set headerOptions in the response
115         * @param headerOptions HeaderOptions vector consisting of OCHeaderOption objects
116         */
117         void setHeaderOptions(const HeaderOptions& headerOptions)
118         {
119             m_headerOptions = headerOptions;
120         }
121
122         /**
123         * This API allows to set request handle
124         *
125         * @param requestHandle - OCRequestHandle type used to set the request handle
126         */
127         void setRequestHandle(const OCRequestHandle& requestHandle)
128         {
129             m_requestHandle = requestHandle;
130         }
131
132         /**
133         * This API allows to set the resource handle
134         *
135         * @param resourceHandle - OCResourceHandle type used to set the resource handle
136         */
137         void setResourceHandle(const OCResourceHandle& resourceHandle)
138         {
139             m_resourceHandle = resourceHandle;
140         }
141
142         /**
143         * This API allows to set the EntityHandler response result
144         *
145         * @param responseResult - OCEntityHandlerResult type to set the result value
146         */
147         void setResponseResult(const OCEntityHandlerResult& responseResult)
148         {
149             m_responseResult = responseResult;
150         }
151
152         /**
153         *  API to set the entire resource attribute representation
154         *  @param rep reference to the resource's representation
155         *  @param interface specifies the interface
156         */
157         void setResourceRepresentation(OCRepresentation& rep, std::string iface) {
158             m_interface = iface;
159             m_representation = rep;
160         }
161
162         /**
163         *  API to set the entire resource attribute representation
164         *  @param rep rvalue reference to the resource's representation
165         *  @param interface specifies the interface
166         */
167         void setResourceRepresentation(OCRepresentation&& rep, std::string iface) {
168             setResourceRepresentation(rep, iface);
169         }
170
171         /**
172         *  API to set the entire resource attribute representation
173         *  @param rep reference to the resource's representation
174         */
175         void setResourceRepresentation(OCRepresentation& rep) {
176             // Call the default
177             m_interface = DEFAULT_INTERFACE;
178             m_representation = rep;
179         }
180
181         /**
182         *  API to set the entire resource attribute representation
183         *  @param rep rvalue reference to the resource's representation
184         */
185         void setResourceRepresentation(OCRepresentation&& rep) {
186             // Call the above function
187             setResourceRepresentation(rep);
188         }
189     private:
190         std::string m_newResourceUri;
191         int m_errorCode;
192         HeaderOptions m_headerOptions;
193         std::string m_interface;
194         OCRepresentation m_representation;
195         OCRequestHandle m_requestHandle;
196         OCResourceHandle m_resourceHandle;
197         OCEntityHandlerResult m_responseResult;
198
199     private:
200         friend class InProcServerWrapper;
201
202         OCRepPayload* getPayload() const
203         {
204             MessageContainer inf;
205             OCRepresentation first(m_representation);
206
207             if(m_interface==LINK_INTERFACE)
208             {
209                 first.setInterfaceType(InterfaceType::LinkParent);
210             }
211             else if(m_interface==BATCH_INTERFACE)
212             {
213                 first.setInterfaceType(InterfaceType::BatchParent);
214             }
215             else
216             {
217                 first.setInterfaceType(InterfaceType::DefaultParent);
218             }
219
220             inf.addRepresentation(first);
221
222             for(const OCRepresentation& rep : m_representation.getChildren())
223             {
224                 OCRepresentation cur(rep);
225
226                 if(m_interface==LINK_INTERFACE)
227                 {
228                     cur.setInterfaceType(InterfaceType::LinkChild);
229                 }
230                 else if(m_interface==BATCH_INTERFACE)
231                 {
232                     cur.setInterfaceType(InterfaceType::BatchChild);
233                 }
234                 else
235                 {
236                     cur.setInterfaceType(InterfaceType::DefaultChild);
237                 }
238
239                 inf.addRepresentation(cur);
240
241             }
242
243             return inf.getPayload();
244         }
245     public:
246
247         /**
248         * Get error code
249         */
250         int getErrorCode() const
251         {
252             return m_errorCode;
253         }
254
255         /**
256          * Get the Response Representation
257          */
258         const OCRepresentation& getResourceRepresentation() const
259         {
260             return m_representation;
261         }
262         /**
263         * This API allows to retrieve headerOptions from a response
264         */
265         const HeaderOptions& getHeaderOptions() const
266         {
267             return m_headerOptions;
268         }
269
270         /**
271         * This API retrieves the request handle
272         *
273         * @return OCRequestHandle value
274         */
275         const OCRequestHandle& getRequestHandle() const
276         {
277             return m_requestHandle;
278         }
279
280         /**
281         * This API retrieves the resource handle
282         *
283         * @return OCResourceHandle value
284         */
285         const OCResourceHandle& getResourceHandle() const
286         {
287             return m_resourceHandle;
288         }
289
290         /**
291         * This API retrieves the entity handle response result
292         *
293         * @return OCEntityHandler result value
294         */
295         OCEntityHandlerResult getResponseResult() const
296         {
297             return m_responseResult;
298         }
299     };
300
301 } // namespace OC
302
303 #endif // OC_RESOURCERESPONSE_H_