1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 "request_sender.h"
22 #include "simulator_logger.h"
23 #include "simulator_utils.h"
26 #define TAG "GET_REQUEST_SNDR"
28 RequestSender::RequestSender(RequestType type, std::shared_ptr<OC::OCResource> &ocResource)
29 : m_type(type), m_ocResource(ocResource) {}
31 void RequestSender::sendRequest(const std::map<std::string, std::string> &queryParam,
32 ResponseCallback responseCb, bool verifyResponse)
34 sendRequest(std::string(), queryParam, nullptr, responseCb, verifyResponse);
37 void RequestSender::sendRequest(const std::string &interfaceType,
38 const std::map<std::string, std::string> &queryParam,
39 ResponseCallback responseCb, bool verifyResponse)
41 sendRequest(interfaceType, queryParam, nullptr, responseCb, verifyResponse);
44 void RequestSender::sendRequest(const std::map<std::string, std::string> &queryParam,
45 SimulatorResourceModelSP resourceModel,
46 ResponseCallback responseCb, bool verifyResponse)
48 sendRequest(std::string(), queryParam, resourceModel, responseCb, verifyResponse);
51 void RequestSender::sendRequest(const std::string &interfaceType,
52 const std::map<std::string, std::string> &queryParam,
53 SimulatorResourceModelSP resourceModel,
54 ResponseCallback responseCb, bool verifyResponse)
56 // Add query paramter "if" if interfaceType is not empty
57 OC::QueryParamsMap queryParamCpy(queryParam);
58 if (!interfaceType.empty())
59 queryParamCpy["if"] = interfaceType;
61 // Add the request into request list
62 RequestDetailSP requestDetail(new RequestDetail);
63 requestDetail->type = m_type;
64 requestDetail->queryParam = queryParamCpy;
65 requestDetail->body = resourceModel;
66 requestDetail->verifyResponse = verifyResponse;
67 requestDetail->responseCb = responseCb;
69 int requestId = m_requestList.add(requestDetail);
71 OCStackResult ocResult = send(queryParamCpy, resourceModel, std::bind(
72 &RequestSender::onResponseReceived, this,
73 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, requestId));
74 if (OC_STACK_OK != ocResult)
76 OIC_LOG_V(ERROR, TAG, "Sending request failed [errorcode: %d]", ocResult);
77 m_requestList.remove(requestId);
78 throw SimulatorException(static_cast<SimulatorResult>(ocResult), "Failed to send request!");
82 void RequestSender::setRequestModel(const RequestModelSP &requestModel)
84 m_requestModel = requestModel;
87 void RequestSender::onResponseReceived(const OC::HeaderOptions &headerOptions,
88 const OC::OCRepresentation &rep, const int errorCode, int requestId)
90 SIM_LOG(ILogger::INFO, "Response received..." << "\n" << getPayloadString(rep));
92 // Ignore the response recieved for invalid requests
93 RequestDetailSP request = m_requestList.remove(requestId);
97 // Validate the response as per the schema given by RAML
98 ValidationStatus validationStatus {false, SIMULATOR_ERROR};
99 if (request->verifyResponse && m_requestModel
100 && !errorCode) // TODO: Validate responses other than "200"
102 validationStatus.errorCode = m_requestModel->validateResponse(200, rep);
103 validationStatus.isVerified = true;
106 SimulatorResourceModelSP resourceModel = std::make_shared<SimulatorResourceModel>(
107 SimulatorResourceModel::build(rep));
108 request->responseCb(static_cast<SimulatorResult>(errorCode), resourceModel);
111 GETRequestSender::GETRequestSender(std::shared_ptr<OC::OCResource> &ocResource)
112 : RequestSender(RequestType::RQ_TYPE_GET, ocResource) {}
114 OCStackResult GETRequestSender::send(OC::QueryParamsMap &queryParams,
115 SimulatorResourceModelSP &resourceModel, OC::GetCallback callback)
117 SIM_LOG(ILogger::INFO, "Sending GET request..." << "\n**Payload Details**\n" << getRequestString(
120 return m_ocResource->get(queryParams, callback);
123 PUTRequestSender::PUTRequestSender(std::shared_ptr<OC::OCResource> &ocResource)
124 : RequestSender(RequestType::RQ_TYPE_PUT, ocResource) {}
126 OCStackResult PUTRequestSender::send(OC::QueryParamsMap &queryParams,
127 SimulatorResourceModelSP &resourceModel, OC::GetCallback callback)
129 OC::OCRepresentation ocRep;
131 ocRep = resourceModel->getOCRepresentation();
133 SIM_LOG(ILogger::INFO, "Sending PUT request..." << "\n**Payload Details**\n" << getRequestString(
134 queryParams, ocRep));
135 return m_ocResource->put(ocRep, queryParams, callback);
138 POSTRequestSender::POSTRequestSender(std::shared_ptr<OC::OCResource> &ocResource)
139 : RequestSender(RequestType::RQ_TYPE_POST, ocResource) {}
141 OCStackResult POSTRequestSender::send(OC::QueryParamsMap &queryParams,
142 SimulatorResourceModelSP &resourceModel, OC::GetCallback callback)
144 OC::OCRepresentation ocRep;
146 ocRep = resourceModel->getOCRepresentation();
148 SIM_LOG(ILogger::INFO, "Sending POST request..." << "\n**Payload Details**\n" << getRequestString(
149 queryParams, ocRep));
150 return m_ocResource->post(ocRep, queryParams, callback);