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, long stringLengthLimit, long containerLengthLimit) 

Source Link

Document

Create a TCompactProtocol.

Usage

From source file:org.apache.sentry.hdfs.SentryHDFSServiceClientDefaultImpl.java

License:Apache License

public SentryHDFSServiceClientDefaultImpl(Configuration conf) throws IOException {
    this.conf = conf;
    Preconditions.checkNotNull(this.conf, "Configuration object cannot be null");
    this.serverAddress = NetUtils.createSocketAddr(
            Preconditions.checkNotNull(conf.get(ClientConfig.SERVER_RPC_ADDRESS),
                    "Config key " + ClientConfig.SERVER_RPC_ADDRESS + " is required"),
            conf.getInt(ClientConfig.SERVER_RPC_PORT, ClientConfig.SERVER_RPC_PORT_DEFAULT));
    this.connectionTimeout = conf.getInt(ClientConfig.SERVER_RPC_CONN_TIMEOUT,
            ClientConfig.SERVER_RPC_CONN_TIMEOUT_DEFAULT);
    kerberos = ClientConfig.SECURITY_MODE_KERBEROS
            .equalsIgnoreCase(conf.get(ClientConfig.SECURITY_MODE, ClientConfig.SECURITY_MODE_KERBEROS).trim());
    transport = new TSocket(serverAddress.getHostName(), serverAddress.getPort(), connectionTimeout);
    if (kerberos) {
        String serverPrincipal = Preconditions.checkNotNull(conf.get(ClientConfig.PRINCIPAL),
                ClientConfig.PRINCIPAL + " is required");

        // Resolve server host in the same way as we are doing on server side
        serverPrincipal = SecurityUtil.getServerPrincipal(serverPrincipal, serverAddress.getAddress());
        LOGGER.info("Using server kerberos principal: " + serverPrincipal);

        serverPrincipalParts = SaslRpcServer.splitKerberosName(serverPrincipal);
        Preconditions.checkArgument(serverPrincipalParts.length == 3,
                "Kerberos principal should have 3 parts: " + serverPrincipal);
        boolean wrapUgi = "true".equalsIgnoreCase(conf.get(ClientConfig.SECURITY_USE_UGI_TRANSPORT, "true"));
        transport = new UgiSaslClientTransport(AuthMethod.KERBEROS.getMechanismName(), null,
                serverPrincipalParts[0], serverPrincipalParts[1], ClientConfig.SASL_PROPERTIES, null, transport,
                wrapUgi);//from  w ww.  ja v  a 2 s.  c  o  m
    } else {
        serverPrincipalParts = null;
    }
    try {
        transport.open();
    } catch (TTransportException e) {
        throw new IOException("Transport exception while opening transport: " + e.getMessage(), e);
    }
    LOGGER.info("Successfully opened transport: " + transport + " to " + serverAddress);
    TProtocol tProtocol = null;
    long maxMessageSize = conf.getLong(ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE,
            ServiceConstants.ClientConfig.SENTRY_HDFS_THRIFT_MAX_MESSAGE_SIZE_DEFAULT);
    if (conf.getBoolean(ClientConfig.USE_COMPACT_TRANSPORT, ClientConfig.USE_COMPACT_TRANSPORT_DEFAULT)) {
        tProtocol = new TCompactProtocol(transport, maxMessageSize, maxMessageSize);
    } else {
        tProtocol = new TBinaryProtocol(transport, maxMessageSize, maxMessageSize, true, true);
    }
    TMultiplexedProtocol protocol = new TMultiplexedProtocol(tProtocol,
            SentryHDFSServiceClient.SENTRY_HDFS_SERVICE_NAME);
    client = new SentryHDFSService.Client(protocol);
    LOGGER.info("Successfully created client");
}