Skip to content

Commit 2d8ddc1

Browse files
author
Karl Rieb
committed
Use different User-Agent strings for V1 and V2 clients.
1 parent 2fa2b48 commit 2d8ddc1

5 files changed

Lines changed: 118 additions & 59 deletions

File tree

src/com/dropbox/core/DbxOAuth1Upgrader.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.dropbox.core.http.HttpRequestor;
44
import com.dropbox.core.json.JsonReadException;
55
import com.dropbox.core.json.JsonReader;
6+
import com.dropbox.core.v1.DbxClientV1;
67
import static com.dropbox.core.util.LangUtil.mkAssert;
78

89
import com.fasterxml.jackson.core.JsonLocation;
@@ -46,13 +47,20 @@ public String createOAuth2AccessToken(DbxOAuth1AccessToken token)
4647
{
4748
if (token == null) throw new IllegalArgumentException("'token' can't be null");
4849
return DbxRequestUtil.doPostNoAuth(
49-
requestConfig, appInfo.host.api, "1/oauth2/token_from_oauth1",
50-
null, getHeaders(token), new DbxRequestUtil.ResponseHandler<String>() {
51-
public String handle(HttpRequestor.Response response) throws DbxException {
52-
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
53-
return DbxRequestUtil.readJsonFromResponse(ResponseReader, response);
50+
requestConfig,
51+
DbxClientV1.USER_AGENT_ID,
52+
appInfo.host.api,
53+
"1/oauth2/token_from_oauth1",
54+
null,
55+
getHeaders(token),
56+
new DbxRequestUtil.ResponseHandler<String>() {
57+
@Override
58+
public String handle(HttpRequestor.Response response) throws DbxException {
59+
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
60+
return DbxRequestUtil.readJsonFromResponse(ResponseReader, response);
61+
}
5462
}
55-
});
63+
);
5664
}
5765

5866
/**
@@ -63,13 +71,20 @@ public void disableOAuth1AccessToken(DbxOAuth1AccessToken token)
6371
{
6472
if (token == null) throw new IllegalArgumentException("'token' can't be null");
6573
DbxRequestUtil.doPostNoAuth(
66-
requestConfig, appInfo.host.api, "1/disable_access_token",
67-
null, getHeaders(token), new DbxRequestUtil.ResponseHandler<Void>() {
68-
public Void handle(HttpRequestor.Response response) throws DbxException {
69-
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
70-
return null;
74+
requestConfig,
75+
DbxClientV1.USER_AGENT_ID,
76+
appInfo.host.api,
77+
"1/disable_access_token",
78+
null,
79+
getHeaders(token),
80+
new DbxRequestUtil.ResponseHandler<Void>() {
81+
@Override
82+
public Void handle(HttpRequestor.Response response) throws DbxException {
83+
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
84+
return null;
85+
}
7186
}
72-
});
87+
);
7388
}
7489

7590
private ArrayList<HttpRequestor.Header> getHeaders(DbxOAuth1AccessToken token)

src/com/dropbox/core/DbxRequestUtil.java

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ private static String encodeUrlParams(/*@Nullable*/String userLocale,
8686
}
8787

8888
public static List<HttpRequestor.Header> addAuthHeader(/*@Nullable*/List<HttpRequestor.Header> headers,
89-
String accessToken)
89+
String accessToken)
9090
{
9191
if (headers == null) headers = new ArrayList<HttpRequestor.Header>();
9292
headers.add(new HttpRequestor.Header("Authorization", "Bearer " + accessToken));
9393
return headers;
9494
}
9595

9696
public static List<HttpRequestor.Header> addSelectUserHeader(/*@Nullable*/List<HttpRequestor.Header> headers,
97-
String memberId)
97+
String memberId)
9898
{
9999
if (memberId == null) throw new IllegalArgumentException("'memberId' is null");
100100
if (headers == null) headers = new ArrayList<HttpRequestor.Header>();
@@ -103,27 +103,32 @@ public static List<HttpRequestor.Header> addSelectUserHeader(/*@Nullable*/List<H
103103
}
104104

105105
public static List<HttpRequestor.Header> addUserAgentHeader(/*@Nullable*/List<HttpRequestor.Header> headers,
106-
DbxRequestConfig requestConfig)
106+
DbxRequestConfig requestConfig,
107+
String clientUserAgentIdentifier)
107108
{
108109
if (headers == null) headers = new ArrayList<HttpRequestor.Header>();
109-
headers.add(buildUserAgentHeader(requestConfig));
110+
headers.add(buildUserAgentHeader(requestConfig, clientUserAgentIdentifier));
110111
return headers;
111112
}
112113

113-
public static HttpRequestor.Header buildUserAgentHeader(DbxRequestConfig requestConfig)
114+
public static HttpRequestor.Header buildUserAgentHeader(DbxRequestConfig requestConfig, String clientUserAgentIdentifier)
114115
{
115-
return new HttpRequestor.Header("User-Agent", requestConfig.clientIdentifier + " Dropbox-Java-SDK/" + DbxSdkVersion.Version);
116+
return new HttpRequestor.Header("User-Agent", requestConfig.clientIdentifier + " " + clientUserAgentIdentifier + "/" + DbxSdkVersion.Version);
116117
}
117118

118119
/**
119120
* Convenience function for making HTTP GET requests.
120121
*/
121-
public static HttpRequestor.Response startGet(DbxRequestConfig requestConfig, String accessToken, String host, String path,
122+
public static HttpRequestor.Response startGet(DbxRequestConfig requestConfig,
123+
String accessToken,
124+
String clientUserAgentIdentifier,
125+
String host,
126+
String path,
122127
/*@Nullable*/String/*@Nullable*/[] params,
123128
/*@Nullable*/List<HttpRequestor.Header> headers)
124129
throws DbxException.NetworkIO
125130
{
126-
headers = addUserAgentHeader(headers, requestConfig);
131+
headers = addUserAgentHeader(headers, requestConfig, clientUserAgentIdentifier);
127132
headers = addAuthHeader(headers, accessToken);
128133

129134
String url = buildUrlWithParams(requestConfig.userLocale, host, path, params);
@@ -138,13 +143,15 @@ public static HttpRequestor.Response startGet(DbxRequestConfig requestConfig, St
138143
/**
139144
* Convenience function for making HTTP PUT requests.
140145
*/
141-
public static HttpRequestor.Uploader startPut(DbxRequestConfig requestConfig, String accessToken,
146+
public static HttpRequestor.Uploader startPut(DbxRequestConfig requestConfig,
147+
String accessToken,
148+
String clientUserAgentIdentifier,
142149
String host, String path,
143150
/*@Nullable*/String/*@Nullable*/[] params,
144151
/*@Nullable*/List<HttpRequestor.Header> headers)
145152
throws DbxException.NetworkIO
146153
{
147-
headers = addUserAgentHeader(headers, requestConfig);
154+
headers = addUserAgentHeader(headers, requestConfig, clientUserAgentIdentifier);
148155
headers = addAuthHeader(headers, accessToken);
149156

150157
String url = buildUrlWithParams(requestConfig.userLocale, host, path, params);
@@ -159,7 +166,9 @@ public static HttpRequestor.Uploader startPut(DbxRequestConfig requestConfig, St
159166
/**
160167
* Convenience function for making HTTP POST requests.
161168
*/
162-
public static HttpRequestor.Response startPostNoAuth(DbxRequestConfig requestConfig, String host,
169+
public static HttpRequestor.Response startPostNoAuth(DbxRequestConfig requestConfig,
170+
String clientUserAgentIdentifier,
171+
String host,
163172
String path,
164173
/*@Nullable*/String/*@Nullable*/[] params,
165174
/*@Nullable*/List<HttpRequestor.Header> headers)
@@ -170,21 +179,24 @@ public static HttpRequestor.Response startPostNoAuth(DbxRequestConfig requestCon
170179
if (headers == null) headers = new ArrayList<HttpRequestor.Header>();
171180
headers.add(new HttpRequestor.Header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
172181

173-
return startPostRaw(requestConfig, host, path, encodedParams, headers);
182+
return startPostRaw(requestConfig, clientUserAgentIdentifier, host, path, encodedParams, headers);
174183
}
175184

185+
176186
/**
177187
* Convenience function for making HTTP POST requests. Like startPostNoAuth but takes byte[] instead of params.
178188
*/
179-
public static HttpRequestor.Response startPostRaw(DbxRequestConfig requestConfig, String host,
189+
public static HttpRequestor.Response startPostRaw(DbxRequestConfig requestConfig,
190+
String clientUserAgentIdentifier,
191+
String host,
180192
String path,
181193
byte[] body,
182194
/*@Nullable*/List<HttpRequestor.Header> headers)
183195
throws DbxException.NetworkIO
184196
{
185197
String uri = buildUri(host, path);
186198

187-
headers = addUserAgentHeader(headers, requestConfig);
199+
headers = addUserAgentHeader(headers, requestConfig, clientUserAgentIdentifier);
188200
headers.add(new HttpRequestor.Header("Content-Length", Integer.toString(body.length)));
189201

190202
try {
@@ -345,13 +357,17 @@ public static abstract class ResponseHandler<T>
345357
public abstract T handle(HttpRequestor.Response response) throws DbxException;
346358
}
347359

348-
public static <T> T doGet(DbxRequestConfig requestConfig, String accessToken, String host, String path,
360+
public static <T> T doGet(DbxRequestConfig requestConfig,
361+
String accessToken,
362+
String clientUserAgentIdentifier,
363+
String host,
364+
String path,
349365
/*@Nullable*/String/*@Nullable*/[] params,
350366
/*@Nullable*/List<HttpRequestor.Header> headers,
351367
ResponseHandler<T> handler)
352368
throws DbxException
353369
{
354-
HttpRequestor.Response response = startGet(requestConfig, accessToken, host, path, params, headers);
370+
HttpRequestor.Response response = startGet(requestConfig, accessToken, clientUserAgentIdentifier, host, path, params, headers);
355371
try {
356372
return handler.handle(response);
357373
}
@@ -366,23 +382,30 @@ public static <T> T doGet(DbxRequestConfig requestConfig, String accessToken, St
366382
}
367383
}
368384

369-
public static <T> T doPost(DbxRequestConfig requestConfig, String accessToken, String host, String path,
385+
public static <T> T doPost(DbxRequestConfig requestConfig,
386+
String accessToken,
387+
String clientUserAgentIdentifier,
388+
String host,
389+
String path,
370390
/*@Nullable*/String/*@Nullable*/[] params,
371391
/*@Nullable*/List<HttpRequestor.Header> headers,
372392
ResponseHandler<T> handler)
373393
throws DbxException
374394
{
375395
headers = addAuthHeader(headers, accessToken);
376-
return doPostNoAuth(requestConfig, host, path, params, headers, handler);
396+
return doPostNoAuth(requestConfig, clientUserAgentIdentifier, host, path, params, headers, handler);
377397
}
378398

379-
public static <T> T doPostNoAuth(DbxRequestConfig requestConfig, String host, String path,
399+
public static <T> T doPostNoAuth(DbxRequestConfig requestConfig,
400+
String clientUserAgentIdentifier,
401+
String host,
402+
String path,
380403
/*@Nullable*/String/*@Nullable*/[] params,
381404
/*@Nullable*/List<HttpRequestor.Header> headers,
382405
ResponseHandler<T> handler)
383406
throws DbxException
384407
{
385-
HttpRequestor.Response response = startPostNoAuth(requestConfig, host, path, params, headers);
408+
HttpRequestor.Response response = startPostNoAuth(requestConfig, clientUserAgentIdentifier, host, path, params, headers);
386409
return finishResponse(response, handler);
387410
}
388411

src/com/dropbox/core/DbxWebAuthHelper.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.dropbox.core.http.HttpRequestor;
44
import com.dropbox.core.util.StringUtil;
5+
import com.dropbox.core.v1.DbxClientV1;
56

67
import java.util.ArrayList;
78

@@ -10,8 +11,10 @@
1011

1112
abstract class DbxWebAuthHelper
1213
{
13-
public static String getAuthorizeUrl(DbxAppInfo appInfo, /*@Nullable*/String userLocale,
14-
/*@Nullable*/String redirectUri, /*@Nullable*/String state)
14+
public static String getAuthorizeUrl(DbxAppInfo appInfo,
15+
/*@Nullable*/String userLocale,
16+
/*@Nullable*/String redirectUri,
17+
/*@Nullable*/String state)
1518
{
1619
return DbxRequestUtil.buildUrlWithParams(userLocale,
1720
appInfo.host.web, "1/oauth2/authorize", new String[] {
@@ -22,9 +25,11 @@ public static String getAuthorizeUrl(DbxAppInfo appInfo, /*@Nullable*/String use
2225
});
2326
}
2427

25-
public static DbxAuthFinish finish(DbxAppInfo appInfo, DbxRequestConfig requestConfig,
26-
String code, /*@Nullable*/String originalRedirectUri)
27-
throws DbxException
28+
public static DbxAuthFinish finish(DbxAppInfo appInfo,
29+
DbxRequestConfig requestConfig,
30+
String code,
31+
/*@Nullable*/String originalRedirectUri)
32+
throws DbxException
2833
{
2934
if (code == null) throw new IllegalArgumentException("'code' can't be null");
3035

@@ -40,15 +45,21 @@ public static DbxAuthFinish finish(DbxAppInfo appInfo, DbxRequestConfig requestC
4045
String base64Credentials = StringUtil.base64Encode(StringUtil.stringToUtf8(credentials));
4146
headers.add(new HttpRequestor.Header("Authorization", "Basic " + base64Credentials));
4247

43-
return DbxRequestUtil.doPostNoAuth(requestConfig, appInfo.host.api, "1/oauth2/token",
44-
params, headers, new DbxRequestUtil.ResponseHandler<DbxAuthFinish>()
45-
{
46-
@Override
47-
public DbxAuthFinish handle(HttpRequestor.Response response) throws DbxException
48-
{
49-
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
50-
return DbxRequestUtil.readJsonFromResponse(DbxAuthFinish.Reader, response);
48+
return DbxRequestUtil.doPostNoAuth(
49+
requestConfig,
50+
DbxClientV1.USER_AGENT_ID,
51+
appInfo.host.api,
52+
"1/oauth2/token",
53+
params,
54+
headers,
55+
new DbxRequestUtil.ResponseHandler<DbxAuthFinish>() {
56+
@Override
57+
public DbxAuthFinish handle(HttpRequestor.Response response) throws DbxException
58+
{
59+
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
60+
return DbxRequestUtil.readJsonFromResponse(DbxAuthFinish.Reader, response);
61+
}
5162
}
52-
});
63+
);
5364
}
5465
}

src/com/dropbox/core/v1/DbxClientV1.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
*/
3737
public final class DbxClientV1
3838
{
39+
public static final String USER_AGENT_ID = "Dropbox-Java-SDK";
40+
3941
private final DbxRequestConfig requestConfig;
4042
private final String accessToken;
4143
private final DbxHost host;
@@ -474,7 +476,7 @@ public void disableAccessToken()
474476
String host = this.host.content;
475477

476478
boolean passedOwnershipOfStream = false;
477-
HttpRequestor.Response response = DbxRequestUtil.startGet(requestConfig, accessToken, host, apiPath, params, null);
479+
HttpRequestor.Response response = DbxRequestUtil.startGet(requestConfig, accessToken, USER_AGENT_ID, host, apiPath, params, null);
478480
try {
479481
if (response.statusCode == 404) return null;
480482
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
@@ -738,7 +740,7 @@ public Uploader startUploadFileSingle(String targetPath, DbxWriteMode writeMode,
738740
headers.add(new HttpRequestor.Header("Content-Type", "application/octet-stream"));
739741
headers.add(new HttpRequestor.Header("Content-Length", Long.toString(numBytes)));
740742

741-
HttpRequestor.Uploader uploader = DbxRequestUtil.startPut(requestConfig, accessToken, host, apiPath, writeMode.params, headers);
743+
HttpRequestor.Uploader uploader = DbxRequestUtil.startPut(requestConfig, accessToken, USER_AGENT_ID, host, apiPath, writeMode.params, headers);
742744

743745
return new SingleUploader(uploader, numBytes);
744746
}
@@ -928,7 +930,7 @@ private <E extends Throwable> HttpRequestor.Response chunkedUploadCommon(String[
928930
headers.add(new HttpRequestor.Header("Content-Type", "application/octet-stream"));
929931
headers.add(new HttpRequestor.Header("Content-Length", Long.toString(chunkSize)));
930932

931-
HttpRequestor.Uploader uploader = DbxRequestUtil.startPut(requestConfig, accessToken, host.content, apiPath, params, headers);
933+
HttpRequestor.Uploader uploader = DbxRequestUtil.startPut(requestConfig, accessToken, USER_AGENT_ID, host.content, apiPath, params, headers);
932934
try {
933935
try {
934936
NoThrowOutputStream nt = new NoThrowOutputStream(uploader.body);
@@ -1696,18 +1698,24 @@ public DbxLongpollDeltaResult getLongpollDelta(String cursor, int timeout)
16961698
"timeout", Integer.toString(timeout),
16971699
};
16981700

1699-
return DbxRequestUtil.doGet(getRequestConfig(), getAccessToken(), host.notify,
1700-
"1/longpoll_delta", params, null,
1701-
new DbxRequestUtil.ResponseHandler<DbxLongpollDeltaResult>()
1702-
{
1701+
return DbxRequestUtil.doGet(
1702+
getRequestConfig(),
1703+
getAccessToken(),
1704+
USER_AGENT_ID,
1705+
host.notify,
1706+
"1/longpoll_delta",
1707+
params,
1708+
null,
1709+
new DbxRequestUtil.ResponseHandler<DbxLongpollDeltaResult>() {
17031710
@Override
17041711
public DbxLongpollDeltaResult handle(HttpRequestor.Response response)
17051712
throws DbxException
17061713
{
17071714
if (response.statusCode != 200) throw DbxRequestUtil.unexpectedStatus(response);
17081715
return DbxRequestUtil.readJsonFromResponse(DbxLongpollDeltaResult.Reader, response);
17091716
}
1710-
});
1717+
}
1718+
);
17111719
}
17121720

17131721
// -----------------------------------------------------------------
@@ -2187,7 +2195,7 @@ private <T> T doGet(String host, String path, /*@Nullable*/String/*@Nullable*/[]
21872195
DbxRequestUtil.ResponseHandler<T> handler)
21882196
throws DbxException
21892197
{
2190-
return DbxRequestUtil.doGet(requestConfig, accessToken, host, path, params, headers, handler);
2198+
return DbxRequestUtil.doGet(requestConfig, accessToken, USER_AGENT_ID, host, path, params, headers, handler);
21912199
}
21922200

21932201
// Convenience function that calls RequestUtil.doPost with the first two parameters filled in.
@@ -2197,7 +2205,7 @@ public <T> T doPost(String host, String path,
21972205
DbxRequestUtil.ResponseHandler<T> handler)
21982206
throws DbxException
21992207
{
2200-
return DbxRequestUtil.doPost(requestConfig, accessToken, host, path, params, headers, handler);
2208+
return DbxRequestUtil.doPost(requestConfig, accessToken, USER_AGENT_ID, host, path, params, headers, handler);
22012209
}
22022210

22032211
/**

0 commit comments

Comments
 (0)