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