Skip to content

Commit

Permalink
Nima webclient support for properties (#7028)
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <thibault.vallin@oracle.com>
  • Loading branch information
tvallin authored Jun 21, 2023
1 parent d1000c3 commit 61fd0fd
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ClientRequestImpl implements Http2ClientRequest {
private final int maxFrameSize;
private final long maxHeaderListSize;
private final int connectionPrefetch;
private final Map<String, String> properties;

private WritableHeaders<?> explicitHeaders;
private Tls tls;
Expand Down Expand Up @@ -84,6 +85,7 @@ class ClientRequestImpl implements Http2ClientRequest {
this.maxFrameSize = client.maxFrameSize();
this.maxHeaderListSize = client.maxHeaderListSize();
this.connectionPrefetch = client.prefetch();
this.properties = client.properties();
this.tls = tls == null || !tls.enabled() ? null : tls;
this.query = query;
this.followRedirects = client.followRedirects();
Expand Down Expand Up @@ -204,6 +206,12 @@ public Http2ClientRequest skipUriEncoding() {
return this;
}

@Override
public Http2ClientRequest property(String propertyName, String propertyValue) {
properties.put(propertyName, propertyValue);
return this;
}

@Override
public Http2ClientRequest priority(int priority) {
if (priority < 1 || priority > 256) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ default <T> T request(Class<T> type) {
*/
B skipUriEncoding();

/**
* Add a property to be used by this request.
*
* @param propertyName property name
* @param propertyValue property value
* @return updated builder instance
*/
B property(String propertyName, String propertyValue);

/**
* Handle output stream.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.helidon.nima.webclient;

import java.net.URI;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -47,6 +48,7 @@ public class LoomClient implements WebClient {
private final boolean followRedirects;
private final Headers defaultHeaders;
private final ParserMode mediaTypeParserMode;
private final Map<String, String> properties;

/**
* Construct this instance from a subclass of builder.
Expand All @@ -63,6 +65,7 @@ protected LoomClient(WebClient.Builder<?, ?> builder) {
this.followRedirects = builder.followRedirect();
this.defaultHeaders = builder.defaultHeaders();
this.mediaTypeParserMode = builder.mediaTypeParserMode();
this.properties = builder.properties();
}

/**
Expand Down Expand Up @@ -146,6 +149,15 @@ public Headers defaultHeaders() {
return defaultHeaders;
}

/**
* Properties configured for this client.
*
* @return properties
*/
public Map<String, String> properties() {
return properties;
}

/**
* Media type parsing mode for HTTP {@code Content-Type} header.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.net.URI;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -78,6 +80,7 @@ abstract class Builder<B extends Builder<B, C>, C extends WebClient> implements
private int maxRedirect;
private WritableHeaders<?> defaultHeaders = WritableHeaders.create();
private ParserMode mediaTypeParserMode = ParserMode.STRICT;
private Map<String, String> properties = new HashMap<>();

/**
* Common builder base for all the client builder.
Expand Down Expand Up @@ -298,6 +301,18 @@ public B headers(Function<ClientRequestHeaders, WritableHeaders<?>> headersConsu
return identity();
}

/**
* Properties configured by user when creating this client.
*
* @param properties that were configured (mutable)
* @return updated builder instance
*/
public B properties(Map<String, String> properties) {
Objects.requireNonNull(properties);
this.properties = properties;
return identity();
}

/**
* Remove header with the selected name from the default headers.
*
Expand Down Expand Up @@ -372,5 +387,8 @@ int maxRedirect() {
return maxRedirect;
}

Map<String, String> properties() {
return properties;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ClientRequestImpl implements Http1ClientRequest {
private final String requestId;
private final Http1ClientConfig clientConfig;
private final MediaContext mediaContext;
private final Map<String, String> properties;

private WritableHeaders<?> explicitHeaders = WritableHeaders.create();
private boolean followRedirects;
Expand All @@ -67,9 +68,11 @@ class ClientRequestImpl implements Http1ClientRequest {
ClientRequestImpl(Http1ClientConfig clientConfig,
Http.Method method,
UriHelper helper,
UriQueryWriteable query) {
UriQueryWriteable query,
Map<String, String> properties) {
this.method = method;
this.uri = helper;
this.properties = properties;

this.clientConfig = clientConfig;
this.mediaContext = clientConfig.mediaContext();
Expand All @@ -86,8 +89,9 @@ class ClientRequestImpl implements Http1ClientRequest {
private ClientRequestImpl(ClientRequestImpl request,
Http.Method method,
UriHelper helper,
UriQueryWriteable query) {
this(request.clientConfig, method, helper, query);
UriQueryWriteable query,
Map<String, String> properties) {
this(request.clientConfig, method, helper, query, properties);
this.followRedirects = request.followRedirects;
this.maxRedirects = request.maxRedirects;
this.tls = request.tls;
Expand Down Expand Up @@ -224,6 +228,12 @@ public Http1ClientRequest skipUriEncoding() {
return this;
}

@Override
public Http1ClientRequest property(String propertyName, String propertyValue) {
this.properties.put(propertyName, propertyValue);
return this;
}

Http1ClientConfig clientConfig() {
return clientConfig;
}
Expand Down Expand Up @@ -271,11 +281,11 @@ private ClientResponseImpl invokeWithFollowRedirectsEntity(Object entity) {
//Method and entity is required to be the same as with original request with 307 and 308 requests
if (clientResponse.status() == Http.Status.TEMPORARY_REDIRECT_307
|| clientResponse.status() == Http.Status.PERMANENT_REDIRECT_308) {
clientRequest = new ClientRequestImpl(this, method, redirectUri, newQuery);
clientRequest = new ClientRequestImpl(this, method, redirectUri, newQuery, properties);
} else {
//It is possible to change to GET and send no entity with all other redirect codes
entityToBeSent = BufferData.EMPTY_BYTES; //We do not want to send entity after this redirect
clientRequest = new ClientRequestImpl(this, Http.Method.GET, redirectUri, newQuery);
clientRequest = new ClientRequestImpl(this, Http.Method.GET, redirectUri, newQuery, properties);
}
}
throw new IllegalStateException("Maximum number of request redirections ("
Expand Down Expand Up @@ -324,8 +334,6 @@ private ClientResponseImpl invokeServices(WebClientService.Chain callChain,

ClientRequestHeaders headers = ClientRequestHeaders.create(explicitHeaders);

Map<String, String> properties = new HashMap<>();

WebClientServiceRequest serviceRequest = new ServiceRequestImpl(uri,
method,
Http.Version.V1_1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Http1ClientRequest method(Http.Method method) {
UriQueryWriteable query = UriQueryWriteable.create();
UriHelper helper = (uri() == null) ? UriHelper.create() : UriHelper.create(uri(), query);

return new ClientRequestImpl(clientConfig, method, helper, query);
return new ClientRequestImpl(clientConfig, method, helper, query, properties());
}

Http1ClientConfig clientConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,10 @@ public FakeHttpClientRequest connection(ClientConnection connection) {
public FakeHttpClientRequest skipUriEncoding() {
return null;
}

@Override
public FakeHttpClientRequest property(String propertyName, String propertyValue) {
return null;
}
}
}

0 comments on commit 61fd0fd

Please sign in to comment.