use of org.springframework.http.client.SimpleClientHttpRequestFactory 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.SimpleClientHttpRequestFactory in project spring-framework by spring-projects.
the class RequestMappingViewResolutionIntegrationTests method redirect.
// SPR-15291
@Test
public void redirect() throws Exception {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory() {
@Override
protected void prepareConnection(HttpURLConnection conn, String method) throws IOException {
super.prepareConnection(conn, method);
conn.setInstanceFollowRedirects(false);
}
};
URI uri = new URI("http://localhost:" + this.port + "/redirect");
RequestEntity<Void> request = RequestEntity.get(uri).accept(MediaType.ALL).build();
ResponseEntity<Void> response = new RestTemplate(factory).exchange(request, Void.class);
assertEquals(HttpStatus.SEE_OTHER, response.getStatusCode());
assertEquals("/", response.getHeaders().getLocation().toString());
}
use of org.springframework.http.client.SimpleClientHttpRequestFactory in project midpoint by Evolveum.
the class SimpleSmsTransport method send.
@Override
public void send(Message message, String transportName, Event event, Task task, OperationResult parentResult) {
OperationResult result = parentResult.createSubresult(DOT_CLASS + "send");
result.addCollectionOfSerializablesAsParam("message recipient(s)", message.getTo());
result.addParam("message subject", message.getSubject());
SystemConfigurationType systemConfiguration = NotificationFunctionsImpl.getSystemConfiguration(cacheRepositoryService, new OperationResult("dummy"));
if (systemConfiguration == null || systemConfiguration.getNotificationConfiguration() == null) {
String msg = "No notifications are configured. SMS notification to " + message.getTo() + " will not be sent.";
LOGGER.warn(msg);
result.recordWarning(msg);
return;
}
// after "sms:"
String smsConfigName = transportName.length() > NAME.length() ? transportName.substring(NAME.length() + 1) : null;
SmsConfigurationType found = null;
for (SmsConfigurationType smsConfigurationType : systemConfiguration.getNotificationConfiguration().getSms()) {
if ((smsConfigName == null && smsConfigurationType.getName() == null) || (smsConfigName != null && smsConfigName.equals(smsConfigurationType.getName()))) {
found = smsConfigurationType;
break;
}
}
if (found == null) {
String msg = "SMS configuration '" + smsConfigName + "' not found. SMS notification to " + message.getTo() + " will not be sent.";
LOGGER.warn(msg);
result.recordWarning(msg);
return;
}
SmsConfigurationType smsConfigurationType = found;
String file = smsConfigurationType.getRedirectToFile();
if (file != null) {
writeToFile(message, file, null, result);
return;
}
if (smsConfigurationType.getGateway().isEmpty()) {
String msg = "SMS gateway(s) are not defined, notification to " + message.getTo() + " will not be sent.";
LOGGER.warn(msg);
result.recordWarning(msg);
return;
}
String from = smsConfigurationType.getDefaultFrom() != null ? smsConfigurationType.getDefaultFrom() : "";
if (message.getTo().isEmpty()) {
String msg = "There is no recipient to send the notification to.";
LOGGER.warn(msg);
result.recordWarning(msg);
return;
}
String to = message.getTo().get(0);
if (message.getTo().size() > 1) {
String msg = "Currently it is possible to send the SMS to one recipient only. Among " + message.getTo() + " the chosen one is " + to + " (the first one).";
LOGGER.warn(msg);
}
for (SmsGatewayConfigurationType smsGatewayConfigurationType : smsConfigurationType.getGateway()) {
OperationResult resultForGateway = result.createSubresult(DOT_CLASS + "send.forGateway");
resultForGateway.addContext("gateway name", smsGatewayConfigurationType.getName());
try {
String url = evaluateExpressionChecked(smsGatewayConfigurationType.getUrl(), getDefaultVariables(from, to, message), "sms gateway url", task, result);
LOGGER.debug("Sending SMS to URL " + url);
if (smsGatewayConfigurationType.getRedirectToFile() != null) {
writeToFile(message, smsGatewayConfigurationType.getRedirectToFile(), url, resultForGateway);
result.computeStatus();
return;
} else {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = requestFactory.createRequest(new URI(url), HttpMethod.GET);
ClientHttpResponse response = request.execute();
LOGGER.debug("Result: " + response.getStatusCode() + "/" + response.getStatusText());
if (response.getStatusCode().series() != HttpStatus.Series.SUCCESSFUL) {
throw new SystemException("SMS gateway communication failed: " + response.getStatusCode() + ": " + response.getStatusText());
}
LOGGER.info("Message sent successfully to " + message.getTo() + " via gateway " + smsGatewayConfigurationType.getName() + ".");
resultForGateway.recordSuccess();
result.recordSuccess();
return;
}
} catch (Throwable t) {
String msg = "Couldn't send SMS to " + message.getTo() + " via " + smsGatewayConfigurationType.getName() + ", trying another gateway, if there is any";
LoggingUtils.logException(LOGGER, msg, t);
resultForGateway.recordFatalError(msg, t);
}
}
LOGGER.warn("No more SMS gateways to try, notification to " + message.getTo() + " will not be sent.");
result.recordWarning("Notification to " + message.getTo() + " could not be sent.");
}
use of org.springframework.http.client.SimpleClientHttpRequestFactory in project spring-security-oauth by spring-projects.
the class ServerRunning method getRestTemplate.
public RestTemplate getRestTemplate() {
RestTemplate client = new RestTemplate();
client.setRequestFactory(new SimpleClientHttpRequestFactory() {
@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
super.prepareConnection(connection, httpMethod);
connection.setInstanceFollowRedirects(false);
}
});
client.setErrorHandler(new ResponseErrorHandler() {
// Pass errors through in response entity for status code analysis
public boolean hasError(ClientHttpResponse response) throws IOException {
return false;
}
public void handleError(ClientHttpResponse response) throws IOException {
}
});
return client;
}
use of org.springframework.http.client.SimpleClientHttpRequestFactory in project spring-boot by spring-projects.
the class RestTemplateBuilderTests method readTimeoutCanBeConfiguredOnAWrappedRequestFactory.
@Test
public void readTimeoutCanBeConfiguredOnAWrappedRequestFactory() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
this.builder.requestFactory(new BufferingClientHttpRequestFactory(requestFactory)).setReadTimeout(1234).build();
assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout")).isEqualTo(1234);
}
Aggregations