Search in sources :

Example 11 with TTransport

use of org.apache.thrift.transport.TTransport in project jstorm by alibaba.

the class KerberosSaslTransportPlugin method connect.

@Override
public TTransport connect(TTransport transport, String serverHost, String asUser) throws TTransportException, IOException {
    // create an authentication callback handler
    ClientCallbackHandler client_callback_handler = new ClientCallbackHandler(login_conf);
    // login our user
    Login login = null;
    try {
        // specify a configuration object to be used
        Configuration.setConfiguration(login_conf);
        // now login
        login = new Login(AuthUtils.LOGIN_CONTEXT_CLIENT, client_callback_handler);
    } catch (LoginException ex) {
        LOG.error("Server failed to login in principal:" + ex, ex);
        throw new RuntimeException(ex);
    }
    final Subject subject = login.getSubject();
    if (subject.getPrivateCredentials(KerberosTicket.class).isEmpty()) {
        // error
        throw new RuntimeException("Fail to verify user principal with section \"" + AuthUtils.LOGIN_CONTEXT_CLIENT + "\" in login configuration file " + login_conf);
    }
    final String principal = StringUtils.isBlank(asUser) ? getPrincipal(subject) : asUser;
    String serviceName = AuthUtils.get(login_conf, AuthUtils.LOGIN_CONTEXT_CLIENT, "serviceName");
    if (serviceName == null) {
        serviceName = AuthUtils.SERVICE;
    }
    Map<String, String> props = new TreeMap<String, String>();
    props.put(Sasl.QOP, "auth");
    props.put(Sasl.SERVER_AUTH, "false");
    LOG.debug("SASL GSSAPI client transport is being established");
    final TTransport sasalTransport = new TSaslClientTransport(KERBEROS, principal, serviceName, serverHost, props, null, transport);
    // open Sasl transport with the login credential
    try {
        Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {

            public Void run() {
                try {
                    LOG.debug("do as:" + principal);
                    sasalTransport.open();
                } catch (Exception e) {
                    LOG.error("Client failed to open SaslClientTransport to interact with a server during session initiation: " + e, e);
                }
                return null;
            }
        });
    } catch (PrivilegedActionException e) {
        throw new RuntimeException(e);
    }
    return sasalTransport;
}
Also used : KerberosTicket(javax.security.auth.kerberos.KerberosTicket) PrivilegedActionException(java.security.PrivilegedActionException) TSaslClientTransport(org.apache.thrift.transport.TSaslClientTransport) Login(org.apache.zookeeper.Login) TreeMap(java.util.TreeMap) Subject(javax.security.auth.Subject) LoginException(javax.security.auth.login.LoginException) TTransportException(org.apache.thrift.transport.TTransportException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) LoginException(javax.security.auth.login.LoginException) TTransport(org.apache.thrift.transport.TTransport)

Example 12 with TTransport

use of org.apache.thrift.transport.TTransport in project lucida by claritylab.

the class QAClient method main.

public static void main(String[] args) {
    // Collect the port number.
    int port = 8083;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }
    // User.
    String LUCID = "Clinc";
    QuerySpec create_spec = new QuerySpec();
    // Knowledge.
    final QueryInput knowledge_text = createQueryInput("text", "Clinc is created by Jason and Lingjia.", "1234567");
    final QueryInput knowledge_url = createQueryInput("url", "https://en.wikipedia.org/wiki/Apple_Inc.", "abcdefg");
    final QuerySpec knowledge = createQuerySpec("knowledge", new ArrayList<QueryInput>() {

        {
            add(knowledge_text);
            add(knowledge_url);
        }
    });
    // Unlearn.
    final QueryInput knowledge_unlearn_input = createQueryInput("unlearn", "", "abcdefg");
    final QuerySpec knowledge_unlearn_spec = createQuerySpec("unlearn knowledge", new ArrayList<QueryInput>() {

        {
            add(knowledge_unlearn_input);
        }
    });
    // Query.
    final QueryInput query_input = createQueryInput("text", "Who created Clinc?", "");
    final QuerySpec query = createQuerySpec("query", new ArrayList<QueryInput>() {

        {
            add(query_input);
        }
    });
    // Initialize thrift objects.
    // TTransport transport = new TSocket("clarity08.eecs.umich.edu", port);
    TTransport transport = new TSocket("localhost", port);
    TProtocol protocol = new TBinaryProtocol(new TFramedTransport(transport));
    LucidaService.Client client = new LucidaService.Client(protocol);
    try {
        // Talk to the server.
        transport.open();
        System.out.println("///// Connecting to OpenEphyra at port " + port + " ... /////");
        // Learn and ask.
        client.create(LUCID, create_spec);
        client.learn(LUCID, knowledge);
        System.out.println("///// Query input: /////");
        System.out.println(query_input.data.get(0));
        String answer = client.infer(LUCID, query);
        // Print the answer.
        System.out.println("///// Answer: /////");
        System.out.println(answer);
        // Unlearn and ask again.
        client.learn(LUCID, knowledge_unlearn_spec);
        System.out.println("///// Query input: /////");
        System.out.println(query_input.data.get(0));
        answer = client.infer(LUCID, query);
        // Print the answer.
        System.out.println("///// Answer: /////");
        System.out.println(answer);
        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 13 with TTransport

use of org.apache.thrift.transport.TTransport in project storm by apache.

the class TBackoffConnect method doConnectWithRetry.

public TTransport doConnectWithRetry(ITransportPlugin transportPlugin, TTransport underlyingTransport, String host, String asUser) throws IOException {
    boolean connected = false;
    TTransport transportResult = null;
    while (!connected) {
        try {
            transportResult = transportPlugin.connect(underlyingTransport, host, asUser);
            connected = true;
        } catch (TTransportException ex) {
            retryNext(ex);
        }
    }
    return transportResult;
}
Also used : TTransportException(org.apache.thrift.transport.TTransportException) TTransport(org.apache.thrift.transport.TTransport)

Example 14 with TTransport

use of org.apache.thrift.transport.TTransport in project hbase by apache.

the class HttpDoAsClient method run.

private void run() throws Exception {
    TTransport transport = new TSocket(host, port);
    transport.open();
    String url = "http://" + host + ":" + port;
    THttpClient httpClient = new THttpClient(url);
    httpClient.open();
    TProtocol protocol = new TBinaryProtocol(httpClient);
    Hbase.Client client = new Hbase.Client(protocol);
    byte[] t = bytes("demo_table");
    //
    // Scan all tables, look for the demo table and delete it.
    //
    System.out.println("scanning tables...");
    for (ByteBuffer name : refresh(client, httpClient).getTableNames()) {
        System.out.println("  found: " + utf8(name.array()));
        if (utf8(name.array()).equals(utf8(t))) {
            if (refresh(client, httpClient).isTableEnabled(name)) {
                System.out.println("    disabling table: " + utf8(name.array()));
                refresh(client, httpClient).disableTable(name);
            }
            System.out.println("    deleting table: " + utf8(name.array()));
            refresh(client, httpClient).deleteTable(name);
        }
    }
    //
    // Create the demo table with two column families, entry: and unused:
    //
    ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
    ColumnDescriptor col;
    col = new ColumnDescriptor();
    col.name = ByteBuffer.wrap(bytes("entry:"));
    col.timeToLive = Integer.MAX_VALUE;
    col.maxVersions = 10;
    columns.add(col);
    col = new ColumnDescriptor();
    col.name = ByteBuffer.wrap(bytes("unused:"));
    col.timeToLive = Integer.MAX_VALUE;
    columns.add(col);
    System.out.println("creating table: " + utf8(t));
    try {
        refresh(client, httpClient).createTable(ByteBuffer.wrap(t), columns);
    } catch (AlreadyExists ae) {
        System.out.println("WARN: " + ae.message);
    }
    System.out.println("column families in " + utf8(t) + ": ");
    Map<ByteBuffer, ColumnDescriptor> columnMap = refresh(client, httpClient).getColumnDescriptors(ByteBuffer.wrap(t));
    for (ColumnDescriptor col2 : columnMap.values()) {
        System.out.println("  column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions));
    }
    transport.close();
    httpClient.close();
}
Also used : ColumnDescriptor(org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor) ArrayList(java.util.ArrayList) THttpClient(org.apache.thrift.transport.THttpClient) ByteBuffer(java.nio.ByteBuffer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) TTransport(org.apache.thrift.transport.TTransport) THttpClient(org.apache.thrift.transport.THttpClient) AlreadyExists(org.apache.hadoop.hbase.thrift.generated.AlreadyExists) Hbase(org.apache.hadoop.hbase.thrift.generated.Hbase) TSocket(org.apache.thrift.transport.TSocket)

Example 15 with TTransport

use of org.apache.thrift.transport.TTransport in project hive by apache.

the class TestTCTLSeparatedProtocol method testShouldThrowRunTimeExceptionIfUnableToInitializeTokenizer.

public void testShouldThrowRunTimeExceptionIfUnableToInitializeTokenizer() throws Exception {
    TCTLSeparatedProtocol separatedProtocol = new TCTLSeparatedProtocol(new TTransport() {

        @Override
        public void close() {
        }

        @Override
        public boolean isOpen() {
            return false;
        }

        @Override
        public void open() throws TTransportException {
        }

        @Override
        public int read(byte[] buf, int off, int len) throws TTransportException {
            throw new TTransportException();
        }

        @Override
        public void write(byte[] buf, int off, int len) throws TTransportException {
        }
    });
    separatedProtocol.initialize(null, new Properties());
    try {
        separatedProtocol.readStructBegin();
        fail("Runtime Exception is expected if the intialization of tokenizer failed.");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TTransportException);
    }
}
Also used : TTransportException(org.apache.thrift.transport.TTransportException) TTransport(org.apache.thrift.transport.TTransport) Properties(java.util.Properties) TTransportException(org.apache.thrift.transport.TTransportException) TCTLSeparatedProtocol(org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol)

Aggregations

TTransport (org.apache.thrift.transport.TTransport)99 TSocket (org.apache.thrift.transport.TSocket)43 TProtocol (org.apache.thrift.protocol.TProtocol)42 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)36 TFramedTransport (org.apache.thrift.transport.TFramedTransport)28 TTransportException (org.apache.thrift.transport.TTransportException)20 Test (org.junit.Test)20 TException (org.apache.thrift.TException)18 IOException (java.io.IOException)12 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 ArrayList (java.util.ArrayList)9 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)8 HashMap (java.util.HashMap)6 Socket (java.net.Socket)5 TCLIService (org.apache.hive.service.rpc.thrift.TCLIService)5 THttpClient (org.apache.thrift.transport.THttpClient)4 TSaslClientTransport (org.apache.thrift.transport.TSaslClientTransport)4 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)3 Request (com.alibaba.dubbo.remoting.exchange.Request)3 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)3