import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
public class SeVuService {
private static final Log log = LogFactory.getLog(SeVuService.class);
private static final String SE_VU_URL = "https://api.scaleengine.net/0.8/";
private static final String API_KEY = "your-key-here";
private static final String API_SECRET = "your-secret-here";
private static final String PASS = "your-pass-here";
private static final String APPLICATION_NAME = "app-name";
private static final Integer CDN = 1;//your cdn id here
public JSONObject viewerCounts(String url) throws JSONException, IOException {
final LinkedHashMap paramMap = getBaseMap("vsn.viewercount");
paramMap.put("app", APPLICATION_NAME);
paramMap.put("url", url);
return createSignatureAndPost(paramMap);
}
//expire time should be 150-250% the length of the video
//number of uses probably 10-20
public JSONObject createTicket(String remoteAddress, String video, int secondsToExpire)
throws JSONException, IOException {
final LinkedHashMap paramMap = getBaseMap("sevu.request");
paramMap.put("pass", PASS);
paramMap.put("ip", remoteAddress);
paramMap.put("video", video);
paramMap.put("app", APPLICATION_NAME);
paramMap.put("uses", 1);
paramMap.put("expire", "+" + secondsToExpire + " seconds");
return createSignatureAndPost(paramMap);
}
public JSONObject searchTickets(String video) throws JSONException, IOException {
final LinkedHashMap paramMap = getBaseMap("sevu.search");
paramMap.put("video", video);
return createSignatureAndPost(paramMap);
}
public JSONObject ticketStatus(String ticketKey) throws JSONException, IOException {
final LinkedHashMap paramMap = getBaseMap("sevu.status");
paramMap.put("key", ticketKey);
return createSignatureAndPost(paramMap);
}
public JSONObject revokeTicket(String ticketKey) throws JSONException, IOException {
final LinkedHashMap paramMap = getBaseMap("sevu.revoke");
paramMap.put("key", ticketKey);
return createSignatureAndPost(paramMap);
}
public JSONObject errorLog(String ticketKey) throws JSONException, IOException {
final LinkedHashMap paramMap = getBaseMap("sevu.errorlog");
paramMap.put("key", ticketKey);
//paramMap.put("pass", PASS);
//paramMap.put("ip", remoteAddress);
//paramMap.put("video", video);
//paramMap.put("app", APPLICATION_NAME);
//paramMap.put("success", 1);
//paramMap.put("seek", 0);
//paramMap.put("limit", 100);
return createSignatureAndPost(paramMap);
}
private JSONObject createSignatureAndPost(final LinkedHashMap paramMap)
throws JSONException, IOException {
paramMap.put("timestamp", new Date().getTime()/1000);
String json = new JSONObject(paramMap).toString();
//php encodes slashes in a non-standard way such that it needs
//to be added AFTER json encoding
json = json.replace("/", "/");
paramMap.put("signature", generateSignature(json));
final JSONObject responseJsonObject = postMethod(new JSONObject(paramMap));
return responseJsonObject;
}
private LinkedHashMap getBaseMap(final String command) {
final LinkedHashMap paramMap = new LinkedHashMap();
paramMap.put("api_key", API_KEY);
paramMap.put("cdn", CDN);
paramMap.put("command", command);
return paramMap;
}
private String generateSignature(String requestData) {
String signature = null;
try {
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret = new SecretKeySpec(API_SECRET.getBytes(), "HmacSHA256");
mac.init(secret);
signature = new String(Base64.encodeBase64(mac.doFinal(requestData.getBytes())));
} catch (Exception e) {
System.out.println(e.getMessage());
}
return signature;
}
private JSONObject postMethod(JSONObject jsonObject) throws JSONException, IOException {
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(SE_VU_URL);
final String json = jsonObject.toString().replace("/", "/");
log.trace(json);
postMethod.addParameter(new NameValuePair("json", json));
httpClient.executeMethod(postMethod);
String response = postMethod.getResponseBodyAsString();
log.info("response from postMethod: " + response);
return new JSONObject(new JSONTokener(new InputStreamReader(postMethod.getResponseBodyAsStream())));
}
}
{
"code": 2002,
"message": "ScaleEngine Virtual Usher Ticket Granted",
"status": "success",
"ticket": {
"key": "mystream.53ffc996dd39d5.39102367",
"pass": "g63clVAI5wFPxY9vwVOJvw6L",
"ip": "0.0.0.0/0",
"video": "mystream",
"app": "myapp-sevu",
"created_date": "2014-08-29 00:30:14",
"used_date": "0000-00-00 00:00:00",
"uses": "5",
"active": "1"
},
"handle_time":"0.3795 seconds"
}