Skip to content

Commit 34d6201

Browse files
committed
refactoring some classes
1 parent a81d17e commit 34d6201

File tree

11 files changed

+94
-96
lines changed

11 files changed

+94
-96
lines changed

cloudkey.jar

3.32 KB
Binary file not shown.

examples/MediaCreate.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%@ page import="net.dmcloud.*"%>
1+
<%@ page import="net.dmcloud.cloudkey.*"%>
22
<%@ page import="net.dmcloud.util.*"%>
33

44
<%@ include file="Config.jsp"%>

examples/MediaList.jsp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1>My videos on DM Cloud</h1>
22

3-
<%@ page import="net.dmcloud.*"%>
3+
<%@ page import="net.dmcloud.cloudkey.*"%>
44
<%@ page import="net.dmcloud.util.*"%>
55
<%@ page import="java.util.Map"%>
66
<%@ page import="java.net.InetAddress"%>
@@ -33,9 +33,9 @@ try
3333
out.write("<p>Title : " + item.pull("meta.title") + "</p>");
3434
out.write("<p><img src=\"" + item.pull("assets.jpeg_thumbnail_auto.stream_url") + "\" /></p>");
3535
String[] referers = {server_url};
36-
String embed_url = cloud.mediaGetEmbedUrl(item.get("id").toString(), CloudKey.CLOUDKEY_SECLEVEL_REFERER, "", "", "", null, referers, 0);
36+
String embed_url = cloud.mediaGetEmbedUrl(item.get("id").toString(), CloudKey.SECLEVEL_REFERER, "", "", "", null, referers, 0);
3737
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>");
38-
String stream_url = cloud.mediaGetStreamUrl(item.get("id").toString(), "mp4_h264_aac", CloudKey.CLOUDKEY_SECLEVEL_REFERER, "", "", "", null, referers, 0, "", false);
38+
String stream_url = cloud.mediaGetStreamUrl(item.get("id").toString(), "mp4_h264_aac", CloudKey.SECLEVEL_REFERER, "", "", "", null, referers, 0, "", false);
3939
String dl_url = item.pull("assets.source.download_url");
4040
out.write("<p><a href=\"" + dl_url + "\">Download source</a></p>");
4141
out.write("<p><a href=\"" + stream_url + "\">Stream url</a></p>");

examples/Upload.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
background-color: #ffafae;
5959
}
6060
</style>
61-
<%@ page import="net.dmcloud.*"%>
61+
<%@ page import="net.dmcloud.cloudkey.*"%>
6262
<%@ page import="net.dmcloud.util.*"%>
6363
<%@ page import="java.net.InetAddress"%>
6464
<%@ include file="Config.jsp"%>

src/net/dmcloud/CloudKey_Exception.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
1-
package net.dmcloud;
1+
package net.dmcloud.cloudkey;
22

33
import java.util.Map;
4-
54
import net.dmcloud.util.*;
6-
75
import org.codehaus.jackson.map.ObjectMapper;
86

9-
public class CloudKey_Api
7+
public class Api
108
{
11-
public static int CLOUDKEY_SECLEVEL_NONE = 0;
12-
public static int CLOUDKEY_SECLEVEL_DELEGATE = 1 << 0;
13-
public static int CLOUDKEY_SECLEVEL_ASNUM = 1 << 1;
14-
public static int CLOUDKEY_SECLEVEL_IP = 1 << 2;
15-
public static int CLOUDKEY_SECLEVEL_USERAGENT = 1 << 3;
16-
public static int CLOUDKEY_SECLEVEL_USEONCE = 1 << 4;
17-
public static int CLOUDKEY_SECLEVEL_COUNTRY = 1 << 5;
18-
public static int CLOUDKEY_SECLEVEL_REFERER = 1 << 6;
9+
public static int SECLEVEL_NONE = 0;
10+
public static int SECLEVEL_DELEGATE = 1 << 0;
11+
public static int SECLEVEL_ASNUM = 1 << 1;
12+
public static int SECLEVEL_IP = 1 << 2;
13+
public static int SECLEVEL_USERAGENT = 1 << 3;
14+
public static int SECLEVEL_USEONCE = 1 << 4;
15+
public static int SECLEVEL_COUNTRY = 1 << 5;
16+
public static int SECLEVEL_REFERER = 1 << 6;
1917

20-
public static String CLOUDKEY_API_URL = "http://api.dmcloud.net";
21-
public static String CLOUDKEY_CDN_URL = "http://cdn.dmcloud.net";
22-
public static String CLOUDKEY_STATIC_URL = "http://static.dmcloud.net";
18+
public static String API_URL = "http://api.dmcloud.net";
19+
public static String CDN_URL = "http://cdn.dmcloud.net";
20+
public static String STATIC_URL = "http://static.dmcloud.net";
2321

2422
protected String user_id = null;
2523
protected String api_key = null;
2624
protected String base_url = null;
2725
protected String cdn_url = null;
2826
protected String proxy = null;
2927

30-
public CloudKey_Api(String _user_id, String _api_key) throws Exception
28+
public Api(String _user_id, String _api_key) throws Exception
3129
{
32-
this(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
30+
this(_user_id, _api_key, CloudKey.API_URL, CloudKey.CDN_URL, "");
3331
}
3432

35-
public CloudKey_Api(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
33+
public Api(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
3634
{
3735
if (_user_id == null || _api_key == null)
3836
{
39-
throw new CloudKey_Exception("You must provide valid user_id and api_key parameters");
37+
throw new DCException("You must provide valid user_id and api_key parameters");
4038
}
4139
this.user_id = _user_id;
4240
this.api_key = _api_key;
@@ -53,10 +51,10 @@ public DCObject call(String call, DCObject args) throws Exception
5351
.push("call", call)
5452
.push("args", args);
5553

56-
jo.push("auth", this.user_id + ":" + CloudKey_Helpers.md5(this.user_id + CloudKey_Helpers.normalize(jo) + this.api_key));
54+
jo.push("auth", this.user_id + ":" + Helpers.md5(this.user_id + Helpers.normalize(jo) + this.api_key));
5755

5856
String json_encoded = mapper.writeValueAsString(jo);
59-
String response = CloudKey_Helpers.curl(this.base_url + "/api", json_encoded);
57+
String response = Helpers.curl(this.base_url + "/api", json_encoded);
6058
DCObject json_response = DCObject.create(mapper.readValue(response, Map.class));
6159
if (json_response.containsKey("error"))
6260
{
@@ -83,7 +81,7 @@ public DCObject call(String call, DCObject args) throws Exception
8381
default : message = "RPCException (error:" + json_response.pull("error.code").toString() + ")"; break;
8482
}
8583
message += " : " + json_response.pull("error.message").toString();
86-
throw new CloudKey_Exception(message, error_code);
84+
throw new DCException(message, error_code);
8785
}
8886
return DCObject.create((Map)json_response.get("result"));
8987
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.dmcloud;
1+
package net.dmcloud.cloudkey;
22

33
import java.io.*;
44
import java.util.Map;
@@ -13,45 +13,45 @@
1313
import org.apache.commons.httpclient.methods.multipart.Part;
1414
import org.codehaus.jackson.map.ObjectMapper;
1515

16-
public class CloudKey extends CloudKey_Api
16+
public class CloudKey extends Api
1717
{
1818
public CloudKey(String _user_id, String _api_key) throws Exception
1919
{
20-
super(_user_id, _api_key, CLOUDKEY_API_URL, CLOUDKEY_CDN_URL, "");
20+
super(_user_id, _api_key, CloudKey.API_URL, CloudKey.CDN_URL, "");
2121
}
2222

2323
public CloudKey(String _user_id, String _api_key, String _base_url, String _cdn_url, String _proxy) throws Exception
2424
{
2525
super(_user_id, _api_key, _base_url, _cdn_url, _proxy);
2626
}
2727

28-
public String mediaGetEmbedUrl(String id) throws CloudKey_Exception
28+
public String mediaGetEmbedUrl(String id) throws DCException
2929
{
30-
return this.mediaGetEmbedUrl(CLOUDKEY_API_URL, id, CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0);
30+
return this.mediaGetEmbedUrl(CloudKey.API_URL, id, CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0);
3131
}
3232

33-
public String mediaGetEmbedUrl(String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws CloudKey_Exception
33+
public String mediaGetEmbedUrl(String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
3434
{
35-
return this.mediaGetEmbedUrl(CLOUDKEY_API_URL, id, seclevel, asnum, ip, useragent, countries, referers, expires);
35+
return this.mediaGetEmbedUrl(CloudKey.API_URL, id, seclevel, asnum, ip, useragent, countries, referers, expires);
3636
}
3737

38-
public String mediaGetEmbedUrl(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws CloudKey_Exception
38+
public String mediaGetEmbedUrl(String url, String id, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
3939
{
4040
String _url = url + "/embed/" + this.user_id + "/" + id;
41-
return CloudKey_Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires);
41+
return Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires);
4242
}
4343

44-
public String mediaGetStreamUrl(String id) throws CloudKey_Exception
44+
public String mediaGetStreamUrl(String id) throws DCException
4545
{
46-
return this.mediaGetStreamUrl(CLOUDKEY_API_URL, id, "mp4_h264_aac", CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
46+
return this.mediaGetStreamUrl(CloudKey.API_URL, id, "mp4_h264_aac", CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
4747
}
4848

49-
public String mediaGetStreamUrl(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
49+
public String mediaGetStreamUrl(String id, String asset_name, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires, String extension, Boolean download) throws DCException
5050
{
51-
return this.mediaGetStreamUrl(CLOUDKEY_API_URL, id, asset_name, seclevel, asnum, ip, useragent, countries, referers, expires, extension, download);
51+
return this.mediaGetStreamUrl(CloudKey.API_URL, id, asset_name, seclevel, asnum, ip, useragent, countries, referers, expires, extension, download);
5252
}
5353

54-
public String mediaGetStreamUrl(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
54+
public String mediaGetStreamUrl(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 DCException
5555
{
5656
if (extension == "")
5757
{
@@ -60,12 +60,12 @@ public String mediaGetStreamUrl(String url, String id, String asset_name, int se
6060
}
6161
if (asset_name.length() >= 15 && asset_name.substring(0, 15) == "jpeg_thumbnail_")
6262
{
63-
return CLOUDKEY_STATIC_URL + this.user_id + "/" + id + "/" + asset_name + "." + extension;
63+
return CloudKey.STATIC_URL + this.user_id + "/" + id + "/" + asset_name + "." + extension;
6464
}
6565
else
6666
{
6767
String _url = this.cdn_url + "/route/" + this.user_id + "/" + id + "/" + asset_name + ((extension != "") ? "." + extension : "");
68-
return CloudKey_Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires) + (download ? "&throttle=0&helper=0&cache=0" : "");
68+
return Helpers.sign_url(_url, this.api_key, seclevel, asnum, ip, useragent, countries, referers, expires) + (download ? "&throttle=0&helper=0&cache=0" : "");
6969
}
7070
}
7171

@@ -126,12 +126,12 @@ public String mediaCreate(File f, DCArray assets_names, DCObject meta) throws Ex
126126
}
127127
else
128128
{
129-
throw new CloudKey_Exception("Upload failed.");
129+
throw new DCException("Upload failed.");
130130
}
131131
}
132132
catch (Exception e)
133133
{
134-
throw new CloudKey_Exception("Upload failed.");
134+
throw new DCException("Upload failed.");
135135
}
136136
finally
137137
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.dmcloud.cloudkey;
2+
3+
public class DCException extends Exception
4+
{
5+
int code = 0;
6+
7+
public DCException(String message)
8+
{
9+
super(message);
10+
}
11+
12+
public DCException(String message, int _code)
13+
{
14+
this(message);
15+
this.code = _code;
16+
}
17+
18+
public int getCode()
19+
{
20+
return this.code;
21+
}
22+
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.dmcloud;
1+
package net.dmcloud.cloudkey;
22

33
import java.io.*;
44
import java.net.*;
@@ -7,7 +7,7 @@
77
import java.util.*;
88
import java.util.zip.Deflater;
99

10-
public class CloudKey_Helpers
10+
public class Helpers
1111
{
1212
static public String curl(String targetURL, String urlParameters)
1313
{
@@ -100,12 +100,12 @@ else if (o instanceof ArrayList)
100100
return normalize;
101101
}
102102

103-
static public String sign_url(String url, String secret) throws CloudKey_Exception
103+
static public String sign_url(String url, String secret) throws DCException
104104
{
105-
return sign_url(url, secret, CloudKey.CLOUDKEY_SECLEVEL_NONE, "", "", "", null, null, 0);
105+
return sign_url(url, secret, CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0);
106106
}
107107

108-
static public String sign_url(String url, String secret, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws CloudKey_Exception
108+
static public String sign_url(String url, String secret, int seclevel, String asnum, String ip, String useragent, String[] countries, String[] referers, int expires) throws DCException
109109
{
110110
expires = (expires == 0) ? (int)(new Date().getTime() + 7200) : expires;
111111

@@ -120,45 +120,45 @@ static public String sign_url(String url, String secret, int seclevel, String as
120120

121121
String secparams = "";
122122
ArrayList<String> public_secparams = new ArrayList<String>();
123-
if ((seclevel & CloudKey.CLOUDKEY_SECLEVEL_DELEGATE) == 0)
123+
if ((seclevel & CloudKey.SECLEVEL_DELEGATE) == 0)
124124
{
125-
if ((seclevel & CloudKey.CLOUDKEY_SECLEVEL_ASNUM) > 0)
125+
if ((seclevel & CloudKey.SECLEVEL_ASNUM) > 0)
126126
{
127127
if (asnum == "")
128128
{
129-
throw new CloudKey_Exception("IP security level required and no IP address provided.");
129+
throw new DCException("IP security level required and no IP address provided.");
130130
}
131131
secparams += asnum;
132132
}
133-
if ((seclevel & CloudKey.CLOUDKEY_SECLEVEL_IP) > 0)
133+
if ((seclevel & CloudKey.SECLEVEL_IP) > 0)
134134
{
135135
if (asnum == "")
136136
{
137-
throw new CloudKey_Exception("IP security level required and no IP address provided.");
137+
throw new DCException("IP security level required and no IP address provided.");
138138
}
139139
secparams += ip;
140140
}
141-
if ((seclevel & CloudKey.CLOUDKEY_SECLEVEL_USERAGENT) > 0)
141+
if ((seclevel & CloudKey.SECLEVEL_USERAGENT) > 0)
142142
{
143143
if (asnum == "")
144144
{
145-
throw new CloudKey_Exception("USERAGENT security level required and no user-agent provided.");
145+
throw new DCException("USERAGENT security level required and no user-agent provided.");
146146
}
147147
secparams += useragent;
148148
}
149-
if ((seclevel & CloudKey.CLOUDKEY_SECLEVEL_COUNTRY) > 0)
149+
if ((seclevel & CloudKey.SECLEVEL_COUNTRY) > 0)
150150
{
151151
if (countries == null || countries.length == 0)
152152
{
153-
throw new CloudKey_Exception("COUNTRY security level required and no country list provided.");
153+
throw new DCException("COUNTRY security level required and no country list provided.");
154154
}
155155
public_secparams.add("cc=" + implode(",", countries).toLowerCase());
156156
}
157-
if ((seclevel & CloudKey.CLOUDKEY_SECLEVEL_REFERER) > 0)
157+
if ((seclevel & CloudKey.SECLEVEL_REFERER) > 0)
158158
{
159159
if (referers == null || referers.length == 0)
160160
{
161-
throw new CloudKey_Exception("REFERER security level required and no referer list provided.");
161+
throw new DCException("REFERER security level required and no referer list provided.");
162162
}
163163
try
164164
{
@@ -173,7 +173,7 @@ static public String sign_url(String url, String secret, int seclevel, String as
173173
}
174174
catch (Exception e)
175175
{
176-
throw new CloudKey_Exception(e.getMessage());
176+
throw new DCException(e.getMessage());
177177
}
178178
}
179179
}
@@ -187,7 +187,7 @@ static public String sign_url(String url, String secret, int seclevel, String as
187187
}
188188
catch (Exception e)
189189
{
190-
throw new CloudKey_Exception(e.getMessage());
190+
throw new DCException(e.getMessage());
191191
}
192192
}
193193
String rand = "";

0 commit comments

Comments
 (0)