Java Code Examples for org.springframework.web.client.ResourceAccessException

Following code examples demonstrate how to use org.springframework.web.client.ResourceAccessExceptionfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.client.ResourceAccessExceptionand various code implementation of this class.

    public Partida buscarPorId(String id) {
        Optional<Partida> partida = repo.findById(id);
        return partida
                .orElseThrow(() -> new ResourceAccessException("Essa partida nao existe."));
    } 


    public static JSONObject getJson(String url) throws JSONException, ResourceAccessException {
        RestTemplate restTemplate = new RestTemplate();
        String jsonText = restTemplate.getForEntity(url, String.class).getBody();
        return new JSONObject(jsonText);
    } 

    public void testRestClientGetForItemOne() {
        Item item = createItem();
        logger.debug("******** TESTING ********");
        ResponseEntity<String> response = null;

        try {
            response = restTemplate.getForEntity(
                    BASE_URI + "/1", String.class);

            assertThat(response, is(not(nullValue())));
            assertThat(HttpStatus.OK, is(equalTo(response.getStatusCode())));

            assertThat(response.getBody(), is(not(nullValue())));
            logger.debug("********** Job Request Response: {}", response.getBody());
        } catch (ResourceAccessException e) {
            logger.error("\n\n********************************************\n"+
                         "************ Server NOT running ************\n" +
                         "********************************************\n");
        }
    } 

    public void testRestClientGet() {
        Item item = createItem();
        logger.debug("******** TESTING ********");
        ResponseEntity<String> response = null;

        try {
            response = restTemplate.getForEntity(
                    BASE_URI + "/" + item.getId(), String.class);

            assertThat(response, is(not(nullValue())));
            assertThat(HttpStatus.OK, is(equalTo(response.getStatusCode())));

            assertThat(response.getBody(), is(not(nullValue())));
            logger.debug("********** Job Request Response: {}", response.getBody());
        } catch (ResourceAccessException e) {
            logger.error("\n\n********************************************\n"+
                         "************ Server NOT running ************\n" +
                         "********************************************\n");
        }

    } 

    protected Object doInBackground(Object[] params) {
        try {
            ResponseEntity<ParkingSpaceHistory[]> response =
                    restTemplate.exchange(
                            uri,
                            HttpMethod.GET,
                            new HttpEntity<ParkingSpaceHistory[]>(requestHeaders),
                            ParkingSpaceHistory[].class);
            return response;
        } catch (HttpClientErrorException e) {
            Log.e(TAG, e.getStatusText());
            return new ResponseEntity<String>(e.getStatusCode());
        } catch (ResourceAccessException ae) {
            Log.e(TAG, ae.getLocalizedMessage(), ae);
            return null;
        }
    } 

	public boolean isRequestAuthorized(HttpServletRequest arg0) {
		
		try {
			
		} catch (Exception e) {
			throw new ResourceAccessException("SSO UNAUTHORIZED");
		}
		return false;
	} 

	public Account findOne(String username) {

		Account account = accountRepository.findOne(username) ;

		if (account == null){
			throw new ResourceAccessException("The given account is not found! username="+username) ;
		}

		return account;
	} 

    protected Object doInBackground(Object[] params) {
        try {
            ResponseEntity<ParkingSpaceHistory[]> response =
                    restTemplate.exchange(
                            uri,
                            HttpMethod.GET,
                            new HttpEntity<ParkingSpaceHistory[]>(requestHeaders),
                            ParkingSpaceHistory[].class);
            return response;
        } catch (HttpClientErrorException e) {
            Log.e(TAG, e.getStatusText());
            return new ResponseEntity<String>(e.getStatusCode());
        } catch (ResourceAccessException ae) {
            Log.e(TAG, ae.getLocalizedMessage(), ae);
            return null;
        }
    } 

	public static String getAccessToken(String appid){
		String access_token = null;
		if(StringUtils.isBlank(access_token)){
			try {
				access_token = postAccessToken(appid);
			} catch (Exception e) {
				if(e instanceof ResourceAccessException){
					log.info(e.getMessage(),e);
					for (int i = 0; i < 5; i++) {
						try {
							access_token = postAccessToken(appid);
							if(StringUtils.isNotBlank(access_token)){
								break;
							}
						} catch (ResourceAccessException e2) {
							log.info(e2.getMessage(),e);
						} catch (Exception e2) {
							log.error(e2.getMessage(),e);
						}
					}
				}else{
					log.error(e.getMessage(),e);
				}
			}
		}
		return access_token;
	} 

	protected T doInBackground(Void... arg0) {
		
		RestTemplate restTemplate = new RestTemplate();
		if (consumer != null) {
			restTemplate.setRequestFactory(new SpringRequestFactory(consumer));
		}
		
		
		
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.registerModule(new VideoDeserializerModule());
		MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
		converter.setObjectMapper(mapper);
		restTemplate.getMessageConverters().add(converter);
		
		ModelBase entity = null;
		try {
		    entity = restTemplate.getForObject(this.url, ModelBase.class);
		} catch (HttpClientErrorException e) {
			e.printStackTrace();
			exception = e;
		} catch (ResourceAccessException e) {
			e.printStackTrace();
			Log.e(LOG_TAG, "url was " + url);
			exception = e;
		}
		
	    
	    T result;
	    try {
	    	result = (T) entity;
	    } catch (ClassCastException e) {
	    	e.printStackTrace();
	    	exception = e;
	    	result = null;
	    }
	    
	    Log.d(LOG_TAG, "Response received. Returning entity.");
	    return result;
	} 

    protected boolean execute() {
        try {

            logger.info("waiting for service at url : {}",url);
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

            if(response.getStatusCode().is2xxSuccessful()) {
                return true;
            }else {
                return false;
            }

        }catch (ResourceAccessException ex) {
            return false;
        }catch (Exception ex) {
            logger.error(ex.getMessage(),ex);
            return false;
        }
    } 

    public boolean retry(RetryException e) {
        if (e.getException() instanceof ResourceAccessException){
            return true;
        }
        return false;
    } 

	private static void checkEmail(Serializable o) {
		if (o instanceof Customer && StringUtils.isEmpty(((Customer) o).getEmail())) {
			throw new ResourceAccessException("Email is required");
		}
		
	} 

   public void testFailure() {

      new Expectations() {
         {
            injectableRestTemplate.postForEntity(injectableURI, "testRequestBody", String.class);
            result = new ResourceAccessException("testException123");
         }
      };

      testSDWDepositor.deposit("testRequestBody");
   }