Example usage for org.apache.thrift.protocol TCompactProtocol TCompactProtocol

List of usage examples for org.apache.thrift.protocol TCompactProtocol TCompactProtocol

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TCompactProtocol TCompactProtocol.

Prototype

public TCompactProtocol(TTransport transport) 

Source Link

Document

Create a TCompactProtocol.

Usage

From source file:org.thriftzmq.TZMQSimpleServerTest.java

License:Apache License

/**
 * Test of echo method/*from   ww  w .ja va 2 s  .  c  om*/
 */
@Test
public void testEcho() throws TException, InterruptedException {
    logger.info("echo");
    TZMQSimpleServer server = createServer(TCP_ENDPOINT);
    server.startAndWait();
    TTransport clientTransport = TZMQClientFactory.create(context, TCP_ENDPOINT);
    Service1.Client client = new Service1.Client(new TCompactProtocol(clientTransport));
    clientTransport.open();
    String s = "abcdABCD";
    String r = client.echo(s);
    assertEquals(s, r);
    clientTransport.close();
    server.stopAndWait();
}

From source file:org.thriftzmq.TZMQSimpleServerTest.java

License:Apache License

/**
 * Test of echo method with long argument
 */// www .  j a v a2  s .c  o m
@Test
public void testEchoLong() throws TException, InterruptedException {
    logger.info("echoLong");
    TZMQSimpleServer server = createServer(TCP_ENDPOINT);
    server.startAndWait();
    TTransport clientTransport = TZMQClientFactory.create(context, TCP_ENDPOINT);
    Service1.Client client = new Service1.Client(new TCompactProtocol(clientTransport));
    clientTransport.open();
    //String s = "abcdABCD";
    int l = 1024 * 1024;
    char c[] = new char[l];
    Random rand = new Random(12345);
    for (int i = 0; i < l; i++) {
        c[i] = (char) (rand.nextInt(0x80 - 0x20) + 0x20);
    }
    String s = new String(c);
    String r = client.echo(s);
    assertEquals(s, r);
    clientTransport.close();
    server.stopAndWait();
}

From source file:org.thriftzmq.TZMQSimpleServerTest.java

License:Apache License

/**
 * Test of voidMethod method//from  w  ww  .  j  a  v  a  2  s. c o m
 */
@Test
public void testVoidMethod() throws TException, InterruptedException {
    logger.info("voidMethod");
    TZMQSimpleServer server = createServer(TCP_ENDPOINT);
    server.startAndWait();
    TTransport clientTransport = TZMQClientFactory.create(context, TCP_ENDPOINT);
    Service1.Client client = new Service1.Client(new TCompactProtocol(clientTransport));
    clientTransport.open();
    String s = "abcdABCD";
    client.voidMethod(s);
    clientTransport.close();
    server.stopAndWait();
}

From source file:org.wso2.carbon.apimgt.gateway.handlers.security.thrift.ThriftAuthClient.java

License:Apache License

public String getSessionId(String userName, String password) throws AuthenticationException {
    try {/*  w  w w.j a  va  2s .  c o m*/
        //if (sessionId == null) {
        TProtocol protocol = new TCompactProtocol(client);
        AuthenticatorService.Client authClient = new AuthenticatorService.Client(protocol);
        client.open();
        sessionId = authClient.authenticate(userName, password);
        client.close();
        //}
    } catch (TTransportException e) {
        throw new AuthenticationException("Error in authenticating with thrift client..");
    } catch (TException e) {
        throw new AuthenticationException("Error in authenticating with thrift client..");
    } catch (AuthenticationException e) {
        throw new AuthenticationException("Error in authenticating with thrift client..");
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return sessionId;
}

From source file:org.wso2.carbon.databridge.agent.internal.endpoint.thrift.client.ThriftClientPoolFactory.java

License:Open Source License

@Override
public Object createClient(String protocol, String hostName, int port) throws DataEndpointException {
    if (protocol.equalsIgnoreCase(DataEndpointConfiguration.Protocol.TCP.toString())) {

        TTransport receiverTransport = null;
        receiverTransport = new TSocket(hostName, port);

        TProtocol tProtocol = new TBinaryProtocol(receiverTransport);
        ThriftEventTransmissionService.Client client = new ThriftEventTransmissionService.Client(tProtocol);
        try {/*from w  ww .  j av a 2s  .  c  o m*/
            receiverTransport.open();
        } catch (TTransportException e) {
            throw new DataEndpointException("Error while making the connection." + e.getMessage(), e);
        }

        return client;
    } else {
        THttpClient client = null;
        try {
            client = new THttpClient("http://" + hostName + ":" + port + "/thriftReceiver");
            TProtocol tProtocol = new TCompactProtocol(client);
            ThriftEventTransmissionService.Client publisherClient = new ThriftEventTransmissionService.Client(
                    tProtocol);
            client.open();
            return publisherClient;
        } catch (TTransportException e) {
            throw new DataEndpointException("Error while making the connection." + e.getMessage(), e);
        }
    }
}

From source file:org.wso2.carbon.databridge.agent.thrift.internal.pool.client.general.ClientPoolFactory.java

License:Open Source License

@Override
public ThriftEventTransmissionService.Client makeObject(Object key) throws TTransportException {
    String[] keyElements = key.toString().split(AgentConstants.SEPARATOR);
    if (keyElements[0].equals(ReceiverConfiguration.Protocol.TCP.toString())) {

        String[] hostNameAndPort = keyElements[1].split(AgentConstants.HOSTNAME_AND_PORT_SEPARATOR);

        TTransport receiverTransport = null;
        try {//www  . j a va 2  s  .co m
            receiverTransport = new TSocket(HostAddressFinder.findAddress(hostNameAndPort[0]),
                    Integer.parseInt(hostNameAndPort[1]));
        } catch (SocketException ignored) {
            //already checked
        }
        TProtocol protocol = new TBinaryProtocol(receiverTransport);
        ThriftEventTransmissionService.Client client = new ThriftEventTransmissionService.Client(protocol);
        receiverTransport.open();

        return client;
    } else {
        THttpClient client = new THttpClient("http://" + keyElements[1] + "/thriftReceiver");
        TProtocol protocol = new TCompactProtocol(client);
        ThriftEventTransmissionService.Client publisherClient = new ThriftEventTransmissionService.Client(
                protocol);
        client.open();
        return publisherClient;
    }
}

From source file:org.wso2.carbon.databridge.agent.thrift.internal.pool.client.secure.SecureClientPoolFactory.java

License:Open Source License

@Override
public ThriftSecureEventTransmissionService.Client makeObject(Object key)
        throws AgentSecurityException, TTransportException {
    String[] keyElements = key.toString().split(AgentConstants.SEPARATOR);
    if (keyElements[2].equals(ReceiverConfiguration.Protocol.TCP.toString())) {
        if (params == null) {
            if (trustStore == null) {
                trustStore = System.getProperty("javax.net.ssl.trustStore");
                if (trustStore == null) {
                    throw new AgentSecurityException("No trustStore found");
                }/*  w  w  w  .ja v a  2s.co m*/
                // trustStore = "/home/suho/projects/wso2/trunk/carbon/distribution/product/modules/distribution/target/wso2carbon-4.0.0-SNAPSHOT/repository/resources/security/client-truststore.jks";
            }

            if (trustStorePassword == null) {
                trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
                if (trustStorePassword == null) {
                    throw new AgentSecurityException("No trustStore password found");
                }
                //trustStorePassword = "wso2carbon";
            }

            params = new TSSLTransportFactory.TSSLTransportParameters();
            params.setTrustStore(trustStore, trustStorePassword);
        }

        String[] hostNameAndPort = keyElements[3].split(AgentConstants.HOSTNAME_AND_PORT_SEPARATOR);

        TTransport receiverTransport = null;
        try {
            receiverTransport = TSSLTransportFactory.getClientSocket(
                    HostAddressFinder.findAddress(hostNameAndPort[0]), Integer.parseInt(hostNameAndPort[1]), 0,
                    params);
        } catch (SocketException ignored) {
            //already checked
        }

        TProtocol protocol = new TBinaryProtocol(receiverTransport);
        return new ThriftSecureEventTransmissionService.Client(protocol);
    } else {
        try {
            TrustManager easyTrustManager = new X509TrustManager() {
                public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                        throws java.security.cert.CertificateException {
                }

                public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                        throws java.security.cert.CertificateException {
                }

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            String[] hostNameAndPort = keyElements[3].split(AgentConstants.HOSTNAME_AND_PORT_SEPARATOR);

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { easyTrustManager }, null);
            SSLSocketFactory sf = new SSLSocketFactory(sslContext);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme httpsScheme = new Scheme("https", sf, Integer.parseInt(hostNameAndPort[1]));

            DefaultHttpClient client = new DefaultHttpClient();
            client.getConnectionManager().getSchemeRegistry().register(httpsScheme);

            THttpClient tclient = new THttpClient("https://" + keyElements[3] + "/securedThriftReceiver",
                    client);
            TProtocol protocol = new TCompactProtocol(tclient);
            ThriftSecureEventTransmissionService.Client authClient = new ThriftSecureEventTransmissionService.Client(
                    protocol);
            tclient.open();
            return authClient;
        } catch (Exception e) {
            throw new AgentSecurityException("Cannot create Secure client for " + keyElements[3], e);
        }
    }
}

From source file:org.wso2.carbon.identity.entitlement.pep.agent.thrift.Authenticator.java

License:Open Source License

private boolean authenticate() throws Exception {
    boolean isAuthenticated;
    try {//from   w  w  w. ja v  a  2s  .  c o m
        THttpClient client = new THttpClient(serverUrl);
        TProtocol protocol = new TCompactProtocol(client);
        AuthenticatorService.Client authClient = new AuthenticatorService.Client(protocol);
        client.open();
        sessionId = authClient.authenticate(userName, password);
        client.close();
        isAuthenticated = true;
    } catch (TException e) {
        throw new EntitlementAgentException("Error while authenticating with ThriftAuthenticator", e);
    }
    return isAuthenticated;

}

From source file:org.wso2.carbon.identity.entitlement.proxy.thrift.Authenticator.java

License:Open Source License

private boolean authenticate() throws EntitlementProxyException {
    boolean isAuthenticated;
    try {/*from   w ww  . j ava  2 s  . c om*/
        THttpClient client = new THttpClient(serverUrl);
        TProtocol protocol = new TCompactProtocol(client);
        AuthenticatorService.Client authClient = new AuthenticatorService.Client(protocol);
        client.open();
        sessionId = authClient.authenticate(userName, password);
        client.close();
        isAuthenticated = true;
    } catch (Exception e) {
        throw new EntitlementProxyException("Error while authenticating with ThriftAuthenticator", e);
    }
    return isAuthenticated;

}

From source file:org.wso2.carbon.identity.thrift.authentication.client.internal.pool.SecureClientPoolFactory.java

License:Open Source License

@Override
public AuthenticatorService.Client makeObject(Object key)
        throws ThriftAuthenticationException, TTransportException {
    String[] keyElements = constructKeyElements((String) key);
    if (keyElements[0].equals(ThriftAuthenticationClient.Protocol.SSL.toString())) {
        if (params == null) {
            if (trustStore == null) {
                trustStore = System.getProperty("javax.net.ssl.trustStore");
                if (trustStore == null) {
                    throw new ThriftAuthenticationException("No trustStore found");
                }//ww w  .  j a  va 2  s  . c  o m
            }

            if (trustStorePassword == null) {
                trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
                if (trustStorePassword == null) {
                    throw new ThriftAuthenticationException("No trustStore password found");
                }
                //trustStorePassword = "wso2carbon";
            }

            params = new TSSLTransportFactory.TSSLTransportParameters();
            params.setTrustStore(trustStore, trustStorePassword);
        }

        TTransport receiverTransport = TSSLTransportFactory.getClientSocket(keyElements[1],
                Integer.parseInt(keyElements[2]), 0, params);

        TProtocol protocol = new TBinaryProtocol(receiverTransport);
        return new AuthenticatorService.Client(protocol);
    } else {
        try {
            TrustManager easyTrustManager = new X509TrustManager() {
                public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                        throws java.security.cert.CertificateException {
                }

                public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                        throws java.security.cert.CertificateException {
                }

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            //                String[] hostNameAndPort = keyElements[3].split(ThriftAuthenticationClientConstants.HOSTNAME_AND_PORT_SEPARATOR);

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { easyTrustManager }, null);
            SSLSocketFactory sf = new SSLSocketFactory(sslContext);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme httpsScheme = new Scheme("https", sf, Integer.parseInt(keyElements[2]));

            DefaultHttpClient client = new DefaultHttpClient();
            client.getConnectionManager().getSchemeRegistry().register(httpsScheme);

            THttpClient tclient = new THttpClient(
                    "https://" + keyElements[1] + ":" + keyElements[2] + "/thriftAuthenticator", client);
            TProtocol protocol = new TCompactProtocol(tclient);
            AuthenticatorService.Client authClient = new AuthenticatorService.Client(protocol);
            tclient.open();
            return authClient;
        } catch (Exception e) {
            throw new ThriftAuthenticationException(
                    "Cannot create Secure client for " + keyElements[1] + ":" + keyElements[2], e);
        }
    }
}