Search in sources :

Example 46 with THttpClient

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

the class TestThrift2HttpServer method talkToThriftServer.

@Override
protected void talkToThriftServer(String url, int customHeaderSize) throws Exception {
    THttpClient httpClient = new THttpClient(url);
    httpClient.open();
    if (customHeaderSize > 0) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < customHeaderSize; i++) {
            sb.append("a");
        }
        httpClient.setCustomHeader("User-Agent", sb.toString());
    }
    try {
        TProtocol prot;
        prot = new TBinaryProtocol(httpClient);
        THBaseService.Client client = new THBaseService.Client(prot);
        TTableName tTableName = new TTableName();
        tTableName.setNs(Bytes.toBytes(""));
        tTableName.setQualifier(Bytes.toBytes(TABLENAME));
        if (!tableCreated) {
            Assert.assertTrue(!client.tableExists(tTableName));
            TTableDescriptor tTableDescriptor = new TTableDescriptor();
            tTableDescriptor.setTableName(tTableName);
            TColumnFamilyDescriptor columnFamilyDescriptor = new TColumnFamilyDescriptor();
            columnFamilyDescriptor.setName(Bytes.toBytes(TABLENAME));
            tTableDescriptor.addToColumns(columnFamilyDescriptor);
            client.createTable(tTableDescriptor, new ArrayList<>());
            tableCreated = true;
        }
        Assert.assertTrue(client.tableExists(tTableName));
    } finally {
        httpClient.close();
    }
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TTableName(org.apache.hadoop.hbase.thrift2.generated.TTableName) TProtocol(org.apache.thrift.protocol.TProtocol) TColumnFamilyDescriptor(org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor) THttpClient(org.apache.thrift.transport.THttpClient) TTableDescriptor(org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor) THttpClient(org.apache.thrift.transport.THttpClient) THBaseService(org.apache.hadoop.hbase.thrift2.generated.THBaseService)

Example 47 with THttpClient

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

the class TestHs2ConnectionMetricsHttp method getHttpClient.

private TCLIService.Client getHttpClient() throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    Map<String, String> headers = new HashMap<>();
    headers.put("Connection", "close");
    httpClient.addRequestInterceptor(new BasicHttpRequestInterceptor(USERNAME, PASSWORD, null, null, false, headers));
    TTransport transport = new THttpClient(getHttpUrl(), httpClient);
    TProtocol protocol = new TBinaryProtocol(transport);
    return new TCLIService.Client(protocol);
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) HashMap(java.util.HashMap) TProtocol(org.apache.thrift.protocol.TProtocol) THttpClient(org.apache.thrift.transport.THttpClient) TTransport(org.apache.thrift.transport.TTransport) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) THttpClient(org.apache.thrift.transport.THttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 48 with THttpClient

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

the class TestHttpCookieAuthenticationTest method getCookieStoreFromConnection.

// ((InternalHttpClient) ((THttpClient) ((HiveConnection) connection).transport).client).cookieStore.getCookies()
private CookieStore getCookieStoreFromConnection(Connection connection) throws Exception {
    CookieStore cookieStore = null;
    if (connection instanceof HiveConnection) {
        HiveConnection hiveConnection = (HiveConnection) connection;
        Field transportField = hiveConnection.getClass().getDeclaredField("transport");
        transportField.setAccessible(true);
        TTransport transport = (TTransport) transportField.get(hiveConnection);
        if (transport instanceof THttpClient) {
            THttpClient httpTransport = (THttpClient) transport;
            Field clientField = httpTransport.getClass().getDeclaredField("client");
            clientField.setAccessible(true);
            HttpClient httpClient = (HttpClient) clientField.get(httpTransport);
            Field cookieStoreField = httpClient.getClass().getDeclaredField("cookieStore");
            cookieStoreField.setAccessible(true);
            cookieStore = (CookieStore) cookieStoreField.get(httpClient);
        }
    }
    return cookieStore;
}
Also used : CookieStore(org.apache.http.client.CookieStore) Field(java.lang.reflect.Field) THttpClient(org.apache.thrift.transport.THttpClient) HttpClient(org.apache.http.client.HttpClient) THttpClient(org.apache.thrift.transport.THttpClient) TTransport(org.apache.thrift.transport.TTransport) HiveConnection(org.apache.hive.jdbc.HiveConnection)

Example 49 with THttpClient

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

the class TestThriftHttpCLIServiceFeatures method testCustomCookies.

/**
 * Test additional http headers passed to request interceptor.
 * @throws Exception
 */
@Test
public void testCustomCookies() throws Exception {
    TTransport transport;
    DefaultHttpClient hClient = new DefaultHttpClient();
    String httpUrl = getHttpUrl();
    Map<String, String> additionalHeaders = new HashMap<String, String>();
    Map<String, String> cookieHeaders = new HashMap<String, String>();
    cookieHeaders.put("key1", "value1");
    cookieHeaders.put("key2", "value2");
    HttpBasicAuthInterceptorWithLogging authInt = new HttpBasicAuthInterceptorWithLogging(ThriftCLIServiceTest.USERNAME, ThriftCLIServiceTest.PASSWORD, null, null, false, additionalHeaders, cookieHeaders);
    hClient.addRequestInterceptor(authInt);
    transport = new THttpClient(httpUrl, hClient);
    TCLIService.Client httpClient = getClient(transport);
    // Create a new open session request object
    TOpenSessionReq openReq = new TOpenSessionReq();
    httpClient.OpenSession(openReq).getSessionHandle();
    String cookieHeader = authInt.getCookieHeader();
    assertTrue(cookieHeader.contains("key1=value1"));
    assertTrue(cookieHeader.contains("key2=value2"));
}
Also used : HashMap(java.util.HashMap) THttpClient(org.apache.thrift.transport.THttpClient) TTransport(org.apache.thrift.transport.TTransport) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TCLIService(org.apache.hive.service.rpc.thrift.TCLIService) Test(org.junit.Test)

Example 50 with THttpClient

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

the class TestThriftHttpCLIServiceFeatures method testAdditionalHttpHeaders.

/**
 * Test additional http headers passed to request interceptor.
 * @throws Exception
 */
@Test
public void testAdditionalHttpHeaders() throws Exception {
    TTransport transport;
    DefaultHttpClient hClient = new DefaultHttpClient();
    String httpUrl = getHttpUrl();
    Map<String, String> additionalHeaders = new HashMap<String, String>();
    additionalHeaders.put("key1", "value1");
    additionalHeaders.put("key2", "value2");
    HttpBasicAuthInterceptorWithLogging authInt = new HttpBasicAuthInterceptorWithLogging(ThriftCLIServiceTest.USERNAME, ThriftCLIServiceTest.PASSWORD, null, null, false, additionalHeaders, null);
    hClient.addRequestInterceptor(authInt);
    transport = new THttpClient(httpUrl, hClient);
    TCLIService.Client httpClient = getClient(transport);
    // Create a new open session request object
    TOpenSessionReq openReq = new TOpenSessionReq();
    httpClient.OpenSession(openReq).getSessionHandle();
    ArrayList<String> headers = authInt.getRequestHeaders();
    for (String h : headers) {
        assertTrue(h.contains("key1:value1"));
        assertTrue(h.contains("key2:value2"));
    }
}
Also used : HashMap(java.util.HashMap) THttpClient(org.apache.thrift.transport.THttpClient) TTransport(org.apache.thrift.transport.TTransport) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TCLIService(org.apache.hive.service.rpc.thrift.TCLIService) Test(org.junit.Test)

Aggregations

THttpClient (org.apache.thrift.transport.THttpClient)51 TProtocol (org.apache.thrift.protocol.TProtocol)34 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)18 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)12 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)9 Test (org.junit.Test)9 TTransport (org.apache.thrift.transport.TTransport)8 Hbase (org.apache.hadoop.hbase.thrift.generated.Hbase)5 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)5 RequestData (org.simbasecurity.api.service.thrift.RequestData)5 ApacheHttpTransport (com.google.api.client.http.apache.ApacheHttpTransport)4 IOException (java.io.IOException)4 PServiceCall (net.morimekta.providence.PServiceCall)4 TestService (net.morimekta.test.providence.service.TestService)4 Client (org.simbasecurity.api.service.thrift.AuthenticationFilterService.Client)4 HashMap (java.util.HashMap)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 TCLIService (org.apache.hive.service.rpc.thrift.TCLIService)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 ActionDescriptor (org.simbasecurity.api.service.thrift.ActionDescriptor)3