Skip to content

Commit 0a40f84

Browse files
Send User-Agent (#412)
* Send User-Agent for MapzenMap requests & create generic http handler * Send User-Agent for MapzenSearch requests * Send User-Agent for all MapzenRouter requests * Rm trailing ;
1 parent 670556c commit 0a40f84

26 files changed

+877
-215
lines changed

config/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
<suppress checks="TypeName" files="R.java" />
1919
<suppress checks="ConstantName" files="R.java" />
2020
<suppress checks="ParameterNumberCheck" files="src/main/java/com/mapzen/android/graphics/MapzenMap.java"/>
21+
<suppress checks="[a-zA-Z0-9]*" files="src/main/java/com/mapzen/android/graphics/TmpHttpHandler.java"/>
2122
</suppressions>

core/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ project.archivesBaseName = POM_ARTIFACT_ID
2121

2222
ext.tangram_version = "0.6.3"
2323

24+
def SDK_VERSION = hasProperty('version') ? '"' + version + '"' : "null";
25+
2426
android {
2527
compileSdkVersion 25
2628
buildToolsVersion '25.0.2'
@@ -31,6 +33,7 @@ android {
3133
versionCode 1
3234
versionName "1.0"
3335
resValue "string", "tangram_version", "${tangram_version}"
36+
buildConfigField "String", "SDK_VERSION", SDK_VERSION
3437
}
3538

3639
buildTypes {

core/src/main/java/com/mapzen/android/core/CoreAndroidModule.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.mapzen.android.core;
22

3-
import com.mapzen.android.graphics.TileHttpHandler;
3+
import com.mapzen.android.graphics.MapzenMapHttpHandler;
44
import com.mapzen.android.search.SearchInitializer;
55

66
import android.content.Context;
77
import android.content.res.Resources;
88

9+
import java.util.Map;
10+
911
import javax.inject.Singleton;
1012

1113
import dagger.Module;
@@ -42,10 +44,18 @@
4244
}
4345

4446
/**
45-
* Provides HTTP handler to append API key to outgoing vector tile requests.
47+
* Provides HTTP handler to configure User-Agent for outgoing vector tile requests.
4648
*/
47-
@Provides @Singleton public TileHttpHandler provideTileHttpHandler() {
48-
return new TileHttpHandler();
49+
@Provides @Singleton public MapzenMapHttpHandler provideTileHttpHandler() {
50+
return new MapzenMapHttpHandler() {
51+
@Override public Map<String, String> queryParamsForRequest() {
52+
return null;
53+
}
54+
55+
@Override public Map<String, String> headersForRequest() {
56+
return null;
57+
}
58+
};
4959
}
5060

5161
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.mapzen.android.core;
2+
3+
import com.mapzen.BuildConfig;
4+
5+
import android.os.Build;
6+
7+
import java.util.Map;
8+
9+
/**
10+
* Generic SDK interface for service-specific handlers to implement.
11+
*/
12+
public interface GenericHttpHandler {
13+
14+
String HEADER_USER_AGENT = "User-Agent";
15+
String USER_AGENT = "android-sdk;" + BuildConfig.SDK_VERSION + ";" + Build.VERSION.RELEASE;
16+
17+
/**
18+
* Return query parameters to be appended to every request.
19+
* @return
20+
*/
21+
Map<String, String> queryParamsForRequest();
22+
23+
/**
24+
* Return headers to be added to every request.
25+
* @return
26+
*/
27+
Map<String, String> headersForRequest();
28+
}

core/src/main/java/com/mapzen/android/graphics/MapInitializer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MapInitializer {
2121

2222
private Context context;
2323

24-
private TileHttpHandler tileHttpHandler;
24+
private MapzenMapHttpHandler mapzenMapHttpHandler;
2525

2626
private MapDataManager mapDataManager;
2727

@@ -34,11 +34,11 @@ public class MapInitializer {
3434
/**
3535
* Creates a new instance.
3636
*/
37-
@Inject MapInitializer(Context context, TileHttpHandler tileHttpHandler,
37+
@Inject MapInitializer(Context context, MapzenMapHttpHandler mapzenMapHttpHandler,
3838
MapDataManager mapDataManager, MapStateManager mapStateManager,
3939
SceneUpdateManager sceneUpdateManager) {
4040
this.context = context;
41-
this.tileHttpHandler = tileHttpHandler;
41+
this.mapzenMapHttpHandler = mapzenMapHttpHandler;
4242
this.mapDataManager = mapDataManager;
4343
this.mapStateManager = mapStateManager;
4444
this.sceneUpdateManager = sceneUpdateManager;
@@ -92,7 +92,7 @@ private void loadMap(final MapView mapView, String sceneFile, final OnMapReadyCa
9292
mapStateManager.isPathOverlayEnabled());
9393
getTangramView(mapView).getMapAsync(new com.mapzen.tangram.MapView.OnMapReadyCallback() {
9494
@Override public void onMapReady(MapController mapController) {
95-
mapController.setHttpHandler(tileHttpHandler);
95+
mapController.setHttpHandler(mapzenMapHttpHandler.httpHandler());
9696
MapzenManager mapzenManager = MapzenManager.instance(mapView.getContext());
9797
callback.onMapReady(
9898
new MapzenMap(mapView, mapController, new OverlayManager(mapView, mapController,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.mapzen.android.graphics;
2+
3+
import com.mapzen.android.core.GenericHttpHandler;
4+
import com.mapzen.tangram.HttpHandler;
5+
6+
import java.io.File;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import okhttp3.Callback;
11+
import okhttp3.Headers;
12+
import okhttp3.OkHttpClient;
13+
import okhttp3.Request;
14+
15+
/**
16+
* Base class for HTTP requests made by {@link MapzenMap}.
17+
*/
18+
public abstract class MapzenMapHttpHandler implements GenericHttpHandler {
19+
20+
private HttpHandler httpHandler;
21+
RequestEnqueuer requestEnqueuer;
22+
23+
/**
24+
* Public constructor with no cache configured.
25+
*/
26+
public MapzenMapHttpHandler() {
27+
this(null, 0);
28+
}
29+
30+
/**
31+
* Public constructor with cache configured.
32+
* @param directory cache directory
33+
* @param maxSize max cache directory size in bytes
34+
*/
35+
public MapzenMapHttpHandler(File directory, long maxSize) {
36+
httpHandler = new InternalHttpHandler(directory, maxSize);
37+
requestEnqueuer = new RequestEnqueuer();
38+
}
39+
40+
/**
41+
* Underlying Tangram handler.
42+
* @return
43+
*/
44+
HttpHandler httpHandler() {
45+
return httpHandler;
46+
}
47+
48+
private class InternalHttpHandler extends TmpHttpHandler {
49+
50+
public InternalHttpHandler(File directory, long maxSize) {
51+
super(directory, maxSize);
52+
}
53+
54+
@Override public boolean onRequest(String url, Callback cb) {
55+
Map<String, String> customParams = queryParamsForRequest();
56+
if (customParams != null) {
57+
for (String key : customParams.keySet()) {
58+
url = url.concat(key + "=" + customParams.get(key) + "&");
59+
}
60+
}
61+
62+
Map<String, String> headers = new HashMap<>();
63+
headers.put(HEADER_USER_AGENT, USER_AGENT);
64+
Map<String, String> customHeaders = headersForRequest();
65+
if (customHeaders != null) {
66+
headers.putAll(customHeaders);
67+
}
68+
69+
requestEnqueuer.enqueueRequest(okClient, cb, url, headers);
70+
return true;
71+
}
72+
73+
@Override public void onCancel(String url) {
74+
super.onCancel(url);
75+
}
76+
}
77+
78+
/**
79+
* Class to handle creating and enqueuing requests.
80+
*/
81+
class RequestEnqueuer {
82+
/**
83+
* Creates and enqueues a {@link Request}.
84+
* @param client
85+
* @param callback
86+
* @param url
87+
* @param headers
88+
*/
89+
void enqueueRequest(OkHttpClient client, Callback callback, String url,
90+
Map<String, String> headers) {
91+
Request request = new Request.Builder()
92+
.url(url)
93+
.headers(Headers.of(headers))
94+
.build();
95+
client.newCall(request).enqueue(callback);
96+
}
97+
}
98+
}

core/src/main/java/com/mapzen/android/graphics/TileHttpHandler.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)