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

Upgrade to the MicroProfile REST Client 4.0 API #312

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Add the new header() method to the RestClientBuilder implementation.
Signed-off-by: James R. Perkins <jperkins@redhat.com>
jamezp committed Jun 20, 2024
commit 547cdf1803bbef181b775432faea244d15ec9c83
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
@@ -66,6 +67,7 @@
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.ParamConverterProvider;

import org.eclipse.microprofile.config.Config;
@@ -85,6 +87,7 @@
import org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory;
import org.jboss.resteasy.concurrent.ContextualExecutorService;
import org.jboss.resteasy.concurrent.ContextualExecutors;
import org.jboss.resteasy.core.Headers;
import org.jboss.resteasy.microprofile.client.async.AsyncInterceptorRxInvokerProvider;
import org.jboss.resteasy.microprofile.client.async.AsyncInvocationInterceptorThreadContext;
import org.jboss.resteasy.microprofile.client.header.ClientHeadersRequestFilter;
@@ -104,7 +107,6 @@ public class RestClientBuilderImpl implements RestClientBuilder {
private static final DefaultMediaTypeFilter DEFAULT_MEDIA_TYPE_FILTER = new DefaultMediaTypeFilter();
private static final Collection<Method> IGNORED_METHODS = new ArrayList<>();
public static final MethodInjectionFilter METHOD_INJECTION_FILTER = new MethodInjectionFilter();
public static final ClientHeadersRequestFilter HEADERS_REQUEST_FILTER = new ClientHeadersRequestFilter();

static ResteasyProviderFactory PROVIDER_FACTORY;

@@ -117,6 +119,8 @@ public static void setProviderFactory(ResteasyProviderFactory providerFactory) {
PROVIDER_FACTORY = providerFactory;
}

private final MultivaluedMap<String, Object> headers;

public RestClientBuilderImpl() {
builderDelegate = new MpClientBuilderImpl();

@@ -140,6 +144,7 @@ public RestClientBuilderImpl() {
} catch (Throwable e) {

}
headers = new Headers<>();
}

public Configuration getConfigurationWrapper() {
@@ -162,6 +167,13 @@ public RestClientBuilder queryParamStyle(QueryParamStyle queryParamStyle) {
return this;
}

@Override
public RestClientBuilder header(final String name, final Object value) {
headers.add(Objects.requireNonNull(name, "A header name is required."),
Objects.requireNonNull(value, "Value for header is required."));
return this;
}

@Override
public RestClientBuilder proxyAddress(String host, int port) {
if (host == null) {
@@ -323,7 +335,7 @@ public <T> T build(Class<T> aClass, ClientHttpEngine httpEngine)
}
resteasyClientBuilder.register(DEFAULT_MEDIA_TYPE_FILTER);
resteasyClientBuilder.register(METHOD_INJECTION_FILTER);
resteasyClientBuilder.register(HEADERS_REQUEST_FILTER);
resteasyClientBuilder.register(new ClientHeadersRequestFilter(headers));
register(new MpPublisherMessageBodyReader(executorService));
resteasyClientBuilder.sslContext(sslContext);
resteasyClientBuilder.trustStore(trustStore);
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@

import org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory;
import org.eclipse.microprofile.rest.client.ext.DefaultClientHeadersFactoryImpl;
import org.jboss.resteasy.core.Headers;
import org.jboss.resteasy.microprofile.client.impl.MpClientInvocation;
import org.jboss.resteasy.microprofile.client.utils.ClientRequestContextUtils;

@@ -49,6 +50,26 @@ public class ClientHeadersRequestFilter implements ClientRequestFilter {

private static final MultivaluedMap<String, String> EMPTY_MAP = new MultivaluedHashMap<>();

private final MultivaluedMap<String, Object> defaultHeaders;

/**
* Creates a new filter.
*/
public ClientHeadersRequestFilter() {
this.defaultHeaders = new Headers<>();
}

/**
* Creates a new filter which will add each default header by default to the request headers. Note the values of
* the {@code defaultHeaders} will be appended and header values are not replaced.
*
* @param defaultHeaders the default headers to add
*/
public ClientHeadersRequestFilter(final MultivaluedMap<String, Object> defaultHeaders) {
this.defaultHeaders = new Headers<>();
this.defaultHeaders.putAll(defaultHeaders);
}

@Override
public void filter(ClientRequestContext requestContext) {
Method method = ClientRequestContextUtils.getMethod(requestContext);
@@ -80,6 +101,8 @@ public void filter(ClientRequestContext requestContext) {

factory.ifPresent(f -> f.update(incomingHeaders, headers)
.forEach((key, values) -> requestContext.getHeaders().put(key, castToListOfObjects(values))));

this.defaultHeaders.forEach((name, values) -> requestContext.getHeaders().addAll(name, values));
}

private static List<Object> castToListOfObjects(List<String> values) {