PutPolicy.java 2.03 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
package com.lyms.cm.qiniu;

import org.json.JSONException;
import org.json.JSONStringer;

/**
* The PutPolicy class used to generate a upload token. To upload a file, you
* should obtain upload authorization from Qiniu cloud strage platform. By a
* pair of valid accesskey and secretkey, we generate a upload token. When
* upload a file, the upload token is transmissed as a part of the file stream,
* or as an accessory part of the HTTP Headers.
*/

public class PutPolicy {
/** 必选。可以是 bucketName 或者 bucketName:key */
public String scope;
/** 可选 */
public String callbackUrl;
/** 可选 */
public String callbackBody;
/** 可选 */
public String returnUrl;
/** 可选 */
public String returnBody;
/** 可选 */
public String asyncOps;
/** 可选 */
public String endUser;
/** 可选 */
public long expires;

public PutPolicy(String scope) {
this.scope = scope;
}

private String marshal() throws JSONException {
JSONStringer stringer = new JSONStringer();
stringer.object();
stringer.key("scope").value(this.scope);
if (this.callbackUrl != null && this.callbackUrl.length() > 0) {
stringer.key("callbackUrl").value(this.callbackUrl);
}
if (this.returnUrl != null && this.returnUrl.length() > 0) {
stringer.key("returnUrl").value(this.returnUrl);
}
if (this.asyncOps != null && this.asyncOps.length() > 0) {
stringer.key("asyncOps").value(this.asyncOps);
}

if (this.returnBody != null && this.returnBody.length() > 0) {
stringer.key("returnBody").value(this.returnBody);
}

stringer.key("deadline").value(this.expires);
stringer.endObject();

return stringer.toString();
}

/**
* makes an upload token.
* @param mac
* @return
* @throws AuthException
* @throws JSONException
*/
public String token(Mac mac) throws AuthException, JSONException {
if (this.expires == 0) {
this.expires = 3600; // 3600s, default.
}
this.expires = System.currentTimeMillis() / 1000 + expires;
byte[] data = this.marshal().getBytes();
return DigestAuth.signWithData(mac, data);
}

}