@Test // SPR-9325, SPR-13860 public void ioException() throws Exception { String url = "http://example.com/resource?access_token=123"; mockSentRequest(GET, url); mockHttpMessageConverter(new MediaType("foo", "bar"), String.class); given(request.execute()).willThrow(new IOException("Socket failure")); try { template.getForObject(url, String.class); fail("RestClientException expected"); } catch (ResourceAccessException ex) { assertEquals("I/O error on GET request for \"http://example.com/resource\": " + "Socket failure; nested exception is java.io.IOException: Socket failure", ex.getMessage()); } }
/** * Execute the given method on the provided URI. The * {@link org.springframework.http.client.ClientHttpRequest} * is processed using the {@link RequestCallback}; the response with * the {@link ResponseExtractor}. * @param url the fully-expanded URL to connect to * @param method the HTTP method to execute (GET, POST, etc.) * @param requestCallback object that prepares the request (can be {@code null}) * @param responseExtractor object that extracts the return value from the response (can * be {@code null}) * @return an arbitrary object, as returned by the {@link ResponseExtractor} */ protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method, @Nullable AsyncRequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException { Assert.notNull(url, "'url' must not be null"); Assert.notNull(method, "'method' must not be null"); try { org.springframework.http.client.AsyncClientHttpRequest request = createAsyncRequest(url, method); if (requestCallback != null) { requestCallback.doWithRequest(request); } ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync(); return new ResponseExtractorFuture<>(method, url, responseFuture, responseExtractor); } catch (IOException ex) { throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + url + "\":" + ex.getMessage(), ex); } }
if (maxRetry <= 0) { logger.error("Failed to connect to ({}:{})", getCurHost(), getCurPort(), ex); throw new ResourceAccessException( "I/O error on " + method.name() + " request for \"" + url + "\":" + ex.getMessage(), new IOException(ex)); } else { maxRetry--;
Throwable t = e.getCause(); if (t != null && t instanceof SocketTimeoutException) { if (logger.isTraceEnabled()) { logger.trace("Retrying tunnel write after receiving " + e.getClass().getName() + ": " + e.getMessage()); logger.error("Caught exception: " + e.getClass().getName() + ": " + e.getMessage()); logger.error("Closing tunnel " + this); stop();
Throwable t = e.getCause(); if (t != null && t instanceof SocketTimeoutException) { retry = true; if (logger.isTraceEnabled()) { logger.trace("Retrying tunnel read after receiving " + e.getClass().getName() + ": " + e.getMessage()); logger.error("Caught exception: " + e.getClass().getName() + ": " + e.getMessage()); logger.error("Closing tunnel " + this); stop();
@Test // SPR-15900 public void ioExceptionWithEmptyQueryString() throws Exception { // http://example.com/resource? URI uri = new URI("http", "example.com", "/resource", "", null); given(converter.canRead(String.class, null)).willReturn(true); given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(parseMediaType("foo/bar"))); given(requestFactory.createRequest(uri, GET)).willReturn(request); given(request.getHeaders()).willReturn(new HttpHeaders()); given(request.execute()).willThrow(new IOException("Socket failure")); try { template.getForObject(uri, String.class); fail("RestClientException expected"); } catch (ResourceAccessException ex) { assertEquals("I/O error on GET request for \"http://example.com/resource\": " + "Socket failure; nested exception is java.io.IOException: Socket failure", ex.getMessage()); } }
String query = url.getRawQuery(); resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource); throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + resource + "\": " + ex.getMessage(), ex);
public static boolean isUnknownHostException(CoreException e) { Throwable cause = e.getStatus().getException(); if (cause instanceof ResourceAccessException) { return ((ResourceAccessException) cause).getCause() instanceof UnknownHostException; } return false; }
throw new OperationFailureException(operr("failed to %s to %s, status code: %s, response body: %s", method.toString().toLowerCase(), url, e.getStatusCode(), e.getResponseBodyAsString())); } catch (ResourceAccessException e) { throw new OperationFailureException(operr("failed to %s to %s, IO Error: %s", method.toString().toLowerCase(), url, e.getMessage()));
String query = url.getRawQuery(); resource = (query != null ? resource.substring(0, resource.indexOf('?')) : resource); throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + resource + "\": " + ex.getMessage(), ex);
@Override public Boolean call() throws Exception { try { restTemplate.getForObject(uri, String.class); } catch (ResourceAccessException e) { System.out.println(e.getMessage()); return false; } return true; } });
/** * Execute the given method on the provided URI. The * {@link org.springframework.http.client.ClientHttpRequest} * is processed using the {@link RequestCallback}; the response with * the {@link ResponseExtractor}. * @param url the fully-expanded URL to connect to * @param method the HTTP method to execute (GET, POST, etc.) * @param requestCallback object that prepares the request (can be {@code null}) * @param responseExtractor object that extracts the return value from the response (can * be {@code null}) * @return an arbitrary object, as returned by the {@link ResponseExtractor} */ protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method, @Nullable AsyncRequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException { Assert.notNull(url, "'url' must not be null"); Assert.notNull(method, "'method' must not be null"); try { org.springframework.http.client.AsyncClientHttpRequest request = createAsyncRequest(url, method); if (requestCallback != null) { requestCallback.doWithRequest(request); } ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync(); return new ResponseExtractorFuture<>(method, url, responseFuture, responseExtractor); } catch (IOException ex) { throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + url + "\":" + ex.getMessage(), ex); } }
@Override public boolean execute() { try { putPayload(manageClient, "/manage/v2/properties", payload); } catch (ResourceAccessException rae) { /** * This is odd. Plenty of other Manage endpoints cause ML to restart, but this one seems to trigger * the restart before the PUT finishes. But - the PUT still seems to update the cluster properties * correctly. So we catch this error and figure that we can wait for ML to restart like it normally * would. */ if (logger.isInfoEnabled()) { logger.info("Ignoring somewhat expected error while updating local cluster properties: " + rae.getMessage()); } } return true; } });
@Override protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { try { Resource[] resources = resourcePatternResolver.getResources(pattern); if (logger.isDebugEnabled()) { logger.debug( "Resolved Resources for '" + pattern + "': " + Arrays.toString(resources)); } return chain.resolveResource(request, requestPath, Arrays.asList(resources)); } catch (IOException ex) { throw new ResourceAccessException("Couldn't resolve resources for \"" + pattern + "\"", ex); } }
private <T> Optional<T> call(String url, HttpMethod method, HttpEntity<?> entity, Class<T> clazz) { LOGGER.debug("Request to geoserver : {}, {} > {}", method, url, entity == null ? "{}" : entity.getBody()); ResponseEntity<T> result; try { result = restTemplate.exchange(url, method, entity, clazz); } catch (ResourceAccessException e) { LOGGER.error("Fail to access Geoserver : {}", e.getMessage()); throw new EngineException("Fail to access Geoserver : " + e.getMessage(), e); } catch (Exception e) { LOGGER.error("Fail to process response : {}", e.getMessage()); throw new EngineException("Fail to process response : " + e.getMessage(), e); } return Optional.ofNullable(result.getBody()); }
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findOne(username); Authentication auth; if(user != null){ return user; } SecurityContext securityContext = SecurityContextHolder.getContext(); if (securityContext != null) { auth = securityContext.getAuthentication(); if (auth != null) { Object principal = auth.getPrincipal(); if (principal instanceof User) { return (User) principal; } } } //fallback throw new ResourceAccessException("No found user for username: "+username); }
private <T> Optional<T> call(String url, HttpMethod method, HttpEntity<?> entity, Class<T> clazz) { LOGGER.debug("Request to engine : {}, {} > {}", method, url, entity == null ? "{}" : entity.getBody()); ResponseEntity<T> result; try { result = restTemplate.exchange(url, method, entity, clazz); } catch (ResourceAccessException e) { LOGGER.error("Fail to access Engine : {}", e.getMessage()); throw new EngineException("Fail to access Engine : " + e.getMessage(), e); } catch (Exception e) { LOGGER.error("Fail to process response : {}", e.getMessage()); throw new EngineException("Fail to process response : " + e.getMessage(), e); } return Optional.ofNullable(result.getBody()); }