Spec compliance change to move oc/core/d to oic/p.
[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 __OCRESOURCERESPONSE_H
29 #define __OCRESOURCERESPONSE_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{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 rep reference to the resource's representation
130         *  @param interface specifies the interface
131         */
132         void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
133             m_interface = interface;
134             m_representation = rep;
135         }
136
137         /**
138         *  API to set the entire resource attribute representation
139         *  @param rep rvalue reference to the resource's representation
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 rep reference to the resource's representation
149         */
150         void setResourceRepresentation(OCRepresentation& rep) {
151             // Call the default
152             m_interface = DEFAULT_INTERFACE;
153             m_representation = rep;
154         }
155
156         /**
157         *  API to set the entire resource attribute representation
158         *  @param rep rvalue reference to the resource's representation
159         */
160         void setResourceRepresentation(OCRepresentation&& rep) {
161             // Call the above function
162             setResourceRepresentation(rep);
163         }
164     private:
165         std::string m_newResourceUri;
166         int m_errorCode;
167         HeaderOptions m_headerOptions;
168         std::string m_interface;
169         OCRepresentation m_representation;
170         OCRequestHandle m_requestHandle;
171         OCResourceHandle m_resourceHandle;
172         OCEntityHandlerResult m_responseResult;
173
174     private:
175         friend class InProcServerWrapper;
176
177         std::string getPayload() const
178         {
179             MessageContainer inf;
180             OCRepresentation first(m_representation);
181
182             if(m_interface==LINK_INTERFACE)
183             {
184                 first.setInterfaceType(InterfaceType::LinkParent);
185             }
186             else if(m_interface==BATCH_INTERFACE)
187             {
188                 first.setInterfaceType(InterfaceType::BatchParent);
189             }
190             else
191             {
192                 first.setInterfaceType(InterfaceType::DefaultParent);
193             }
194
195             inf.addRepresentation(first);
196
197             for(const OCRepresentation& rep : m_representation.getChildren())
198             {
199                 OCRepresentation cur(rep);
200
201                 if(m_interface==LINK_INTERFACE)
202                 {
203                     cur.setInterfaceType(InterfaceType::LinkChild);
204                 }
205                 else if(m_interface==BATCH_INTERFACE)
206                 {
207                     cur.setInterfaceType(InterfaceType::BatchChild);
208                 }
209                 else
210                 {
211                     cur.setInterfaceType(InterfaceType::DefaultChild);
212                 }
213
214                 inf.addRepresentation(cur);
215
216             }
217
218             return inf.getJSONRepresentation(OCInfoFormat::ExcludeOC);
219         }
220     public:
221
222         /**
223         * Get error code
224         */
225         int getErrorCode() const;
226
227         /**
228          * Get the Response Representation
229          */
230         const OCRepresentation& getResourceRepresentation() const
231         {
232             return m_representation;
233         }
234         /**
235         * This API allows to retrieve headerOptions from a response
236         */
237         const HeaderOptions& getHeaderOptions() const
238         {
239             return m_headerOptions;
240         }
241
242         /**
243         * This API retrieves the request handle
244         *
245         * @return OCRequestHandle value
246         */
247         const OCRequestHandle& getRequestHandle() const
248         {
249             return m_requestHandle;
250         }
251
252         /**
253         * This API retrieves the resource handle
254         *
255         * @return OCResourceHandle value
256         */
257         const OCResourceHandle& getResourceHandle() const
258         {
259             return m_resourceHandle;
260         }
261
262         /**
263         * This API retrieves the entity handle response result
264         *
265         * @return OCEntityHandler result value
266         */
267         const OCEntityHandlerResult getResponseResult() const
268         {
269             return m_responseResult;
270         }
271     };
272
273 } // namespace OC
274
275 #endif //__OCRESOURCERESPONSE_H
276