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

[CALCITE-5534] Add userAgent JDBC param #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -91,7 +91,10 @@ public enum BuiltInConnectionProperty implements ConnectionProperty {
TRANSPARENT_RECONNECTION("transparent_reconnection", Type.BOOLEAN, Boolean.FALSE, false),

/** Number of rows to fetch per call. */
FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false);
FETCH_SIZE("fetch_size", Type.NUMBER, AvaticaStatement.DEFAULT_FETCH_SIZE, false),

/** User-Agent header to identify requests performed by a particular application **/
USER_AGENT("userAgent", Type.STRING, "", false);

private final String camelName;
private final Type type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public interface ConnectionConfig {
boolean transparentReconnectionEnabled();
/** @see BuiltInConnectionProperty#FETCH_SIZE */
int fetchSize();
/** @see BuiltInConnectionProperty#USER_AGENT */
String userAgent();
}

// End ConnectionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public int fetchSize() {
return BuiltInConnectionProperty.FETCH_SIZE.wrap(properties).getInt();
}

public String userAgent() {
return BuiltInConnectionProperty.USER_AGENT.wrap(properties).getString();
}

/** Converts a {@link Properties} object containing (name, value)
* pairs into a map whose keys are
* {@link org.apache.calcite.avatica.InternalProperty} objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class AvaticaCommonsHttpClientImpl implements AvaticaHttpClient, HttpClie
protected CredentialsProvider credentialsProvider = null;
protected Lookup<AuthSchemeFactory> authRegistry = null;
protected Object userToken;
protected String userAgent;

public AvaticaCommonsHttpClientImpl(URL url) {
this.uri = toURI(Objects.requireNonNull(url));
Expand Down Expand Up @@ -119,6 +120,10 @@ protected void initializeClient(PoolingHttpClientConnectionManager pool) {
HttpPost post = new HttpPost(uri);
post.setEntity(entity);

if (null != userAgent) {
post.addHeader("User-Agent", userAgent);
}

try (CloseableHttpResponse response = execute(post, context)) {
final int statusCode = response.getCode();
if (HttpURLConnection.HTTP_OK == statusCode
Expand All @@ -145,6 +150,11 @@ protected void initializeClient(PoolingHttpClientConnectionManager pool) {
}
}

@Override
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}

// Visible for testing
CloseableHttpResponse execute(HttpPost post, HttpClientContext context)
throws IOException, ClientProtocolException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public interface AvaticaHttpClient {
*/
byte[] send(byte[] request);

/**
* Sets the "User-Agent" HTTP header for all request to the Avatica server.
*
* @param userAgent The "User-Agent" to use. Configurable via
* {@link org.apache.calcite.avatica.BuiltInConnectionProperty#USER_AGENT}
*/
void setUserAgent(String userAgent);

}

// End AvaticaHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public static AvaticaHttpClientFactoryImpl getInstance() {
client = new DoAsAvaticaHttpClient(client, kerberosUtil);
}

if (!config.userAgent().isEmpty()) {
client.setUserAgent(config.userAgent());
}

return client;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
public class AvaticaHttpClientImpl implements AvaticaHttpClient {
protected final URL url;
protected String userAgent;

public AvaticaHttpClientImpl(URL url) {
this.url = url;
Expand All @@ -43,6 +44,11 @@ public byte[] send(byte[] request) {
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);

if (null != userAgent) {
connection.setRequestProperty("User-Agent", userAgent);
}

try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(request);
wr.flush();
Expand All @@ -69,6 +75,11 @@ public byte[] send(byte[] request) {
}
}

@Override
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}

HttpURLConnection openConnection() throws IOException {
return (HttpURLConnection) url.openConnection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public DoAsAvaticaHttpClient(AvaticaHttpClient wrapped, KerberosConnection kerbe
}
});
}

@Override
public void setUserAgent(String userAgent) {
wrapped.setUserAgent(userAgent);
}
}

// End DoAsAvaticaHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public byte[] send(byte[] request) {
return client.send(request);
}

@Override
public void setUserAgent(String userAgent) {}

}

/**
Expand Down