Permalink
Browse files

Implemented detailedHistory in java client and updated unit test func…

…tionality with junit4
  • Loading branch information...
1 parent e0710fc commit 4429deb2387d9629b66e3247676bfd226a331627 Leonardo Redmond committed Sep 1, 2012
View
Binary file not shown.
View
@@ -4,5 +4,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/async-http-client-1.6.5-shaded.jar"/>
<classpathentry kind="lib" path="lib/slf4j-nop-1.6.2.jar"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
@@ -3,6 +3,7 @@
import com.ning.http.client.*;
import com.ning.http.client.AsyncHttpClientConfig.Builder;
import org.json.JSONArray;
+import org.json.JSONException;
import org.json.JSONObject;
import java.net.URLEncoder;
@@ -42,9 +43,10 @@
private String PUBLISH_KEY = "";
private String SUBSCRIBE_KEY = "";
private String SECRET_KEY = "";
- private String CIPHER_KEY = "";
+ public String CIPHER_KEY = "";
private boolean SSL = false;
private String sessionUUID = "";
+ private String parameters = "";
private class ChannelStatus {
String channel;
@@ -439,7 +441,7 @@ private void _subscribe(HashMap<String, Object> args) {
message = pc.decrypt(message);
}
if (callback != null)
- callback.subscribeCallback(channel, message);
+ if (!callback.subscribeCallback(channel, message)) return;
} else {
JSONArray arr = messages.optJSONArray(i);
@@ -451,7 +453,7 @@ private void _subscribe(HashMap<String, Object> args) {
;
}
if (callback != null)
- callback.subscribeCallback(channel, arr);
+ if(!callback.subscribeCallback(channel, arr)) return;
} else {
String msgs = messages.getString(0);
if (this.CIPHER_KEY.length() > 0) {
@@ -460,7 +462,7 @@ private void _subscribe(HashMap<String, Object> args) {
msgs = pc.decrypt(msgs);
}
if (callback != null)
- callback.subscribeCallback(channel, msgs);
+ if(!callback.subscribeCallback(channel, msgs)) return;
}
}
}
@@ -660,7 +662,7 @@ private void _presence(HashMap<String, Object> args) {
message = pc.decrypt(message);
}
if (callback != null)
- callback.presenceCallback(channel, message);
+ if(!callback.presenceCallback(channel, message)) return;
} else {
JSONArray arr = messages.optJSONArray(i);
@@ -669,10 +671,9 @@ private void _presence(HashMap<String, Object> args) {
PubnubCrypto pc = new PubnubCrypto(
this.CIPHER_KEY);
arr = pc.decryptJSONArray(arr);
- ;
}
if (callback != null)
- callback.presenceCallback(channel, arr);
+ if (!callback.presenceCallback(channel, arr)) return;
} else {
String msgs = messages.getString(0);
if (this.CIPHER_KEY.length() > 0) {
@@ -681,7 +682,7 @@ private void _presence(HashMap<String, Object> args) {
msgs = pc.decrypt(msgs);
}
if (callback != null)
- callback.presenceCallback(channel, msgs);
+ if (!callback.presenceCallback(channel, msgs)) return;
}
}
}
@@ -772,7 +773,55 @@ public JSONArray history(HashMap<String, Object> args) {
return response;
}
}
+
+ /**
+ * DetailedHistory
+ *
+ * DetailedHistory from PubNub Cloud.
+ *
+ * @return JSONArray of detailed history.
+ */
+ public JSONArray detailedHistory(String channel, long start, long end, int count, Boolean reverse) {
+ parameters = "";
+ if (count == -1) count = 100;
+ if (count == -1) count = 100;
+ parameters = "?count=" + count;
+ if (reverse)
+ parameters = parameters + "&" + "reverse=" + reverse.toString().toLowerCase();
+ if (start != -1)
+ parameters = parameters + "&" + "start=" + Long.toString(start).toLowerCase();
+ if (end != -1)
+ parameters = parameters + "&" + "end=" + Long.toString(end).toLowerCase();
+
+ List<String> url = new ArrayList<String>();
+ url.add("v2");
+ url.add("history");
+ url.add("sub-key");
+ url.add(this.SUBSCRIBE_KEY);
+ url.add("channel");
+ url.add(channel);
+
+ JSONArray response = _request(url);
+
+ if (this.CIPHER_KEY.length() > 0) {
+ PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
+ try {
+ return pc.decryptJSONArray(response.getJSONArray(0));
+ } catch (JSONException e) {
+ return response;
+ }
+ } else {
+ return response;
+ }
+ }
+
+ public JSONArray detailedHistory(String channel, long start, boolean reverse) {
+ return detailedHistory(channel, start, -1, -1, reverse);
+ }
+ public JSONArray detailedHistory(String channel, int count) {
+ return detailedHistory(channel, -1, -1, count, false);
+ }
/**
* Time
*
@@ -834,7 +883,8 @@ private JSONArray _request(List<String> url_components) {
StringBuilder url = new StringBuilder();
Iterator<String> url_iterator = url_components.iterator();
String request_for = url_components.get(0);
-
+ String request_type = url_components.get(1);
+
url.append(this.ORIGIN);
// Generate URL with UTF-8 Encoding
@@ -854,6 +904,8 @@ private JSONArray _request(List<String> url_components) {
}
if (request_for.equals("subscribe") || request_for.equals("presence"))
url.append("?uuid=").append(this.sessionUUID);
+ if (request_for.equals("v2") && request_type.equals("history"))
+ url.append(parameters);
AsyncHttpClient ahc = null;
try {
@@ -27,6 +27,9 @@ public static void main(String[] params) {
System.out.println("\nRunning here_now()");
HereNowExample();
+ System.out.println("\nRunning detailedHistory()");
+ DetailedHistoryExample();
+
System.out.println("\nRunning presence()");
PresenceExample();
@@ -39,7 +42,7 @@ private static void PublishExample() {
String publish_key = "demo";
String subscribe_key = "demo";
String secret_key = "demo";
- String cipher_key = ""; // (Cipher key is optional)
+ String cipher_key = "enigma"; // (Cipher key is optional)
String channel = "hello_world";
int publish_message_count = 1;
@@ -97,22 +100,16 @@ private static void PublishExample() {
response = pubnub.publish(args);
System.out.println(response);
-
- // Pause
- try {
- Thread.currentThread().sleep(100);
- } catch (Exception ex) {
- }
}
}
private static void HistoryExample() {
String publish_key = "demo";
String subscribe_key = "demo";
String secret_key = "demo";
- String cipher_key = ""; // (Cipher key is optional)
+ String cipher_key = "enigma"; // (Cipher key is optional)
String channel = "hello_world";
- int limit = 4;
+ int limit = 1;
Pubnub pubnub = new Pubnub(
publish_key,
@@ -150,6 +147,46 @@ private static void HistoryExample() {
}
}
+ private static void DetailedHistoryExample() {
+ String publish_key = "demo";
+ String subscribe_key = "demo";
+ String secret_key = "demo";
+ String cipher_key = "enigma"; // (Cipher key is optional)
+ String channel = "hello_world";
+ int count = 1;
+
+ Pubnub pubnub = new Pubnub(
+ publish_key,
+ subscribe_key,
+ secret_key,
+ cipher_key,
+ true
+ );
+
+ // Get History
+ JSONArray response = pubnub.detailedHistory(channel, count);
+
+ // Print Response from PubNub JSONP REST Service
+ System.out.println(response);
+
+ try {
+ if (response != null) {
+ for (int i = 0; i < response.length(); i ++) {
+ JSONObject jsono = response.optJSONObject(i);
+ if (jsono != null) {
+ @SuppressWarnings("rawtypes")
+ Iterator keys = jsono.keys();
+ while (keys.hasNext()) {
+ System.out.println(jsono.get(keys.next().toString()) + " ");
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
private static void TimestampExample() {
String publish_key = "demo";
String subscribe_key = "demo";
@@ -222,7 +259,7 @@ public void errorCallback(String channel, Object message) {
@Override
public void connectCallback(String channel) {
System.out.println("Connected to channel :" + channel);
-
+ System.out.println("Waiting for a message from publisher ...");
}
@Override
@@ -303,7 +340,7 @@ public void errorCallback(String channel, Object message) {
@Override
public void connectCallback(String channel) {
System.out.println("Connected to channel :" + channel);
-
+ System.out.println("Waiting for subscribe or unsubscribe message ...");
}
@Override
Oops, something went wrong. Retry.

0 comments on commit 4429deb

Please sign in to comment.