Skip to content

Commit e726886

Browse files
committed
FAToken and use base64 from apache common codec
1 parent b438161 commit e726886

File tree

8 files changed

+244
-38
lines changed

8 files changed

+244
-38
lines changed

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<target name="compile" description="Compiles CloudKey" depends="init">
1515
<javac srcdir="${src}" destdir="${build}">
1616
<classpath>
17-
<path location="${thirdparty}/commons-codec-1.4.jar"/>
17+
<path location="${thirdparty}/commons-codec-1.6.jar"/>
1818
<path location="${thirdparty}/commons-httpclient-3.1.jar"/>
1919
<path location="${thirdparty}/commons-logging-1.1.1.jar"/>
2020
<path location="${thirdparty}/jackson-core-asl-1.8.1.jar"/>

cloudkey-1.0.jar

1.28 KB
Binary file not shown.

examples/Example.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import net.dmcloud.cloudkey.*;
2+
import net.dmcloud.util.*;
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
import java.util.ArrayList;
6+
7+
public class Example {
8+
9+
private static String user_id = "4c1a4d3edede832bfd000002";
10+
private static String api_key = "52a32c7890338770e3ea1601214c02142d297298";
11+
private static String base_url = "http://sebest.api.dev.int.dmcloud.net";
12+
13+
14+
public static void mediaList() {
15+
try {
16+
CloudKey cloud = new CloudKey(user_id, api_key, base_url, CloudKey.CDN_URL, "");
17+
DCObject result = cloud.call
18+
(
19+
"media.list",
20+
DCObject.create()
21+
.push("fields", DCArray.create()
22+
.push("id")
23+
.push("meta.title")
24+
.push("assets.jpeg_thumbnail_auto.stream_url")
25+
.push("assets.mp4_h264_aac.video_width")
26+
.push("assets.mp4_h264_aac.video_height")
27+
.push("assets.source.download_url")
28+
)
29+
);
30+
31+
DCArray list = DCArray.create((ArrayList)result.get("list"));
32+
for(int i=0; i<list.size(); i++)
33+
{
34+
DCObject item = DCObject.create((Map)list.get(i));
35+
System.out.println("-----------------------------------");
36+
System.out.println("Title : " + item.pull("meta.title"));
37+
System.out.println("Thumbnail URL: " + item.pull("assets.jpeg_thumbnail_auto.stream_url"));
38+
String embed_url = cloud.mediaGetEmbedUrl(item.get("id").toString(), CloudKey.SECLEVEL_DELEGATE | CloudKey.SECLEVEL_ASNUM | CloudKey.SECLEVEL_USERAGENT, "", "", "", null, null, 0);
39+
System.out.println("Embed " + item.pull("assets.mp4_h264_aac.video_width") + "x" + item.pull("assets.mp4_h264_aac.video_height") + " :" + embed_url);
40+
String stream_url = cloud.mediaGetStreamUrl(item.get("id").toString(), "mp4_h264_aac", CloudKey.SECLEVEL_NONE, "", "", "", null, null, 0, "", false);
41+
String dl_url = item.pull("assets.source.download_url");
42+
System.out.println("Download source :" + dl_url);
43+
System.out.println("Stream url :" + stream_url);
44+
}
45+
} catch(Exception e) {
46+
System.out.println("Error : " + e.getMessage());
47+
}
48+
}
49+
50+
public static void testToken() {
51+
FAToken token1 = new FAToken(user_id, "111111111111111111111111", api_key, 1338947483);
52+
token1.setCallback("http://www.dmcloud.com/fa2");
53+
token1.setData("firstname", "john");
54+
token1.setData("lastname", "doe");
55+
token1.setRight("start_date", "1327946483");
56+
token1.setRight("end_date", "1338947483");
57+
token1.setRight("playback_window", "600");
58+
System.out.println("user id " + token1.getUserId());
59+
System.out.println("Expired? " + token1.isExpired());
60+
System.out.println(token1.getUserId());
61+
try {
62+
System.out.println(token1.toJSON());
63+
System.out.println(token1.toBase64());
64+
FAToken token2 = new FAToken(token1.toJSON(), api_key);
65+
System.out.println("2 " + token2.getUserId());
66+
FAToken token3 = new FAToken(token1.toBase64(), api_key);
67+
System.out.println("3 " + token3.getUserId());
68+
token3.setExpires(10);
69+
System.out.println("Expired? " + token3.isExpired());
70+
HashMap<String,String> map = token3.getRights();
71+
if (map != null ) {
72+
for (Map.Entry<String,String> entry : map.entrySet()) {
73+
System.out.println("Right " + entry.getKey() + " -> " + entry.getValue());
74+
}
75+
}
76+
} catch(Exception e) {
77+
System.out.println("oups");
78+
System.out.println(e);
79+
}
80+
}
81+
82+
public static void main(String[] args) {
83+
//mediaList();
84+
testToken();
85+
}
86+
87+
}

examples/compile.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
javac -cp ../cloudkey-1.0.jar Example.java
2+
java -cp ../cloudkey-1.0.jar:.:../third-party/commons-httpclient-3.1.jar:../third-party/commons-codec-1.6.jar:../third-party/jackson-core-asl-1.8.1.jar:../third-party/jackson-mapper-asl-1.8.1.jar Example

src/net/dmcloud/cloudkey/Helpers.java

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.security.NoSuchAlgorithmException;
77
import java.util.*;
88
import java.util.zip.Deflater;
9+
import org.apache.commons.codec.binary.Base64;
910

1011
public class Helpers
1112
{
@@ -183,7 +184,7 @@ static public String sign_url(String url, String secret, int seclevel, String as
183184
{
184185
try
185186
{
186-
public_secparams_encoded = Base64.encode(gzcompress(implode("&", public_secparams)));
187+
public_secparams_encoded = Base64.encodeBase64String(gzcompress(implode("&", public_secparams)));
187188
}
188189
catch (Exception e)
189190
{
@@ -281,39 +282,3 @@ private static byte[] gzcompress(String inputString) throws Exception
281282
}
282283
}
283284

284-
class Base64
285-
{
286-
private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/";
287-
288-
public static byte[] zeroPad(int length, byte[] bytes)
289-
{
290-
byte[] padded = new byte[length]; // initialized to zero by JVM
291-
System.arraycopy(bytes, 0, padded, 0, bytes.length);
292-
return padded;
293-
}
294-
295-
public static String encode(byte[] stringArray)
296-
{
297-
String encoded = "";
298-
299-
// determine how many padding bytes to add to the output
300-
int paddingCount = (3 - (stringArray.length % 3)) % 3;
301-
// add any necessary padding to the input
302-
stringArray = zeroPad(stringArray.length + paddingCount, stringArray);
303-
// process 3 bytes at a time, churning out 4 output bytes
304-
// worry about CRLF insertions later
305-
for (int i = 0; i < stringArray.length; i += 3)
306-
{
307-
int j = ((stringArray[i] & 0xff) << 16) +
308-
((stringArray[i + 1] & 0xff) << 8) +
309-
(stringArray[i + 2] & 0xff);
310-
encoded = encoded + base64code.charAt((j >> 18) & 0x3f) +
311-
base64code.charAt((j >> 12) & 0x3f) +
312-
base64code.charAt((j >> 6) & 0x3f) +
313-
base64code.charAt(j & 0x3f);
314-
}
315-
// replace encoded padding nulls with "="
316-
return encoded.substring(0, encoded.length() -
317-
paddingCount) + "==".substring(0, paddingCount);
318-
}
319-
}

src/net/dmcloud/util/FAToken.java

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package net.dmcloud.util;
2+
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
import java.util.Date;
6+
import java.io.IOException;
7+
import net.dmcloud.util.DCObject;
8+
import net.dmcloud.cloudkey.Helpers;
9+
import org.codehaus.jackson.map.ObjectMapper;
10+
import org.apache.commons.codec.binary.Base64;
11+
12+
public class FAToken {
13+
14+
private String user_id = null;
15+
private String media_id = null;
16+
private String api_key = null;
17+
private HashMap<String,String> rights;
18+
private HashMap<String,String> data;
19+
private String callback = null;
20+
private Integer expires = 0;
21+
22+
public static int DEFAULT_TIMEOUT = 600;
23+
24+
public FAToken(String token, String api_key) throws Exception {
25+
this(token, api_key, true);
26+
}
27+
28+
public FAToken(String token, String api_key, Boolean check_auth) throws Exception {
29+
ObjectMapper mapper;
30+
DCObject token_data;
31+
String auth;
32+
33+
this.api_key = api_key;
34+
35+
if (Base64.isBase64(token)) {
36+
token = new String(Base64.decodeBase64(token));
37+
}
38+
39+
mapper = new ObjectMapper();
40+
token_data = DCObject.create(mapper.readValue(token, Map.class));
41+
user_id = token_data.get("user_id").toString();
42+
media_id = token_data.get("media_id").toString();
43+
expires = Integer.parseInt(token_data.get("expires").toString());
44+
if (token_data.containsKey("rights")) {
45+
rights = (HashMap<String,String>)token_data.get("rights");
46+
}
47+
if (token_data.containsKey("data")) {
48+
data = (HashMap<String,String>)token_data.get("data");
49+
}
50+
if (token_data.containsKey("callback")) {
51+
callback = token_data.get("callback").toString();
52+
}
53+
54+
if (check_auth == true) {
55+
auth = token_data.get("auth").toString();
56+
57+
token_data.remove("auth");
58+
if (!getAuth(token_data).equals(auth)) {
59+
throw new Exception("Auth doesn't match");
60+
}
61+
}
62+
}
63+
64+
public FAToken(String user_id, String media_id, String api_key) {
65+
this(user_id, media_id, api_key, (int)(new Date().getTime() / 1000) + DEFAULT_TIMEOUT);
66+
}
67+
68+
public FAToken(String user_id, String media_id, String api_key, Integer expires) {
69+
this.user_id = user_id;
70+
this.media_id = media_id;
71+
this.api_key = api_key;
72+
this.rights = new HashMap<String,String>();
73+
this.data = new HashMap<String,String>();
74+
this.expires = expires;
75+
}
76+
77+
public Boolean isExpired() {
78+
Date expireDate = new Date(Long.valueOf(expires) * 1000);
79+
return expireDate.before(new Date());
80+
}
81+
82+
public void setExpires(Integer expires) {
83+
this.expires = expires;
84+
}
85+
86+
public Integer getExpires() {
87+
return this.expires;
88+
}
89+
90+
public String getUserId() {
91+
return user_id;
92+
}
93+
94+
public String getMediaId() {
95+
return media_id;
96+
}
97+
98+
public void setRight(String name, String value) {
99+
rights.put(name, value);
100+
}
101+
102+
public HashMap<String,String> getRights() {
103+
return rights;
104+
}
105+
106+
public void setData(String name, String value) {
107+
data.put(name, value);
108+
}
109+
110+
public HashMap<String,String> getData() {
111+
return data;
112+
}
113+
114+
public void setCallback(String callback) {
115+
this.callback = callback;
116+
}
117+
118+
public String getCallback() {
119+
return this.callback;
120+
}
121+
122+
public String getAuth(DCObject token_data) {
123+
return Helpers.md5(Helpers.normalize(token_data) + api_key);
124+
}
125+
126+
public String toJSON() throws Exception {
127+
ObjectMapper mapper;
128+
DCObject token_data;
129+
130+
token_data = DCObject.create()
131+
.push("user_id", user_id)
132+
.push("media_id", media_id)
133+
.push("expires", expires);
134+
if (callback != null) {
135+
token_data.push("callback", callback);
136+
}
137+
if (!this.rights.isEmpty()) {
138+
token_data.push("rights", rights);
139+
}
140+
if (!this.data.isEmpty()) {
141+
token_data.push("data", data);
142+
}
143+
mapper = new ObjectMapper();
144+
token_data.push("auth", getAuth(token_data));
145+
return mapper.writeValueAsString(token_data);
146+
}
147+
148+
public String toBase64() throws Exception {
149+
return Base64.encodeBase64String(toJSON().getBytes());
150+
}
151+
152+
}

third-party/commons-codec-1.4.jar

-56.8 KB
Binary file not shown.

third-party/commons-codec-1.6.jar

227 KB
Binary file not shown.

0 commit comments

Comments
 (0)