2 Created on Aug 15, 2017
15 from robot_library import DcaeVariables
18 from cStringIO import StringIO
20 from StringIO import StringIO
23 class DMaaPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
25 def __init__(self, dmaap_simulator, *args):
26 self.dmaap_simulator = dmaap_simulator
27 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args)
30 if 'POST' not in self.requestline:
33 resp_code = self.parse_the_posted_data()
36 self.send_successful_response()
38 self.send_response(resp_code)
40 def parse_the_posted_data(self):
41 topic = self.extract_topic_from_path()
42 content_len = self.get_content_length()
43 post_body = self.rfile.read(content_len)
44 post_body = self.get_json_part_of_post_body(post_body)
45 event = "{\"" + topic + "\":" + post_body + "}"
46 if self.dmaap_simulator.enque_event(event):
49 print "enque event fails"
53 def get_json_part_of_post_body(self, post_body):
54 indx = post_body.index("{")
56 post_body = post_body[indx:]
59 def extract_topic_from_path(self):
60 return self.path["/events/".__len__():]
62 def get_content_length(self):
63 return int(self.headers.getheader('content-length', 0))
65 def send_successful_response(self):
66 if 'clientThrottlingState' in self.requestline:
67 self.send_response(204)
69 self.send_response(200)
70 self.send_header('Content-Type', 'application/json')
72 self.wfile.write("{'count': 1, 'serverTimeMs': 3}")