-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add x-spice-user-agent header (#25)
* feat: Add user agent generation and test * fix: MacOS and Windows test runs for useragent * fix: User agent OS name and version on Windows * feat: Add x-spice-user-agent header
- Loading branch information
Showing
6 changed files
with
257 additions
and
7 deletions.
There are no files selected for viewing
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
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,42 @@ | ||
package ai.spice; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.arrow.flight.CallHeaders; | ||
import org.apache.arrow.flight.CallInfo; | ||
import org.apache.arrow.flight.CallStatus; | ||
import org.apache.arrow.flight.FlightClientMiddleware; | ||
import org.apache.arrow.flight.FlightClientMiddleware.Factory; | ||
import org.apache.arrow.flight.auth2.ClientIncomingAuthHeaderMiddleware; | ||
|
||
public class HeaderAuthMiddlewareFactory implements Factory { | ||
private final ClientIncomingAuthHeaderMiddleware.Factory authFactory; | ||
private final Map<String, String> headers; | ||
|
||
public HeaderAuthMiddlewareFactory(ClientIncomingAuthHeaderMiddleware.Factory authFactory, | ||
Map<String, String> headers) { | ||
this.authFactory = authFactory; | ||
this.headers = headers; | ||
} | ||
|
||
@Override | ||
public FlightClientMiddleware onCallStarted(CallInfo callInfo) { | ||
return new FlightClientMiddleware() { | ||
@Override | ||
public void onBeforeSendingHeaders(CallHeaders callHeaders) { | ||
authFactory.onCallStarted(callInfo).onBeforeSendingHeaders(callHeaders); | ||
headers.forEach(callHeaders::insert); | ||
} | ||
|
||
@Override | ||
public void onHeadersReceived(CallHeaders callHeaders) { | ||
authFactory.onCallStarted(callInfo).onHeadersReceived(callHeaders); | ||
} | ||
|
||
@Override | ||
public void onCallCompleted(CallStatus callStatus) { | ||
authFactory.onCallStarted(callInfo).onCallCompleted(callStatus); | ||
} | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2024 The Spice.ai OSS Authors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package ai.spice; | ||
|
||
public class Version { | ||
/** | ||
* spice-java SDK version, defined statically to support more platforms vs | ||
* relying on .jar packaging | ||
*/ | ||
public static final String SPICE_JAVA_VERSION; | ||
|
||
static { | ||
SPICE_JAVA_VERSION = "0.3.0"; | ||
} | ||
} |
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,44 @@ | ||
/* | ||
Copyright 2024 The Spice.ai OSS Authors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package ai.spice; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class UserAgentTest | ||
extends TestCase { | ||
public void testUserAgent() throws ExecutionException, InterruptedException { | ||
// use a regex to match the expected user agent | ||
String regex = "spice-java \\d+\\.\\d+\\.\\d+ \\((Linux|Windows|Darwin)/[\\d\\w\\.\\-\\_]+ (x86_64|aarch64|i386)\\)"; | ||
Pattern pattern = Pattern.compile(regex); | ||
|
||
String userAgent = Config.getUserAgent(); | ||
Matcher matcher = pattern.matcher(userAgent); | ||
|
||
assertTrue("User agent did not match the expected pattern: " + userAgent, matcher.matches()); | ||
} | ||
} |