Skip to content

Commit c1c4260

Browse files
committed
Introduced a new image tag for jsps, you can use it like this:
<cl:image id="profilePhoto" crop="crop" height="120" width="120" extraClasses="profilePhoto" publicId="${actionBean.worker.cloudinaryPublicId}" format="jpg" />
1 parent d4e4a05 commit c1c4260

File tree

4 files changed

+125
-5
lines changed

4 files changed

+125
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ public void testFetch() {
189189

190190
@Test
191191
public void testCname() {
192-
// should support extenal cname
192+
// should support external cname
193193
String result = cloudinary.url().cname("hello.com").generate("test");
194194
assertEquals("http://hello.com/test123/image/upload/test", result);
195195
}
196196

197197
@Test
198198
public void testCnameSubdomain() {
199-
// should support extenal cname with cdn_subdomain on
199+
// should support external cname with cdn_subdomain on
200200
String result = cloudinary.url().cname("hello.com").cdnSubdomain(true).generate("test");
201201
assertEquals("http://a2.hello.com/test123/image/upload/test", result);
202202
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.cloudinary.taglib;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import javax.servlet.jsp.JspException;
8+
import javax.servlet.jsp.JspWriter;
9+
import javax.servlet.jsp.tagext.DynamicAttributes;
10+
import javax.servlet.jsp.tagext.SimpleTagSupport;
11+
12+
import com.cloudinary.*;
13+
14+
/**
15+
* <cl:img source='test' height='101' width='100' transform="crop" />
16+
*
17+
* Transformation transformation = new Transformation().width(100).height(101).crop("crop");
18+
* String result = cloudinary.url().transformation(transformation).imageTag("test",
19+
* Cloudinary.asMap("alt", "my image"));
20+
*
21+
* <img src='http://res.cloudinary.com/test123/image/upload/c_crop,h_101,w_100/test' alt='my image'
22+
* height='101' width='100'/>
23+
*
24+
* @author jpollak
25+
*
26+
*/
27+
public class CloudinaryImageTag extends SimpleTagSupport implements DynamicAttributes {
28+
29+
private String id = null;
30+
private String extraClasses = null;
31+
32+
private String publicId = null;
33+
private String format = null;
34+
35+
/** stores the dynamic attributes */
36+
private Map<String,Object> tagAttrs = new HashMap<String,Object>();
37+
38+
public void doTag() throws JspException, IOException {
39+
Cloudinary cloudinary = Singleton.getCloudinary();
40+
if (cloudinary == null) {
41+
throw new JspException("Cloudinary config could not be located");
42+
}
43+
44+
JspWriter out = getJspContext().getOut();
45+
46+
Map<String, String> attributes = new HashMap<String, String>();
47+
if (id != null) {
48+
attributes.put("id", id);
49+
}
50+
if (extraClasses != null) {
51+
attributes.put("class", extraClasses);
52+
}
53+
54+
Transformation transformation = new Transformation().params(tagAttrs);
55+
Url url = cloudinary.url().transformation(transformation).format(format);
56+
57+
out.println(url.imageTag(publicId, attributes));
58+
}
59+
60+
public void setId(String id) {
61+
this.id = id;
62+
}
63+
64+
public String getId() {
65+
return id;
66+
}
67+
68+
public void setExtraClasses(String extraClasses) {
69+
this.extraClasses = extraClasses;
70+
}
71+
72+
public String getExtraClass() {
73+
return extraClasses;
74+
}
75+
76+
public void setPublicId(String publicId) {
77+
this.publicId = publicId;
78+
}
79+
80+
public String getPublicId() {
81+
return publicId;
82+
}
83+
84+
public void setFormat(String format) {
85+
this.format = format;
86+
}
87+
88+
public String getFormat() {
89+
return format;
90+
}
91+
92+
@Override
93+
public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
94+
tagAttrs.put(name, value);
95+
}
96+
}

cloudinary-taglib/src/main/java/com/cloudinary/taglib/CloudinaryUploadTag.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import javax.servlet.jsp.JspException;
88
import javax.servlet.jsp.tagext.SimpleTagSupport;
99

10-
import org.apache.commons.lang3.StringEscapeUtils;
11-
1210
import com.cloudinary.*;
1311

1412
public class CloudinaryUploadTag extends SimpleTagSupport {

cloudinary-taglib/src/main/resources/META-INF/cloudinary.tld

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<rtexprvalue>true</rtexprvalue>
3232
</attribute>
3333
<attribute>
34-
<name>extraClasses</name>
34+
<name>class</name>
3535
<required>false</required>
3636
<rtexprvalue>true</rtexprvalue>
3737
</attribute>
@@ -41,4 +41,30 @@
4141
<rtexprvalue>true</rtexprvalue>
4242
</attribute>
4343
</tag>
44+
<tag>
45+
<name>image</name>
46+
<tag-class>com.cloudinary.taglib.CloudinaryImageTag</tag-class>
47+
<body-content>scriptless</body-content>
48+
<attribute>
49+
<name>id</name>
50+
<required>false</required>
51+
<rtexprvalue>true</rtexprvalue>
52+
</attribute>
53+
<attribute>
54+
<name>extraClasses</name>
55+
<required>false</required>
56+
<rtexprvalue>true</rtexprvalue>
57+
</attribute>
58+
<attribute>
59+
<name>publicId</name>
60+
<required>true</required>
61+
<rtexprvalue>true</rtexprvalue>
62+
</attribute>
63+
<attribute>
64+
<name>format</name>
65+
<required>false</required>
66+
<rtexprvalue>true</rtexprvalue>
67+
</attribute>
68+
<dynamic-attributes>true</dynamic-attributes>
69+
</tag>
4470
</taglib>

0 commit comments

Comments
 (0)