List of usage examples for org.springframework.web.client ResourceAccessException getCause
public synchronized Throwable getCause()
From source file:com.sitewhere.rest.service.SiteWhereClient.java
/** * Send a REST request and handle the response. * /* ww w. java 2 s.c om*/ * @param url * @param method * @param input * @param clazz * @param vars * @return * @throws SiteWhereSystemException */ protected <S, T> S sendRest(String url, HttpMethod method, T input, Class<S> clazz, Map<String, String> vars) throws SiteWhereSystemException { try { HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", getAuthHeader()); HttpEntity<T> entity = new HttpEntity<T>(input, headers); ResponseEntity<S> response = getClient().exchange(url, method, entity, clazz, vars); return response.getBody(); } catch (ResourceAccessException e) { if (e.getCause() instanceof SiteWhereSystemException) { throw (SiteWhereSystemException) e.getCause(); } throw new RuntimeException(e); } }
From source file:org.zalando.stups.oauth2.spring.server.LeakTokenTest.java
@Test public void testSocketTimeoutException() { ResourceAccessException targetException = null; RestTemplate restTemplate = new TestInternalRestTemplate(new HttpComponentsClientHttpRequestFactory()); try {/*from w w w . j a va 2 s . c o m*/ restTemplate.exchange(DefaultTokenInfoRequestExecutor.buildRequestEntity(URI.create(URL), TOKEN), Map.class); } catch (ResourceAccessException e) { targetException = e; } // WE EXPECT NOT TO SEE ANYTHING FROM THE TOKEN Assertions.assertThat(targetException.getMessage()).startsWith(MESSAGE_STARTSWITH); Assertions.assertThat(targetException.getMessage()).doesNotContain(TOKEN); Assertions.assertThat(targetException.getCause().getMessage()).startsWith(JUST_FOR_TESTING); Assertions.assertThat(targetException.getCause().getMessage()).doesNotContain(TOKEN); }
From source file:io.seldon.client.services.ApiServiceImpl.java
private Object performPost(final String endpointUrl, ResourceBean resourceBean) { logger.info("* POST Endpoint: " + endpointUrl); if (token == null) { ResourceBean r = getToken();//from ww w . j ava 2 s. c o m if (r instanceof ErrorBean) return r; } String url = endpointUrl + "&oauth_token=" + token; RestTemplate template = createRestTemplate(); logger.debug("* Posting: " + resourceBean); logger.debug("** Endpoint: " + url); // API return types for posts vary: Map on success, ErrorBean on failure -- we're forced to use Object here: ResponseEntity<Object> responseEntity = null; try { responseEntity = template.postForEntity(url, resourceBean, Object.class); } catch (ResourceAccessException e) { if (e.getCause() instanceof SocketTimeoutException) { return createTimeoutBean(); } } Object body = responseEntity.getBody(); logger.debug("** Response: " + body); if (body instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) body; String response = (String) map.get("response"); String errorMessage = (String) map.get("error_msg"); // if this is an error bean if (response == null && errorMessage != null) { response = errorMessage; } if (response != null) { // not great; some posts return response maps, some return resource beans if (!response.equals("ok")) { if (response.equals("Token expired")) { logger.debug("Token expired; acquiring a fresh one..."); ResourceBean r = getToken(); if (r instanceof ErrorBean) return r; return performPost(endpointUrl, resourceBean); } else { return new ErrorBean(map); } } } } return body; }
From source file:io.seldon.client.services.ApiServiceImpl.java
private Object performPut(final String endpointUrl, ResourceBean resourceBean) { logger.info("* PUT Endpoint: " + endpointUrl); if (token == null) { ResourceBean r = getToken();//from w ww. j av a 2 s . c o m if (r instanceof ErrorBean) return r; } String url = endpointUrl + "&oauth_token=" + token; RestTemplate template = createRestTemplate(); logger.debug("* PUTting: " + resourceBean); logger.debug("** Endpoint: " + url); // API return types for posts vary: Map on success, ErrorBean on failure -- we're forced to use Object here: //template.put(url, resourceBean); HttpEntity<ResourceBean> entity = new HttpEntity<ResourceBean>(resourceBean); ResponseEntity<Object> responseEntity = null; try { responseEntity = template.exchange(url, HttpMethod.PUT, entity, Object.class); } catch (ResourceAccessException e) { if (e.getCause() instanceof SocketTimeoutException) { return createTimeoutBean(); } } Object body = responseEntity.getBody(); logger.debug("PUT response: " + body); if (body instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) body; String response = (String) map.get("response"); String errorMessage = (String) map.get("error_msg"); // if this is an error bean if (response == null && errorMessage != null) { response = errorMessage; } if (response != null) { // not great; some posts return response maps, some return resource beans if (!response.equals("ok")) { if (response.equals("Token expired")) { logger.debug("Token expired; acquiring a fresh one..."); ResourceBean r = getToken(); if (r instanceof ErrorBean) return r; return performPut(endpointUrl, resourceBean); } else { return new ErrorBean(map); } } } } return body; }
From source file:com.sitewhere.rest.client.SiteWhereClient.java
/** * Send a REST request and handle the response. * /* w w w .j a va 2 s . c o m*/ * @param url * @param method * @param input * @param clazz * @param vars * @return * @throws SiteWhereSystemException */ protected <S, T> S sendRest(String url, HttpMethod method, T input, Class<S> clazz, Map<String, String> vars) throws SiteWhereSystemException { try { HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", getAuthHeader()); headers.add(ISiteWhereWebConstants.HEADER_TENANT_TOKEN, getTenantAuthToken()); HttpEntity<T> entity = new HttpEntity<T>(input, headers); ResponseEntity<S> response = getClient().exchange(url, method, entity, clazz, vars); return response.getBody(); } catch (ResourceAccessException e) { if (e.getCause() instanceof SiteWhereSystemException) { throw (SiteWhereSystemException) e.getCause(); } throw new RuntimeException(e); } }
From source file:com.sitewhere.rest.client.SiteWhereClient.java
protected <S, T> S sendBinary(String url, HttpMethod method, T input, Class<S> clazz, Map<String, String> vars) throws SiteWhereSystemException { try {// w ww . j a v a 2 s. c o m HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", getAuthHeader()); headers.add(ISiteWhereWebConstants.HEADER_TENANT_TOKEN, getTenantAuthToken()); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); HttpEntity<T> entity = new HttpEntity<T>(input, headers); ResponseEntity<S> response = getClient().exchange(url, method, entity, clazz, vars); return response.getBody(); } catch (ResourceAccessException e) { if (e.getCause() instanceof SiteWhereSystemException) { throw (SiteWhereSystemException) e.getCause(); } throw new RuntimeException(e); } }
From source file:com.sitewhere.rest.client.SiteWhereClient.java
/** * Send a REST request and return response as byte array. * // w w w.j a v a2 s. c om * @param url * @param method * @param vars * @return * @throws SiteWhereSystemException */ protected <T> byte[] sendRestWithBinaryResponse(String url, HttpMethod method, Map<String, String> vars) throws SiteWhereSystemException { try { HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", getAuthHeader()); headers.add(ISiteWhereWebConstants.HEADER_TENANT_TOKEN, getTenantAuthToken()); ResponseEntity<byte[]> response = getClient().exchange(url, method, new HttpEntity<byte[]>(headers), byte[].class, vars); return response.getBody(); } catch (ResourceAccessException e) { if (e.getCause() instanceof SiteWhereSystemException) { throw (SiteWhereSystemException) e.getCause(); } throw new RuntimeException(e); } }