use of org.springframework.http.client.ClientHttpRequestExecution in project spring-framework by spring-projects.
the class BasicAuthorizationInterceptorTests method interceptShouldAddHeader.
@Test
public void interceptShouldAddHeader() throws Exception {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
byte[] body = new byte[] {};
new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body, execution);
verify(execution).execute(request, body);
assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization"));
}
use of org.springframework.http.client.ClientHttpRequestExecution in project spring-security-oauth by spring-projects.
the class ImplicitProviderTests method testPostForAutomaticApprovalToken.
@Test
@OAuth2ContextConfiguration(resource = AutoApproveImplicit.class, initialize = false)
public void testPostForAutomaticApprovalToken() throws Exception {
final ImplicitAccessTokenProvider implicitProvider = new ImplicitAccessTokenProvider();
implicitProvider.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(new ClientHttpRequestInterceptor() {
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse result = execution.execute(request, body);
latestHeaders = result.getHeaders();
return result;
}
}));
context.setAccessTokenProvider(implicitProvider);
context.getAccessTokenRequest().setCookie(cookie);
assertNotNull(context.getAccessToken());
assertTrue("Wrong location header: " + latestHeaders.getLocation().getFragment(), latestHeaders.getLocation().getFragment().contains("scope=read write trust"));
}
use of org.springframework.http.client.ClientHttpRequestExecution in project ETSMobile-Android2 by ApplETS.
the class MonETSNotificationsRequest method loadDataFromNetwork.
@Override
public MonETSNotificationList loadDataFromNetwork() throws Exception {
accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
if (accounts.length > 0) {
authToken = accountManager.peekAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE);
}
String url = context.getString(R.string.portail_api_base_url);
if (onlyNewNotifs) {
url += "/api/notification/dossier/1";
} else {
url += "/api/notification";
}
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().set("Cookie", authToken);
return execution.execute(requestWrapper, body);
}
};
RestTemplate restTemplate = getRestTemplate();
List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
list.add(interceptor);
restTemplate.setInterceptors(list);
try {
return restTemplate.getForObject(url, MonETSNotificationList.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode().value() == 401) {
if (accounts.length > 0) {
accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
authToken = accountManager.blockingGetAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE, true);
interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().set("Cookie", authToken);
return execution.execute(requestWrapper, body);
}
};
list.clear();
list.add(interceptor);
restTemplate.setInterceptors(list);
}
}
} finally {
return restTemplate.getForObject(url, MonETSNotificationList.class);
}
}
use of org.springframework.http.client.ClientHttpRequestExecution in project sic by belluccifranco.
the class FacturacionIntegrationTest method setup.
@Before
public void setup() {
String md5Test = "098f6bcd4621d373cade4e832627b4f6";
usuarioRepository.save(new UsuarioBuilder().withNombre("test").withPassword(md5Test).build());
// Interceptor de RestTemplate para JWT
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add((ClientHttpRequestInterceptor) (HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> {
request.getHeaders().set("Authorization", "Bearer " + token);
return execution.execute(request, body);
});
restTemplate.getRestTemplate().setInterceptors(interceptors);
// ErrorHandler para RestTemplate
restTemplate.getRestTemplate().setErrorHandler(new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
HttpStatus.Series series = response.getStatusCode().series();
return (HttpStatus.Series.CLIENT_ERROR.equals(series) || HttpStatus.Series.SERVER_ERROR.equals(series));
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
String mensaje = IOUtils.toString(response.getBody());
throw new RestClientResponseException(mensaje, response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), null, Charset.defaultCharset());
}
});
}
Aggregations