-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rest client async header propagation with usage of Helidon Context (#…
…2735) Rest client header propagation with usage of Helidon Context Signed-off-by: David Kral <david.k.kral@oracle.com>
- Loading branch information
Showing
7 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...client/src/main/java/io/helidon/microprofile/restclient/HelidonInboundHeaderProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...rc/main/java/io/helidon/microprofile/restclient/HelidonRequestHeaderAutoDiscoverable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...t-client/src/main/java/io/helidon/microprofile/restclient/HelidonRequestHeaderFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...oprofile/rest-client/src/main/java/io/helidon/microprofile/restclient/InboundHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...main/resources/META-INF/services/org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |