Include app name and version into User-Agent header
The User-Agent header that the client sends is included in the HTTP
request log on server side. Having the app name and the version
included in the User-Agent header allows us to see from the logs how
much the app is used.
To modify the User-Agent header we need a dependency to the Apache
HTTP client. According to [1] we must add org.apache.http.legacy as
used library since the app has sdk 23 as target.
[1] http://stackoverflow.com/questions/26024908/apache-httpclient-android-gradle
Change-Id: Ie2f4dccfa1876986a98f3e985ab3624153db7ad5
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/app/build.gradle b/app/build.gradle
index 3d75bb6..69123dc 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -18,6 +18,8 @@
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
+
+ useLibrary 'org.apache.http.legacy'
}
dependencies {
diff --git a/app/src/main/java/com/google/reviewit/app/Gerrit.java b/app/src/main/java/com/google/reviewit/app/Gerrit.java
index 1771c55..e3cb6e6 100644
--- a/app/src/main/java/com/google/reviewit/app/Gerrit.java
+++ b/app/src/main/java/com/google/reviewit/app/Gerrit.java
@@ -14,14 +14,32 @@
package com.google.reviewit.app;
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+
import com.google.gerrit.extensions.api.GerritApi;
import com.urswolfer.gerrit.client.rest.GerritAuthData;
import com.urswolfer.gerrit.client.rest.GerritRestApiFactory;
+import com.urswolfer.gerrit.client.rest.http.HttpClientBuilderExtension;
+
+import org.apache.http.Header;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHeaders;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.protocol.HttpContext;
+
+import java.io.IOException;
public class Gerrit {
+ private final Context context;
private com.google.gerrit.extensions.api.GerritApi api;
- Gerrit(final ConfigManager cfgManager) {
+ Gerrit(Context context, final ConfigManager cfgManager) {
+ this.context = context;
QueryConfig queryCfg = cfgManager.getQueryConfig();
ServerConfig serverCfg = cfgManager.getServerConfig(queryCfg.serverId);
api = connect(serverCfg);
@@ -37,7 +55,41 @@
private GerritApi connect(ServerConfig serverCfg) {
return new GerritRestApiFactory().create(
new GerritAuthData.Basic(
- serverCfg.url, serverCfg.user, serverCfg.password));
+ serverCfg.url, serverCfg.user, serverCfg.password),
+ new HttpClientBuilderExtension() {
+ @Override
+ public HttpClientBuilder extend(
+ HttpClientBuilder httpClientBuilder,
+ GerritAuthData authData) {
+ httpClientBuilder.addInterceptorLast(new HttpRequestInterceptor() {
+ public void process(HttpRequest request, HttpContext context)
+ throws HttpException, IOException {
+ Header existingUserAgent =
+ request.getFirstHeader(HttpHeaders.USER_AGENT);
+ String userAgent =
+ String.format("%s/%s", getApplicationName(), getVersion());
+ userAgent += " using " + existingUserAgent.getValue();
+ request.setHeader(HttpHeaders.USER_AGENT, userAgent);
+ }
+ });
+ return super.extend(httpClientBuilder, authData);
+ }
+ });
+ }
+
+ public String getApplicationName() {
+ int stringId = context.getApplicationInfo().labelRes;
+ return context.getString(stringId);
+ }
+
+ public String getVersion() {
+ try {
+ PackageInfo pInfo = context.getPackageManager()
+ .getPackageInfo(context.getPackageName(), 0);
+ return pInfo.versionName;
+ } catch (PackageManager.NameNotFoundException e) {
+ return "n/a";
+ }
}
public GerritApi api() {
diff --git a/app/src/main/java/com/google/reviewit/app/ReviewItApp.java b/app/src/main/java/com/google/reviewit/app/ReviewItApp.java
index 3c1a88b..07cef0f 100644
--- a/app/src/main/java/com/google/reviewit/app/ReviewItApp.java
+++ b/app/src/main/java/com/google/reviewit/app/ReviewItApp.java
@@ -130,7 +130,7 @@
private Gerrit getGerrit() {
if (gerrit == null) {
- gerrit = new Gerrit(getConfigManager());
+ gerrit = new Gerrit(this, getConfigManager());
}
return gerrit;
}