Skip to content
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

Ensure HttpClientOptions is passed fully to HttpClientBuilders #31079

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ the main ServiceBusClientBuilder. -->
<!-- Some classes are named *Builder but are not @ServiceClientBuilder -->
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" files="com.azure.core.http.netty.NettyAsyncHttpClientBuilder"/>
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" files="com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder"/>
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" files="com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder"/>
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" files="com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder"/>
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" files="com.azure.core.http.vertx.VertxAsyncHttpClientBuilder"/>

<!-- False positive in this case : This should not change behavior for UseCaughtExceptionCauseCheck when checking for throwing caught exceptions -->
Expand Down
6 changes: 3 additions & 3 deletions sdk/core/azure-core-http-jdk-httpclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ The following sections provide several code snippets covering some of the most c
Create a HttpClient.

```java readme-sample-createBasicClient
HttpClient client = new JdkAsyncHttpClientBuilder().build();
HttpClient client = new JdkHttpClientBuilder().build();
```

Create a HttpClient using a connection timeout of 60 seconds.

```java readme-sample-createClientWithConnectionTimeout
HttpClient client = new JdkAsyncHttpClientBuilder().connectionTimeout(Duration.ofSeconds(60)).build();
HttpClient client = new JdkHttpClientBuilder().connectionTimeout(Duration.ofSeconds(60)).build();
```

### Create a Client with Proxy

Create a HttpClient that is using a proxy.

```java readme-sample-createProxyClient
HttpClient client = new JdkAsyncHttpClientBuilder()
HttpClient client = new JdkHttpClientBuilder()
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
.build();
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Builder to configure and build an instance of the azure-core {@link HttpClient} type using the JDK HttpClient APIs,
* first introduced as preview in JDK 9, but made generally available from JDK 11 onwards.
*/
public class JdkAsyncHttpClientBuilder {
public class JdkHttpClientBuilder {

private static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(60);
private static final String JAVA_HOME = System.getProperty("java.home");
Expand All @@ -55,7 +55,7 @@ public class JdkAsyncHttpClientBuilder {
DEFAULT_RESTRICTED_HEADERS = Collections.unmodifiableSet(treeSet);
}

private static final ClientLogger LOGGER = new ClientLogger(JdkAsyncHttpClientBuilder.class);
private static final ClientLogger LOGGER = new ClientLogger(JdkHttpClientBuilder.class);

private java.net.http.HttpClient.Builder httpClientBuilder;
private Duration connectionTimeout;
Expand All @@ -64,18 +64,18 @@ public class JdkAsyncHttpClientBuilder {
private Executor executor;

/**
* Creates JdkAsyncHttpClientBuilder.
* Creates JdkHttpClientBuilder.
*/
public JdkAsyncHttpClientBuilder() {
public JdkHttpClientBuilder() {
}

/**
* Creates JdkAsyncHttpClientBuilder from the builder of an existing {@link java.net.http.HttpClient.Builder}.
* Creates JdkHttpClientBuilder from the builder of an existing {@link java.net.http.HttpClient.Builder}.
*
* @param httpClientBuilder the HttpClient builder to use
* @throws NullPointerException if {@code httpClientBuilder} is null
*/
public JdkAsyncHttpClientBuilder(java.net.http.HttpClient.Builder httpClientBuilder) {
public JdkHttpClientBuilder(java.net.http.HttpClient.Builder httpClientBuilder) {
this.httpClientBuilder = Objects.requireNonNull(httpClientBuilder, "'httpClientBuilder' cannot be null.");
}

Expand All @@ -86,10 +86,10 @@ public JdkAsyncHttpClientBuilder(java.net.http.HttpClient.Builder httpClientBuil
* newly built {@code HttpClient}.
*
* @param executor the executor to be used for asynchronous and dependent tasks
* @return the updated JdkAsyncHttpClientBuilder object
* @return the updated JdkHttpClientBuilder object
* @throws NullPointerException if {@code executor} is null
*/
public JdkAsyncHttpClientBuilder executor(Executor executor) {
public JdkHttpClientBuilder executor(Executor executor) {
this.executor = Objects.requireNonNull(executor, "executor can not be null");
return this;
}
Expand All @@ -99,20 +99,20 @@ public JdkAsyncHttpClientBuilder executor(Executor executor) {
*
* <p><strong>Code Samples</strong></p>
*
* <!-- src_embed com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.connectionTimeout#Duration -->
* <!-- src_embed com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.connectionTimeout#Duration -->
* <pre>
* HttpClient client = new JdkAsyncHttpClientBuilder&#40;&#41;
* HttpClient client = new JdkHttpClientBuilder&#40;&#41;
* .connectionTimeout&#40;Duration.ofSeconds&#40;250&#41;&#41; &#47;&#47; connection timeout of 250 seconds
* .build&#40;&#41;;
* </pre>
* <!-- end com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.connectionTimeout#Duration -->
* <!-- end com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.connectionTimeout#Duration -->
*
* The default connection timeout is 60 seconds.
*
* @param connectionTimeout the connection timeout
* @return the updated JdkAsyncHttpClientBuilder object
* @return the updated JdkHttpClientBuilder object
*/
public JdkAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
public JdkHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
// setConnectionTimeout can be null
this.connectionTimeout = connectionTimeout;
return this;
Expand All @@ -123,22 +123,22 @@ public JdkAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
*
* <p><strong>Code Samples</strong></p>
*
* <!-- src_embed com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.proxy#ProxyOptions -->
* <!-- src_embed com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.proxy#ProxyOptions -->
* <pre>
* final String proxyHost = &quot;&lt;proxy-host&gt;&quot;; &#47;&#47; e.g. localhost
* final int proxyPort = 9999; &#47;&#47; Proxy port
* ProxyOptions proxyOptions = new ProxyOptions&#40;ProxyOptions.Type.HTTP,
* new InetSocketAddress&#40;proxyHost, proxyPort&#41;&#41;;
* HttpClient client = new JdkAsyncHttpClientBuilder&#40;&#41;
* HttpClient client = new JdkHttpClientBuilder&#40;&#41;
* .proxy&#40;proxyOptions&#41;
* .build&#40;&#41;;
* </pre>
* <!-- end com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.proxy#ProxyOptions -->
* <!-- end com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.proxy#ProxyOptions -->
*
* @param proxyOptions The proxy configuration to use.
* @return the updated {@link JdkAsyncHttpClientBuilder} object
* @return the updated JdkHttpClientBuilder object
*/
public JdkAsyncHttpClientBuilder proxy(ProxyOptions proxyOptions) {
public JdkHttpClientBuilder proxy(ProxyOptions proxyOptions) {
// proxyOptions can be null
this.proxyOptions = proxyOptions;
return this;
Expand All @@ -151,9 +151,9 @@ public JdkAsyncHttpClientBuilder proxy(ProxyOptions proxyOptions) {
* configuration store}, use {@link Configuration#NONE} to bypass using configuration settings during construction.
*
* @param configuration The configuration store used to
* @return The updated JdkAsyncHttpClientBuilder object.
* @return The updated JdkHttpClientBuilder object.
*/
public JdkAsyncHttpClientBuilder configuration(Configuration configuration) {
public JdkHttpClientBuilder configuration(Configuration configuration) {
this.configuration = configuration;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpClientProvider;
import com.azure.core.util.Configuration;
import com.azure.core.util.HttpClientOptions;

/**
* An {@link HttpClientProvider} that provides an implementation of HttpClient based on native JDK HttpClient.
Expand All @@ -20,7 +21,7 @@ public final class JdkHttpClientProvider implements HttpClientProvider {

// Enum Singleton Pattern
private enum GlobalJdkAsyncHttpClient {
HTTP_CLIENT(new JdkAsyncHttpClientBuilder().build());
HTTP_CLIENT(new JdkHttpClientBuilder().build());

private final HttpClient httpClient;

Expand Down Expand Up @@ -50,6 +51,20 @@ public HttpClient createInstance() {
if (enableHttpClientSharing) {
return GlobalJdkAsyncHttpClient.HTTP_CLIENT.getHttpClient();
}
return new JdkAsyncHttpClientBuilder().build();
return new JdkHttpClientBuilder().build();
}

@Override
public HttpClient createInstance(HttpClientOptions clientOptions) {
if (clientOptions == null) {
return createInstance();
}

JdkHttpClientBuilder builder = new JdkHttpClientBuilder();
builder = builder.proxy(clientOptions.getProxyOptions())
.configuration(clientOptions.getConfiguration())
.connectionTimeout(clientOptions.getConnectTimeout());

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@
import java.time.Duration;

/**
* Code snippets for {@link JdkAsyncHttpClientBuilder}
* Code snippets for {@link JdkHttpClientBuilder}
*/
public class JdkAsyncHttpClientBuilderJavaDocCodeSnippets {
public class JdkHttpClientBuilderJavaDocCodeSnippets {

/**
* Code snippet for simple http client instantiation.
*/
public void simpleInstantiation() {
// BEGIN: com.azure.core.http.jdk.httpclient.instantiation-simple
HttpClient client = new JdkAsyncHttpClientBuilder()
HttpClient client = new JdkHttpClientBuilder()
.build();
// END: com.azure.core.http.jdk.httpclient.instantiation-simple
}

public void proxySample() {
// BEGIN: com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.proxy#ProxyOptions
// BEGIN: com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.proxy#ProxyOptions
final String proxyHost = "<proxy-host>"; // e.g. localhost
final int proxyPort = 9999; // Proxy port
ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort));
HttpClient client = new JdkAsyncHttpClientBuilder()
HttpClient client = new JdkHttpClientBuilder()
.proxy(proxyOptions)
.build();
// END: com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.proxy#ProxyOptions
// END: com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.proxy#ProxyOptions

}

public void proxyBasicAuthenticationSample() {

// BEGIN: com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder#setProxyAuthenticator
// BEGIN: com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder#setProxyAuthenticator
final String proxyHost = "<proxy-host>"; // e.g. localhost
final int proxyPort = 9999; // Proxy port
final String proxyUser = "<proxy-user>";
Expand All @@ -48,20 +48,20 @@ public void proxyBasicAuthenticationSample() {
ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort));
proxyOptions = proxyOptions.setCredentials(proxyUser, proxyPassword);
HttpClient client = new JdkAsyncHttpClientBuilder()
HttpClient client = new JdkHttpClientBuilder()
.proxy(proxyOptions)
.build();
// END: com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder#setProxyAuthenticator
// END: com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder#setProxyAuthenticator

}

public void connectionTimeoutSample() {

// BEGIN: com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.connectionTimeout#Duration
HttpClient client = new JdkAsyncHttpClientBuilder()
// BEGIN: com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.connectionTimeout#Duration
HttpClient client = new JdkHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(250)) // connection timeout of 250 seconds
.build();
// END: com.azure.core.http.jdk.httpclient.JdkAsyncHttpClientBuilder.connectionTimeout#Duration
// END: com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder.connectionTimeout#Duration

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ReadmeSamples {
*/
public void createBasicClient() {
// BEGIN: readme-sample-createBasicClient
HttpClient client = new JdkAsyncHttpClientBuilder().build();
HttpClient client = new JdkHttpClientBuilder().build();
// END: readme-sample-createBasicClient
}

Expand All @@ -31,7 +31,7 @@ public void createBasicClient() {
*/
public void createClientWithConnectionTimeout() {
// BEGIN: readme-sample-createClientWithConnectionTimeout
HttpClient client = new JdkAsyncHttpClientBuilder().connectionTimeout(Duration.ofSeconds(60)).build();
HttpClient client = new JdkHttpClientBuilder().connectionTimeout(Duration.ofSeconds(60)).build();
// END: readme-sample-createClientWithConnectionTimeout
}

Expand All @@ -40,7 +40,7 @@ public void createClientWithConnectionTimeout() {
*/
public void createProxyClient() {
// BEGIN: readme-sample-createProxyClient
HttpClient client = new JdkAsyncHttpClientBuilder()
HttpClient client = new JdkHttpClientBuilder()
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
.build();
// END: readme-sample-createProxyClient
Expand Down
Loading