Search in sources :

Example 6 with SimpleClientHttpRequestFactory

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"));
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 7 with SimpleClientHttpRequestFactory

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());
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) HttpURLConnection(java.net.HttpURLConnection) RestTemplate(org.springframework.web.client.RestTemplate) URI(java.net.URI) Test(org.junit.Test)

Example 8 with SimpleClientHttpRequestFactory

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.");
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) SystemException(com.evolveum.midpoint.util.exception.SystemException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 9 with SimpleClientHttpRequestFactory

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;
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) HttpURLConnection(java.net.HttpURLConnection) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) RestTemplate(org.springframework.web.client.RestTemplate) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 10 with SimpleClientHttpRequestFactory

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);
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) BufferingClientHttpRequestFactory(org.springframework.http.client.BufferingClientHttpRequestFactory) Test(org.junit.Test)

Aggregations

SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)10 URI (java.net.URI)5 Test (org.junit.Test)5 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)4 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)4 BufferingClientHttpRequestFactory (org.springframework.http.client.BufferingClientHttpRequestFactory)3 RestTemplate (org.springframework.web.client.RestTemplate)3 HttpURLConnection (java.net.HttpURLConnection)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Proxy (org.springframework.boot.devtools.autoconfigure.RemoteDevToolsProperties.Proxy)1 Bean (org.springframework.context.annotation.Bean)1 ClientHttpRequestExecution (org.springframework.http.client.ClientHttpRequestExecution)1 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)1 InterceptingClientHttpRequestFactory (org.springframework.http.client.InterceptingClientHttpRequestFactory)1 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)1