Skip to content

Commit 5fd48bb

Browse files
author
Amir Tocker
committed
Support Client Hints.
1 parent 2ce2a7e commit 5fd48bb

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Configuration {
3838
public boolean useRootPath;
3939
public int timeout;
4040
public boolean loadStrategies = true;
41+
public boolean clientHints = false;
4142

4243
public Configuration() {
4344
}
@@ -90,8 +91,34 @@ public void update(Map config) {
9091
this.useRootPath = ObjectUtils.asBoolean(config.get("use_root_path"), false);
9192
this.loadStrategies = ObjectUtils.asBoolean(config.get("load_strategies"), true);
9293
this.timeout = ObjectUtils.asInteger(config.get("timeout"), 0);
94+
this.clientHints = ObjectUtils.asBoolean(config.get("client_hints"), false);
9395
}
9496

97+
@SuppressWarnings("rawtypes")
98+
public Map<String, Object> asMap() {
99+
Map<String, Object> map = new HashMap<String, Object>();
100+
map.put("cloud_name", cloudName);
101+
map.put("api_key", apiKey);
102+
map.put("api_secret", apiSecret);
103+
map.put("secure_distribution", secureDistribution);
104+
map.put("cname", cname);
105+
map.put("secure", secure);
106+
map.put("private_cdn", privateCdn);
107+
map.put("cdn_subdomain", cdnSubdomain);
108+
map.put("shorten", shorten);
109+
map.put("upload_prefix", uploadPrefix);
110+
map.put("callback", callback);
111+
map.put("proxy_host", proxyHost);
112+
map.put("proxy_port", proxyPort);
113+
map.put("secure_cdn_subdomain", secureCdnSubdomain);
114+
map.put("use_root_path", useRootPath);
115+
map.put("load_strategies", loadStrategies);
116+
map.put("timeout", timeout);
117+
map.put("client_hints", clientHints);
118+
return map;
119+
}
120+
121+
95122
public Configuration(Configuration other) {
96123
this.cloudName = other.cloudName;
97124
this.apiKey = other.apiKey;
@@ -109,6 +136,7 @@ public Configuration(Configuration other) {
109136
this.secureCdnSubdomain = other.secureCdnSubdomain;
110137
this.useRootPath = other.useRootPath;
111138
this.timeout = other.timeout;
139+
this.clientHints = other.clientHints;
112140
}
113141

114142
/**
@@ -198,6 +226,7 @@ public static class Builder {
198226
private boolean useRootPath;
199227
private boolean loadStrategies = true;
200228
private int timeout;
229+
private boolean clientHints = false;
201230

202231
/**
203232
* Set the HTTP connection timeout.
@@ -214,7 +243,9 @@ public Builder setTimeout(int timeout) {
214243
* Creates a {@link Configuration} with the arguments supplied to this builder
215244
*/
216245
public Configuration build() {
217-
return new Configuration(cloudName, apiKey, apiSecret, secureDistribution, cname, uploadPrefix, secure, privateCdn, cdnSubdomain, shorten, callback, proxyHost, proxyPort, secureCdnSubdomain, useRootPath, timeout, loadStrategies);
246+
final Configuration configuration = new Configuration(cloudName, apiKey, apiSecret, secureDistribution, cname, uploadPrefix, secure, privateCdn, cdnSubdomain, shorten, callback, proxyHost, proxyPort, secureCdnSubdomain, useRootPath, timeout, loadStrategies);
247+
configuration.clientHints = clientHints;
248+
return configuration;
218249
}
219250

220251
/**
@@ -317,6 +348,11 @@ public Builder setLoadStrategies(boolean loadStrategies) {
317348
return this;
318349
}
319350

351+
public Builder setClientHints(boolean clientHints) {
352+
this.clientHints = clientHints;
353+
return this;
354+
}
355+
320356
/**
321357
* Initialize builder from existing {@link Configuration}
322358
*
@@ -341,6 +377,7 @@ public Builder from(Configuration other) {
341377
this.useRootPath = other.useRootPath;
342378
this.loadStrategies = other.loadStrategies;
343379
this.timeout = other.timeout;
380+
this.clientHints = other.clientHints;
344381
return this;
345382
}
346383
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ public String imageTag(String source, Map<String, String> attributes) {
506506
boolean hiDPI = transformation().isHiDPI();
507507
boolean responsive = transformation().isResponsive();
508508

509-
if (hiDPI || responsive) {
509+
if (!config.clientHints && (hiDPI || responsive)) {
510510
attributes.put("data-src", url);
511511
String extraClass = responsive ? "cld-responsive" : "cld-hidpi";
512512
attributes.put("class", (StringUtils.isBlank(attributes.get("class")) ? "" : attributes.get("class") + " ") + extraClass);

cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,26 @@ public void testImageTag() {
494494
result);
495495
}
496496

497+
@Test
498+
public void testClientHints() {
499+
String testTag;
500+
String message = "should not implement responsive behaviour if client hints is true";
501+
cloudinary.config.clientHints = true;
502+
Transformation trans = new Transformation()
503+
.crop("scale")
504+
.width("auto")
505+
.dpr("auto");
506+
testTag = cloudinary.url().transformation(trans).imageTag("sample.jpg");
507+
assertTrue(testTag.startsWith("<img") );
508+
assertFalse(testTag.contains("class="));
509+
assertFalse(testTag.contains("data-src"));
510+
assertTrue(testTag.contains("src='http://res.cloudinary.com/test123/image/upload/c_scale,dpr_auto,w_auto/sample.jpg'"));
511+
testTag = cloudinary.url().transformation(trans).imageTag("sample.jpg");
512+
assertTrue(testTag.startsWith("<img") );
513+
assertFalse(testTag.contains("class="));
514+
assertFalse(testTag.contains("data-src"));
515+
assertTrue(testTag.contains("src='http://res.cloudinary.com/test123/image/upload/c_scale,dpr_auto,w_auto/sample.jpg'"));
516+
}
497517
@Test
498518
public void testFolders() {
499519
// should add version if public_id contains /

0 commit comments

Comments
 (0)