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 <OCResourceResponse.h>
24 #include <ResourceAttributesConverter.h>
25 #include <RCSResourceObject.h>
26 #include <ResourceAttributesUtils.h>
32 using namespace OIC::Service;
34 typedef std::function< OC::OCRepresentation(RCSResourceObject&) > OCRepresentationGetter;
36 OC::OCRepresentation getOCRepresentationFromResource(RCSResourceObject& resource)
38 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
39 return ResourceAttributesConverter::toOCRepresentation(resource.getAttributes());
42 OC::OCRepresentation getOCRepresentation(const RCSResourceAttributes& attrs)
44 return ResourceAttributesConverter::toOCRepresentation(attrs);
47 template< typename T >
48 OCRepresentationGetter wrapGetOCRepresentation(T&& attrs)
50 return std::bind(getOCRepresentation, std::forward<T>(attrs));
53 std::shared_ptr< OC::OCResourceResponse > doBuildResponse(RCSResourceObject& resource,
54 int errorCode, OCRepresentationGetter ocRepGetter)
56 auto response = std::make_shared< OC::OCResourceResponse >();
58 response->setResponseResult(OC_EH_OK);
59 response->setErrorCode(errorCode);
60 response->setResourceRepresentation(ocRepGetter(resource));
65 AttrKeyValuePairs applyAcceptMethod(RCSResourceObject& resource,
66 const RCSResourceAttributes& requestAttrs)
68 RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
70 return replaceAttributes(resource.getAttributes(), requestAttrs);
73 AttrKeyValuePairs applyDefaultMethod(RCSResourceObject& resource,
74 const RCSResourceAttributes& requestAttrs)
76 RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
78 if (resource.getSetRequestHandlerPolicy()
79 != RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE
80 && !acceptableAttributes(resource.getAttributes(), requestAttrs))
82 return AttrKeyValuePairs{ };
85 return replaceAttributes(resource.getAttributes(), requestAttrs);
88 AttrKeyValuePairs applyIgnoreMethod(RCSResourceObject&, const RCSResourceAttributes&)
90 return AttrKeyValuePairs();
93 auto getApplyAcceptanceFunc(RCSSetResponse::AcceptanceMethod method) ->
94 std::function<AttrKeyValuePairs(RCSResourceObject&, const RCSResourceAttributes&)>
98 case RCSSetResponse::AcceptanceMethod::DEFAULT:
99 return applyDefaultMethod;
101 case RCSSetResponse::AcceptanceMethod::ACCEPT:
102 return applyAcceptMethod;
104 case RCSSetResponse::AcceptanceMethod::IGNORE:
105 return applyIgnoreMethod;
108 return applyIgnoreMethod;
111 } // unnamed namespace
117 constexpr int RequestHandler::DEFAULT_ERROR_CODE;
119 RequestHandler::RequestHandler() :
120 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, DEFAULT_ERROR_CODE,
121 getOCRepresentationFromResource) }
125 RequestHandler::RequestHandler(int errorCode) :
126 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, errorCode,
127 getOCRepresentationFromResource) }
131 RequestHandler::RequestHandler(const RCSResourceAttributes& attrs, int errorCode) :
132 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, errorCode,
133 wrapGetOCRepresentation(attrs)) }
137 RequestHandler::RequestHandler(RCSResourceAttributes&& attrs, int errorCode) :
138 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, errorCode,
139 wrapGetOCRepresentation(std::move(attrs))) }
143 std::shared_ptr< OC::OCResourceResponse > RequestHandler::buildResponse(
144 RCSResourceObject& resource)
146 return m_holder(resource);
150 SetRequestHandler::SetRequestHandler() :
155 SetRequestHandler::SetRequestHandler(int errorCode) :
156 RequestHandler{ errorCode }
161 SetRequestHandler::SetRequestHandler(const RCSResourceAttributes& attrs, int errorCode) :
162 RequestHandler{ attrs, errorCode }
166 SetRequestHandler::SetRequestHandler(RCSResourceAttributes&& attrs, int errorCode) :
167 RequestHandler{ std::move(attrs), errorCode }
171 AttrKeyValuePairs SetRequestHandler::applyAcceptanceMethod(
172 RCSSetResponse::AcceptanceMethod method, RCSResourceObject& resource,
173 const RCSResourceAttributes& requestAttrs) const
175 return getApplyAcceptanceFunc(method)(resource, requestAttrs);