List of usage examples for com.squareup.okhttp Response code
int code
To view the source code for com.squareup.okhttp Response code.
Click Source Link
From source file:it.smartcommunitylab.ApiClient.java
License:Apache License
/** * Handle the given response, return the deserialized object when the response is successful. * * @param <T> Type/* w w w . j a va2 s . co m*/ * @param response Response * @param returnType Return type * @throws ApiException If the response has a unsuccessful status code or * fail to deserialize the response body * @return Type */ public <T> T handleResponse(Response response, Type returnType) throws ApiException { if (response.isSuccessful()) { if (returnType == null || response.code() == 204) { // returning null if the returnType is not defined, // or the status code is 204 (No Content) if (response.body() != null) { try { response.body().close(); } catch (IOException e) { throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap()); } } return null; } else { return deserialize(response, returnType); } } else { String respBody = null; if (response.body() != null) { try { respBody = response.body().string(); } catch (IOException e) { throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap()); } } throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody); } }
From source file:javajo.sample.codic.Codic.java
License:Apache License
public List<Translation> translate(String text) { URL url = new HttpUrl.Builder().scheme("https").host("api.codic.jp").addPathSegment("v1") .addPathSegment("engine").addPathSegment("translate.json").addQueryParameter("text", text).build() .url();//from w w w. ja v a 2 s .c o m Request request = new Builder().url(url).addHeader(CODIC_HTTP_AUTH_HEADER, "Bearer " + accessToken).get() .build(); try { Response response = new OkHttpClient().newCall(request).execute(); int code = response.code(); if (code != 200) { throw new CodicException( "Exception in calling API[" + API_ENTRY_URL + "] with status " + code + "."); } Genson genson = new GensonBuilder().useMethods(true).create(); String string = response.body().string(); return genson.deserialize(string, new GenericType<List<Translation>>() { }); } catch (IOException e) { throw new CodicException("Exception in calling API[" + API_ENTRY_URL + "].", e); } }
From source file:keywhiz.auth.xsrf.XsrfServletFilterIntegrationTest.java
License:Apache License
@Test public void xsrfNotRequiredForLogin() throws Exception { Response response = client.newCall(buildLoginPost(DbSeedCommand.defaultUser, DbSeedCommand.defaultPassword)) .execute();//from ww w . j ava 2 s. com assertThat(response.code()).isNotEqualTo(401); }
From source file:keywhiz.auth.xsrf.XsrfServletFilterIntegrationTest.java
License:Apache License
@Test public void xsrfNotRequiredForLogout() throws Exception { Request request = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), "")) .url(testUrl("/admin/logout")).build(); Response response = client.newCall(request).execute(); assertThat(response.code()).isNotEqualTo(401); }
From source file:keywhiz.auth.xsrf.XsrfServletFilterIntegrationTest.java
License:Apache License
@Test public void rejectsForAdminUrlWithoutXsrf() throws Exception { noXsrfClient.newCall(buildLoginPost(DbSeedCommand.defaultUser, DbSeedCommand.defaultPassword)).execute(); Request request = new Request.Builder().url(testUrl("/admin/clients")).get().build(); Response response = noXsrfClient.newCall(request).execute(); assertThat(response.code()).isEqualTo(401); }
From source file:keywhiz.auth.xsrf.XsrfServletFilterIntegrationTest.java
License:Apache License
@Test public void allowsForAdminUrlWithXsrf() throws Exception { client.newCall(buildLoginPost(DbSeedCommand.defaultUser, DbSeedCommand.defaultPassword)).execute(); Request request = new Request.Builder().url(testUrl("/admin/clients")).get().build(); Response response = client.newCall(request).execute(); assertThat(response.code()).isNotEqualTo(401); }
From source file:keywhiz.client.KeywhizClient.java
License:Apache License
/** * Maps some of the common HTTP errors to the corresponding exceptions. *//*from ww w .java2s .c om*/ private void throwOnCommonError(Response response) throws IOException { int status = response.code(); switch (status) { case HttpStatus.SC_BAD_REQUEST: throw new MalformedRequestException(); case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE: throw new UnsupportedMediaTypeException(); case HttpStatus.SC_NOT_FOUND: throw new NotFoundException(); case HttpStatus.SC_UNAUTHORIZED: throw new UnauthorizedException(); case HttpStatus.SC_FORBIDDEN: throw new ForbiddenException(); case HttpStatus.SC_CONFLICT: throw new ConflictException(); case HttpStatus.SC_UNPROCESSABLE_ENTITY: throw new ValidationException(); } if (status >= 400) { throw new IOException("Unexpected status code on response: " + status); } }
From source file:keywhiz.NamedAssetsBundleTest.java
License:Apache License
@Test public void faviconAccessible() throws Exception { Request request = new Request.Builder().url(testUrl("/favicon.ico")).get().build(); Response response = client.newCall(request).execute(); assertThat(response.code()).isEqualTo(200); }
From source file:keywhiz.NamedAssetsBundleTest.java
License:Apache License
@Test public void robotsAccessible() throws Exception { Request request = new Request.Builder().url(testUrl("/robots.txt")).get().build(); Response response = client.newCall(request).execute(); assertThat(response.code()).isEqualTo(200); }
From source file:keywhiz.NamedAssetsBundleTest.java
License:Apache License
@Test public void crossdomainAccessible() throws Exception { Request request = new Request.Builder().url(testUrl("/crossdomain.xml")).get().build(); Response response = client.newCall(request).execute(); assertThat(response.code()).isEqualTo(200); }