List of usage examples for org.springframework.http.client ClientHttpResponse getStatusCode
HttpStatus getStatusCode() throws IOException;
From source file:com.art4ul.jcoonsample.client.service.TestService.java
public void doTest() { restClient.setBaseUrl("http://localhost:8080"); LOG.info("Example #1: Send GET request"); ResultModel result = restClient.exampleGetRequest("world"); LOG.info("simpleGetRequestTest return: {} \n", result.getResult()); LOG.info("Example #2: Send POST request with custom base URL"); UserModel userModel = new UserModel("Tester", "sam", "123456"); result = restClient.examplePostRequest("http://localhost:8080", userModel); LOG.info("simpleGetRequestTest return: {} \n", result.getResult()); LOG.info("Example #3: Send POST request with custom HTTP header"); result = restClient.examplePostRequestWithHeader(userModel, "test value"); LOG.info("simpleGetRequestTest return: {} \n", result.getResult()); LOG.info("Example #4: Send Get request with path variable"); result = restClient.exampleGetRequestWithPathVariable("test"); LOG.info("simpleGetRequestTest return: {} \n", result.getResult()); LOG.info("Example #5: Handle HTTP exception"); ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() { @Override/*from w w w . jav a 2s . c o m*/ public boolean hasError(ClientHttpResponse response) throws IOException { return true; } @Override public void handleError(ClientHttpResponse response) throws IOException { throw new CustomException( "HTTP code: " + response.getStatusCode() + " text: " + response.getStatusText()); } }; try { result = restClient.exampleException("test", responseErrorHandler); } catch (CustomException ex) { LOG.info("Exception: " + ex.getMessage()); } }
From source file:com.neiljbrown.brighttalk.channels.reportingapi.client.spring.ApiResponseErrorHandlerTest.java
/** * Creates a mock implementation of {@link ClientHttpResponse} and configures it to return supplied data. * <p>// w w w . ja va 2 s. c om * ClientHttpResponse needs mocking for test fixtures which rely on setting response headers as for some reason Spring * 4.0's MockClientHttpResponse doesn't support setting response headers. * * @param httpStatus The {@link HttpStatus} which the mock should be configured to return for invocations of * {@link ClientHttpResponse#getStatusCode()}. * @param headers The {@link HttpHeaders} which the mock should be configured to return for invocations of * {@link ClientHttpResponse#getHeaders()}. * @param body A string representation of the data which the mock should be configured to return for invocations of * {@link ClientHttpResponse#getBody()}. * @return The created {@link ClientHttpResponse}. */ private ClientHttpResponse createMockClientHttpResponse(HttpStatus httpStatus, HttpHeaders headers, final String body) { ClientHttpResponse mockClientHttpResponse = EasyMock.createNiceMock(ClientHttpResponse.class); try { EasyMock.expect(mockClientHttpResponse.getStatusCode()).andReturn(httpStatus).anyTimes(); EasyMock.expect(mockClientHttpResponse.getRawStatusCode()).andReturn(httpStatus.value()).anyTimes(); EasyMock.expect(mockClientHttpResponse.getHeaders()).andReturn(headers).anyTimes(); // Return a new InputStream for each call to ClientHttpResponse.getBody() rather than an exhausted one IAnswer<InputStream> iAnswer = new IAnswer<InputStream>() { public InputStream answer() { return body != null ? new ByteArrayInputStream(body.getBytes()) : null; } }; EasyMock.expect(mockClientHttpResponse.getBody()).andAnswer(iAnswer).anyTimes(); } catch (IOException e) { throw new RuntimeException(e); } return mockClientHttpResponse; }
From source file:sparklr.common.AbstractEmptyAuthorizationCodeProviderTests.java
@BeforeOAuth2Context public void setupAccessTokenProvider() { accessTokenProvider = new AuthorizationCodeAccessTokenProvider() { private ResponseExtractor<OAuth2AccessToken> extractor = super.getResponseExtractor(); private ResponseExtractor<ResponseEntity<Void>> authExtractor = super.getAuthorizationResponseExtractor(); private ResponseErrorHandler errorHandler = super.getResponseErrorHandler(); @Override/* w w w . ja va 2s.c om*/ protected ResponseErrorHandler getResponseErrorHandler() { return new DefaultResponseErrorHandler() { public void handleError(ClientHttpResponse response) throws IOException { response.getHeaders(); response.getStatusCode(); tokenEndpointResponse = response; errorHandler.handleError(response); } }; } @Override protected ResponseExtractor<OAuth2AccessToken> getResponseExtractor() { return new ResponseExtractor<OAuth2AccessToken>() { public OAuth2AccessToken extractData(ClientHttpResponse response) throws IOException { try { response.getHeaders(); response.getStatusCode(); tokenEndpointResponse = response; return extractor.extractData(response); } catch (ResourceAccessException e) { return null; } } }; } @Override protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() { return new ResponseExtractor<ResponseEntity<Void>>() { public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException { response.getHeaders(); response.getStatusCode(); tokenEndpointResponse = response; return authExtractor.extractData(response); } }; } }; context.setAccessTokenProvider(getAccessTokenProvider()); }
From source file:org.springframework.social.mixcloud.api.impl.MixcloudErrorHandler.java
@Override public void handleError(ClientHttpResponse response) throws IOException { SocialException exception = extractExceptionFromResponse(response); if (exception == null) { handleUncategorizedError(response, exception); }/*from ww w.j a va2 s .com*/ handleMixcloudError(response.getStatusCode(), exception); // if not otherwise handled, do default handling and wrap with // UncategorizedApiException handleUncategorizedError(response, exception); }
From source file:org.springframework.social.lastfm.api.impl.LastFmErrorHandler.java
@Override public void handleError(ClientHttpResponse response) throws IOException { Map<Integer, String> errorDetails = extractErrorDetailsFromResponse(response); if (errorDetails == null || errorDetails.size() == 0) { handleUncategorizedError(response, errorDetails); }// w w w . j a v a2 s.c o m handleLastFmError(response.getStatusCode(), errorDetails); // if not otherwise handled, do default handling and wrap with // UncategorizedApiException handleUncategorizedError(response, errorDetails); }
From source file:com.zhm.config.MyAuthorizationCodeAccessTokenProvider.java
protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() { return new ResponseExtractor<ResponseEntity<Void>>() { public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException { return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode()); }//from w w w .j av a2 s. co m }; }
From source file:org.springframework.social.exfm.api.impl.ExFmErrorHandler.java
@Override public void handleError(ClientHttpResponse response) throws IOException { Status errorDetails = extractErrorDetailsFromResponse(response); if (errorDetails == null || errorDetails.getStatus_code().equals("200")) { handleUncategorizedError(response, errorDetails); }/*from w w w .j a va 2 s .co m*/ handleExFmError(response.getStatusCode(), errorDetails); // if not otherwise handled, do default handling and wrap with // UncategorizedApiException handleUncategorizedError(response, errorDetails); }
From source file:com.pepaproch.gtswsdl.LoginRequestInterceptor.java
private ClientHttpResponse logResponse(ClientHttpResponse response) throws IOException { ClientHttpResponse result = new ClientHttpResponse() { InputStream in;// w w w . j a v a 2 s . co m InputStream oldIn = response.getBody(); String jsonResponse; @Override public HttpStatus getStatusCode() throws IOException { return response.getStatusCode(); } @Override public int getRawStatusCode() throws IOException { return response.getRawStatusCode(); } @Override public String getStatusText() throws IOException { return response.getStatusText(); } @Override public void close() { try { Logger.getLogger(LoginRequestInterceptor.class).log(Level.DEBUG, jsonResponse); in.close(); oldIn.close(); } catch (IOException ex) { java.util.logging.Logger.getLogger(LoginRequestInterceptor.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } } @Override public InputStream getBody() throws IOException { if (in == null) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { byte[] readBuffer = new byte[4 * 1024]; int lenght; while ((lenght = oldIn.read(readBuffer)) != -1) { baos.write(readBuffer, 0, lenght); } baos.flush(); jsonResponse = baos.toString(); in = new ByteArrayInputStream(baos.toByteArray()); } } return in; } @Override public HttpHeaders getHeaders() { return response.getHeaders(); } }; return result; }
From source file:org.springframework.social.soundcloud.api.impl.SoundCloudErrorHandler.java
@Override public void handleError(ClientHttpResponse response) throws IOException { List<Map<String, String>> errorDetails = extractErrorDetailsFromResponse(response); if (errorDetails == null) { handleUncategorizedError(response, errorDetails); }/*from www .j av a 2s . co m*/ handleSoundCloudError(response.getStatusCode(), errorDetails); // if not otherwise handled, do default handling and wrap with UncategorizedApiException handleUncategorizedError(response, errorDetails); }
From source file:org.opencredo.couchdb.CouchDbIntegrationTest.java
@Before public void setUpTestDatabase() throws Exception { assumeDatabaseIsUpAndRunning();/*from ww w . j a va2 s . co m*/ RestTemplate template; if (credentials.length > 0) template = new BasicAuthRestTemplate(credentials[0], credentials[1], databaseUrl()); else template = new BasicAuthRestTemplate(); template.setErrorHandler(new DefaultResponseErrorHandler() { @Override public void handleError(ClientHttpResponse response) throws IOException { // do nothing, error status will be handled in the switch statement } }); ResponseEntity<String> response = template.getForEntity(databaseUrl(), String.class); HttpStatus statusCode = response.getStatusCode(); switch (statusCode) { case NOT_FOUND: createNewTestDatabase(); break; case OK: deleteExisitingTestDatabase(); createNewTestDatabase(); break; default: throw new IllegalStateException("Unsupported http status [" + statusCode + "]"); } }