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

Rest client inbound headers provider added #4593

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.microprofile.restclient;

import java.util.List;
import java.util.Map;

/**
* This provider is executed if no headers found for propagation from inbound request via normal propagation mechanism.
* If headers of the inbound request are found, this provider is ignored.
*
* @author David Kral
*/
public interface InboundHeadersProvider {

/**
* Return inbound request headers.
*
* @return inbound request headers
*/
Map<String, List<String>> inboundHeaders();

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class InterfaceModel {
private final List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories;
private final Set<ResponseExceptionMapper> responseExceptionMappers;
private final Set<ParamConverterProvider> paramConverterProviders;
private final Set<InboundHeadersProvider> inboundHeadersProviders;
private final Set<Annotation> interceptorAnnotations;
private final BeanManager beanManager;

Expand All @@ -82,20 +83,23 @@ class InterfaceModel {
* @param restClientClass interface class
* @param responseExceptionMappers registered exception mappers
* @param paramConverterProviders registered parameter providers
* @param inboundHeadersProviders registered inbound header providers
* @param asyncInterceptorFactories async interceptor factories
* @param injectionManager
* @return new model instance
*/
static InterfaceModel from(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
Set<InboundHeadersProvider> inboundHeadersProviders,
List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories,
InjectionManager injectionManager,
BeanManager beanManager) {
return new Builder(restClientClass,
responseExceptionMappers,
paramConverterProviders,
asyncInterceptorFactories,
inboundHeadersProviders,
injectionManager,
beanManager).build();
}
Expand All @@ -113,6 +117,7 @@ private InterfaceModel(Builder builder) {
this.interceptorAnnotations = builder.interceptorAnnotations;
this.creationalContext = builder.creationalContext;
this.asyncInterceptorFactories = builder.asyncInterceptorFactories;
this.inboundHeadersProviders = builder.inboundHeadersProviders;
this.beanManager = builder.beanManager;
}

Expand Down Expand Up @@ -188,6 +193,15 @@ Set<ResponseExceptionMapper> getResponseExceptionMappers() {
return responseExceptionMappers;
}

/**
* Returns {@link Set} of registered {@link InboundHeadersProvider}
*
* @return registered inbound header providers
*/
Set<InboundHeadersProvider> getInboundHeadersProviders() {
return inboundHeadersProviders;
}

/**
* Returns {@link Set} of registered {@link ParamConverterProvider}
*
Expand Down Expand Up @@ -249,6 +263,7 @@ private static class Builder {

private final Class<?> restClientClass;

private final Set<InboundHeadersProvider> inboundHeadersProviders;
private final InjectionManager injectionManager;
private final BeanManager beanManager;
private String pathValue;
Expand All @@ -266,13 +281,15 @@ private Builder(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories,
Set<InboundHeadersProvider> inboundHeadersProviders,
InjectionManager injectionManager,
BeanManager beanManager) {
this.injectionManager = injectionManager;
this.restClientClass = restClientClass;
this.responseExceptionMappers = responseExceptionMappers;
this.paramConverterProviders = paramConverterProviders;
this.asyncInterceptorFactories = asyncInterceptorFactories;
this.inboundHeadersProviders = inboundHeadersProviders;
this.beanManager = beanManager;
filterAllInterceptorAnnotations();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand Down Expand Up @@ -122,6 +123,7 @@ private MethodModel(Builder builder) {
subResourceModel = RestClientModel.from(returnType.getRawType(),
interfaceModel.getResponseExceptionMappers(),
interfaceModel.getParamConverterProviders(),
interfaceModel.getInboundHeadersProviders(),
interfaceModel.getAsyncInterceptorFactories(),
interfaceModel.getInjectionManager(),
interfaceModel.getBeanManager());
Expand Down Expand Up @@ -374,11 +376,17 @@ private MultivaluedMap<String, String> resolveCustomHeaders(Object[] args) {
args[parameterModel.getParamPosition()]));

MultivaluedMap<String, String> inbound = new MultivaluedHashMap<>();
HeadersContext.get().ifPresent(headersContext -> inbound.putAll(headersContext.inboundHeaders()));
Optional<HeadersContext> headersContext = HeadersContext.get();
headersContext.ifPresent(hc -> inbound.putAll(hc.inboundHeaders()));
if (!headersContext.isPresent()) {
for (InboundHeadersProvider provider : interfaceModel.getInboundHeadersProviders()) {
inbound.putAll(provider.inboundHeaders());
}
}

AtomicReference<MultivaluedMap<String, String>> toReturn = new AtomicReference<>(customHeaders);
interfaceModel.getClientHeadersFactory().ifPresent(clientHeadersFactory -> toReturn
.set(clientHeadersFactory.update(inbound, customHeaders)));
interfaceModel.getClientHeadersFactory()
.ifPresent(clientHeadersFactory -> toReturn.set(clientHeadersFactory.update(inbound, customHeaders)));
return toReturn.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class RestClientBuilderImpl implements RestClientBuilder {

private final Set<ResponseExceptionMapper> responseExceptionMappers;
private final Set<ParamConverterProvider> paramConverterProviders;
private final Set<InboundHeadersProvider> inboundHeaderProviders;
private final List<AsyncInvocationInterceptorFactoryPriorityWrapper> asyncInterceptorFactories;
private final Config config;
private final ConfigWrapper configWrapper;
Expand All @@ -100,6 +101,7 @@ class RestClientBuilderImpl implements RestClientBuilder {
clientBuilder = ClientBuilder.newBuilder();
responseExceptionMappers = new HashSet<>();
paramConverterProviders = new HashSet<>();
inboundHeaderProviders = new HashSet<>();
asyncInterceptorFactories = new ArrayList<>();
config = ConfigProvider.getConfig();
configWrapper = new ConfigWrapper(clientBuilder.getConfiguration());
Expand Down Expand Up @@ -194,6 +196,7 @@ public <T> T build(Class<T> interfaceClass) throws IllegalStateException, RestCl
RestClientModel restClientModel = RestClientModel.from(interfaceClass,
responseExceptionMappers,
paramConverterProviders,
inboundHeaderProviders,
new ArrayList<>(asyncInterceptorFactories),
injectionManagerExposer.injectionManager,
CdiUtil.getBeanManager());
Expand Down Expand Up @@ -389,7 +392,8 @@ private boolean isSupportedCustomProvider(Class<?> providerClass) {
return ResponseExceptionMapper.class.isAssignableFrom(providerClass)
|| ParamConverterProvider.class.isAssignableFrom(providerClass)
|| AsyncInvocationInterceptorFactory.class.isAssignableFrom(providerClass)
|| ConnectorProvider.class.isAssignableFrom(providerClass);
|| ConnectorProvider.class.isAssignableFrom(providerClass)
|| InboundHeadersProvider.class.isAssignableFrom(providerClass);
}

private void registerCustomProvider(Object instance, Integer priority) {
Expand All @@ -414,6 +418,9 @@ private void registerCustomProvider(Object instance, Integer priority) {
if (instance instanceof ConnectorProvider) {
connector = (ConnectorProvider) instance;
}
if (instance instanceof InboundHeadersProvider) {
inboundHeaderProviders.add((InboundHeadersProvider) instance);
}
}

private static class InjectionManagerExposer implements Feature {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -46,22 +46,25 @@ class RestClientModel {
/**
* Creates new instance of the {@link RestClientModel} base on interface class.
*
* @param restClientClass rest client interface
* @param responseExceptionMappers registered exception mappers
* @param paramConverterProviders registered param converters
* @param asyncInterceptors registered async interceptor factories
* @param restClientClass rest client interface
* @param responseExceptionMappers registered exception mappers
* @param paramConverterProviders registered param converters
* @param inboundHeadersProviders registered inbound header providers
* @param asyncInterceptorFactories registered async interceptor factories
* @param injectionManager
* @return new instance
*/
static RestClientModel from(Class<?> restClientClass,
Set<ResponseExceptionMapper> responseExceptionMappers,
Set<ParamConverterProvider> paramConverterProviders,
Set<InboundHeadersProvider> inboundHeadersProviders,
List<AsyncInvocationInterceptorFactory> asyncInterceptorFactories,
InjectionManager injectionManager,
BeanManager beanManager) {
InterfaceModel interfaceModel = InterfaceModel.from(restClientClass,
responseExceptionMappers,
paramConverterProviders,
inboundHeadersProviders,
asyncInterceptorFactories,
injectionManager,
beanManager);
Expand Down
Loading