List of usage examples for com.squareup.okhttp Response isSuccessful
public boolean isSuccessful()
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 w w w .ja v a2s .c o m 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 v a2 s. co 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 w w .j a v a 2s . c om 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//from w ww . jav a 2 s . 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 ww w .j a v a2 s . com*/ 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();// www . j a v a 2 s . co m // 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/*from w w w . java 2s . 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 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 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 retrieveOIDCTokensBlocking(final Context context) throws IOException { if (isReady()) return;/* w w w . j av a 2s . c o m*/ Request request = createRetrieveOIDCTokensRequest(context); Response response = App.httpClient.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Failed to get tokens"); OIDCTokenResponse tokens = App.jsonSerializer.read(OIDCTokenResponse.class, response.body().byteStream()); setTokens(tokens.client_id, tokens.client_secret); }
From source file:info.curtbinder.reefangel.service.ControllerTask.java
License:Creative Commons License
public void run() { // Communicate with controller // clear out the error code on run rapp.clearErrorCode();/*from w w w. j av a 2s . c om*/ Response response = null; boolean fInterrupted = false; broadcastUpdateStatus(R.string.statusStart); try { URL url = new URL(host.toString()); OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(host.getConnectTimeout(), TimeUnit.MILLISECONDS); client.setReadTimeout(host.getReadTimeout(), TimeUnit.MILLISECONDS); Request.Builder builder = new Request.Builder(); builder.url(url); if (host.isDeviceAuthenticationEnabled()) { String creds = Credentials.basic(host.getWifiUsername(), host.getWifiPassword()); builder.header("Authorization", creds); } Request req = builder.build(); broadcastUpdateStatus(R.string.statusConnect); response = client.newCall(req).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); if (Thread.interrupted()) throw new InterruptedException(); } catch (MalformedURLException e) { rapp.error(1, e, "MalformedURLException"); } catch (SocketTimeoutException e) { rapp.error(5, e, "SocketTimeoutException"); } catch (ConnectException e) { rapp.error(3, e, "ConnectException"); } catch (UnknownHostException e) { String msg = "Unknown Host: " + host.toString(); UnknownHostException ue = new UnknownHostException(msg); rapp.error(4, ue, "UnknownHostException"); } catch (EOFException e) { EOFException eof = new EOFException(rapp.getString(R.string.errorAuthentication)); rapp.error(3, eof, "EOFException"); } catch (IOException e) { rapp.error(3, e, "IOException"); } catch (InterruptedException e) { fInterrupted = true; } processResponse(response, fInterrupted); }