Skip to content

Apache HttpClient 5.0 followup #1357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@
</configuration>
</plugin>



</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.util.Timeout;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;

/**
* {@link FactoryBean} to set up a {@link CloseableHttpClient} using HttpComponents HttpClient 5.
Expand All @@ -43,6 +44,18 @@
*/
public class HttpComponents5ClientFactory implements FactoryBean<CloseableHttpClient> {

/**
* AuthScope to match any Host.
* <p>
* <b>HEADS-UP</b>: ANY has been removed from {@link AuthScope} since httpcomponents version 5.x. It has been
* redefined here to ease migration from httpcomponents 4. The associated functionality might be removed in a future
* version of apache httpcomponents. Consider using a {@link ClientInterceptor} to implement http client agnostic
* preemptive basic auth.
*
* @see AuthScope#AuthScope(String, String, int, String, String)
*/
public static final AuthScope ANY = new AuthScope(null, null, -1, null, null);

private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ofSeconds(60);

private static final Duration DEFAULT_READ_TIMEOUT = Duration.ofSeconds(60);
Expand All @@ -53,7 +66,7 @@ public class HttpComponents5ClientFactory implements FactoryBean<CloseableHttpCl

private int maxTotalConnections = -1;

private AuthScope authScope = null;
private AuthScope authScope = ANY;

private Credentials credentials = null;

Expand All @@ -78,7 +91,7 @@ public void setCredentials(Credentials credentials) {
/**
* Sets the authentication scope to be used. Only used when the {@code credentials} property has been set.
* <p>
* By default, the {@link AuthScope#ANY} is used.
* By default, the {@link #ANY} is used.
*
* @see #setCredentials(Credentials)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpRequestInterceptor;
import org.apache.hc.core5.http.protocol.HttpContext;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -178,7 +179,10 @@ public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost)

@Override
public void afterPropertiesSet() throws Exception {
this.httpClient = clientFactory.getObject();

if (this.clientFactory != null) {
this.httpClient = clientFactory.getObject();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.springframework.ws.transport.http;

import java.time.Duration;

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

class HttpComponents5MessageSenderTest {

@Test
void afterPropertiesSet_createHttpClient() throws Exception {
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender();
assertThat(messageSender.getHttpClient()).isNull();
Duration timeout = Duration.ofSeconds(1);
assertThatCode(() -> messageSender.setConnectionTimeout(timeout)).doesNotThrowAnyException();
messageSender.afterPropertiesSet();
assertThat(messageSender.getHttpClient()).isNotNull();
}

@Test
void afterPropertiesSet_httpClientAlreadySet() throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender(httpClient);
Duration timeout = Duration.ofSeconds(1);
assertThatCode(() -> messageSender.setConnectionTimeout(timeout)).isInstanceOf(IllegalStateException.class);
messageSender.afterPropertiesSet();
assertThat(messageSender.getHttpClient()).isSameAs(httpClient);
}

}