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