protected TProtocol requestTransport(String url) throws TTransportException { // probably not thread safe, but we need it? Not atm. TTransport act; if (!activeTransports.containsKey(url)) { logger.log(Level.DEBUG ,"Creating new transport for: " + url); activeTransports.put(url, new THttpClient(url)); } act = activeTransports.get(url); if (!act.isOpen()) { act.open(); } // THINK: always create new protocol? return new TJSONProtocol(act); }
@Test public void testThriftClient() throws TException, IOException, Failure { ApacheHttpTransport transport = new ApacheHttpTransport(); THttpClient httpClient = new THttpClient(endpoint().toString(), transport.getHttpClient()); TBinaryProtocol protocol = new TBinaryProtocol(httpClient); net.morimekta.test.thrift.service.TestService.Iface client = new net.morimekta.test.thrift.service.TestService.Client(protocol); doAnswer(i -> new Response("reply")) .when(impl) .test(any(Request.class)); net.morimekta.test.thrift.service.Response response = client.test(new net.morimekta.test.thrift.service.Request("call")); assertThat(response.getText(), is("reply")); verify(impl).test(any(Request.class)); verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class)); verifyNoMoreInteractions(impl, instrumentation); }
@Test public void testThriftClient_void() throws TException, IOException, Failure { ApacheHttpTransport transport = new ApacheHttpTransport(); THttpClient httpClient = new THttpClient(endpoint().toString(), transport.getHttpClient()); TBinaryProtocol protocol = new TBinaryProtocol(httpClient); net.morimekta.test.thrift.service.TestService.Iface client = new net.morimekta.test.thrift.service.TestService.Client(protocol); AtomicBoolean called = new AtomicBoolean(); doAnswer(i -> { called.set(true); return null; }).when(impl).voidMethod(55); client.voidMethod(55); waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).untilTrue(called); verify(impl).voidMethod(55); verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class)); verifyNoMoreInteractions(impl, instrumentation); }
@Test public void testThriftClient_oneway() throws TException, IOException, Failure { ApacheHttpTransport transport = new ApacheHttpTransport(); THttpClient httpClient = new THttpClient(endpoint().toString(), transport.getHttpClient()); TBinaryProtocol protocol = new TBinaryProtocol(httpClient); net.morimekta.test.thrift.service.TestService.Iface client = new net.morimekta.test.thrift.service.TestService.Client(protocol); AtomicBoolean called = new AtomicBoolean(); doAnswer(i -> { called.set(true); return null; }).when(impl).ping(); client.ping(); waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).untilTrue(called); verify(impl).ping(); verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), isNull()); verifyNoMoreInteractions(impl, instrumentation); }
@Test public void testThriftClient_failure() throws TException, IOException, Failure { ApacheHttpTransport transport = new ApacheHttpTransport(); THttpClient httpClient = new THttpClient(endpoint().toString(), transport.getHttpClient()); TBinaryProtocol protocol = new TBinaryProtocol(httpClient); net.morimekta.test.thrift.service.TestService.Iface client = new net.morimekta.test.thrift.service.TestService.Client(protocol); AtomicBoolean called = new AtomicBoolean(); doAnswer(i -> { called.set(true); throw new Failure("test"); }).when(impl).voidMethod(55); try { client.voidMethod(55); } catch (net.morimekta.test.thrift.service.Failure e) { assertEquals("test", e.getText()); } waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).untilTrue(called); verify(impl).voidMethod(55); verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class)); verifyNoMoreInteractions(impl, instrumentation); }
@Override protected TTransport newTransport(String uri, HttpHeaders headers) throws TTransportException { final SSLContext sslContext; try { sslContext = SSLContextBuilder.create() .loadTrustMaterial((TrustStrategy) (chain, authType) -> true) .build(); } catch (GeneralSecurityException e) { throw new TTransportException("failed to initialize an SSL context", e); } final THttpClient client = new THttpClient( uri, HttpClientBuilder.create() .setSSLContext(sslContext) .build()); client.setCustomHeaders( headers.names().stream() .collect(toImmutableMap(AsciiString::toString, name -> String.join(", ", headers.getAll(name))))); return client; }
private boolean authenticate() throws EntitlementProxyException { boolean isAuthenticated; try { 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; }
private void talkToThriftServer(int customHeaderSize) throws Exception { THttpClient httpClient = new THttpClient( "http://"+ HConstants.LOCALHOST + ":" + port); 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); Hbase.Client client = new Hbase.Client(prot); if (!tableCreated){ TestThriftServer.createTestTables(client); tableCreated = true; } TestThriftServer.checkTableList(client); } finally { httpClient.close(); } }
private Hbase.Client refresh(Hbase.Client client, THttpClient httpClient) { httpClient.setCustomHeader("doAs", doAsUser); if(secure) { try { httpClient.setCustomHeader("Authorization", generateTicket()); } catch (GSSException e) { e.printStackTrace(); } } return client; }
@Override protected TTransport getTransport(String serverHost, int serverPort) throws STException { try { return new THttpClient(serverHost); } catch (TTransportException e) { throw new STException("Get http transport failed !", e); } }
@Before public void setUp() throws Exception { TTransport transport = new THttpClient("http://localhost:" + port + "/api"); TProtocol protocol = protocolFactory.getProtocol(transport); client = new TGreetingService.Client(protocol); }
public static void main(String[] args) throws Exception { THttpClient transport = new THttpClient("http://localhost:8080/api"); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); PingPongService.Client client = new PingPongService.Client(new TMultiplexedProtocol(protocol, "PingPongService")); Ping ping = new Ping(); ping.setMessage("Hello"); Pong pong = client.knock(ping); LoggerFactory.getLogger(Client.class).info("Got answer: "+pong.getAnswer()); }
private AwesomeService.Client createClient() throws TTransportException { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http"); uriBuilder.setHost("localhost"); uriBuilder.setPort(SERVER_PORT); TTransport transport = new THttpClient(uriBuilder.toString()); transport.open(); TProtocol protocol = new TJSONProtocol(transport); return new AwesomeService.Client(protocol); }
private Hbase.Client refresh(Hbase.Client client, THttpClient httpClient) { if(secure) { httpClient.setCustomHeader("doAs", "hbase"); try { httpClient.setCustomHeader("Authorization", generateTicket()); } catch (GSSException e) { e.printStackTrace(); } } return client; }
private TTransport openTransport(RNodeAddress addr, SocketListener socketListener) throws ConnectionException { if (addr.isSetHttpAddr()) { try { // TODO #32: Integrate SocketListener into HTTP connections. return new THttpClient(addr.getHttpAddr().getUrl()); } catch (TTransportException e) { throw new ConnectionException("Could not open connection to " + addr, e); } } TTransport transport = new DiqubeClientSocket(addr.getDefaultAddr().getHost(), addr.getDefaultAddr().getPort(), socketTimeout, socketListener); return new TFramedTransport(transport); }
public static void main(String[] args) throws IOException, TException { THttpClient trans = new THttpClient("http://localhost:8080/thrift-servlet"); TJSONProtocol proto = new TJSONProtocol(trans); TradeHistory.Client client = new TradeHistory.Client(proto); for (int i = 0; i < 1000000; i++) { trans.open(); TradeReport tr = client.get_last_sale("AAPL"); trans.close(); } }
/** * Gets the t protocol. * * @param connectionUrl the connection url * @param connectionTimeout the connection timeout * @return the t protocol * @throws Exception the exception */ private static TProtocol getTProtocol(String connectionUrl, int connectionTimeout) throws Exception { try { THttpClient client = new THttpClient(connectionUrl); client.setConnectTimeout(connectionTimeout); TTransport transport = client; transport.open(); TProtocol protocol = new TJSONProtocol(transport); return protocol; } catch(Exception ex) { logger.error(ex.getMessage(), ex); throw ex; } }
protected TTransport getTransport() throws TTransportException { return new THttpClient(getServiceUrl(), httpClientUtil.getHttpClient()); }
private AuroraAdmin.Client getClient(HttpClient httpClient) throws TTransportException { final TTransport httpClientTransport = new THttpClient(formatUrl(API_PATH), httpClient); addTearDown(httpClientTransport::close); return new AuroraAdmin.Client(new TJSONProtocol(httpClientTransport)); }
private ProjectService.Iface getThriftProjectClient() throws TTransportException { THttpClient thriftClient = new THttpClient(thriftServerUrl + "/projects/thrift"); TProtocol protocol = new TCompactProtocol(thriftClient); return new ProjectService.Client(protocol); }
private LicenseService.Iface getThriftLicenseClient() throws TTransportException { THttpClient thriftClient = new THttpClient(thriftServerUrl + "/licenses/thrift"); TProtocol protocol = new TCompactProtocol(thriftClient); return new LicenseService.Client(protocol); }
private ComponentService.Iface getThriftComponentClient() throws TTransportException { THttpClient thriftClient = new THttpClient(thriftServerUrl + "/components/thrift"); TProtocol protocol = new TCompactProtocol(thriftClient); return new ComponentService.Client(protocol); }
private UserService.Iface getThriftUserClient() throws TTransportException { THttpClient thriftClient = new THttpClient(thriftServerUrl + "/users/thrift"); TProtocol protocol = new TCompactProtocol(thriftClient); return new UserService.Client(protocol); }
private VendorService.Iface getThriftVendorClient() throws TTransportException { THttpClient thriftClient = new THttpClient(thriftServerUrl + "/vendors/thrift"); TProtocol protocol = new TCompactProtocol(thriftClient); return new VendorService.Client(protocol); }
@Override public LoginResult login(@Nonnull String id, @Nonnull String password, @Nullable String certificate, @Nullable LoginCallback loginCallback) throws Exception { this.id = id; this.password = password; this.certificate = certificate; IdentityProvider provider; JsonNode sessionInfo; String sessionKey; boolean keepLoggedIn = true; String accessLocation = this.ip; // Login to LINE server. if (id.matches(EMAIL_REGEX)) { provider = IdentityProvider.LINE; // LINE sessionInfo = getJsonResult(LINE_SESSION_LINE_URL); } else { provider = IdentityProvider.NAVER_KR; // NAVER sessionInfo = getJsonResult(LINE_SESSION_NAVER_URL); } sessionKey = sessionInfo.get("session_key").asText(); String message = (char) (sessionKey.length()) + sessionKey + (char) (id.length()) + id + (char) (password.length()) + password; String[] keyArr = sessionInfo.get("rsa_key").asText().split(","); String keyName = keyArr[0]; String n = keyArr[1]; String e = keyArr[2]; BigInteger modulus = new BigInteger(n, 16); BigInteger pubExp = new BigInteger(e, 16); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp); RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] enBytes = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)); String encryptString = Hex.encodeHexString(enBytes); THttpClient transport = new THttpClient(LINE_HTTP_URL, httpClient); transport.open(); LoginResult result; TProtocol protocol = new TCompactProtocol(transport); this.client = new TalkService.Client(protocol); result = this.client.loginWithIdentityCredentialForCertificate(provider, keyName, encryptString, keepLoggedIn, accessLocation, this.systemName, this.certificate); if (result.getType() == LoginResultType.REQUIRE_DEVICE_CONFIRM) { setAuthToken(result.getVerifier()); if (loginCallback != null) { loginCallback.onDeviceConfirmRequired(result.getPinCode()); } else { throw new Exception("Device confirmation is required. Please set " + LoginCallback.class.getSimpleName() + " to get the pin code"); } // await for pinCode to be certified, it will return a verifier afterward. loginWithVerifierForCertificate(); } else if (result.getType() == LoginResultType.SUCCESS) { // if param certificate has passed certification setAuthToken(result.getAuthToken()); } // Once the client passed the verification, switch connection to HTTP_IN_URL loginWithAuthToken(getAuthToken()); return result; }
public void loginWithAuthToken(String authToken) throws Exception { THttpClient transport = new THttpClient(LINE_HTTP_IN_URL, httpClient); transport.setCustomHeader(X_LINE_ACCESS, authToken); transport.open(); TProtocol protocol = new TCompactProtocol(transport); setClient(new TalkService.Client(protocol)); setAuthToken(authToken); }
public AuthQrcode loginWithQrCode() throws Exception { // Request QrCode from LINE server. // Map<String, String> json = null; boolean keepLoggedIn = false; THttpClient transport = new THttpClient(LINE_HTTP_URL, httpClient); transport.open(); TProtocol protocol = new TCompactProtocol(transport); this.client = new TalkService.Client(protocol); AuthQrcode result = this.client.getAuthQrcode(keepLoggedIn, systemName); setAuthToken(result.getVerifier()); System.out.println("Retrieved QR Code."); return result; // await for QR code to be certified, it will return a verifier afterward. // loginWithVerifier(); }
public static TTransport openTransportForClient(String portAndAlias) throws TTransportException { final THttpClient transport = new THttpClient(getURLForPortAndAlias(portAndAlias)); transport.open(); return transport; }
public void setUp() throws Exception { TTransport transport = new THttpClient("http://localhost:" + port + "/greetings/api"); TProtocol protocol = protocolFactory.getProtocol(transport); client = new TGreetingExternalService.Client(protocol); }