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

Update User Agent methods and format #225

Merged
merged 16 commits into from Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,6 +31,11 @@ public class SocketFactory implements com.mysql.jdbc.SocketFactory {

private Socket socket;

static {
CoreSocketFactory.addArtifactId("mysql-socket-factory-connector-j-5");
}


@Override
public Socket connect(String hostname, int portNumber, Properties props) throws IOException {
socket = CoreSocketFactory.connect(props);
Expand Down
Expand Up @@ -31,6 +31,10 @@ public class SocketFactory implements com.mysql.cj.api.io.SocketFactory {

private Socket socket;

static {
CoreSocketFactory.addArtifactId("mysql-socket-factory-connector-j-6");
}

@Override
public Socket connect(String host, int portNumber, Properties props, int loginTimeout)
throws IOException {
Expand Down
Expand Up @@ -32,6 +32,10 @@
*/
public class SocketFactory implements com.mysql.cj.protocol.SocketFactory {

static {
CoreSocketFactory.addArtifactId("mysql-socket-factory-connector-j-8");
}

@Override
public <T extends Closeable> T connect(
String host, int portNumber, PropertySet props, int loginTimeout) throws IOException {
Expand Down
Expand Up @@ -92,6 +92,10 @@ public final class CoreSocketFactory {
private final SQLAdmin adminApi;
private final int serverProxyPort;

private static List<String> userAgents = new ArrayList<String>();
private static String version = getVersion();


@VisibleForTesting
CoreSocketFactory(
ListenableFuture<KeyPair> localKeyPair,
Expand Down Expand Up @@ -290,7 +294,7 @@ private static SQLAdmin createAdminApiClient(HttpRequestInitializer requestIniti
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
SQLAdmin.Builder adminApiBuilder =
new Builder(httpTransport, jsonFactory, requestInitializer)
.setApplicationName(getApplicationName());
.setApplicationName(getUserAgents());
if (rootUrl != null) {
logTestPropertyWarning(API_ROOT_URL_PROPERTY);
adminApiBuilder.setRootUrl(rootUrl);
Expand Down Expand Up @@ -333,6 +337,40 @@ private static KeyPair generateRsaKeyPair() {
return generator.generateKeyPair();
}

private static String getVersion() {
try {
Properties packageInfo = new Properties();
packageInfo.load(CoreSocketFactory.class.getClassLoader().getResourceAsStream(
"com.google.cloud.sql/project.properties"));
return packageInfo.getProperty("version", "unknown");
} catch (IOException e) {
return "unknown";
}
}

/** Sets the default string which is appended to the SQLAdmin API client User-Agent header. */
public static void addArtifactId(String artifactId) {
String userAgent = artifactId + "/" + version;
if (!userAgents.contains(userAgent)) {
userAgents.add(userAgent);
}
}



/** Returns the default string which is appended to the SQLAdmin API client User-Agent header. */
private static String getUserAgents() {
return String.join(" ", userAgents) + " " + getApplicationName();
}

/** Returns the current User-Agent header set for the underlying SQLAdmin API client. */
public static String getApplicationName() {
if (coreSocketFactory != null) {
return coreSocketFactory.adminApi.getApplicationName();
}
return System.getProperty(USER_TOKEN_PROPERTY_NAME, "");
kurtisvg marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets the User-Agent header for requests made using the underlying SQLAdmin API client.
*
Expand All @@ -345,12 +383,4 @@ public static void setApplicationName(String applicationName) {
}
System.setProperty(USER_TOKEN_PROPERTY_NAME, applicationName);
}

/** Returns the current User-Agent header set for the underlying SQLAdmin API client. */
public static String getApplicationName() {
if (coreSocketFactory != null) {
return coreSocketFactory.adminApi.getApplicationName();
}
return System.getProperty(USER_TOKEN_PROPERTY_NAME, "Cloud SQL Java Socket Factory");
}
}
@@ -0,0 +1 @@
version=${project.version}
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -87,6 +87,12 @@
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -224,6 +230,7 @@
</execution>
</executions>
</plugin>

</plugins>
</build>
</profile>
Expand Down
Expand Up @@ -38,6 +38,10 @@ public class SocketFactory extends javax.net.SocketFactory {

private Properties props;

static {
CoreSocketFactory.addArtifactId("postgres-socket-factory");
}

/**
* Implements the {@link SocketFactory} constructor, which can be used to create authenticated
* connections to a Cloud SQL instance.
Expand All @@ -52,7 +56,6 @@ public SocketFactory(Properties info) {
DEPRECATED_SOCKET_ARG, CoreSocketFactory.CLOUD_SQL_INSTANCE_PROPERTY));
info.setProperty(CoreSocketFactory.CLOUD_SQL_INSTANCE_PROPERTY, oldInstanceKey);
}

this.props = info;
}

Expand Down