/** * Return the error handler. */ public ResponseErrorHandler getErrorHandler() { return this.syncTemplate.getErrorHandler(); }
/** * Return the error handler. */ public ResponseErrorHandler getErrorHandler() { return this.syncTemplate.getErrorHandler(); }
/** * 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); } }
/** * 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); } }
@Test void customRestTemplateBuilderIsUsed() { this.contextRunner.withUserConfiguration(CustomRestTemplateConfiguration.class) .run((context) -> { assertThat(context).hasSingleBean(InitializrMetadataProvider.class); RestTemplate restTemplate = (RestTemplate) new DirectFieldAccessor( context.getBean(InitializrMetadataProvider.class)) .getPropertyValue("restTemplate"); assertThat(restTemplate.getErrorHandler()) .isSameAs(CustomRestTemplateConfiguration.errorHandler); }); }
public static String getCause(RestTemplate restTemplate) { ResponseErrorHandler responseErrorHandler = restTemplate.getErrorHandler(); if (responseErrorHandler instanceof RestErrorHandler) { RestErrorHandler errorHandler = (RestErrorHandler) responseErrorHandler; return errorHandler.getCause(); } return null; } }
/** * Check RestTemplate contains required converters */ private void initRestTemplate() { boolean isContainsConverter = false; for (HttpMessageConverter<?> httpMessageConverter : this.restTemplate.getMessageConverters()) { if (MappingJacksonRPC2HttpMessageConverter.class.isAssignableFrom(httpMessageConverter.getClass())) { isContainsConverter = true; break; } } if (!isContainsConverter) { final MappingJacksonRPC2HttpMessageConverter messageConverter = new MappingJacksonRPC2HttpMessageConverter(); messageConverter.setObjectMapper(this.getObjectMapper()); final List<HttpMessageConverter<?>> restMessageConverters = new ArrayList<>(); restMessageConverters.addAll(this.restTemplate.getMessageConverters()); // Place JSON-RPC converter on the first place! restMessageConverters.add(0, messageConverter); this.restTemplate.setMessageConverters(restMessageConverters); } // use specific JSON-RPC error handler if it has not been changed to custom if (restTemplate.getErrorHandler() instanceof org.springframework.web.client.DefaultResponseErrorHandler) { restTemplate.setErrorHandler(JsonRpcResponseErrorHandler.INSTANCE); } }
/** * Return the error handler. */ public ResponseErrorHandler getErrorHandler() { return this.syncTemplate.getErrorHandler(); }
/** * Return the error handler. */ public ResponseErrorHandler getErrorHandler() { return this.syncTemplate.getErrorHandler(); }
public ResponseErrorHandler getErrorHandler() { return restTemplate.getErrorHandler(); }
public static String getCause(RestTemplate restTemplate) { ResponseErrorHandler responseErrorHandler = restTemplate.getErrorHandler(); if (responseErrorHandler instanceof RestErrorHandler) { RestErrorHandler errorHandler = (RestErrorHandler) responseErrorHandler; return errorHandler.getCause(); } return null; } }
public boolean getIgnoreNotFound() { return (getRestTemplate().getErrorHandler() instanceof Ignore404sErrorHandler); }
private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException { if (Log.isLoggable(TAG, Log.WARN)) { try { Log.w(TAG, method.name() + " request for \"" + url + "\" resulted in " + response.getStatusCode() + " (" + response.getStatusText() + "); invoking error handler"); } catch (IOException e) { // ignore } } getErrorHandler().handleError(response); }
private void init(RestTemplate restTemplate) { setErrorHandler(restTemplate.getErrorHandler()); setMessageConverters(restTemplate.getMessageConverters()); }
private void init(RestTemplate restTemplate) { setErrorHandler(restTemplate.getErrorHandler()); setMessageConverters(restTemplate.getMessageConverters()); }
/** * 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); } }
/** * 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); } }
private void initTransfer(final long firstByteInRange) throws IOException { final ClientHttpRequest request = restTemplate.getRequestFactory().createRequest(uri, HttpMethod.GET); if (firstByteInRange > 0) { request.getHeaders().add("Range", "bytes:" + firstByteInRange + "-"); } final ClientHttpResponse response = request.execute(); if (restTemplate.getErrorHandler().hasError(response)) { state = StreamingResourceState.ERROR; throw new IllegalStateException("Transfer initialization failed, error code " + response.getStatusCode() + ":" + response.getStatusText()); } state = StreamingResourceState.TRANSFER_IN_PROGRESS; inputStream = response.getBody(); contentLength = response.getHeaders().getContentLength(); }
public void setIgnoreNotFound(boolean b) { if (b) { getRestTemplate().setErrorHandler(new Ignore404sErrorHandler()); } else { if (getRestTemplate().getErrorHandler() instanceof Ignore404sErrorHandler) { getRestTemplate().setErrorHandler(new DefaultResponseErrorHandler()); } } }
/** * Creates a new {@code TestRestTemplate} with the same configuration as this one, * except that it will send basic authorization headers using the given * {@code username} and {@code password}. * @param username the username * @param password the password * @return the new template * @since 1.4.1 */ public TestRestTemplate withBasicAuth(String username, String password) { RestTemplate restTemplate = new RestTemplateBuilder() .messageConverters(getRestTemplate().getMessageConverters()) .interceptors(getRestTemplate().getInterceptors()) .uriTemplateHandler(getRestTemplate().getUriTemplateHandler()).build(); TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username, password, this.httpClientOptions); testRestTemplate.getRestTemplate() .setErrorHandler(getRestTemplate().getErrorHandler()); return testRestTemplate; }