Skip to content

Commit

Permalink
Rest client async header propagation with usage of Helidon Context (#…
Browse files Browse the repository at this point in the history
…2735)

Rest client header propagation with usage of Helidon Context

Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent authored Feb 5, 2021
1 parent 3ce1930 commit f68d7e5
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.restclient;

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

import io.helidon.common.context.Contexts;

import org.glassfish.jersey.microprofile.restclient.InboundHeadersProvider;

/**
* Provider which propagates inbound request headers via Helidon {@link io.helidon.common.context.Context}.
*/
class HelidonInboundHeaderProvider implements InboundHeadersProvider {

@Override
public Map<String, List<String>> inboundHeaders() {
return Contexts.context()
.flatMap(context -> context.get(InboundHeaders.class))
.map(InboundHeaders::getInboundHeaders)
.orElse(Map.of());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.restclient;

import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.core.FeatureContext;

import org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable;

/**
* Automatically discovered JAX-RS provider which registers Rest Client specific components to the server.
*/
@ConstrainedTo(RuntimeType.SERVER)
public class HelidonRequestHeaderAutoDiscoverable implements ForcedAutoDiscoverable {
@Override
public void configure(FeatureContext context) {
if (!context.getConfiguration().isRegistered(HelidonRequestHeaderFilter.class)) {
context.register(HelidonRequestHeaderFilter.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.restclient;

import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;

import io.helidon.common.context.Contexts;

/**
* Server side request filter used for propagation of request headers to server client request.
* Uses {@link Contexts} to propagate headers.
*/
@ConstrainedTo(RuntimeType.SERVER)
class HelidonRequestHeaderFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext requestContext) {
Contexts.context().ifPresent(context -> context.register(new InboundHeaders(requestContext.getHeaders())));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.restclient;

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

/**
* Wrapper object from obtained inbound request headers.
*/
class InboundHeaders {

private final Map<String, List<String>> inboundHeaders;

InboundHeaders(Map<String, List<String>> inboundHeaders) {
this.inboundHeaders = inboundHeaders;
}

public Map<String, List<String>> getInboundHeaders() {
return inboundHeaders;
}
}
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, 2021 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,5 +49,7 @@ public void onNewClient(Class<?> aClass, RestClientBuilder restClientBuilder) {
} catch (Exception e) {
LOGGER.log(Level.FINE, "Failed to replace executor service for a REST Client: " + aClass, e);
}

restClientBuilder.register(new HelidonInboundHeaderProvider());
}
}
9 changes: 8 additions & 1 deletion microprofile/rest-client/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,9 +21,16 @@
requires java.logging;
requires transitive microprofile.rest.client.api;
requires io.helidon.common.context;
requires jersey.common;
requires jersey.mp.rest.client;
requires java.ws.rs;

exports io.helidon.microprofile.restclient;
// needed for jersey injection
opens io.helidon.microprofile.restclient to hk2.locator,hk2.utils,weld.core.impl, io.helidon.microprofile.cdi;

provides org.eclipse.microprofile.rest.client.spi.RestClientListener
with io.helidon.microprofile.restclient.MpRestClientListener;
provides org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable
with io.helidon.microprofile.restclient.HelidonRequestHeaderAutoDiscoverable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

io.helidon.microprofile.restclient.HelidonRequestHeaderAutoDiscoverable

0 comments on commit f68d7e5

Please sign in to comment.