gnodet-bot commented on code in PR #30686: URL: https://github.com/apache/camel/pull/30686#discussion_r4671432975
########## components/camel-opensearch/src/main/java/org/apache/camel/component/opensearch/NodeScopedApiKeyInterceptor.java: ########## @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.component.opensearch; + +import java.util.Collection; +import java.util.function.Supplier; + +import org.apache.hc.client5.http.protocol.HttpClientContext; +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpRequestInterceptor; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.net.URIAuthority; + +/** + * Adds the API key in the Authorization header of the requests to the nodes of the cluster the client talks to: the + * configured hosts, and the nodes found by the sniffer when it is enabled. A request to any other host, such as the + * target of a redirect, does not get the key. + * <p> + * The key is not set as a default header of the client, because a default header is part of the request the client + * redirects, whatever the host of the redirect. An interceptor runs for each request, with the host it is sent to. + */ +final class NodeScopedApiKeyInterceptor implements HttpRequestInterceptor { + + private final String authorization; + private final Supplier<? extends Collection<HttpHost>> nodes; + + NodeScopedApiKeyInterceptor(String apiKey, Supplier<? extends Collection<HttpHost>> nodes) { + this.authorization = "ApiKey " + apiKey; + this.nodes = nodes; + } + + @Override + public void process(HttpRequest request, EntityDetails entity, HttpContext context) { + if (isToNode(request, context)) { + request.setHeader(HttpHeaders.AUTHORIZATION, authorization); + } + } + + private boolean isToNode(HttpRequest request, HttpContext context) { + if (context != null && HttpClientContext.castOrCreate(context).getHttpRoute() != null) { + HttpHost target = HttpClientContext.castOrCreate(context).getHttpRoute().getTargetHost(); + return NodeScopedCredentialsProvider.isNode(nodes.get(), target.getSchemeName(), target.getHostName(), + target.getPort()); + } Review Comment: 💡 `HttpClientContext.castOrCreate(context)` is called twice in the same guard block. While cheap (it's essentially a cast when `context` is already an `HttpClientContext`), extracting it to a local variable avoids the redundant call and makes the control flow clearer: ```suggestion private boolean isToNode(HttpRequest request, HttpContext context) { if (context != null) { HttpClientContext clientContext = HttpClientContext.castOrCreate(context); if (clientContext.getHttpRoute() != null) { HttpHost target = clientContext.getHttpRoute().getTargetHost(); return NodeScopedCredentialsProvider.isNode(nodes.get(), target.getSchemeName(), target.getHostName(), target.getPort()); } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
