/** * Handle the given response, performing appropriate logging and * invoking the {@link ResponseErrorHandler} if necessary. * <p>Can be overridden in subclasses. * @param url the fully-expanded URL to connect to * @param method the HTTP method to execute (GET, POST, etc.) * @param response the resulting {@link ClientHttpResponse} * @throws IOException if propagated from {@link ResponseErrorHandler} * @since 4.1.6 * @see #setErrorHandler */ protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { ResponseErrorHandler errorHandler = getErrorHandler(); boolean hasError = errorHandler.hasError(response); if (logger.isDebugEnabled()) { try { int code = response.getRawStatusCode(); HttpStatus status = HttpStatus.resolve(code); logger.debug("Response " + (status != null ? status : code)); } catch (IOException ex) { // ignore } } if (hasError) { errorHandler.handleError(url, method, response); } }
/** * Alternative to {@link #handleError(ClientHttpResponse)} with extra * information providing access to the request URL and HTTP method. * @param url the request URL * @param method the HTTP method * @param response the response with the error * @throws IOException in case of I/O errors * @since 5.0 */ default void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { handleError(response); }
@Override public boolean hasError(ClientHttpResponse response) throws IOException { return this.defaultErrorHandler.hasError(response); }
/** * Alternative to {@link #handleError(ClientHttpResponse)} with extra * information providing access to the request URL and HTTP method. * @param url the request URL * @param method the HTTP method * @param response the response with the error * @throws IOException in case of I/O errors * @since 5.0 */ default void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { handleError(response); }
@Override @Nullable protected final T adapt(ClientHttpResponse response) throws ExecutionException { try { if (!getErrorHandler().hasError(response)) { logResponseStatus(this.method, this.url, response); } else { handleResponseError(this.method, this.url, response); } return convertResponse(response); } catch (Throwable ex) { throw new ExecutionException(ex); } finally { response.close(); } }
/** * Handle the given response, performing appropriate logging and * invoking the {@link ResponseErrorHandler} if necessary. * <p>Can be overridden in subclasses. * @param url the fully-expanded URL to connect to * @param method the HTTP method to execute (GET, POST, etc.) * @param response the resulting {@link ClientHttpResponse} * @throws IOException if propagated from {@link ResponseErrorHandler} * @since 4.1.6 * @see #setErrorHandler */ protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { ResponseErrorHandler errorHandler = getErrorHandler(); boolean hasError = errorHandler.hasError(response); if (logger.isDebugEnabled()) { try { int code = response.getRawStatusCode(); HttpStatus status = HttpStatus.resolve(code); logger.debug("Response " + (status != null ? status : code)); } catch (IOException ex) { // ignore } } if (hasError) { errorHandler.handleError(url, method, response); } }
private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException { if (logger.isWarnEnabled()) { try { logger.warn("Async " + method.name() + " request for \"" + url + "\" resulted in " + response.getRawStatusCode() + " (" + response.getStatusText() + "); invoking error handler"); } catch (IOException ex) { // ignore } } getErrorHandler().handleError(url, method, response); }
@Override @Nullable protected final T adapt(ClientHttpResponse response) throws ExecutionException { try { if (!getErrorHandler().hasError(response)) { logResponseStatus(this.method, this.url, response); } else { handleResponseError(this.method, this.url, response); } return convertResponse(response); } catch (Throwable ex) { throw new ExecutionException(ex); } finally { response.close(); } }
protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { ResponseErrorHandler errorHandler = getErrorHandler(); boolean hasError = errorHandler.hasError(response); if (logger.isDebugEnabled()) { try { logger.debug(method.name() + " request for \"" + url + "\" resulted in " + response.getRawStatusCode() + " (" + response.getStatusText() + ")" + (hasError ? "; invoking error handler" : "")); } catch (IOException ex) { // ignore } } if (hasError) { errorHandler.handleError(response); } }
private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException { if (logger.isWarnEnabled()) { try { logger.warn("Async " + method.name() + " request for \"" + url + "\" resulted in " + response.getRawStatusCode() + " (" + response.getStatusText() + "); invoking error handler"); } catch (IOException ex) { // ignore } } getErrorHandler().handleError(url, method, response); }
public boolean hasError(ClientHttpResponse response) throws IOException { return HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series()) || this.errorHandler.hasError(response); }
/** * Handle the given response, performing appropriate logging and * invoking the {@link ResponseErrorHandler} if necessary. * <p>Can be overridden in subclasses. * @param url the fully-expanded URL to connect to * @param method the HTTP method to execute (GET, POST, etc.) * @param response the resulting {@link ClientHttpResponse} * @throws IOException if propagated from {@link ResponseErrorHandler} * @since 4.1.6 * @see #setErrorHandler */ protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { ResponseErrorHandler errorHandler = getErrorHandler(); boolean hasError = errorHandler.hasError(response); if (logger.isDebugEnabled()) { try { int code = response.getRawStatusCode(); HttpStatus status = HttpStatus.resolve(code); logger.debug("Response " + (status != null ? status : code)); } catch (IOException ex) { // ignore } } if (hasError) { errorHandler.handleError(url, method, response); } }
@Override public void handleError(ClientHttpResponse response) throws IOException { if (!HttpStatus.BAD_REQUEST.equals(response.getStatusCode())) { this.defaultErrorHandler.handleError(response); } // A Bearer Token Error may be in the WWW-Authenticate response header // See https://tools.ietf.org/html/rfc6750#section-3 OAuth2Error oauth2Error = this.readErrorFromWwwAuthenticate(response.getHeaders()); if (oauth2Error == null) { oauth2Error = this.oauth2ErrorConverter.read(OAuth2Error.class, response); } throw new OAuth2AuthorizationException(oauth2Error); }
private void mockResponseStatus(HttpStatus responseStatus) throws Exception { given(request.execute()).willReturn(response); given(errorHandler.hasError(response)).willReturn(responseStatus.isError()); given(response.getStatusCode()).willReturn(responseStatus); given(response.getRawStatusCode()).willReturn(responseStatus.value()); given(response.getStatusText()).willReturn(responseStatus.getReasonPhrase()); }
/** * Handle the given response, performing appropriate logging and * invoking the {@link ResponseErrorHandler} if necessary. * <p>Can be overridden in subclasses. * @param url the fully-expanded URL to connect to * @param method the HTTP method to execute (GET, POST, etc.) * @param response the resulting {@link ClientHttpResponse} * @throws IOException if propagated from {@link ResponseErrorHandler} * @since 4.1.6 * @see #setErrorHandler */ protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { ResponseErrorHandler errorHandler = getErrorHandler(); boolean hasError = errorHandler.hasError(response); if (logger.isDebugEnabled()) { try { logger.debug(method.name() + " request for \"" + url + "\" resulted in " + response.getRawStatusCode() + " (" + response.getStatusText() + ")" + (hasError ? "; invoking error handler" : "")); } catch (IOException ex) { // ignore } } if (hasError) { errorHandler.handleError(url, method, response); } }
errorHandler.handleError(response); errorHandler.handleError(bufferedResponse); errorHandler.handleError(bufferedResponse);
@Override public boolean hasError(ClientHttpResponse response) throws IOException { return this.defaultErrorHandler.hasError(response); }
public Metadata putFile(String path, byte[] file) { UriTemplate uriTemplate = new UriTemplate(FILE_PUT_URL); URI uri = uriTemplate.expand(appFolderUrl, path); try { ClientHttpRequest request = getRestTemplate().getRequestFactory().createRequest(uri, HttpMethod.PUT); request.getBody().write(file); ClientHttpResponse response = request.execute(); ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler(); if (errorHandler.hasError(response)) { errorHandler.handleError(response); return null; } else { InputStream stream = response.getBody(); return objectMapper.readValue(stream, Metadata.class); } } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void errorHandling() throws Exception { String url = "http://example.com"; mockSentRequest(GET, url); mockResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR); willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR)) .given(errorHandler).handleError(new URI(url), GET, response); try { template.execute(url, GET, null, null); fail("HttpServerErrorException expected"); } catch (HttpServerErrorException ex) { // expected } verify(response).close(); }
@Override public boolean hasError(ClientHttpResponse response) throws IOException { return errorHandler.hasError(response); } }