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 async header propagation with usage of Helidon Context #2735

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
@@ -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());
}
}
5 changes: 4 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,12 @@
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;

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