1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "RequestHandler.h"
23 #include "ResourceAttributesConverter.h"
24 #include "RCSResourceObject.h"
25 #include "ResourceAttributesUtils.h"
29 using namespace OIC::Service;
31 AttrKeyValuePairs applyAcceptMethod(RCSResourceObject& resource,
32 const RCSResourceAttributes& requestAttrs)
34 RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
36 return replaceAttributes(resource.getAttributes(), requestAttrs);
39 AttrKeyValuePairs applyDefaultMethod(RCSResourceObject& resource,
40 const RCSResourceAttributes& requestAttrs)
42 RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
44 if (resource.getSetRequestHandlerPolicy()
45 != RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE
46 && !acceptableAttributes(resource.getAttributes(), requestAttrs))
48 return AttrKeyValuePairs{ };
51 return replaceAttributes(resource.getAttributes(), requestAttrs);
54 AttrKeyValuePairs applyIgnoreMethod(RCSResourceObject&, const RCSResourceAttributes&)
56 return AttrKeyValuePairs();
59 auto getApplyAcceptanceFunc(RCSSetResponse::AcceptanceMethod method) ->
60 std::function<AttrKeyValuePairs(RCSResourceObject&, const RCSResourceAttributes&)>
64 case RCSSetResponse::AcceptanceMethod::DEFAULT:
65 return applyDefaultMethod;
67 case RCSSetResponse::AcceptanceMethod::ACCEPT:
68 return applyAcceptMethod;
70 case RCSSetResponse::AcceptanceMethod::IGNORE:
71 return applyIgnoreMethod;
74 return applyIgnoreMethod;
77 } // unnamed namespace
83 constexpr int RequestHandler::DEFAULT_ERROR_CODE;
85 RequestHandler::RequestHandler() :
86 m_errorCode{ DEFAULT_ERROR_CODE },
92 RequestHandler::RequestHandler(int errorCode) :
93 m_errorCode{ errorCode },
100 RequestHandler::RequestHandler(const RCSResourceAttributes& attrs, int errorCode) :
101 m_errorCode{ errorCode },
103 m_ocRep{ ResourceAttributesConverter::toOCRepresentation(attrs) }
107 RequestHandler::RequestHandler(RCSResourceAttributes&& attrs, int errorCode) :
108 m_errorCode{ errorCode },
110 m_ocRep{ ResourceAttributesConverter::toOCRepresentation(std::move(attrs)) }
114 int RequestHandler::getErrorCode() const
119 bool RequestHandler::hasCustomRepresentation() const
124 OC::OCRepresentation RequestHandler::getRepresentation() const
129 SetRequestHandler::SetRequestHandler() :
134 SetRequestHandler::SetRequestHandler(int errorCode) :
135 RequestHandler{ errorCode }
140 SetRequestHandler::SetRequestHandler(const RCSResourceAttributes& attrs, int errorCode) :
141 RequestHandler{ attrs, errorCode }
145 SetRequestHandler::SetRequestHandler(RCSResourceAttributes&& attrs, int errorCode) :
146 RequestHandler{ std::move(attrs), errorCode }
150 AttrKeyValuePairs SetRequestHandler::applyAcceptanceMethod(
151 RCSSetResponse::AcceptanceMethod method, RCSResourceObject& resource,
152 const RCSResourceAttributes& requestAttrs) const
154 return getApplyAcceptanceFunc(method)(resource, requestAttrs);