Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / serverBuilder / src / RequestHandler.cpp
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 #include <RequestHandler.h>
22
23 #include <OCResourceResponse.h>
24 #include <ResourceAttributesConverter.h>
25 #include <RCSResourceObject.h>
26
27 namespace
28 {
29     using namespace OIC::Service;
30
31     typedef std::function< OC::OCRepresentation(RCSResourceObject&) > OCRepresentationGetter;
32
33     OC::OCRepresentation getOCRepresentationFromResource(RCSResourceObject& resource)
34     {
35         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
36         return ResourceAttributesConverter::toOCRepresentation(resource.getAttributes());
37     }
38
39     OC::OCRepresentation getOCRepresentation(const RCSResourceAttributes& attrs)
40     {
41         return ResourceAttributesConverter::toOCRepresentation(attrs);
42     }
43
44     template< typename T >
45     OCRepresentationGetter wrapGetOCRepresentation(T&& attrs)
46     {
47         return std::bind(getOCRepresentation, std::forward<T>(attrs));
48     }
49
50     std::shared_ptr< OC::OCResourceResponse > doBuildResponse(RCSResourceObject& resource,
51             const OCEntityHandlerResult result, int errorCode, OCRepresentationGetter ocRepGetter)
52     {
53         auto response = std::make_shared< OC::OCResourceResponse >();
54
55         response->setResponseResult(result);
56         response->setErrorCode(errorCode);
57         response->setResourceRepresentation(ocRepGetter(resource));
58
59         return response;
60     }
61
62     AttrKeyValuePairs applyAcceptMethod(RCSResourceObject& resource,
63             const RCSResourceAttributes& requestAttrs)
64     {
65         RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
66
67         return replaceAttributes(resource.getAttributes(), requestAttrs);
68     }
69
70     AttrKeyValuePairs applyDefaultMethod(RCSResourceObject& resource,
71             const RCSResourceAttributes& requestAttrs)
72     {
73         RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
74
75         if (resource.getSetRequestHandlerPolicy()
76             != RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE
77             && !acceptableAttributes(resource.getAttributes(), requestAttrs))
78         {
79             return AttrKeyValuePairs{ };
80         }
81
82         return replaceAttributes(resource.getAttributes(), requestAttrs);
83     }
84
85     AttrKeyValuePairs applyIgnoreMethod(RCSResourceObject&, const RCSResourceAttributes&)
86     {
87         return AttrKeyValuePairs();
88     }
89
90     auto getApplyAcceptanceFunc(RCSSetResponse::AcceptanceMethod method) ->
91             std::function<AttrKeyValuePairs(RCSResourceObject&, const RCSResourceAttributes&)>
92     {
93         switch (method)
94         {
95             case RCSSetResponse::AcceptanceMethod::DEFAULT:
96                 return applyDefaultMethod;
97
98             case RCSSetResponse::AcceptanceMethod::ACCEPT:
99                 return applyAcceptMethod;
100
101             case RCSSetResponse::AcceptanceMethod::IGNORE:
102                 return applyIgnoreMethod;
103         }
104
105         return applyIgnoreMethod;
106     }
107
108 } // unnamed namespace
109
110 namespace OIC
111 {
112     namespace Service
113     {
114         constexpr int RequestHandler::DEFAULT_ERROR_CODE;
115         constexpr OCEntityHandlerResult RequestHandler::DEFAULT_RESULT;
116
117         RequestHandler::RequestHandler() :
118                 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, DEFAULT_RESULT,
119                         DEFAULT_ERROR_CODE, getOCRepresentationFromResource) }
120         {
121         }
122
123         RequestHandler::RequestHandler(const OCEntityHandlerResult& result, int errorCode) :
124                 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, result, errorCode,
125                         getOCRepresentationFromResource) }
126         {
127         }
128
129         RequestHandler::RequestHandler(const RCSResourceAttributes& attrs,
130                 const OCEntityHandlerResult& result, int errorCode) :
131                 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, result, errorCode,
132                         wrapGetOCRepresentation(attrs)) }
133         {
134         }
135
136         RequestHandler::RequestHandler(RCSResourceAttributes&& attrs,
137                 const OCEntityHandlerResult& result, int errorCode) :
138                 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, result, errorCode,
139                         wrapGetOCRepresentation(std::move(attrs))) }
140         {
141         }
142
143         std::shared_ptr< OC::OCResourceResponse > RequestHandler::buildResponse(
144                 RCSResourceObject& resource)
145         {
146             return m_holder(resource);
147         }
148
149
150         SetRequestHandler::SetRequestHandler() :
151                 RequestHandler{ }
152         {
153         }
154
155         SetRequestHandler::SetRequestHandler(const OCEntityHandlerResult& result, int errorCode) :
156                 RequestHandler{ result, errorCode }
157         {
158         }
159
160
161         SetRequestHandler::SetRequestHandler(const RCSResourceAttributes& attrs,
162                 const OCEntityHandlerResult& result, int errorCode) :
163                 RequestHandler{ attrs, result, errorCode }
164         {
165         }
166
167         SetRequestHandler::SetRequestHandler(RCSResourceAttributes&& attrs,
168                 const OCEntityHandlerResult& result, int errorCode) :
169                 RequestHandler{ std::move(attrs), result, errorCode }
170         {
171         }
172
173         AttrKeyValuePairs SetRequestHandler::applyAcceptanceMethod(
174                 RCSSetResponse::AcceptanceMethod method, RCSResourceObject& resource,
175                 const RCSResourceAttributes& requestAttrs) const
176         {
177             return getApplyAcceptanceFunc(method)(resource, requestAttrs);
178         }
179
180     }
181 }