/** * Build the Client used to make HTTP requests with the latest settings, * i.e. objectMapper and debugging. * TODO: better to use the Builder Pattern? */ public ApiClient rebuildHttpClient() { // Add the JSON serialization support to Jersey JacksonJsonProvider jsonProvider = new JacksonJsonProvider(objectMapper); DefaultClientConfig conf = new DefaultClientConfig(); conf.getSingletons().add(jsonProvider); Client client = Client.create(conf); if (debugging) { client.addFilter(new LoggingFilter()); } //to solve the issue of GET:metadata/:guid with accepted encodeing is 'gzip' //in the past, when clients use gzip header, actually it doesn't trigger a gzip encoding... So everything is fine //After the release, the content is return in gzip, while the sdk doesn't handle it correctly client.addFilter(new GZIPContentEncodingFilter(false)); this.httpClient = client; return this; }
@SuppressWarnings("unchecked") public <T> T create(Class<T> clientClass) { Preconditions.checkNotNull(this.namedClient, "NamedClient required"); Preconditions.checkNotNull( this.restAdapterConfig.getMessageSerializer(), "MessageSerializer required"); LOGGER.info("Using NamedClient {}", this.namedClient); LOGGER.info("Using MessageSerializer {}", this.restAdapterConfig.getMessageSerializer()); final RestClient restClient = (RestClient) ClientFactory .getNamedClient(namedClient); restClient.getJerseyClient().addFilter(new LoggerFilter()); if (this.restAdapterConfig.getEncoding() == Encoding.gzip) { restClient.getJerseyClient().addFilter( new GZIPContentEncodingFilter()); } InvocationHandler invocationHandler = new JerseyInvocationHandler( restClient, this.restAdapterConfig); Object proxyInstance = Proxy.newProxyInstance( ClassLoader.getSystemClassLoader(), new Class<?>[] { clientClass }, invocationHandler); return (T) proxyInstance; }
public BlockCypherHTTPClient() { super(); Client client; client= Client.create(); client.setConnectTimeout(StaticNumbers.CONNECT_TIMEOUT); client.setReadTimeout(StaticNumbers.READ_TIMEOUT); client.addFilter(new GZIPContentEncodingFilter(false)); balance = client.resource(prefix + "/addrs/" ); pushTx = client.resource(prefix + "/txs/push"); //transaction.path(path); }
@Test public void validateSignatureWhenContentIsPojo() throws Exception { Connection connection = null; try { // Start the server RequestConfiguration requestConfiguration = RequestConfiguration.builder().withApiKeyQueryParamName("passkey") .withSignatureHttpHeader("duck-duck-signature-header") .withTimestampHttpHeader("duck-duck-timestamp-header") .withVersionHttpHeader("duck-duck-version-header") .build(); ValidatingHttpServer server = new SignatureValidatingHttpServer(port, secretKey, requestConfiguration); connection = server.connect(); // Create a client with the filter that is under test Client client = createClient(); client.addFilter(new HmacClientFilter(apiKey, secretKey, client.getMessageBodyWorkers(), requestConfiguration)); client.addFilter(new GZIPContentEncodingFilter(true)); // Send a pizza in the request body Pizza pizza = new Pizza(); pizza.setTopping("olive"); client.resource(server.getUri()) .type(MediaType.APPLICATION_JSON_TYPE) .put(pizza); } finally { if (connection != null) { connection.close(); } } }
private Client buildDefaultClient() { Client client = new ApacheHttpClient4(buildClientHandler(buildHttpClient()), buildClientConfig()); client.setExecutorService(determineExecutorService()); client.addFilter(new GZIPContentEncodingFilter(true)); return client; }