-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-10467: [FlightRPC][Java] Add the ability to pass arbitrary client headers. #8572
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c65142c
ARROW-10467: [Java] Add the ability to pass arbitrary
kylep-dremio c2f61a6
Change design to allow specification and reception of custom headers.
kylep-dremio ba63089
Add a Flight implementation of CallHeaders, for use when specifying
kylep-dremio 3600b12
Make FlightCallHeaders return null if specific header is not present.
kylep-dremio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
java/flight/flight-core/src/main/java/org/apache/arrow/flight/FlightCallHeaders.java
This file contains hidden or 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,111 @@ | ||
| /* | ||
| * 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.arrow.flight; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.ArrayListMultimap; | ||
| import com.google.common.collect.Iterables; | ||
| import com.google.common.collect.Multimap; | ||
|
|
||
| import io.grpc.Metadata; | ||
|
|
||
| /** | ||
| * An implementation of the Flight headers interface for headers. | ||
| */ | ||
| public class FlightCallHeaders implements CallHeaders { | ||
| private final Multimap<String, Object> keysAndValues; | ||
|
|
||
| public FlightCallHeaders() { | ||
| this.keysAndValues = ArrayListMultimap.create(); | ||
| } | ||
|
|
||
| @Override | ||
| public String get(String key) { | ||
| final Collection<Object> values = this.keysAndValues.get(key); | ||
| if (values.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { | ||
| return new String((byte[]) Iterables.get(values, 0)); | ||
| } | ||
|
|
||
| return (String) Iterables.get(values, 0); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] getByte(String key) { | ||
| final Collection<Object> values = this.keysAndValues.get(key); | ||
| if (values.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { | ||
| return (byte[]) Iterables.get(values, 0); | ||
| } | ||
|
|
||
| return ((String) Iterables.get(values, 0)).getBytes(); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<String> getAll(String key) { | ||
| if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { | ||
| return this.keysAndValues.get(key).stream().map(o -> new String((byte[]) o)).collect(Collectors.toList()); | ||
| } | ||
| return (Collection<String>) (Collection<?>) this.keysAndValues.get(key); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<byte[]> getAllByte(String key) { | ||
| if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { | ||
| return (Collection<byte[]>) (Collection<?>) this.keysAndValues.get(key); | ||
| } | ||
| return this.keysAndValues.get(key).stream().map(o -> ((String) o).getBytes()).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public void insert(String key, String value) { | ||
| this.keysAndValues.put(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void insert(String key, byte[] value) { | ||
| Preconditions.checkArgument(key.endsWith("-bin"), "Binary header is named %s. It must end with %s", key, "-bin"); | ||
| Preconditions.checkArgument(key.length() > "-bin".length(), "empty key name"); | ||
|
|
||
| this.keysAndValues.put(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> keys() { | ||
| return this.keysAndValues.keySet(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsKey(String key) { | ||
| return this.keysAndValues.containsKey(key); | ||
| } | ||
|
|
||
| public String toString() { | ||
| return this.keysAndValues.toString(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
java/flight/flight-core/src/main/java/org/apache/arrow/flight/HeaderCallOption.java
This file contains hidden or 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,52 @@ | ||
| /* | ||
| * 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.arrow.flight; | ||
|
|
||
| import io.grpc.Metadata; | ||
| import io.grpc.stub.AbstractStub; | ||
| import io.grpc.stub.MetadataUtils; | ||
|
|
||
| /** | ||
| * Method option for supplying headers to method calls. | ||
| */ | ||
| public class HeaderCallOption implements CallOptions.GrpcCallOption { | ||
| private final Metadata propertiesMetadata = new Metadata(); | ||
|
|
||
| /** | ||
| * Header property constructor. | ||
| * | ||
| * @param headers the headers that should be sent across. If a header is a string, it should only be valid ASCII | ||
| * characters. Binary headers should end in "-bin". | ||
| */ | ||
| public HeaderCallOption(CallHeaders headers) { | ||
| for (String key : headers.keys()) { | ||
| if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { | ||
| final Metadata.Key<byte[]> metaKey = Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER); | ||
| headers.getAllByte(key).forEach(v -> propertiesMetadata.put(metaKey, v)); | ||
| } else { | ||
| final Metadata.Key<String> metaKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER); | ||
| headers.getAll(key).forEach(v -> propertiesMetadata.put(metaKey, v)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends AbstractStub<T>> T wrapStub(T stub) { | ||
| return MetadataUtils.attachHeaders(stub, propertiesMetadata); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
java/flight/flight-core/src/main/java/org/apache/arrow/flight/ServerHeaderMiddleware.java
This file contains hidden or 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,65 @@ | ||
| /* | ||
| * 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.arrow.flight; | ||
|
|
||
| /** | ||
| * Middleware that's used to extract and pass headers to the server during requests. | ||
| */ | ||
| public class ServerHeaderMiddleware implements FlightServerMiddleware { | ||
| /** | ||
| * Factory for accessing ServerHeaderMiddleware. | ||
| */ | ||
| public static class Factory implements FlightServerMiddleware.Factory<ServerHeaderMiddleware> { | ||
| /** | ||
| * Construct a factory for receiving call headers. | ||
| */ | ||
| public Factory() { | ||
| } | ||
|
|
||
| @Override | ||
| public ServerHeaderMiddleware onCallStarted(CallInfo callInfo, CallHeaders incomingHeaders, | ||
| RequestContext context) { | ||
| return new ServerHeaderMiddleware(incomingHeaders); | ||
| } | ||
| } | ||
|
|
||
| private final CallHeaders headers; | ||
|
|
||
| private ServerHeaderMiddleware(CallHeaders incomingHeaders) { | ||
| this.headers = incomingHeaders; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve the headers for this call. | ||
| */ | ||
| public CallHeaders headers() { | ||
| return headers; | ||
| } | ||
|
|
||
| @Override | ||
| public void onBeforeSendingHeaders(CallHeaders outgoingHeaders) { | ||
| } | ||
|
|
||
| @Override | ||
| public void onCallCompleted(CallStatus status) { | ||
| } | ||
|
|
||
| @Override | ||
| public void onCallErrored(Throwable err) { | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename ServerHeaderMiddleware to ServerPropertyMiddleware? The current name is a bit too generic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name is appropriate given that this is intended to handle all headers for calls, which could be considered properties depending on the FlightServer implementation, but not necessarily.