Skip to content

Commit

Permalink
new: dev: Add user agent header.
Browse files Browse the repository at this point in the history
Send user-agent information in SDK calls. Will track the version of the
SDK for usage in the wild.

Reference: http://stackoverflow.com/questions/3697449/retrieve-version-from-maven-pom-xml-in-code
  • Loading branch information
Rohit Agarwal committed Mar 19, 2015
1 parent 833a9a1 commit 9a966a5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public Client newClient()
return ClientBuilder
.newClient(jerseyConfiguration)
.register(JacksonFeature.class)
.register(UserAgentFilter.class)
.register(ErrorResponseFilter.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void filter(final ClientRequestContext requestContext,
}
}
} catch (Exception e) {
// Silently pass. We don't want anything to to fail because of this filter.
// Silently pass. We don't want anything to fail because of this filter.
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/qubole/qds/sdk/java/client/UserAgentFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.qubole.qds.sdk.java.client;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class UserAgentFilter implements ClientRequestFilter {

public String version;

public UserAgentFilter() throws IOException {
try {
Properties prop = new Properties();
String propFileName = "versionInfo.properties";

InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

if (inputStream != null) {
prop.load(inputStream);
} else {
System.err.println("Version Info file '" + propFileName + "' not found in the classpath");
}

version = prop.getProperty("version");
} catch (Exception e) {
System.err.println("Some error while loading the version info for user-agent string.");
}
}

@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
try {
clientRequestContext.getHeaders().add("User-Agent", "qds-sdk-java-" + version);
} catch (Exception e) {
// Silently pass. We don't want anything to fail because of this filter.
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/versionInfo.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${project.version}

0 comments on commit 9a966a5

Please sign in to comment.