Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / include / RCSResponse.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 classes for creating Get & Set response for the Get & Set request.
25  */
26 #ifndef SERVERBUILDER_RCSRESPONSE_H
27 #define SERVERBUILDER_RCSRESPONSE_H
28
29 #include <cstdint>
30 #include <memory>
31
32 #include <octypes.h>
33
34 namespace OIC
35 {
36     namespace Service
37     {
38         class RCSResourceAttributes;
39
40         class RequestHandler;
41         class SetRequestHandler;
42
43         /**
44          * This class provides factory methods to create the response for a received get request.
45          * The response consists of a result code, an error code and result attributes.
46          *
47          * @see RCSResourceObject
48          */
49         class RCSGetResponse
50         {
51         public:
52             /**
53              * Creates a default RCSGetResponse.
54              * The response will have OC_EH_OK for the result and 200 for the errorCode.
55              * The attributes of RCSResourceObject will be set as the result attributes.
56              *
57              */
58             static RCSGetResponse defaultAction();
59
60             /**
61              * Creates a RCSGetResponse with a result and error code passed.
62              * The attributes of the RCSResourceObject will be set as the result attributes.
63              *
64              * @param result The response result.
65              * @param errorCode The error code to set in response.
66              *
67              */
68             static RCSGetResponse create(const OCEntityHandlerResult& result, int errorCode);
69
70             /**
71              * Creates a RCSGetResponse with custom attributes.
72              * This sends the passed attributes as the result attributes
73              * instead of the one the RCSResourceObject holds.
74              *
75              * @param attrs The attributes to set.
76              *
77              * @see RCSResourceAttributes
78              *
79              */
80             static RCSGetResponse create(const RCSResourceAttributes& attrs);
81
82             /**
83              * @override
84              */
85             static RCSGetResponse create(RCSResourceAttributes&& attrs);
86
87             /**
88              * Creates a RCSGetResponse with a result and error code passed.
89              * This sends the passed attributes as the result attributes
90              * instead of the one the RCSResourceObject holds.
91              *
92              * @param attrs The attributes to set.
93              * @param result The response result.
94              * @param errorCode The error code for response.
95              *
96              * @see RCSResourceAttributes
97              *
98              */
99             static RCSGetResponse create(const RCSResourceAttributes& attrs,
100                     const OCEntityHandlerResult& result, int errorCode);
101
102             /**
103              * @override
104              */
105             static RCSGetResponse create(RCSResourceAttributes&& attrs,
106                     const OCEntityHandlerResult& result, int errorCode);
107
108             //! @cond
109             RequestHandler* getHandler() const;
110             //! @endcond
111
112         private:
113             RCSGetResponse(std::shared_ptr< RequestHandler >&&);
114
115         private:
116             std::shared_ptr< RequestHandler > m_handler;
117         };
118
119         /**
120          * This class provides factory methods to create the response for a received set request.
121          * The response consists of a result code, an error code and result attributes.
122          *
123          * AcceptanceMethod provides ways how to handle attributes from a request.
124          *
125          * @see RCSResourceObject
126          */
127         class RCSSetResponse
128         {
129         public:
130             /**
131              * Options for handling a set request.
132              *
133              * This overrides SetRequestHandlerPolicy.
134              *
135              * @see SetRequestHandlerPolicy
136              *
137              */
138             enum class AcceptanceMethod
139             {
140                 /**
141                  * Follow SetRequestHandlerPolicy of the RCSResourceObject.
142                  */
143                 DEFAULT,
144
145                 /**
146                  * Accept the request attributes even if there is an unknown key or mismatched type.
147                  */
148                 ACCEPT,
149
150                 /**
151                  * Ignore the request attributes.
152                  */
153                 IGNORE
154             };
155
156             /**
157              * Creates a default RCSSetResponse that has AcceptanceMethod::DEFAULT.
158              * The response will have OC_EH_OK for the result and 200 for the errorCode.
159              * The attributes of RCSResourceObject will be set as the result attributes.
160              *
161              */
162             static RCSSetResponse defaultAction();
163
164             /**
165              * Creates a default RCSSetResponse that has AcceptanceMethod::ACCEPT.
166              * The response will have OC_EH_OK for the result and 200 for the errorCode.
167              * The attributes of RCSResourceObject will be set as the result attributes.
168              *
169              */
170             static RCSSetResponse accept();
171
172             /**
173              * Creates a RCSSetResponse that has AcceptanceMethod::ACCEPT
174              * with a result and error code passed.
175              * The attributes of the RCSResourceObject will be set as the result attributes.
176              *
177              * @param result The response result.
178              * @param errorCode The error code to set in response.
179              *
180              */
181             static RCSSetResponse accept(const OCEntityHandlerResult& result, int errorCode);
182
183             /**
184              * Creates a default RCSSetResponse that has AcceptanceMethod::IGNORE.
185              * The response will have OC_EH_OK for the result and 200 for the errorCode.
186              * The attributes of RCSResourceObject will be set as the result attributes.
187              *
188              */
189             static RCSSetResponse ignore();
190
191             /**
192              * Creates a RCSSetResponse that has AcceptanceMethod::IGNORE
193              * with a result and error code passed.
194              * The attributes of the RCSResourceObject will be set as the result attributes.
195              *
196              * @param result The response result.
197              * @param errorCode The error code to set in response.
198              *
199              */
200             static RCSSetResponse ignore(const OCEntityHandlerResult& result, int errorCode);
201
202             /**
203              * Creates a RCSSetResponse that has AcceptanceMethod::DEFAULT
204              * with a result and error code passed.
205              * The attributes of the RCSResourceObject will be set as the result attributes.
206              *
207              * @param result The response result.
208              * @param errorCode The error code to set in response.
209              *
210              */
211             static RCSSetResponse create(const OCEntityHandlerResult& result, int errorCode);
212
213             /**
214              * Creates a RCSSetResponse that has AcceptanceMethod::DEFAULT with custom attributes.
215              * This sends the passed attributes as the result attributes
216              * instead of the one the RCSResourceObject holds.
217              *
218              * @param attrs The attributes to set.
219              *
220              * @see RCSResourceAttributes
221              *
222              */
223             static RCSSetResponse create(const RCSResourceAttributes& attrs);
224
225             /**
226              * @override
227              */
228             static RCSSetResponse create(RCSResourceAttributes &&attrs);
229
230             /**
231              * Creates a RCSSetResponse with a result and error code passed.
232              * This sends the passed attributes as the result attributes
233              * instead of the one the RCSResourceObject holds.
234              *
235              * @param attrs The attributes to set.
236              * @param result The response result.
237              * @param errorCode The error code for response.
238              *
239              * @see RCSResourceAttributes
240              *
241              */
242             static RCSSetResponse create(const RCSResourceAttributes& attrs,
243                     const OCEntityHandlerResult& result, int errorCode);
244
245
246             /**
247              * @override
248              */
249             static RCSSetResponse create(RCSResourceAttributes &&attrs,
250                     const OCEntityHandlerResult &result, int errorCode);
251
252
253             //! @cond/
254             SetRequestHandler* getHandler() const;
255             //! @endcond
256
257             /**
258              * Returns the acceptance method.
259              *
260              */
261             AcceptanceMethod getAcceptanceMethod() const;
262
263             /**
264              * Sets the acceptance method for the RCSSetResponse.
265              *
266              * @param method - AcceptanceMethod value to set
267              *
268              * @return The reference to this RCSSetResponse
269              *
270              * @see AcceptanceMethod
271              *
272              */
273             RCSSetResponse& setAcceptanceMethod(AcceptanceMethod method);
274
275         private:
276             RCSSetResponse(std::shared_ptr< SetRequestHandler >&&);
277             RCSSetResponse(std::shared_ptr< SetRequestHandler >&&, AcceptanceMethod);
278
279         private:
280             AcceptanceMethod m_acceptanceMethod;
281             std::shared_ptr< SetRequestHandler > m_handler;
282         };
283     }
284 }
285
286 #endif // SERVERBUILDER_RCSRESPONSE_H