@Override public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { Assert.notNull(userRequest, "userRequest cannot be null"); String registrationId = userRequest.getClientRegistration().getRegistrationId(); Class<? extends OAuth2User> customUserType; if ((customUserType = this.customUserTypes.get(registrationId)) == null) { return null; } RequestEntity<?> request = this.requestEntityConverter.convert(userRequest); ResponseEntity<? extends OAuth2User> response; try { response = this.restOperations.exchange(request, customUserType); } catch (RestClientException ex) { OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE, "An error occurred while attempting to retrieve the UserInfo Resource: " + ex.getMessage(), null); throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex); } OAuth2User oauth2User = response.getBody(); return oauth2User; }
/** * Update query and header parameters based on authentication settings. * * @param authNames The authentications to apply * @param queryParams The query parameters * @param headerParams The header parameters */ private void updateParamsForAuth(String[] authNames, MultiValueMap<String, String> queryParams, HttpHeaders headerParams) { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { throw new RestClientException("Authentication undefined: " + authName); } auth.applyToParams(queryParams, headerParams); } }
private static void catchException() { printTitle("catchException"); try { restTemplate.getForObject( "http://localhost:8080/spring4-chap17-s/stores/{storeId}/items/{itemId}", String.class, "0A", "I100"); } catch (RestClientException e) { e.printStackTrace(); } }
@Override public OAuth2AccessTokenResponse getTokenResponse(OAuth2ClientCredentialsGrantRequest clientCredentialsGrantRequest) { Assert.notNull(clientCredentialsGrantRequest, "clientCredentialsGrantRequest cannot be null"); RequestEntity<?> request = this.requestEntityConverter.convert(clientCredentialsGrantRequest); ResponseEntity<OAuth2AccessTokenResponse> response; try { response = this.restOperations.exchange(request, OAuth2AccessTokenResponse.class); } catch (RestClientException ex) { OAuth2Error oauth2Error = new OAuth2Error(INVALID_TOKEN_RESPONSE_ERROR_CODE, "An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: " + ex.getMessage(), null); throw new OAuth2AuthorizationException(oauth2Error, ex); } OAuth2AccessTokenResponse tokenResponse = response.getBody(); if (CollectionUtils.isEmpty(tokenResponse.getAccessToken().getScopes())) { // As per spec, in Section 5.1 Successful Access Token Response // https://tools.ietf.org/html/rfc6749#section-5.1 // If AccessTokenResponse.scope is empty, then default to the scope // originally requested by the client in the Token Request tokenResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse) .scopes(clientCredentialsGrantRequest.getClientRegistration().getScopes()) .build(); } return tokenResponse; }
@Override public <T> T execute(URI url, HttpMethod method, @Nullable RequestCallback callback, @Nullable ResponseExtractor<T> extractor) throws RestClientException { try { extractor.extractData(this.responses.remove()); } catch (Throwable t) { throw new RestClientException("Failed to invoke extractor", t); } return null; } }
@Override public OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) { Assert.notNull(authorizationCodeGrantRequest, "authorizationCodeGrantRequest cannot be null"); RequestEntity<?> request = this.requestEntityConverter.convert(authorizationCodeGrantRequest); ResponseEntity<OAuth2AccessTokenResponse> response; try { response = this.restOperations.exchange(request, OAuth2AccessTokenResponse.class); } catch (RestClientException ex) { OAuth2Error oauth2Error = new OAuth2Error(INVALID_TOKEN_RESPONSE_ERROR_CODE, "An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: " + ex.getMessage(), null); throw new OAuth2AuthorizationException(oauth2Error, ex); } OAuth2AccessTokenResponse tokenResponse = response.getBody(); if (CollectionUtils.isEmpty(tokenResponse.getAccessToken().getScopes())) { // As per spec, in Section 5.1 Successful Access Token Response // https://tools.ietf.org/html/rfc6749#section-5.1 // If AccessTokenResponse.scope is empty, then default to the scope // originally requested by the client in the Token Request tokenResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse) .scopes(authorizationCodeGrantRequest.getClientRegistration().getScopes()) .build(); } return tokenResponse; }
throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", ex); throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]");
} catch (RestClientException ex) { OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE, "An error occurred while attempting to retrieve the UserInfo Resource: " + ex.getMessage(), null); throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex);
} else { throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
logger.error("Token Endpoint error response: " + e.getMessage()); throw new AuthenticationServiceException("Unable to obtain Access Token: " + e.getMessage());
throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", ex); throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]");
private byte[] downloadModelWithReferences(String namespace, String name, String version) { try { return restTemplate.getForObject(basePath + "/model/file/{namespace}/{name}/{version}?output=DSL&includeDependencies=true", byte[].class, namespace, name, version); } catch (RestClientException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } }
@Test void rest_client_exception_is_propagated() { template = mock(RestTemplate.class); when(template.getForObject(any(URI.class), any())).thenThrow(new RestClientException("mock")); assertThrows(RestClientException.class, () -> cache.getUrlContent(uri, template)); }
throw ex; logger.debug("getJSONString() : I/O exception {} occurs when processing url={} ", ex.getMessage(), (host + url));
void terminateSpan(@Nullable ClientResponse clientResponse, @Nullable Throwable throwable) { if (clientResponse == null || clientResponse.statusCode() == null) { if (log.isDebugEnabled()) { log.debug("No response was returned. Will close the span [" + this.span + "]"); } handleReceive(this.span, this.ws, clientResponse, throwable); return; } boolean error = clientResponse.statusCode().is4xxClientError() || clientResponse.statusCode().is5xxServerError(); if (error) { if (log.isDebugEnabled()) { log.debug( "Non positive status code was returned from the call. Will close the span [" + this.span + "]"); } throwable = new RestClientException("Status code of the response is [" + clientResponse.statusCode().value() + "] and the reason is [" + clientResponse.statusCode().getReasonPhrase() + "]"); } handleReceive(this.span, this.ws, clientResponse, throwable); }
public void prepareToShutdown() { RestTemplate restTemplate = new RestTemplate(); Boolean response = false; try { String url = "http://" + getHostHostName() + ":" + getHttpInternalPort() + "/auction/live/auction/prepareForShutdown"; response = restTemplate.getForObject(url, Boolean.class); logger.info("prepareToShutdown got response " + response + " for url " + url); } catch (RestClientException e) { logger.info("prepareToShutdown got RestClientException: " + e.getMessage()); } }
message += " and content type [" + requestContentType + "]"; throw new RestClientException(message);
public void releaseAsyncRequests() { RestTemplate restTemplate = new RestTemplate(); Boolean response = false; try { String url = "http://" + getHostHostName() + ":" + getHttpInternalPort() + "/auction/live/auction/release"; response = restTemplate.getForObject(url, Boolean.class); logger.info("releaseAsyncRequests got response " + response + " for url " + url); } catch (RestClientException e) { logger.info("releaseAsyncRequests got RestClientException: " + e.getMessage()); } }
message += " and content type [" + requestContentType + "]"; throw new RestClientException(message);
public boolean isMaster() { RestTemplate restTemplate = new RestTemplate(); Boolean response = false; try { String url = "http://" + getHostHostName() + ":" + getHttpInternalPort() + "/auction/live/auction/isMaster"; response = restTemplate.getForObject(url, Boolean.class); logger.info("isMaster got response " + response + " for url " + url); } catch (RestClientException e) { logger.info("Checking isUp got RestClientException: " + e.getMessage()); return false; } return response; }