use of org.springframework.web.client.RestTemplate in project spring-cloud-config by spring-cloud.
the class ConfigServiceBootstrapConfigurationTest method overrideConfigServicePropertySourceLocatorWhenBeanIsProvided.
@Test
public void overrideConfigServicePropertySourceLocatorWhenBeanIsProvided() {
EnvironmentTestUtils.addEnvironment(this.context, "spring.cloud.config.enabled=true");
this.context.register(ConfigServicePropertySourceLocatorOverrideConfig.class);
this.context.register(ConfigServiceBootstrapConfiguration.class);
this.context.refresh();
ConfigServicePropertySourceLocator locator = this.context.getBean(ConfigServicePropertySourceLocator.class);
Field restTemplateField = ReflectionUtils.findField(ConfigServicePropertySourceLocator.class, "restTemplate");
restTemplateField.setAccessible(true);
RestTemplate restTemplate = (RestTemplate) ReflectionUtils.getField(restTemplateField, locator);
assertThat(restTemplate).isNotNull();
}
use of org.springframework.web.client.RestTemplate in project spring-cloud-config by spring-cloud.
the class ConfigServicePropertySourceLocator method getSecureRestTemplate.
private RestTemplate getSecureRestTemplate(ConfigClientProperties client) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
// TODO 3m5s, make configurable?
requestFactory.setReadTimeout((60 * 1000 * 3) + 5000);
RestTemplate template = new RestTemplate(requestFactory);
String username = client.getUsername();
String password = client.getPassword();
String authorization = client.getAuthorization();
Map<String, String> headers = new HashMap<>(client.getHeaders());
if (password != null && authorization != null) {
throw new IllegalStateException("You must set either 'password' or 'authorization'");
}
if (password != null) {
byte[] token = Base64Utils.encode((username + ":" + password).getBytes());
headers.put("Authorization", "Basic " + new String(token));
} else if (authorization != null) {
headers.put("Authorization", authorization);
}
if (!headers.isEmpty()) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(new GenericRequestHeaderInterceptor(headers)));
}
return template;
}
use of org.springframework.web.client.RestTemplate in project spring-cloud-config by spring-cloud.
the class ConfigServicePropertySourceLocatorTests method failFast.
@Test
public void failFast() throws Exception {
ClientHttpRequestFactory requestFactory = Mockito.mock(ClientHttpRequestFactory.class);
ClientHttpRequest request = Mockito.mock(ClientHttpRequest.class);
ClientHttpResponse response = Mockito.mock(ClientHttpResponse.class);
Mockito.when(requestFactory.createRequest(Mockito.any(URI.class), Mockito.any(HttpMethod.class))).thenReturn(request);
RestTemplate restTemplate = new RestTemplate(requestFactory);
ConfigClientProperties defaults = new ConfigClientProperties(this.environment);
defaults.setFailFast(true);
this.locator = new ConfigServicePropertySourceLocator(defaults);
Mockito.when(request.getHeaders()).thenReturn(new HttpHeaders());
Mockito.when(request.execute()).thenReturn(response);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Mockito.when(response.getHeaders()).thenReturn(headers);
Mockito.when(response.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
Mockito.when(response.getBody()).thenReturn(new ByteArrayInputStream("{}".getBytes()));
this.locator.setRestTemplate(restTemplate);
this.expected.expectCause(IsInstanceOf.instanceOf(IllegalArgumentException.class));
this.expected.expectMessage("fail fast property is set");
this.locator.locate(this.environment);
}
use of org.springframework.web.client.RestTemplate in project spring-cloud-config by spring-cloud.
the class ConfigServicePropertySourceLocatorTests method interceptorShouldAddHeaderWhenAuthorizationPropertySet.
@Test
public void interceptorShouldAddHeaderWhenAuthorizationPropertySet() throws Exception {
ClientHttpRequestFactory requestFactory = Mockito.mock(ClientHttpRequestFactory.class);
ClientHttpRequest request = Mockito.mock(ClientHttpRequest.class);
Mockito.when(requestFactory.createRequest(Mockito.any(URI.class), Mockito.any(HttpMethod.class))).thenReturn(request);
ConfigClientProperties defaults = new ConfigClientProperties(this.environment);
defaults.setAuthorization("Basic dXNlcm5hbWU6cGFzc3dvcmQ=");
this.locator = new ConfigServicePropertySourceLocator(defaults);
RestTemplate restTemplate = ReflectionTestUtils.invokeMethod(this.locator, "getSecureRestTemplate", defaults);
restTemplate.setRequestFactory(requestFactory);
this.locator.setRestTemplate(restTemplate);
this.locator.locate(this.environment);
assertThat(restTemplate.getInterceptors()).hasSize(1);
}
use of org.springframework.web.client.RestTemplate in project webanno by webanno.
the class WebhookService method onApplicationEvent.
@TransactionalEventListener(fallbackExecution = true)
@Async
public void onApplicationEvent(ApplicationEvent aEvent) {
String topic = EVENT_TOPICS.get(aEvent.getClass());
if (topic == null) {
return;
}
Object message;
switch(topic) {
case PROJECT_STATE:
message = new ProjectStateChangeMessage((ProjectStateChangedEvent) aEvent);
break;
case DOCUMENT_STATE:
message = new DocumentStateChangeMessage((DocumentStateChangedEvent) aEvent);
break;
case ANNOTATION_STATE:
message = new AnnotationStateChangeMessage((AnnotationStateChangeEvent) aEvent);
break;
default:
return;
}
for (Webhook hook : configuration.getGlobalHooks()) {
if (!hook.isEnabled() || !hook.getTopics().contains(topic)) {
continue;
}
try {
// Configure rest template without SSL certification check if that is disabled.
RestTemplate restTemplate;
if (hook.isVerifyCertificates()) {
restTemplate = restTemplateBuilder.build();
} else {
restTemplate = restTemplateBuilder.requestFactory(getNonValidatingRequestFactory()).build();
}
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
requestHeaders.set(X_AERO_NOTIFICATION, topic);
// If a secret is set, then add a digest header that allows the client to verify
// the message integrity
String json = JSONUtil.toJsonString(message);
if (isNotBlank(hook.getSecret())) {
String digest = DigestUtils.shaHex(hook.getSecret() + json);
requestHeaders.set(X_AERO_SIGNATURE, digest);
}
HttpEntity<?> httpEntity = new HttpEntity<Object>(json, requestHeaders);
restTemplate.postForEntity(hook.getUrl(), httpEntity, Void.class);
} catch (Exception e) {
log.error("Unable to invoke webhook [{}]", hook, e);
}
}
}
Aggregations