Exemplo n.º 1
0
    def __init__(self, host, port, static_path, fetch_handler, search_handler,
                 search_proxy_handler):
        self.host = host
        self.port = port

        SearchServer.FETCH_HANDLER = fetch_handler
        SearchServer.STATIC_PATH = static_path

        fetch_processor = FetchCommunicationService.Processor(fetch_handler)
        fetch_pfactory = TJSONProtocol.TJSONProtocolFactory()
        SearchServer.FETCH_TSERVER = TServer.TServer(fetch_processor, None,
                                                     None, None,
                                                     fetch_pfactory,
                                                     fetch_pfactory)

        search_processor = SearchService.Processor(search_handler)
        search_pfactory = TJSONProtocol.TJSONProtocolFactory()
        SearchServer.SEARCH_TSERVER = TServer.TServer(search_processor, None,
                                                      None, None,
                                                      search_pfactory,
                                                      search_pfactory)

        search_proxy_processor = SearchProxyService.Processor(
            search_proxy_handler)
        search_proxy_pfactory = TJSONProtocol.TJSONProtocolFactory()
        SearchServer.SEARCH_PROXY_TSERVER = TServer.TServer(
            search_proxy_processor, None, None, None, search_proxy_pfactory,
            search_proxy_pfactory)
Exemplo n.º 2
0
def test_request_transport_integration():
    handler = ReadOnlySchedulerHandler()
    processor = ReadOnlyScheduler.Processor(handler)
    pfactory = TJSONProtocol.TJSONProtocolFactory()
    server = THttpServer.THttpServer(processor, ('localhost', 0), pfactory)
    server_thread = Thread(target=server.serve)
    server_thread.start()
    _, server_port = server.httpd.socket.getsockname()

    response = None

    try:
        transport = TRequestsTransport('http://localhost:%d' % server_port)
        protocol = TJSONProtocol.TJSONProtocol(transport)
        client = ReadOnlyScheduler.Client(protocol)
        response = client.getRoleSummary()
    finally:
        server.httpd.shutdown()

    assert response is not None
    assert response.responseCode == ResponseCode.OK
    assert response.serverInfo.clusterName == 'west'
    assert response.serverInfo.thriftAPIVersion == 3

    transport.close()
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(
        description=
        "Encode a Communication archive as a CSV file, where each row contains a "
        "TJSONProtocol encoded Communication",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        'comms_archive',
        help="A directory, TGZ file or Zip file of Communications")
    parser.add_argument(
        'csv_file',
        help="Output CSV file with TJSONProtocol encoded Communications")
    parser.add_argument('--column-name',
                        default='comm',
                        help="Name to use for CSV column header")
    args = parser.parse_args()

    csv_fh = open(args.csv_file, 'wb')

    fieldnames = [args.column_name]
    writer = unicodecsv.DictWriter(csv_fh,
                                   fieldnames,
                                   lineterminator='\n',
                                   quoting=unicodecsv.QUOTE_ALL)
    writer.writeheader()

    for (comm, filename) in CommunicationReader(args.comms_archive):
        json_communication = TSerialization.serialize(
            comm, TJSONProtocol.TJSONProtocolFactory()).decode('utf-8')
        writer.writerow({args.column_name: json_communication})
Exemplo n.º 4
0
def JSONToThrift(base,
                buf,
                protocol_factory=TJSONProtocol.TJSONProtocolFactory()):
    transport = TTransport.TMemoryBuffer(buf)
    protocol = protocol_factory.getProtocol(transport)
    base.read(protocol)
    return base
Exemplo n.º 5
0
    def __init__(self, host, port, static_path, fetch_handler, store_handler):
        self.host = host
        self.port = port

        AccessHTTPServer.STATIC_PATH = static_path

        AccessHTTPServer.FETCH_HANDLER = fetch_handler
        fetch_processor = FetchCommunicationService.Processor(fetch_handler)
        fetch_pfactory = TJSONProtocol.TJSONProtocolFactory()
        AccessHTTPServer.FETCH_TSERVER = TServer.TServer(
            fetch_processor, None, None, None, fetch_pfactory, fetch_pfactory)

        AccessHTTPServer.STORE_HANDLER = store_handler
        store_processor = StoreCommunicationService.Processor(store_handler)
        store_pfactory = TJSONProtocol.TJSONProtocolFactory()
        AccessHTTPServer.STORE_TSERVER = TServer.TServer(
            store_processor, None, None, None, store_pfactory, store_pfactory)
Exemplo n.º 6
0
def taxon_service():
    handler = TaxonService(services=get_services_dict())
    processor = thrift_service.Processor(handler)
    pfactory = TJSONProtocol.TJSONProtocolFactory()
    server_address = ('127.0.0.1', 9090)

    server = THttpServer.THttpServer(processor, server_address, pfactory)
    return server
Exemplo n.º 7
0
class JSONProtocolTest(AbstractTest, unittest.TestCase):
    protocol_factory = TJSONProtocol.TJSONProtocolFactory()

    def testNestedStructs(self):
        self._deserialize(NestedStructs, '{"1":{"rec":{}}"2":{"rec":{}}}')
        self._deserialize(NestedStructs, '{"1":{"rec":{}},"2":{"rec":{}}}')
        nested = NestedStructs(bonk=Bonk(), bools=Bools())
        protocol_factory = TJSONProtocol.TJSONProtocolFactory(validJSON=True)
        json.loads(Serializer.serialize(protocol_factory, nested))
def web_sync_service():
    processor = Web_Sync.Processor(handler)
    protocol = TJSONProtocol.TJSONProtocolFactory()
    server = TServer.TServer(processor, None, None, None, protocol, protocol)
    itrans = TTransport.TMemoryBuffer(request.data)
    otrans = TTransport.TMemoryBuffer()
    iprot = server.inputProtocolFactory.getProtocol(itrans)
    oprot = server.outputProtocolFactory.getProtocol(otrans)
    server.processor.process(iprot, oprot)
    return make_response(otrans.getvalue())
Exemplo n.º 9
0
 def test_alternative_protocol(self):
     """
     Ensure serialize will use a given thrift protocol factory.
     """
     bonk = Bonk(u"hello–world", 2)
     pf = TJSONProtocol.TJSONProtocolFactory()
     result = serialize(bonk, pf)
     self.assertContains(result, "hello")
     debonk = deserialize(Bonk(), result, pf)
     self.assertEqual(debonk.hello, u"hello–world")
     self.assertEqual(debonk.type, 2)
Exemplo n.º 10
0
    def do_POST(self):
        ''' handling thrift messages '''
        client_host, client_port = self.client_address
        LOG.debug('Processing request from: ' +
                  client_host + ':' +
                  str(client_port))

        # create new thrift handler
        checker_md_docs = self.server.checker_md_docs
        checker_md_docs_map = self.server.checker_md_docs_map
        suppress_handler = self.server.suppress_handler

        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
        input_protocol_factory = protocol_factory
        output_protocol_factory = protocol_factory

        itrans = TTransport.TFileObjectTransport(self.rfile)
        otrans = TTransport.TFileObjectTransport(self.wfile)
        itrans = TTransport.TBufferedTransport(itrans,
                                               int(self.headers['Content-Length']))
        otrans = TTransport.TMemoryBuffer()

        iprot = input_protocol_factory.getProtocol(itrans)
        oprot = output_protocol_factory.getProtocol(otrans)

        try:
            session = self.sc_session()
            acc_handler = ThriftRequestHandler(session,
                                               checker_md_docs,
                                               checker_md_docs_map,
                                               suppress_handler,
                                               self.db_version_info)

            processor = codeCheckerDBAccess.Processor(acc_handler)
            processor.process(iprot, oprot)
            result = otrans.getvalue()

            self.sc_session.remove()

            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return

        except Exception as exn:
            LOG.error(str(exn))
            self.send_error(404, "Request failed.")
            # self.send_header("content-type", "application/x-thrift")
            # self.end_headers()
            # self.wfile.write('')
            return
Exemplo n.º 11
0
 def testNestedStructs(self):
     nested = NestedStructs(bonk=Bonk(), bools=Bools())
     json.loads(self._serialize(nested))
     # Old protocol should be able to deserialize both valid and invalid
     # JSON.
     protocol_factory = TJSONProtocol.TJSONProtocolFactory(validJSON=False)
     Serializer.deserialize(protocol_factory,
                            '{"1":{"rec":{}}"2":{"rec":{}}}',
                            NestedStructs())
     Serializer.deserialize(protocol_factory,
                            '{"1":{"rec":{}},"2":{"rec":{}}}',
                            NestedStructs())
Exemplo n.º 12
0
def communicate():
    transport = THTTPTornadoTransport()
    pfactory = TJSONProtocol.TJSONProtocolFactory()
    client = Scrapper.Client(transport, pfactory)

    futures = [client.scrape('http://google.com/') for i in xrange(100)]

    try:
        yield futures
    except Exception as e:
        print e

    io_loop.stop()
Exemplo n.º 13
0
 def __init__(self, host, port, fetch_handler):
     """
     Args:
     - `host`:
     - `port`:
     - `fetch_handler`:
     """
     self.host = host
     self.port = port
     QuicklimeServer.FETCH_HANDLER = fetch_handler
     processor = FetchCommunicationService.Processor(fetch_handler)
     pfactory = TJSONProtocol.TJSONProtocolFactory()
     QuicklimeServer.TSERVER = TServer.TServer(processor, None, None, None,
                                               pfactory, pfactory)
Exemplo n.º 14
0
def communicate():
    transport = TAMQPTornadoTransport()
    pfactory = TJSONProtocol.TJSONProtocolFactory()
    client = Scrapper.Client(transport, pfactory)

    yield gen.Task(transport.open)

    futures = [client.scrape('http://google.com/') for i in xrange(100)]

    yield futures

    client._transport.close()

    io_loop.stop()
Exemplo n.º 15
0
    def set_factory(self):
        if settings.THRIFTSERVER_BUFFERED:
            self._tfactory = TTransport.TBufferedTransportFactory()
        if settings.THRIFTSERVER_ZLIB:
            self._tfactory = TZlibTransport.TZlibTransportFactory()
        if settings.THRIFTSERVER_FRAMED:
            self._tfactory = TTransport.TFramedTransportFactory()

        if settings.THRIFTSERVER_PROTOCOL == Protocol.BINARY:
            self._pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        elif settings.THRIFTSERVER_PROTOCOL == Protocol.COMPACT:
            self._pfactory = TCompactProtocol.TCompactProtocolFactory()
        elif settings.THRIFTSERVER_PROTOCOL == Protocol.JSON:
            self._pfactory = TJSONProtocol.TJSONProtocolFactory()
        else:
            raise RuntimeError(
                "invalid configuration for THRIFTSERVER_PROTOCOL: {}".format(
                    settings.THRIFTSERVER_PROTOCOL))
Exemplo n.º 16
0
def main():
    # Make stdout output UTF-8, preventing "'ascii' codec can't encode" errors
    sys.stdout = codecs.getwriter('utf8')(sys.stdout)

    parser = argparse.ArgumentParser(
        description="Pretty Print a Concrete file")
    parser.add_argument('--concrete_type', default='communication',
                        choices=['communication', 'tokenlattice'],
                        help='Default: communication')
    parser.add_argument('--protocol', default='simple',
                        choices=['simple', 'TJSONProtocol'],
                        help='Default: simple')
    parser.add_argument('--remove-timestamps', action='store_true',
                        help="Removes timestamps from JSON output")
    parser.add_argument('--remove-uuids', action='store_true',
                        help="Removes UUIDs from JSON output")
    parser.add_argument('concrete_file')
    parser.add_argument('json_file', nargs='?', default='STDOUT')
    concrete.version.add_argparse_argument(parser)
    args = parser.parse_args()

    if args.protocol == 'simple':
        if args.concrete_type == 'communication':
            json_communication = communication_file_to_json(
                args.concrete_file,
                remove_timestamps=args.remove_timestamps,
                remove_uuids=args.remove_uuids
            )
        else:
            json_communication = tokenlattice_file_to_json(args.concrete_file)
    else:
        if args.concrete_type == 'communication':
            comm = read_communication_from_file(args.concrete_file)
            json_communication = TSerialization.serialize(
                comm, TJSONProtocol.TJSONProtocolFactory())
        else:
            raise NotImplementedError

    if args.json_file == 'STDOUT':
        print json_communication
    else:
        f = codecs.open(args.json_file, "w", encoding="utf-8")
        f.write(json_communication)
        f.close()
Exemplo n.º 17
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('csv_file')
    parser.add_argument('comms_zip_file')
    parser.add_argument('--comm-field', default='Answer.modified_comm')
    args = parser.parse_args()

    csv_fh = open(args.csv_file, 'rb')
    reader = unicodecsv.DictReader(csv_fh)

    with CommunicationWriterZip(args.comms_zip_file) as writer:
        for row in reader:
            json_comm = row[args.comm_field]
            comm = Communication()
            TSerialization.deserialize(
                comm,
                json_comm.encode('utf-8'),
                protocol_factory=TJSONProtocol.TJSONProtocolFactory())
            writer.write(comm, comm.id + '.comm')
Exemplo n.º 18
0
def test_requests_transport_session_reuse():
  handler = ReadOnlySchedulerHandler()
  processor = ReadOnlyScheduler.Processor(handler)
  pfactory = TJSONProtocol.TJSONProtocolFactory()
  server = THttpServer.THttpServer(processor, ('localhost', 0), pfactory)
  server_thread = Thread(target=server.serve)
  server_thread.start()
  _, server_port = server.httpd.socket.getsockname()

  try:
    transport = TRequestsTransport('http://localhost:%d' % server_port)
    protocol = TJSONProtocol.TJSONProtocol(transport)
    client = ReadOnlyScheduler.Client(protocol)
    client.getRoleSummary()
    old_session = transport._session
    client.getRoleSummary()
  finally:
    server.httpd.shutdown()

  assert old_session == transport._session

  transport.close()
Exemplo n.º 19
0
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol import TJSONProtocol
from thrift.server import TServer
from simple import Message


class MessageHandler(Message.Iface):
    msgs = ["Apache Thrift!!", "Childhood is a short season", "'Twas brillig"]

    def __init__(self):
        self.msg_index = 0

    def motd(self):
        self.msg_index += 1
        print("Call count: %s" % self.msg_index)
        return MessageHandler.msgs[self.msg_index % 3]


if __name__ == "__main__":
    handler = MessageHandler()
    proc = Message.Processor(handler)
    trans_svr = TSocket.TServerSocket(port=9090)

    trans_fac = TTransport.TFramedTransportFactory()
    proto_fac = TJSONProtocol.TJSONProtocolFactory()

    server = TServer.TThreadedServer(proc, trans_svr, trans_fac, proto_fac)
    server.serve()
Exemplo n.º 20
0
def ThriftToJSON(thrift_object,
              protocol_factory=TJSONProtocol.TJSONProtocolFactory()):
    transport = TTransport.TMemoryBuffer()
    protocol = protocol_factory.getProtocol(transport)
    thrift_object.write(protocol)
    return transport.getvalue()
Exemplo n.º 21
0
def main(cfg):
    if cfg.unix:
        if cfg.addr == "":
            sys.exit("invalid listener unix domain socket: {}".format(cfg.addr))
    else:
        try:
            (host, port) = cfg.addr.rsplit(":", 1)
            port = int(port)
        except ValueError:
            sys.exit("invalid listener address: {}".format(cfg.addr))

    if cfg.response == "success":
        handler = SuccessHandler()
    elif cfg.response == "idl-exception":
        handler = IDLExceptionHandler()
    elif cfg.response == "exception":
        # squelch traceback for the exception we throw
        logging.getLogger().setLevel(logging.CRITICAL)
        handler = ExceptionHandler()
    else:
        sys.exit("unknown server response mode {0}".format(cfg.response))

    processor = Example.Processor(handler)
    if cfg.service is not None:
        # wrap processor with multiplexor
        multi = TMultiplexedProcessor.TMultiplexedProcessor()
        multi.registerProcessor(cfg.service, processor)
        processor = multi

    if cfg.unix:
        transport = TSocket.TServerSocket(unix_socket=cfg.addr)
    else:
        transport = TSocket.TServerSocket(host=host, port=port)

    if cfg.transport == "framed":
        transport_factory = TTransport.TFramedTransportFactory()
    elif cfg.transport == "unframed":
        transport_factory = TTransport.TBufferedTransportFactory()
    elif cfg.transport == "header":
        if cfg.protocol == "binary":
            transport_factory = THeaderTransport.THeaderTransportFactory(
                THeaderTransport.T_BINARY_PROTOCOL)
        elif cfg.protocol == "compact":
            transport_factory = THeaderTransport.THeaderTransportFactory(
                THeaderTransport.T_COMPACT_PROTOCOL)
        else:
            sys.exit("header transport cannot be used with protocol {0}".format(cfg.protocol))
    else:
        sys.exit("unknown transport {0}".format(cfg.transport))

    if cfg.protocol == "binary":
        protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
    elif cfg.protocol == "compact":
        protocol_factory = TCompactProtocol.TCompactProtocolFactory()
    elif cfg.protocol == "json":
        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
    else:
        sys.exit("unknown protocol {0}".format(cfg.protocol))

    print(
        "Thrift Server listening on {0} for {1} {2} requests".format(
            cfg.addr, cfg.transport, cfg.protocol))
    if cfg.service is not None:
        print("Thrift Server service name {0}".format(cfg.service))
    if cfg.response == "idl-exception":
        print("Thrift Server will throw IDL exceptions when defined")
    elif cfg.response == "exception":
        print("Thrift Server will throw Thrift exceptions for all messages")

    server = TServer.TThreadedServer(processor, transport, transport_factory, protocol_factory)
    try:
        server.serve()
    except KeyboardInterrupt:
        print
Exemplo n.º 22
0
        func_stat = FuncStat()
        func.func_stat = func_stat
        ystats.func_stats.append(func)

        func_stat.file = path
        func_stat.line = line
        func_stat.func_name = func_name
        func_stat.calls_count = cc
        func_stat.total_time = ct
        func_stat.own_time = tt

        func.callers = []
        for f, s in callers.items():
            caller_stat = FuncStat()
            func.callers.append(caller_stat)
            path, line, func_name = f
            cc, nc, tt, ct = s
            caller_stat.file = path
            caller_stat.line = line
            caller_stat.func_name = func_name
            caller_stat.calls_count = cc
            caller_stat.total_time = ct
            caller_stat.own_time = tt

    m.validate()

    data = TSerialization.serialize(m, TJSONProtocol.TJSONProtocolFactory())

    sys.stdout.write(data)
    sys.stdout.flush()
Exemplo n.º 23
0
class JSONProtocolTest(AbstractTest):
    protocol_factory = TJSONProtocol.TJSONProtocolFactory()
Exemplo n.º 24
0
    def encode_response(self, obj):
        trans = TTransport.TMemoryBuffer()
        prot = TJSONProtocol.TJSONProtocolFactory().getProtocol(trans)
        obj.write(prot)

        return trans.getvalue()
Exemplo n.º 25
0
    def do_POST(self):
        """
        Handles POST queries, which are usually Thrift messages.
        """

        client_host, client_port = self.client_address
        auth_session = self.__check_session_cookie()
        LOG.debug("%s:%s -- [%s] POST %s", client_host, str(client_port),
                  auth_session.user if auth_session else "Anonymous",
                  self.path)

        # Create new thrift handler.
        checker_md_docs = self.server.checker_md_docs
        checker_md_docs_map = self.server.checker_md_docs_map
        suppress_handler = self.server.suppress_handler
        version = self.server.version

        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
        input_protocol_factory = protocol_factory
        output_protocol_factory = protocol_factory

        itrans = TTransport.TFileObjectTransport(self.rfile)
        itrans = TTransport.TBufferedTransport(
            itrans, int(self.headers['Content-Length']))
        otrans = TTransport.TMemoryBuffer()

        iprot = input_protocol_factory.getProtocol(itrans)
        oprot = output_protocol_factory.getProtocol(otrans)

        if self.server.manager.is_enabled and \
                not self.path.endswith('/Authentication') and \
                not auth_session:
            # Bail out if the user is not authenticated...
            # This response has the possibility of melting down Thrift clients,
            # but the user is expected to properly authenticate first.

            LOG.debug(client_host + ":" + str(client_port) +
                      " Invalid access, credentials not found " +
                      "- session refused.")
            self.send_error(401)
            return

        # Authentication is handled, we may now respond to the user.
        try:
            product_endpoint, api_ver, request_endpoint = \
                routing.split_client_POST_request(self.path)

            product = None
            if product_endpoint:
                # The current request came through a product route, and not
                # to the main endpoint.
                product = self.server.get_product(product_endpoint)
                self.__check_prod_db(product)

            version_supported = routing.is_supported_version(api_ver)
            if version_supported:
                major_version, _ = version_supported

                if major_version == 6:
                    if request_endpoint == 'Authentication':
                        auth_handler = AuthHandler_v6(
                            self.server.manager, auth_session,
                            self.server.config_session)
                        processor = AuthAPI_v6.Processor(auth_handler)
                    elif request_endpoint == 'Products':
                        prod_handler = ProductHandler_v6(
                            self.server, auth_session,
                            self.server.config_session, product, version)
                        processor = ProductAPI_v6.Processor(prod_handler)
                    elif request_endpoint == 'CodeCheckerService':
                        # This endpoint is a product's report_server.
                        if not product:
                            error_msg = "Requested CodeCheckerService on a " \
                                         "nonexistent product: '{0}'." \
                                        .format(product_endpoint)
                            LOG.error(error_msg)
                            raise ValueError(error_msg)

                        if product_endpoint:
                            # The current request came through a
                            # product route, and not to the main endpoint.
                            product = self.server.get_product(product_endpoint)
                            self.__check_prod_db(product)

                        acc_handler = ReportHandler_v6(
                            self.server.manager, product.session_factory,
                            product, auth_session, self.server.config_session,
                            checker_md_docs, checker_md_docs_map,
                            suppress_handler, version)
                        processor = ReportAPI_v6.Processor(acc_handler)
                    else:
                        LOG.debug("This API endpoint does not exist.")
                        error_msg = "No API endpoint named '{0}'." \
                                    .format(self.path)
                        raise ValueError(error_msg)

            else:
                if request_endpoint == 'Authentication':
                    # API-version checking is supported on the auth endpoint.
                    handler = BadAPIHandler(api_ver)
                    processor = AuthAPI_v6.Processor(handler)
                else:
                    # Send a custom, but valid Thrift error message to the
                    # client requesting this action.
                    error_msg = "Incompatible client/server API." \
                                "API versions supported by this server {0}." \
                                .format(get_version_str())

                    raise ValueError(error_msg)

            processor.process(iprot, oprot)
            result = otrans.getvalue()

            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return

        except Exception as exn:
            # Convert every Exception to the proper format which can be parsed
            # by the Thrift clients expecting JSON responses.
            LOG.error(exn.message)
            import traceback
            traceback.print_exc()
            ex = TApplicationException(TApplicationException.INTERNAL_ERROR,
                                       exn.message)
            fname, _, seqid = iprot.readMessageBegin()
            oprot.writeMessageBegin(fname, TMessageType.EXCEPTION, seqid)
            ex.write(oprot)
            oprot.writeMessageEnd()
            oprot.trans.flush()
            result = otrans.getvalue()
            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return
    def do_POST(self):
        """ Handling thrift messages. """
        client_host, client_port = self.client_address
        LOG.debug('Processing request from: ' + client_host + ':' +
                  str(client_port))

        # Create new thrift handler.
        checker_md_docs = self.server.checker_md_docs
        checker_md_docs_map = self.server.checker_md_docs_map
        suppress_handler = self.server.suppress_handler

        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
        input_protocol_factory = protocol_factory
        output_protocol_factory = protocol_factory

        itrans = TTransport.TFileObjectTransport(self.rfile)
        otrans = TTransport.TFileObjectTransport(self.wfile)
        itrans = TTransport.TBufferedTransport(
            itrans, int(self.headers['Content-Length']))
        otrans = TTransport.TMemoryBuffer()

        iprot = input_protocol_factory.getProtocol(itrans)
        oprot = output_protocol_factory.getProtocol(otrans)

        sess_token = self.check_auth_in_request()
        if self.path != '/Authentication' and not sess_token:
            # Bail out if the user is not authenticated...
            # This response has the possibility of melting down Thrift clients,
            # but the user is expected to properly authenticate first.

            LOG.debug(client_host + ":" + str(client_port) +
                      " Invalid access, credentials not found " +
                      "- session refused.")
            self.send_response(401)
            self.send_header("Content-type", "text/plain")
            self.send_header("Content-length", str(0))
            self.end_headers()

            return

        # Authentication is handled, we may now respond to the user.
        try:
            session = self.sc_session()
            acc_handler = ThriftRequestHandler(session, checker_md_docs,
                                               checker_md_docs_map,
                                               suppress_handler,
                                               self.db_version_info)

            if self.path == '/Authentication':
                # Authentication requests must be routed to a different
                # handler.
                auth_handler = ThriftAuthHandler(self.manager, client_host,
                                                 sess_token)
                processor = codeCheckerAuthentication.Processor(auth_handler)
            else:
                acc_handler = ThriftRequestHandler(session, checker_md_docs,
                                                   checker_md_docs_map,
                                                   suppress_handler,
                                                   self.db_version_info)

                processor = codeCheckerDBAccess.Processor(acc_handler)

            processor.process(iprot, oprot)
            result = otrans.getvalue()

            self.sc_session.remove()

            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return

        except Exception as exn:
            LOG.error(str(exn))
            self.send_error(404, "Request failed.")
            return
Exemplo n.º 27
0
import sys
sys.path.append('gen-py')

from hello import HelloSvc
from thrift.protocol import TJSONProtocol
from thrift.server import THttpServer


class HelloSvcHandler:
    def hello_func(self):
        print "Hello Called"
        return "hello from Python"


processor = HelloSvc.Processor(HelloSvcHandler())
protoFactory = TJSONProtocol.TJSONProtocolFactory()
port = 9090
server = THttpServer.THttpServer(processor, ("localhost", port), protoFactory)
print "Python server running on port " + str(port)
server.serve()
Exemplo n.º 28
0
class JSONProtocolTest(AbstractTest, unittest.TestCase):
    protocol_factory = TJSONProtocol.TJSONProtocolFactory()
Exemplo n.º 29
0
    def do_POST(self):
        """
        Handles POST queries, which are usually Thrift messages.
        """

        client_host, client_port = self.client_address
        self.auth_session = self.__check_session_cookie()
        LOG.info("%s:%s -- [%s] POST %s", client_host, str(client_port),
                 self.auth_session.user if self.auth_session else "Anonymous",
                 self.path)

        # Create new thrift handler.
        checker_md_docs = self.server.checker_md_docs
        checker_md_docs_map = self.server.checker_md_docs_map
        version = self.server.version

        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
        input_protocol_factory = protocol_factory
        output_protocol_factory = protocol_factory

        itrans = TTransport.TFileObjectTransport(self.rfile)
        itrans = TTransport.TBufferedTransport(
            itrans, int(self.headers['Content-Length']))
        otrans = TTransport.TMemoryBuffer()

        iprot = input_protocol_factory.getProtocol(itrans)
        oprot = output_protocol_factory.getProtocol(otrans)

        if self.server.manager.is_enabled and \
                not self.path.endswith(('/Authentication',
                                        '/Configuration')) and \
                not self.auth_session:
            # Bail out if the user is not authenticated...
            # This response has the possibility of melting down Thrift clients,
            # but the user is expected to properly authenticate first.
            LOG.debug(
                "%s:%s Invalid access, credentials not found "
                "- session refused.", client_host, str(client_port))

            self.send_thrift_exception("Error code 401: Unauthorized!", iprot,
                                       oprot, otrans)
            return

        # Authentication is handled, we may now respond to the user.
        try:
            product_endpoint, api_ver, request_endpoint = \
                routing.split_client_POST_request(self.path)

            product = None
            if product_endpoint:
                # The current request came through a product route, and not
                # to the main endpoint.
                product = self.__check_prod_db(product_endpoint)

            version_supported = routing.is_supported_version(api_ver)
            if version_supported:
                major_version, _ = version_supported

                if major_version == 6:
                    if request_endpoint == 'Authentication':
                        auth_handler = AuthHandler_v6(
                            self.server.manager, self.auth_session,
                            self.server.config_session)
                        processor = AuthAPI_v6.Processor(auth_handler)
                    elif request_endpoint == 'Configuration':
                        conf_handler = ConfigHandler_v6(
                            self.auth_session, self.server.config_session)
                        processor = ConfigAPI_v6.Processor(conf_handler)
                    elif request_endpoint == 'Products':
                        prod_handler = ProductHandler_v6(
                            self.server, self.auth_session,
                            self.server.config_session, product, version)
                        processor = ProductAPI_v6.Processor(prod_handler)
                    elif request_endpoint == 'CodeCheckerService':
                        # This endpoint is a product's report_server.
                        if not product:
                            error_msg = "Requested CodeCheckerService on a " \
                                         "nonexistent product: '{0}'." \
                                        .format(product_endpoint)
                            LOG.error(error_msg)
                            raise ValueError(error_msg)

                        if product_endpoint:
                            # The current request came through a
                            # product route, and not to the main endpoint.
                            product = self.__check_prod_db(product_endpoint)

                        acc_handler = ReportHandler_v6(
                            self.server.manager, product.session_factory,
                            product, self.auth_session,
                            self.server.config_session, checker_md_docs,
                            checker_md_docs_map, version, self.server.context)
                        processor = ReportAPI_v6.Processor(acc_handler)
                    else:
                        LOG.debug("This API endpoint does not exist.")
                        error_msg = "No API endpoint named '{0}'." \
                                    .format(self.path)
                        raise ValueError(error_msg)

            else:
                error_msg = "The API version you are using is not supported " \
                            "by this server (server API version: {0})!".format(
                                get_version_str())
                self.send_thrift_exception(error_msg, iprot, oprot, otrans)
                return

            processor.process(iprot, oprot)
            result = otrans.getvalue()

            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return

        except Exception as exn:
            LOG.warning(str(exn))
            import traceback
            traceback.print_exc()

            cstringio_buf = itrans.cstringio_buf.getvalue()
            if cstringio_buf:
                itrans = TTransport.TMemoryBuffer(cstringio_buf)
                iprot = input_protocol_factory.getProtocol(itrans)

            self.send_thrift_exception(str(exn), iprot, oprot, otrans)
            return
Exemplo n.º 30
0
    def do_POST(self):
        """
        Handles POST queries, which are usually Thrift messages.
        """

        client_host, client_port = self.client_address
        auth_session = self.__check_auth_in_request()
        LOG.info("{0}:{1} -- [{2}] POST {3}"
                 .format(client_host,
                         str(client_port),
                         auth_session.user if auth_session else "Anonymous",
                         self.path))

        # Create new thrift handler.
        checker_md_docs = self.server.checker_md_docs
        checker_md_docs_map = self.server.checker_md_docs_map
        suppress_handler = self.server.suppress_handler
        version = self.server.version

        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
        input_protocol_factory = protocol_factory
        output_protocol_factory = protocol_factory

        itrans = TTransport.TFileObjectTransport(self.rfile)
        itrans = TTransport.TBufferedTransport(itrans,
                                               int(self.headers[
                                                   'Content-Length']))
        otrans = TTransport.TMemoryBuffer()

        iprot = input_protocol_factory.getProtocol(itrans)
        oprot = output_protocol_factory.getProtocol(otrans)

        if self.server.manager.isEnabled() and \
                not self.path.endswith('/Authentication') and \
                not auth_session:
            # Bail out if the user is not authenticated...
            # This response has the possibility of melting down Thrift clients,
            # but the user is expected to properly authenticate first.

            LOG.debug(client_host + ":" + str(client_port) +
                      " Invalid access, credentials not found " +
                      "- session refused.")
            self.send_error(401)
            return

        # Authentication is handled, we may now respond to the user.
        try:
            product_endpoint, api_ver, request_endpoint = \
                routing.split_client_POST_request(self.path)

            product = None
            if product_endpoint:
                # The current request came through a product route, and not
                # to the main endpoint.
                product = self.server.get_product(product_endpoint)
                if product and not product.connected:
                    # If the product is not connected, try reconnecting...
                    LOG.debug("Request's product '{0}' is not connected! "
                              "Attempting reconnect..."
                              .format(product_endpoint))
                    product.connect()

                    if not product.connected:
                        # If the reconnection fails, send an error to the user.
                        LOG.debug("Product reconnection failed.")
                        self.send_error(  # 500 Internal Server Error
                            500, "Product '{0}' database connection failed!"
                                 .format(product_endpoint))
                        return
                elif not product:
                    LOG.debug("Requested product does not exist.")
                    self.send_error(
                        404, "The product {0} does not exist."
                             .format(product_endpoint))
                    return

            version_supported = routing.is_supported_version(api_ver)
            if version_supported:
                major_version, _ = version_supported

                if major_version == 6:
                    if request_endpoint == 'Authentication':
                        auth_handler = AuthHandler_v6(
                            self.server.manager,
                            auth_session,
                            self.server.config_session)
                        processor = AuthAPI_v6.Processor(auth_handler)
                    elif request_endpoint == 'Products':
                        prod_handler = ProductHandler_v6(
                            self.server,
                            auth_session,
                            self.server.config_session,
                            product,
                            version)
                        processor = ProductAPI_v6.Processor(prod_handler)
                    elif request_endpoint == 'CodeCheckerService':
                        # This endpoint is a product's report_server.
                        if not product:
                            LOG.debug("Requested CodeCheckerService on a "
                                      "nonexistent product.")
                            self.send_error(  # 404 Not Found
                                404,
                                "The specified product '{0}' does not exist!"
                                .format(product_endpoint))
                            return

                        acc_handler = ReportHandler_v6(
                            product.session_factory,
                            product,
                            auth_session,
                            self.server.config_session,
                            checker_md_docs,
                            checker_md_docs_map,
                            suppress_handler,
                            version)
                        processor = ReportAPI_v6.Processor(acc_handler)
                    else:
                        LOG.debug("This API endpoint does not exist.")
                        self.send_error(404,  # 404 Not Fount
                                        "No API endpoint named '{0}'."
                                        .format(self.path))
                        return
            else:
                if request_endpoint == 'Authentication':
                    # API-version checking is supported on the auth endpoint.
                    handler = BadAPIHandler(api_ver)
                    processor = AuthAPI_v6.Processor(handler)
                else:
                    # Send a custom, but valid Thrift error message to the
                    # client requesting this action.
                    LOG.debug("API version v{0} not supported by server."
                              .format(api_ver))
                    self.send_error(400,  # 400 Bad Request
                                    "This API version 'v{0}' is not supported."
                                    .format(api_ver))
                    return

            processor.process(iprot, oprot)
            result = otrans.getvalue()

            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return

        except Exception as exn:
            import traceback
            traceback.print_exc()
            LOG.error(str(exn))
            self.send_error(404, "Request failed.")
            return