List of usage examples for com.squareup.okhttp Callback Callback
Callback
From source file:es.upv.grycap.coreutils.fiber.test.Http2ClientTest.java
License:Apache License
private void submitGet(final Http2Client client, final String url, final Waiter waiter) { client.asyncGet(url, contentType, nocache, new Callback() { @Override/*from ww w . j a va 2s. c om*/ public void onResponse(final Response response) throws IOException { waiter.assertTrue(response.isSuccessful()); final String payload = response.body().source().readUtf8(); waiter.assertThat(payload, allOf(notNullValue(), not(equalTo("")))); if (path.contains("xml")) { isValidXml(payload, objectId); } else if (path.contains("json")) { isValidJson(payload, objectId); } waiter.resume(); } @Override public void onFailure(final Request request, final IOException throwable) { waiter.fail(throwable); } }); }
From source file:es.upv.grycap.coreutils.fiber.test.Http2ClientTest.java
License:Apache License
private void submitPost(final Http2Client client, final String url, final Waiter waiter) { Supplier<String> supplier = null; if (path.contains("xml")) { supplier = () -> "<object>Y</object>"; } else if (path.contains("json")) { supplier = () -> "{ \"object\" : \"X\" }"; }//from w w w.j a va 2 s. c o m client.asyncPost(url, ofNullable(contentType).orElse(newArrayList("application/json")).get(0), supplier, new Callback() { @Override public void onResponse(final Response response) throws IOException { waiter.assertTrue(response.isSuccessful()); waiter.resume(); } @Override public void onFailure(final Request request, final IOException throwable) { waiter.fail(throwable); } }); }
From source file:es.upv.grycap.coreutils.fiber.test.Http2ClientTest.java
License:Apache License
private void submitPut(final Http2Client client, final String url, final Waiter waiter) { Supplier<String> supplier = null; if (path.contains("xml")) { supplier = () -> "<object>Y</object>"; } else if (path.contains("json")) { supplier = () -> "{ \"object\" : \"X\" }"; }//from w ww. ja v a 2 s . c o m client.asyncPut(url, ofNullable(contentType).orElse(newArrayList("application/json")).get(0), supplier, new Callback() { @Override public void onResponse(final Response response) throws IOException { waiter.assertTrue(response.isSuccessful()); waiter.resume(); } @Override public void onFailure(final Request request, final IOException throwable) { waiter.fail(throwable); } }); }
From source file:es.upv.grycap.coreutils.fiber.test.Http2ClientTest.java
License:Apache License
private void submitDelete(final Http2Client client, final String url, final Waiter waiter) { client.asyncDelete(url, new Callback() { @Override// w w w . j a va 2s . c o m public void onResponse(final Response response) throws IOException { waiter.assertTrue(response.isSuccessful()); waiter.resume(); } @Override public void onFailure(final Request request, final IOException throwable) { waiter.fail(throwable); } }); }
From source file:es.upv.grycap.opengateway.core.http.VertxHttp2Client.java
License:Apache License
private Callback wrapCallback(final Handler<AsyncResult<HttpResponse>> resultHandler) { final Context context = vertx.getOrCreateContext(); return new Callback() { @Override//from w w w . j a v a2 s . c om public void onResponse(final Response response) throws IOException { context.runOnContext(v -> { if (!response.isSuccessful()) resultHandler.handle( failedFuture(new IOException(String.format("Unexpected code: %s", response)))); else resultHandler.handle(succeededFuture(new HttpResponse(response))); }); } @Override public void onFailure(final Request request, final IOException throwable) { context.runOnContext(v -> { resultHandler.handle(failedFuture( new IllegalStateException(String.format("Failed request: %s", request), throwable))); }); } }; }
From source file:es.upv.grycap.opengateway.examples.test.AppDaemonTest.java
License:Apache License
@BeforeClass public static void setUp() throws Exception { // install bridges to logging APIs in order to capture Hazelcast and Vert.x messages getLogManager().init();//w w w. j a v a2s .c om // start the daemon daemon = new AppDaemon(); daemon.init(new DaemonContext() { @Override public DaemonController getController() { return null; } @Override public String[] getArguments() { return null; } }); daemon.start(); daemon.awaitHealthy(30l, TimeUnit.SECONDS); uri = "http://localhost:" + port; // test that the server started final Waiter waiter = new Waiter(); http2Client().asyncGet(uri, newArrayList("text/html"), false, new Callback() { @Override public void onResponse(final Response response) throws IOException { waiter.assertTrue(response.isSuccessful()); waiter.assertThat(response.body().contentLength(), greaterThan(0l)); final String payload = response.body().source().readUtf8(); waiter.assertThat(payload, allOf(notNullValue(), not(equalTo("")))); waiter.resume(); } @Override public void onFailure(final Request request, final IOException throwable) { waiter.fail(throwable); } }); waiter.await(30l, TimeUnit.SECONDS); }
From source file:es.upv.grycap.opengateway.examples.test.AppDaemonTest.java
License:Apache License
private void testGetObject(final String id, final Waiter waiter) throws URISyntaxException, MalformedURLException { final Http2Client client = isolatedHttp2Client(); client.asyncGetJson(String.format("%s/%s/%s", uri, path, id), false, new Callback() { @Override/* w w w. j a v a 2 s. co m*/ public void onResponse(final Response response) throws IOException { waiter.assertTrue(response.isSuccessful()); // check response headers final Headers headers = response.headers(); waiter.assertNotNull(headers); waiter.assertNotNull(headers.names()); waiter.assertThat(headers.names().size(), greaterThan(0)); // check response body waiter.assertThat(response.body().contentLength(), greaterThan(0l)); final String payload = response.body().source().readUtf8(); waiter.assertThat(payload, allOf(notNullValue(), not(equalTo("")))); pw.println(" >> Abbreviated response: " + abbreviate(payload, 32)); // parse and check final Gson gson = new Gson(); if (path.contains("products")) { final Product product = gson.fromJson(payload, Product.class); waiter.assertNotNull(product); waiter.assertThat(product, allOf(notNullValue(), equalTo(getProducts().get(id)))); pw.println(" >> Object response: " + product); } else if (path.contains("shipping")) { final Shipping shipping = gson.fromJson(payload, Shipping.class); waiter.assertNotNull(shipping); waiter.assertThat(shipping, allOf(notNullValue(), equalTo(getShipping().get(id)))); pw.println(" >> Object response: " + shipping); } waiter.resume(); } @Override public void onFailure(final Request request, final IOException throwable) { waiter.fail(throwable); } }); }
From source file:es.upv.grycap.opengateway.examples.test.AppDaemonTest.java
License:Apache License
private void testGetList(final Waiter waiter) { final Http2Client client = isolatedHttp2Client(); client.asyncGetJson(String.format("%s/%s", uri, path), false, new Callback() { @Override/*from ww w .j a va 2 s .c o m*/ public void onResponse(final Response response) throws IOException { waiter.assertTrue(response.isSuccessful()); // check response headers final Headers headers = response.headers(); waiter.assertNotNull(headers); waiter.assertNotNull(headers.names()); waiter.assertThat(headers.names().size(), greaterThan(0)); // check response body waiter.assertThat(response.body().contentLength(), greaterThan(0l)); final String payload = response.body().source().readUtf8(); waiter.assertThat(payload, allOf(notNullValue(), not(equalTo("")))); pw.println(" >> Abbreviated response: " + abbreviate(payload, 32)); // parse and check final Gson gson = new Gson(); if (path.contains("products")) { final Type collectionType = new TypeToken<List<Product>>() { }.getType(); final List<Product> products = gson.fromJson(payload, collectionType); waiter.assertNotNull(products); waiter.assertThat(products, allOf(notNullValue(), containsInAnyOrder(getProducts().values().toArray()))); pw.println(" >> Object response: " + products); } else if (path.contains("shipping")) { final Type collectionType = new TypeToken<List<Shipping>>() { }.getType(); final List<Shipping> shipping = gson.fromJson(payload, collectionType); waiter.assertNotNull(shipping); waiter.assertThat(shipping, allOf(notNullValue(), containsInAnyOrder(getShipping().values().toArray()))); pw.println(" >> Object response: " + shipping); } waiter.resume(); } @Override public void onFailure(final Request request, final IOException throwable) { waiter.fail(throwable); } }); }
From source file:fi.aalto.legroup.zop.authentication.OIDCConfig.java
License:Apache License
public static void retrieveOIDCTokens(final Context context, final TokenCallback callback) { tokenVersion++;// w ww . j ava 2 s . c o m final int expectVersion = tokenVersion; Request request = createRetrieveOIDCTokensRequest(context); App.httpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { // TODO? } @Override public void onResponse(Response response) throws IOException { if (tokenVersion != expectVersion) return; OIDCTokenResponse tokens = App.jsonSerializer.read(OIDCTokenResponse.class, response.body().byteStream()); setTokens(tokens.client_id, tokens.client_secret); callback.tokensRetrieved(); } }); }
From source file:lumbermill.internal.elasticsearch.ElasticSearchOkHttpClientImpl.java
License:Apache License
protected void doOkHttpPost(RequestContext requestCtx) { RequestBody body = RequestBody.create(TEXT, requestCtx.signableRequest.payload().get()); Request request = new Request.Builder().url(url).post(body) .headers(Headers.of(requestCtx.signableRequest.headers())).build(); // Add some sanity logging to be able to figure out the load if (LOGGER.isDebugEnabled()) { int requestsInQueue = client.getDispatcher().getQueuedCallCount(); int requestsInProgress = client.getDispatcher().getRunningCallCount(); if (requestsInQueue > 0) { LOGGER.debug("There are {} requests waiting to be processed", requestsInQueue); }/* ww w .j a v a 2 s . co m*/ if (requestsInProgress > 0) { LOGGER.debug("There are {} requests currently executing", requestsInProgress); } } client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { requestCtx.error(IndexFailedException.ofIOException(e)); } @Override public void onResponse(Response response) throws IOException { handleResponse(requestCtx, response); } }); }