summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/element/response/ResponseElement.cpp
blob: ce25036cf951e2bc99eff6a8ca35a9c266a921d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "ResponseElement.h"
#include "uscxml/plugins/invoker/http/HTTPServletInvoker.h"
#include <glog/logging.h>

#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif

namespace uscxml {

#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool connect(pluma::Host& host) {
	host.add( new ResponseElementProvider() );
	return true;
}
#endif

boost::shared_ptr<ExecutableContentImpl> ResponseElement::create(Interpreter* interpreter) {
  boost::shared_ptr<ResponseElement> invoker = boost::shared_ptr<ResponseElement>(new ResponseElement());
	invoker->_interpreter = interpreter;
	return invoker;
}

void ResponseElement::enterElement(const Arabica::DOM::Node<std::string>& node) {
  if (!HAS_ATTR(node, "request") && !HAS_ATTR(node, "requestexpr")) {
    LOG(ERROR) << "Response element requires request or requestexpr";
    return;
  }
  if (HAS_ATTR(node, "requestexpr") && !_interpreter->getDataModel()) {
    LOG(ERROR) << "Response element with requestexpr requires datamodel";
    return;
  }
  if (HAS_ATTR(node, "close")) {
    
  }
  
  std::string requestId = (HAS_ATTR(node, "request") ? ATTR(node, "request") : _interpreter->getDataModel().evalAsString(ATTR(node, "requestexpr")));

  HTTPServletInvoker* servlet = _interpreter->getHTTPServlet();
  tthread::lock_guard<tthread::recursive_mutex> lock(servlet->getMutex());
  
  if (servlet->getRequests().find(requestId) == servlet->getRequests().end()) {
    LOG(ERROR) << "No matching HTTP request for response element";
    return;
  }
  
  std::string statusStr = (HAS_ATTR(node, "status") ? ATTR(node, "status") : "200");
  if (!isNumeric(statusStr.c_str(), 10)) {
    LOG(ERROR) << "Response element with non-numeric status " << statusStr;
    return;
  }
  int status = strTo<int>(statusStr);

  HTTPServer::Request httpReq = servlet->getRequests()[requestId];

  HTTPServer::Reply httpReply(httpReq);
  httpReply.status = status;
  
  HTTPServer::reply(httpReply);
  servlet->getRequests().erase(requestId);
}

void ResponseElement::exitElement(const Arabica::DOM::Node<std::string>& node) {
  
}

}