Skip to content

Commit b4a5c66

Browse files
committed
First pass at an upload tag and support code
1 parent d111b8c commit b4a5c66

File tree

5 files changed

+152
-11
lines changed

5 files changed

+152
-11
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,24 @@ public Map text(String text, Map options) throws IOException {
196196
}
197197
return callApi("text", params, options, null);
198198
}
199+
200+
public void signRequestParams(Map<String, Object> params, Map options) {
201+
String apiKey = Cloudinary.asString(options.get("api_key"), this.cloudinary.getStringConfig("api_key"));
202+
if (apiKey == null)
203+
throw new IllegalArgumentException("Must supply api_key");
204+
String apiSecret = Cloudinary.asString(options.get("api_secret"), this.cloudinary.getStringConfig("api_secret"));
205+
if (apiSecret == null)
206+
throw new IllegalArgumentException("Must supply api_secret");
207+
params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString());
208+
params.put("signature", this.cloudinary.apiSignRequest(params, apiSecret));
209+
params.put("api_key", apiKey);
210+
}
211+
199212

200213
public Map callApi(String action, Map<String, Object> params, Map options, Object file) throws IOException {
201214
if (options == null) options = Cloudinary.emptyMap();
202215
boolean returnError = Cloudinary.asBoolean(options.get("return_error"), false);
203-
String apiKey = Cloudinary.asString(options.get("api_key"), this.cloudinary.getStringConfig("api_key"));
204-
if (apiKey == null)
205-
throw new IllegalArgumentException("Must supply api_key");
206-
String apiSecret = Cloudinary.asString(options.get("api_secret"), this.cloudinary.getStringConfig("api_secret"));
207-
if (apiSecret == null)
208-
throw new IllegalArgumentException("Must supply api_secret");
209-
params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString());
210-
params.put("signature", this.cloudinary.apiSignRequest(params, apiSecret));
211-
params.put("api_key", apiKey);
216+
signRequestParams(params, options);
212217

213218
String apiUrl = cloudinary.cloudinaryApiUrl(action, options);
214219

cloudinary-taglib/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<version>2.0</version>
2525
<scope>provided</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.apache.commons</groupId>
29+
<artifactId>commons-lang3</artifactId>
30+
<version>3.1</version>
31+
</dependency>
2732
</dependencies>
2833

2934
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.cloudinary;
2+
3+
/** This class contains a singleton in a generic way. This class is used by the tags to
4+
* retrieve the Cloudinary configuration.
5+
*
6+
* the containing framework is responsible for registering the cloudinary configuration with the
7+
* Singleton, and then removing it on shutdown. This allows the user to use Spring or any other
8+
* framework without imposing additional dependencies on the cloudinary project.
9+
*
10+
* @author jpollak
11+
*
12+
*/
13+
public class Singleton {
14+
15+
private static Cloudinary cloudinary;
16+
17+
public static void registerCloudinary(Cloudinary cloudinary) {
18+
Singleton.cloudinary = cloudinary;
19+
}
20+
21+
public static void deregisterCloudinary() {
22+
cloudinary = null;
23+
}
24+
25+
public static Cloudinary getCloudinary() {
26+
return cloudinary;
27+
}
28+
}
Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,90 @@
11
package com.cloudinary.taglib;
22

33
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
46

57
import javax.servlet.jsp.JspException;
68
import javax.servlet.jsp.tagext.SimpleTagSupport;
79

10+
import org.apache.commons.lang3.StringEscapeUtils;
11+
12+
import com.cloudinary.*;
13+
814
public class CloudinaryFileInputTag extends SimpleTagSupport {
915

16+
private String resourceType = "auto";
17+
private String fieldName;
18+
private String extraClasses;
19+
1020
public void doTag() throws JspException, IOException {
11-
String renderedHtml = "";
21+
Cloudinary cloudinary = Singleton.getCloudinary();
22+
if (cloudinary == null) {
23+
throw new JspException("Cloudinary config could not be located");
24+
}
25+
26+
StringBuilder renderedHtml = new StringBuilder();
27+
28+
renderedHtml.append("<input ");
29+
30+
Map<String, String> attributes = new HashMap<String, String>();
31+
attributes.put("type", "file");
32+
attributes.put("name", "file");
33+
34+
Map<String, String> options = new HashMap<String, String>();
35+
options.put("resource_type", resourceType);
36+
37+
String cloudinaryUrl = cloudinary.cloudinaryApiUrl("upload", options);
38+
39+
Uploader uploader = new Uploader(cloudinary);
40+
Map<String, Object> params = uploader.buildUploadParams(options);
41+
uploader.signRequestParams(params, options);
42+
43+
attributes.put("data-url", cloudinaryUrl);
44+
45+
StringBuilder jsonParams = new StringBuilder();
46+
jsonParams.append("{");
47+
for (Map.Entry<String, Object> entry : params.entrySet()) {
48+
renderedHtml.append("'" + entry.getKey() + "':'" + entry.getValue() + "',");
49+
}
50+
jsonParams.append("}");
51+
String escapedJsonParams = StringEscapeUtils.escapeHtml4(jsonParams.toString());
1252

13-
getJspContext().getOut().println(renderedHtml);
53+
attributes.put("data-form-data", escapedJsonParams);
54+
attributes.put("data-cloudinary-field", fieldName);
55+
attributes.put("class", extraClasses + " cloudinary-fileupload");
56+
57+
for (Map.Entry<String, String> entry : attributes.entrySet()) {
58+
renderedHtml.append(" " + entry.getKey() + "='" + entry.getValue() + "'");
59+
}
60+
61+
renderedHtml.append("/>");
62+
63+
getJspContext().getOut().println(renderedHtml.toString());
64+
}
65+
66+
public void setResourceType(String resourceType) {
67+
this.resourceType = resourceType;
68+
}
69+
70+
public String GetResourceType() {
71+
return resourceType;
72+
}
73+
74+
public void setFieldName(String fieldName) {
75+
this.fieldName = fieldName;
76+
}
77+
78+
public String getFieldName() {
79+
return fieldName;
80+
}
81+
82+
public void setExtraClasses(String extraClasses) {
83+
this.extraClasses = extraClasses;
84+
}
85+
86+
public String getExtraClasses() {
87+
return extraClasses;
1488
}
89+
1590
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
4+
version="2.0">
5+
<tlib-version>1.1</tlib-version>
6+
<jsp-version>2.0</jsp-version>
7+
<short-name>Cloudinary Taglib</short-name>
8+
<tag>
9+
<name>cloudinaryInput</name>
10+
<tag-class>com.cloudinary.taglib.CloudinaryFileInputTag</tag-class>
11+
<body-content>scriptless</body-content>
12+
<attribute>
13+
<name>fieldName</name>
14+
<required>true</required>
15+
<rtexprvalue>true</rtexprvalue>
16+
</attribute>
17+
<attribute>
18+
<name>extraClasses</name>
19+
<required>false</required>
20+
<rtexprvalue>true</rtexprvalue>
21+
</attribute>
22+
<attribute>
23+
<name>resourceType</name>
24+
<required>false</required>
25+
<rtexprvalue>true</rtexprvalue>
26+
</attribute>
27+
</tag>
28+
</taglib>

0 commit comments

Comments
 (0)