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