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>
29 using namespace OIC::Service;
31 typedef std::function< OC::OCRepresentation(RCSResourceObject&) > OCRepresentationGetter;
33 OC::OCRepresentation getOCRepresentationFromResource(RCSResourceObject& resource)
35 RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
36 return ResourceAttributesConverter::toOCRepresentation(resource.getAttributes());
39 OC::OCRepresentation getOCRepresentation(const RCSResourceAttributes& attrs)
41 return ResourceAttributesConverter::toOCRepresentation(attrs);
44 template< typename T >
45 OCRepresentationGetter wrapGetOCRepresentation(T&& attrs)
47 return std::bind(getOCRepresentation, std::forward<T>(attrs));
50 std::shared_ptr< OC::OCResourceResponse > doBuildResponse(RCSResourceObject& resource,
51 const OCEntityHandlerResult result, int errorCode, OCRepresentationGetter ocRepGetter)
53 auto response = std::make_shared< OC::OCResourceResponse >();
55 response->setResponseResult(result);
56 response->setErrorCode(errorCode);
57 response->setResourceRepresentation(ocRepGetter(resource));
62 AttrKeyValuePairs applyAcceptMethod(RCSResourceObject& resource,
63 const RCSResourceAttributes& requestAttrs)
65 RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
67 return replaceAttributes(resource.getAttributes(), requestAttrs);
70 AttrKeyValuePairs applyDefaultMethod(RCSResourceObject& resource,
71 const RCSResourceAttributes& requestAttrs)
73 RCSResourceObject::LockGuard lock(resource, RCSResourceObject::AutoNotifyPolicy::NEVER);
75 if (resource.getSetRequestHandlerPolicy()
76 != RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE
77 && !acceptableAttributes(resource.getAttributes(), requestAttrs))
79 return AttrKeyValuePairs{ };
82 return replaceAttributes(resource.getAttributes(), requestAttrs);
85 AttrKeyValuePairs applyIgnoreMethod(RCSResourceObject&, const RCSResourceAttributes&)
87 return AttrKeyValuePairs();
90 auto getApplyAcceptanceFunc(RCSSetResponse::AcceptanceMethod method) ->
91 std::function<AttrKeyValuePairs(RCSResourceObject&, const RCSResourceAttributes&)>
95 case RCSSetResponse::AcceptanceMethod::DEFAULT:
96 return applyDefaultMethod;
98 case RCSSetResponse::AcceptanceMethod::ACCEPT:
99 return applyAcceptMethod;
101 case RCSSetResponse::AcceptanceMethod::IGNORE:
102 return applyIgnoreMethod;
105 return applyIgnoreMethod;
108 } // unnamed namespace
114 constexpr int RequestHandler::DEFAULT_ERROR_CODE;
115 constexpr OCEntityHandlerResult RequestHandler::DEFAULT_RESULT;
117 RequestHandler::RequestHandler() :
118 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, DEFAULT_RESULT,
119 DEFAULT_ERROR_CODE, getOCRepresentationFromResource) }
123 RequestHandler::RequestHandler(const OCEntityHandlerResult& result, int errorCode) :
124 m_holder{ std::bind(doBuildResponse, std::placeholders::_1, result, errorCode,
125 getOCRepresentationFromResource) }
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)) }
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))) }
143 std::shared_ptr< OC::OCResourceResponse > RequestHandler::buildResponse(
144 RCSResourceObject& resource)
146 return m_holder(resource);
150 SetRequestHandler::SetRequestHandler() :
155 SetRequestHandler::SetRequestHandler(const OCEntityHandlerResult& result, int errorCode) :
156 RequestHandler{ result, errorCode }
161 SetRequestHandler::SetRequestHandler(const RCSResourceAttributes& attrs,
162 const OCEntityHandlerResult& result, int errorCode) :
163 RequestHandler{ attrs, result, errorCode }
167 SetRequestHandler::SetRequestHandler(RCSResourceAttributes&& attrs,
168 const OCEntityHandlerResult& result, int errorCode) :
169 RequestHandler{ std::move(attrs), result, errorCode }
173 AttrKeyValuePairs SetRequestHandler::applyAcceptanceMethod(
174 RCSSetResponse::AcceptanceMethod method, RCSResourceObject& resource,
175 const RCSResourceAttributes& requestAttrs) const
177 return getApplyAcceptanceFunc(method)(resource, requestAttrs);