Skip to content

Commit 480518a

Browse files
authored
Allow setting the user agent (#235)
1 parent f66644b commit 480518a

File tree

10 files changed

+39
-11
lines changed

10 files changed

+39
-11
lines changed

cloudinary-core/src/main/java/com/cloudinary/Cloudinary.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ public class Cloudinary {
3838
public final static String SHARED_CDN = AKAMAI_SHARED_CDN;
3939

4040
public final static String VERSION = "1.28.1";
41-
public final static String USER_AGENT = "CloudinaryJava/" + VERSION + " (Java " + System.getProperty("java.version") + ")";
41+
static String USER_AGENT_PREFIX = "CloudinaryJava";
42+
public final static String USER_AGENT_JAVA_VERSION = "(Java " + System.getProperty("java.version") + ")";
4243

4344
public final Configuration config;
4445
private AbstractUploaderStrategy uploaderStrategy;
4546
private AbstractApiStrategy apiStrategy;
47+
private String userAgent = USER_AGENT_PREFIX+"/"+ VERSION + " "+USER_AGENT_JAVA_VERSION;
4648

4749
public Uploader uploader() {
4850
return new Uploader(this, uploaderStrategy);
49-
5051
}
5152

5253
public Api api() {
@@ -135,6 +136,23 @@ public String apiSignRequest(Map<String, Object> paramsToSign, String apiSecret)
135136
return Util.produceSignature(paramsToSign, apiSecret, config.signatureAlgorithm);
136137
}
137138

139+
/**
140+
* @return the userAgent that will be sent with every API call.
141+
*/
142+
public String getUserAgent(){
143+
return userAgent;
144+
}
145+
146+
/**
147+
* Set the prefix and version for the user agent that will be sent with every API call
148+
* a userAgent is built from `prefix/version (additional data)`
149+
* @param prefix - the prefix of the userAgent to be set
150+
* @param version - the version of the userAgent to be set
151+
*/
152+
public void setUserAgent(String prefix, String version){
153+
userAgent = prefix+"/"+ version + " ("+USER_AGENT_PREFIX+ " "+VERSION+") " + USER_AGENT_JAVA_VERSION;
154+
}
155+
138156
/**
139157
* Verifies that Cloudinary notification request is genuine by checking its signature.
140158
*

cloudinary-http42/src/main/java/com/cloudinary/http42/ApiStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private ApiResponse getApiResponse(HttpMethod method, Map<String, ?> params, Str
107107
break;
108108
}
109109
request.setHeader("Authorization", getAuthorizationHeaderValue(apiKey, apiSecret, oauthToken));
110-
request.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
110+
request.setHeader("User-Agent", this.api.cloudinary.getUserAgent() + " ApacheHTTPComponents/4.2");
111111
if (contentType.equals("json")) {
112112
JSONObject asJSON = ObjectUtils.toJSON(params);
113113
StringEntity requestEntity = new StringEntity(asJSON.toString(), ContentType.APPLICATION_JSON);

cloudinary-http42/src/main/java/com/cloudinary/http42/UploaderStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
6060
}
6161

6262
HttpPost postMethod = new HttpPost(apiUrl);
63-
postMethod.setHeader("User-Agent", Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.2");
63+
postMethod.setHeader("User-Agent", this.cloudinary().getUserAgent() + " ApacheHTTPComponents/4.2");
6464

6565
Map<String, String> extraHeaders = (Map<String, String>) options.get("extra_headers");
6666
if (extraHeaders != null) {

cloudinary-http43/src/main/java/com/cloudinary/http43/ApiStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void init(Api api) {
4747
super.init(api);
4848

4949
HttpClientBuilder clientBuilder = HttpClients.custom();
50-
clientBuilder.useSystemProperties().setUserAgent(Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.3");
50+
clientBuilder.useSystemProperties().setUserAgent(this.api.cloudinary.getUserAgent() + " ApacheHTTPComponents/4.3");
5151

5252
// If the configuration specifies a proxy then apply it to the client
5353
if (api.cloudinary.config.proxyHost != null && api.cloudinary.config.proxyPort != 0) {

cloudinary-http43/src/main/java/com/cloudinary/http43/UploaderStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void init(Uploader uploader) {
3737
super.init(uploader);
3838

3939
HttpClientBuilder clientBuilder = HttpClients.custom();
40-
clientBuilder.useSystemProperties().setUserAgent(Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.3");
40+
clientBuilder.useSystemProperties().setUserAgent(this.cloudinary().getUserAgent() + " ApacheHTTPComponents/4.3");
4141

4242
// If the configuration specifies a proxy then apply it to the client
4343
if (cloudinary().config.proxyHost != null && cloudinary().config.proxyPort != 0) {

cloudinary-http44/src/main/java/com/cloudinary/http44/ApiStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void init(Api api) {
4949
super.init(api);
5050

5151
HttpClientBuilder clientBuilder = HttpClients.custom();
52-
clientBuilder.useSystemProperties().setUserAgent(Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.4");
52+
clientBuilder.useSystemProperties().setUserAgent(this.api.cloudinary.getUserAgent() + " ApacheHTTPComponents/4.4");
5353

5454
// If the configuration specifies a proxy then apply it to the client
5555
if (api.cloudinary.config.proxyHost != null && api.cloudinary.config.proxyPort != 0) {

cloudinary-http44/src/main/java/com/cloudinary/http44/UploaderStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void init(Uploader uploader) {
3838
super.init(uploader);
3939

4040
HttpClientBuilder clientBuilder = HttpClients.custom();
41-
clientBuilder.useSystemProperties().setUserAgent(Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.4");
41+
clientBuilder.useSystemProperties().setUserAgent(this.cloudinary().getUserAgent() + " ApacheHTTPComponents/4.4");
4242

4343
// If the configuration specifies a proxy then apply it to the client
4444
if (cloudinary().config.proxyHost != null && cloudinary().config.proxyPort != 0) {

cloudinary-http45/src/main/java/com/cloudinary/http45/ApiStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void init(Api api) {
4747
super.init(api);
4848

4949
HttpClientBuilder clientBuilder = HttpClients.custom();
50-
clientBuilder.useSystemProperties().setUserAgent(Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.5");
50+
clientBuilder.useSystemProperties().setUserAgent(this.api.cloudinary.getUserAgent() + " ApacheHTTPComponents/4.5");
5151

5252
// If the configuration specifies a proxy then apply it to the client
5353
if (api.cloudinary.config.proxyHost != null && api.cloudinary.config.proxyPort != 0) {

cloudinary-http45/src/main/java/com/cloudinary/http45/UploaderStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void init(Uploader uploader) {
3434
super.init(uploader);
3535

3636
HttpClientBuilder clientBuilder = HttpClients.custom();
37-
clientBuilder.useSystemProperties().setUserAgent(Cloudinary.USER_AGENT + " ApacheHTTPComponents/4.5");
37+
clientBuilder.useSystemProperties().setUserAgent(cloudinary().getUserAgent() + " ApacheHTTPComponents/4.5");
3838

3939
// If the configuration specifies a proxy then apply it to the client
4040
if (cloudinary().config.proxyHost != null && cloudinary().config.proxyPort != 0) {

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ abstract public class AbstractApiTest extends MockableTest {
4747
public static final String TEST_KEY = "test-key" + SUFFIX;
4848
public static final String API_TEST_RESTORE = "api_test_restore" + SUFFIX;
4949
public static final Set<String> createdFolders = new HashSet<String>();
50+
private static final String CUSTOM_USER_AGENT_PREFIX = "TEST_USER_AGENT";
51+
private static final String CUSTOM_USER_AGENT_VERSION = "9.9.9";
52+
5053

5154
protected Api api;
5255

@@ -126,7 +129,6 @@ public static void tearDownClass() {
126129
}
127130
} catch (Exception ignored) {
128131
}
129-
130132
}
131133

132134
@Rule
@@ -151,6 +153,14 @@ public Map findByAttr(List<Map> elements, String attr, Object value) {
151153
return null;
152154
}
153155

156+
@Test
157+
public void testCustomUserAgent() throws Exception {
158+
// should allow setting a custom user-agent
159+
cloudinary.setUserAgent(CUSTOM_USER_AGENT_PREFIX, CUSTOM_USER_AGENT_VERSION);
160+
Map results = api.ping(ObjectUtils.emptyMap());
161+
//TODO Mock server and assert the header
162+
}
163+
154164
@Test
155165
public void test01ResourceTypes() throws Exception {
156166
// should allow listing resource_types

0 commit comments

Comments
 (0)