2 Created on Aug 15, 2017
15 from robot_library import DcaeVariables
18 from cStringIO import StringIO
20 from StringIO import StringIO
22 class DMaaPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
24 DEFAULT_SUCCES_RESPONSE_CODE=200
25 succes_response_code=DEFAULT_SUCCES_RESPONSE_CODE
27 def __init__(self, dmaap_simulator, *args):
28 self.dmaap_simulator = dmaap_simulator
29 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args)
32 if 'POST' not in self.requestline:
35 resp_code = self.parse_the_posted_data()
38 self.send_successful_response()
40 self.send_response(resp_code)
42 def parse_the_posted_data(self):
43 topic = self.extract_topic_from_path()
44 content_len = self.get_content_length()
45 post_body = self.rfile.read(content_len)
46 post_body = self.get_json_part_of_post_body(post_body)
47 event = "{\"" + topic + "\":" + post_body + "}"
48 if self.dmaap_simulator.enque_event(event):
51 print "enque event fails"
55 def get_json_part_of_post_body(self, post_body):
56 indx = post_body.index("{")
58 post_body = post_body[indx:]
61 def extract_topic_from_path(self):
62 return self.path["/events/".__len__():]
64 def get_content_length(self):
65 return int(self.headers.getheader('content-length', 0))
67 def send_successful_response(self):
68 if 'clientThrottlingState' in self.requestline:
69 self.send_response(204)
71 self.send_response(self.succes_response_code)
72 self.send_header('Content-Type', 'application/json')
74 self.wfile.write("{'count': 1, 'serverTimeMs': 3}")