Skip to content

Commit 9aad938

Browse files
committed
refactoring CloudKey
1 parent f6ac1dc commit 9aad938

File tree

9 files changed

+237
-215
lines changed

9 files changed

+237
-215
lines changed

cloudkey.jar

77 Bytes
Binary file not shown.

examples/Cloud.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ try
3232
out.write("<p>Title : " + item.pull("meta.title") + "</p>");
3333
out.write("<p><img src=\"" + item.pull("assets.jpeg_thumbnail_auto.stream_url") + "\" /></p>");
3434
String[] referers = {server_url};
35-
String embed_url = cloud.get_embed_url(CloudKey.CLOUDKEY_API_URL, item.get("id").toString(), CloudKey.CLOUDKEY_SECLEVEL_REFERER, "", "", "", null, referers, 0);
35+
String embed_url = cloud.getEmbedUrl(CloudKey.CLOUDKEY_API_URL, item.get("id").toString(), CloudKey.CLOUDKEY_SECLEVEL_REFERER, "", "", "", null, referers, 0);
3636
out.write("<iframe width=\"" + item.pull("assets.mp4_h264_aac.video_width") + "\" height=\"" + item.pull("assets.mp4_h264_aac.video_height") + "\" src=\"" + embed_url + "\"></iframe>");
37-
String stream_url = cloud.get_stream_url(CloudKey.CLOUDKEY_API_URL, item.get("id").toString(), "mp4_h264_aac", CloudKey.CLOUDKEY_SECLEVEL_REFERER, "", "", "", null, referers, 0, "", false);
37+
String stream_url = cloud.getStreamUrl(CloudKey.CLOUDKEY_API_URL, item.get("id").toString(), "mp4_h264_aac", CloudKey.CLOUDKEY_SECLEVEL_REFERER, "", "", "", null, referers, 0, "", false);
3838
String dl_url = item.pull("assets.source.download_url");
3939
out.write("<p><a href=\"" + dl_url + "\">Download source</a></p>");
4040
out.write("<p><a href=\"" + stream_url + "\">Stream url</a></p>");

examples/MediaCreate.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<%
44
String url = request.getParameter("url");
55
6-
CloudKey_Media media = new CloudKey_Media(user_id, api_key);
7-
out.write("video id : " + media.create(url));
6+
CloudKey cloud = new CloudKey(user_id, api_key);
7+
out.write("video id : " + cloud.mediaCreate(url));
88
%>

examples/Upload.jsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
<%
6565
String redirect_url = "http://" + InetAddress.getLocalHost().getHostName() + ":8080/examples/MediaCreate.jsp";
6666
67-
CloudKey_Media media = new CloudKey_Media(user_id, api_key);
68-
DCObject result = media.upload(true, true, redirect_url);
67+
CloudKey cloud = new CloudKey(user_id, api_key);
68+
DCObject result = cloud.fileUpload(true, "?", redirect_url);
6969
String status_url = result.pull("status");
7070
String upload_url = result.pull("url");
7171
%>

src/com/dmcloud/CloudKey.java

Lines changed: 118 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,44 @@
11
package com.dmcloud;
22

3+
import java.io.*;
34
import java.util.Map;
5+
import org.apache.commons.httpclient.HttpClient;
6+
import org.apache.commons.httpclient.HttpStatus;
7+
import org.apache.commons.httpclient.methods.PostMethod;
8+
import org.apache.commons.httpclient.methods.multipart.FilePart;
9+
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
10+
import org.apache.commons.httpclient.methods.multipart.Part;
411
import org.codehaus.jackson.map.ObjectMapper;
512

6-
public class CloudKey
13+
public class CloudKey extends CloudKey_Api
714
{
8-
public static int CLOUDKEY_SECLEVEL_NONE = 0;
9-
public static int CLOUDKEY_SECLEVEL_DELEGATE = 1 << 0;
10-
public static int CLOUDKEY_SECLEVEL_ASNUM = 1 << 1;
11-
public static int CLOUDKEY_SECLEVEL_IP = 1 << 2;
12-
public static int CLOUDKEY_SECLEVEL_USERAGENT = 1 << 3;
13-
public static int CLOUDKEY_SECLEVEL_USEONCE = 1 << 4;
14-
public static int CLOUDKEY_SECLEVEL_COUNTRY = 1 << 5;
15-
public static int CLOUDKEY_SECLEVEL_REFERER = 1 << 6;
16-
17-
public static String CLOUDKEY_API_URL = "http://api.dmcloud.net";
18-
public static String CLOUDKEY_CDN_URL = "http://cdn.dmcloud.net";
19-
public static String CLOUDKEY_STATIC_URL = "http://static.dmcloud.net";
20-
21-
protected String user_id = null;
22-
protected String api_key = null;
23-
protected String base_url = null;
24-
protected String cdn_url = null;
25-
protected String proxy = null;
26-
2715
public CloudKey(String _user_id, String _api_key) throws Exception
2816
{
29-
this(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
17+
super(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
3018
}
3119

3220
public CloudKey(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
3321
{
34-
if (_user_id == null || _api_key == null)
35-
{
36-
throw new CloudKey_Exception("You must provide valid user_id and api_key parameters");
37-
}
38-
this.user_id = _user_id;
39-
this.api_key = _api_key;
40-
this.base_url = _base_url;
41-
this.cdn_url = _cdn_url;
42-
this.proxy = _proxy;
43-
}
44-
45-
public DCObject call(String call, DCObject args) throws Exception
46-
{
47-
ObjectMapper mapper = new ObjectMapper();
48-
49-
DCObject jo = DCObject.create()
50-
.push("call", call)
51-
.push("args", args);
52-
53-
jo.push("auth", this.user_id + ":" + CloudKey_Helpers.md5(this.user_id + CloudKey_Helpers.normalize(jo) + this.api_key));
54-
55-
String json_encoded = mapper.writeValueAsString(jo);
56-
String response = CloudKey_Helpers.curl(this.base_url + "/api", json_encoded);
57-
DCObject json_response = DCObject.create(mapper.readValue(response, Map.class));
58-
if (json_response.containsKey("error"))
59-
{
60-
String message = "";
61-
int error_code = Integer.parseInt(json_response.pull("error.code"));
62-
switch (error_code)
63-
{
64-
case 200 : message = "ProcessorException"; break;
65-
case 300 : message = "TransportException"; break;
66-
case 400 : message = "AuthenticationErrorException"; break;
67-
case 410 : message = "RateLimitExceededException"; break;
68-
case 500 : message = "SerializerException"; break;
69-
70-
case 600 : message = "InvalidRequestException"; break;
71-
case 610 : message = "InvalidObjectException"; break;
72-
case 620 : message = "InvalidMethodException"; break;
73-
case 630 : message = "InvalidParamException"; break;
74-
75-
case 1000 : message = "ApplicationException"; break;
76-
case 1010 : message = "NotFoundException"; break;
77-
case 1020 : message = "ExistsException"; break;
78-
case 1030 : message = "LimitExceededException"; break;
79-
80-
default : message = "RPCException (error:" + json_response.pull("error.code").toString() + ")"; break;
81-
}
82-
message += " : " + json_response.pull("error.message").toString();
83-
throw new CloudKey_Exception(message, error_code);
84-
}
85-
return DCObject.create((Map)json_response.get("result"));
22+
super(_user_id, _api_key, _base_url, _cdn_url, _proxy);
8623
}
8724

88-
public String get_embed_url(String id) throws CloudKey_Exception
25+
public String getEmbedUrl(String id) throws CloudKey_Exception
8926
{
90-
return this.get_embed_url(CLOUDKEY_API_URL, id, CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0);
27+
return this.getEmbedUrl(CLOUDKEY_API_URL, id, CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0);
9128
}
9229

93-
public String get_embed_url(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws CloudKey_Exception
30+
public String getEmbedUrl(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws CloudKey_Exception
9431
{
9532
String _url = url + "/embed/" + this.user_id + "/" + id;
9633
return CloudKey_Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires);
9734
}
9835

99-
public String get_stream_url(String id) throws CloudKey_Exception
36+
public String getStreamUrl(String id) throws CloudKey_Exception
10037
{
101-
return this.get_stream_url(CLOUDKEY_API_URL, id, "mp4_h264_aac", CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
38+
return this.getStreamUrl(CLOUDKEY_API_URL, id, "mp4_h264_aac", CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
10239
}
10340

104-
public String get_stream_url(String url, String id, String asset_name, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires, String extension, Boolean download) throws CloudKey_Exception
41+
public String getStreamUrl(String url, String id, String asset_name, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires, String extension, Boolean download) throws CloudKey_Exception
10542
{
10643
if (extension == "")
10744
{
@@ -118,4 +55,106 @@ public String get_stream_url(String url, String id, String asset_name, int secle
11855
return CloudKey_Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires) + (download ? "&throttle=0&helper=0&cache=0" : "");
11956
}
12057
}
58+
59+
public String mediaCreate() throws Exception
60+
{
61+
return mediaCreate("");
62+
}
63+
64+
public String mediaCreate(String url) throws Exception
65+
{
66+
return mediaCreate(url, null, null);
67+
}
68+
69+
public String mediaCreate(String url, DCArray assets_names, DCObject meta) throws Exception
70+
{
71+
DCObject args = DCObject.create().push("url", url);
72+
if (assets_names != null && assets_names.size() > 0)
73+
{
74+
args.push("assets_names", assets_names);
75+
}
76+
if (meta != null && meta.size() > 0)
77+
{
78+
args.push("meta", meta);
79+
}
80+
DCObject result = this.call("media.create", args);
81+
return result.pull("id");
82+
}
83+
84+
85+
public String mediaCreate(File f) throws Exception
86+
{
87+
return this.mediaCreate(f, null, null);
88+
}
89+
90+
public String mediaCreate(File f, DCArray assets_names, DCObject meta) throws Exception
91+
{
92+
String upload_url = this.fileUpload();
93+
94+
PostMethod filePost = null;
95+
try
96+
{
97+
filePost = new PostMethod(upload_url);
98+
99+
Part[] parts = {
100+
new FilePart("file", f)
101+
};
102+
103+
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
104+
HttpClient client = new HttpClient();
105+
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
106+
107+
int status = client.executeMethod(filePost);
108+
if (status == HttpStatus.SC_OK)
109+
{
110+
ObjectMapper mapper = new ObjectMapper();
111+
DCObject json_response = DCObject.create(mapper.readValue(filePost.getResponseBodyAsString(), Map.class));
112+
return this.mediaCreate(json_response.pull("url"), assets_names, meta);
113+
}
114+
else
115+
{
116+
throw new CloudKey_Exception("Upload failed.");
117+
}
118+
}
119+
catch (Exception e)
120+
{
121+
throw new CloudKey_Exception("Upload failed.");
122+
}
123+
finally
124+
{
125+
if (filePost != null)
126+
{
127+
filePost.releaseConnection();
128+
}
129+
}
130+
}
131+
132+
public void mediaDelete(String id) throws Exception
133+
{
134+
this.call("media.delete", DCObject.create().push("id", id));
135+
}
136+
137+
public String fileUpload() throws Exception
138+
{
139+
DCObject result = fileUpload(false, "", "");
140+
return result.pull("url");
141+
}
142+
143+
public DCObject fileUpload(Boolean status, String jsonp_cb, String target) throws Exception
144+
{
145+
DCObject args = DCObject.create();
146+
if (status)
147+
{
148+
args.push("status", true);
149+
}
150+
if (jsonp_cb != "")
151+
{
152+
args.push("jsonp_cb", jsonp_cb);
153+
}
154+
if (target != "")
155+
{
156+
args.push("target", target);
157+
}
158+
return (DCObject) this.call("file.upload", args);
159+
}
121160
}

src/com/dmcloud/CloudKey_Api.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.dmcloud;
2+
3+
import java.util.Map;
4+
import org.codehaus.jackson.map.ObjectMapper;
5+
6+
public class CloudKey_Api
7+
{
8+
public static int CLOUDKEY_SECLEVEL_NONE = 0;
9+
public static int CLOUDKEY_SECLEVEL_DELEGATE = 1 << 0;
10+
public static int CLOUDKEY_SECLEVEL_ASNUM = 1 << 1;
11+
public static int CLOUDKEY_SECLEVEL_IP = 1 << 2;
12+
public static int CLOUDKEY_SECLEVEL_USERAGENT = 1 << 3;
13+
public static int CLOUDKEY_SECLEVEL_USEONCE = 1 << 4;
14+
public static int CLOUDKEY_SECLEVEL_COUNTRY = 1 << 5;
15+
public static int CLOUDKEY_SECLEVEL_REFERER = 1 << 6;
16+
17+
public static String CLOUDKEY_API_URL = "http://api.dmcloud.net";
18+
public static String CLOUDKEY_CDN_URL = "http://cdn.dmcloud.net";
19+
public static String CLOUDKEY_STATIC_URL = "http://static.dmcloud.net";
20+
21+
protected String user_id = null;
22+
protected String api_key = null;
23+
protected String base_url = null;
24+
protected String cdn_url = null;
25+
protected String proxy = null;
26+
27+
public CloudKey_Api(String _user_id, String _api_key) throws Exception
28+
{
29+
this(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
30+
}
31+
32+
public CloudKey_Api(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
33+
{
34+
if (_user_id == null || _api_key == null)
35+
{
36+
throw new CloudKey_Exception("You must provide valid user_id and api_key parameters");
37+
}
38+
this.user_id = _user_id;
39+
this.api_key = _api_key;
40+
this.base_url = _base_url;
41+
this.cdn_url = _cdn_url;
42+
this.proxy = _proxy;
43+
}
44+
45+
public DCObject call(String call, DCObject args) throws Exception
46+
{
47+
ObjectMapper mapper = new ObjectMapper();
48+
49+
DCObject jo = DCObject.create()
50+
.push("call", call)
51+
.push("args", args);
52+
53+
jo.push("auth", this.user_id + ":" + CloudKey_Helpers.md5(this.user_id + CloudKey_Helpers.normalize(jo) + this.api_key));
54+
55+
String json_encoded = mapper.writeValueAsString(jo);
56+
String response = CloudKey_Helpers.curl(this.base_url + "/api", json_encoded);
57+
DCObject json_response = DCObject.create(mapper.readValue(response, Map.class));
58+
if (json_response.containsKey("error"))
59+
{
60+
String message = "";
61+
int error_code = Integer.parseInt(json_response.pull("error.code"));
62+
switch (error_code)
63+
{
64+
case 200 : message = "ProcessorException"; break;
65+
case 300 : message = "TransportException"; break;
66+
case 400 : message = "AuthenticationErrorException"; break;
67+
case 410 : message = "RateLimitExceededException"; break;
68+
case 500 : message = "SerializerException"; break;
69+
70+
case 600 : message = "InvalidRequestException"; break;
71+
case 610 : message = "InvalidObjectException"; break;
72+
case 620 : message = "InvalidMethodException"; break;
73+
case 630 : message = "InvalidParamException"; break;
74+
75+
case 1000 : message = "ApplicationException"; break;
76+
case 1010 : message = "NotFoundException"; break;
77+
case 1020 : message = "ExistsException"; break;
78+
case 1030 : message = "LimitExceededException"; break;
79+
80+
default : message = "RPCException (error:" + json_response.pull("error.code").toString() + ")"; break;
81+
}
82+
message += " : " + json_response.pull("error.message").toString();
83+
throw new CloudKey_Exception(message, error_code);
84+
}
85+
return DCObject.create((Map)json_response.get("result"));
86+
}
87+
}

0 commit comments

Comments
 (0)