04967dc4f305806dfa3fc82f73cfe0f3a2956770
[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 "ResourceAttributesConverter.h"
24 #include "RCSResourceObject.h"
25 #include "ResourceAttributesUtils.h"
26
27 namespace
28 {
29     using namespace OIC::Service;
30
31     AttrKeyValuePairs applyAcceptMethod(RCSResourceObject& resource,
32             const RCSResourceAttributes& requestAttrs)
33     {
34         RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
35
36         return replaceAttributes(resource.getAttributes(), requestAttrs);
37     }
38
39     AttrKeyValuePairs applyDefaultMethod(RCSResourceObject& resource,
40             const RCSResourceAttributes& requestAttrs)
41     {
42         RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
43
44         if (resource.getSetRequestHandlerPolicy()
45             != RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE
46             && !acceptableAttributes(resource.getAttributes(), requestAttrs))
47         {
48             return AttrKeyValuePairs{ };
49         }
50
51         return replaceAttributes(resource.getAttributes(), requestAttrs);
52     }
53
54     AttrKeyValuePairs applyIgnoreMethod(RCSResourceObject&, const RCSResourceAttributes&)
55     {
56         return AttrKeyValuePairs();
57     }
58
59     auto getApplyAcceptanceFunc(RCSSetResponse::AcceptanceMethod method) ->
60             std::function<AttrKeyValuePairs(RCSResourceObject&, const RCSResourceAttributes&)>
61     {
62         switch (method)
63         {
64             case RCSSetResponse::AcceptanceMethod::DEFAULT:
65                 return applyDefaultMethod;
66
67             case RCSSetResponse::AcceptanceMethod::ACCEPT:
68                 return applyAcceptMethod;
69
70             case RCSSetResponse::AcceptanceMethod::IGNORE:
71                 return applyIgnoreMethod;
72         }
73
74         return applyIgnoreMethod;
75     }
76
77 } // unnamed namespace
78
79 namespace OIC
80 {
81     namespace Service
82     {
83         constexpr int RequestHandler::DEFAULT_ERROR_CODE;
84
85         RequestHandler::RequestHandler() :
86                 m_errorCode{ DEFAULT_ERROR_CODE },
87                 m_customRep{ false },
88                 m_ocRep{ }
89         {
90         }
91
92         RequestHandler::RequestHandler(int errorCode) :
93                 m_errorCode{ errorCode },
94                 m_customRep{ false },
95                 m_ocRep{ }
96
97         {
98         }
99
100         RequestHandler::RequestHandler(const RCSResourceAttributes& attrs, int errorCode) :
101                 m_errorCode{ errorCode },
102                 m_customRep{ true },
103                 m_ocRep{ ResourceAttributesConverter::toOCRepresentation(attrs) }
104         {
105         }
106
107         RequestHandler::RequestHandler(RCSResourceAttributes&& attrs, int errorCode) :
108                 m_errorCode{ errorCode },
109                 m_customRep{ true },
110                 m_ocRep{ ResourceAttributesConverter::toOCRepresentation(std::move(attrs)) }
111         {
112         }
113
114         int RequestHandler::getErrorCode() const
115         {
116             return m_errorCode;
117         }
118
119         bool RequestHandler::hasCustomRepresentation() const
120         {
121             return m_customRep;
122         }
123
124         OC::OCRepresentation RequestHandler::getRepresentation() const
125         {
126             return m_ocRep;
127         }
128
129         SetRequestHandler::SetRequestHandler() :
130                 RequestHandler{ }
131         {
132         }
133
134         SetRequestHandler::SetRequestHandler(int errorCode) :
135                 RequestHandler{ errorCode }
136         {
137         }
138
139
140         SetRequestHandler::SetRequestHandler(const RCSResourceAttributes& attrs, int errorCode) :
141                 RequestHandler{ attrs, errorCode }
142         {
143         }
144
145         SetRequestHandler::SetRequestHandler(RCSResourceAttributes&& attrs,  int errorCode) :
146                 RequestHandler{ std::move(attrs), errorCode }
147         {
148         }
149
150         AttrKeyValuePairs SetRequestHandler::applyAcceptanceMethod(
151                 RCSSetResponse::AcceptanceMethod method, RCSResourceObject& resource,
152                 const RCSResourceAttributes& requestAttrs) const
153         {
154             return getApplyAcceptanceFunc(method)(resource, requestAttrs);
155         }
156
157     }
158 }