Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

removed dependency from google app engine, uses http-commons now

  • Loading branch information...
commit 25b6d07ce15c03de37a263ef96caf0cfd7f2aa12 1 parent 84a1927
Julio Capote capotej authored
Showing with 51 additions and 32 deletions.
  1. +36 −27 Pusher.java
  2. +15 −5 PusherRequest.java
63 Pusher.java
View
@@ -1,3 +1,5 @@
+package org.nuklei.main;
+
import java.io.IOException;
@@ -12,14 +14,20 @@
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
-import org.mortbay.log.Log;
-import com.google.appengine.api.urlfetch.HTTPHeader;
-import com.google.appengine.api.urlfetch.HTTPMethod;
-import com.google.appengine.api.urlfetch.HTTPRequest;
-import com.google.appengine.api.urlfetch.HTTPResponse;
-import com.google.appengine.api.urlfetch.URLFetchService;
-import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
+import org.apache.http.Header;
+import org.apache.http.auth.AuthenticationException;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.auth.BasicScheme;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
/**
* Static class to send messages to Pusher's REST API.
@@ -194,7 +202,7 @@ private static String buildAuthenticationSignature(String uriPath, String query)
* @param signature
* @return
*/
- private static URL buildURI(String uriPath, String query, String signature){
+ private static String buildURI(String uriPath, String query, String signature){
StringBuffer buffer = new StringBuffer();
//Protocol
buffer.append("http://");
@@ -208,12 +216,7 @@ private static URL buildURI(String uriPath, String query, String signature){
//Authentication signature
buffer.append("&auth_signature=");
buffer.append(signature);
- //Build URI
- try {
- return new URL(buffer.toString());
- } catch (MalformedURLException e) {
- throw new RuntimeException("Malformed URI");
- }
+ return buffer.toString();
}
/**
@@ -222,8 +225,10 @@ private static URL buildURI(String uriPath, String query, String signature){
* @param event
* @param jsonData
* @return
+ * @throws IOException
+ * @throws ClientProtocolException
*/
- public static HTTPResponse triggerPush(String channel, String event, String jsonData){
+ public static String triggerPush(String channel, String event, String jsonData) throws ClientProtocolException, IOException{
return triggerPush(channel, event, jsonData, "");
}
@@ -234,8 +239,10 @@ public static HTTPResponse triggerPush(String channel, String event, String json
* @param jsonData
* @param socketId
* @return
+ * @throws IOException
+ * @throws ClientProtocolException
*/
- public static HTTPResponse triggerPush(String channel, String event, String jsonData, String socketId){
+ public static String triggerPush(String channel, String event, String jsonData, String socketId) throws ClientProtocolException, IOException{
//Build URI path
String uriPath = buildURIPath(channel);
//Build query
@@ -243,22 +250,24 @@ public static HTTPResponse triggerPush(String channel, String event, String json
//Generate signature
String signature = buildAuthenticationSignature(uriPath, query);
//Build URI
- URL url = buildURI(uriPath, query, signature);
-
- //Create Google APP Engine Fetch URL service and request
- URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
- HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
- request.addHeader(new HTTPHeader("Content-Type", "application/json"));
- request.setPayload(jsonData.getBytes());
-
+ String url = buildURI(uriPath, query, signature);
+
+
+ DefaultHttpClient httpClient = new DefaultHttpClient();
+ HttpContext cntxt = new BasicHttpContext();
+
+ HttpPost httpPost = new HttpPost(url);
+ httpPost.addHeader("Content-Type", "application/json");
+ httpPost.setEntity(new StringEntity(jsonData));
+ org.apache.http.HttpResponse httpResponse = httpClient.execute(httpPost);
+
//Start request
try {
- return urlFetchService.fetch(request);
+ return EntityUtils.toString(httpResponse.getEntity());
} catch (IOException e) {
//Log warning
- Log.warn("Pusher request could not be send to the following URI " + url.toString());
return null;
}
}
-}
+}
20 PusherRequest.java
View
@@ -1,6 +1,10 @@
+package org.nuklei.main;
-import com.google.appengine.api.urlfetch.HTTPResponse;
+import java.io.IOException;
+
+import org.apache.http.client.ClientProtocolException;
+
/**
* An instance of this class can be used to prepopulate properties (event or channel name) with default values
@@ -44,8 +48,10 @@ public PusherRequest(String channelName, String eventName) {
* Triggers a new push with default channelName, eventName properties
* @param jsonData
* @return
+ * @throws IOException
+ * @throws ClientProtocolException
*/
- public HTTPResponse triggerPush(String jsonData){
+ public String triggerPush(String jsonData) throws ClientProtocolException, IOException{
return Pusher.triggerPush(channelName, eventName, jsonData);
}
@@ -54,8 +60,10 @@ public HTTPResponse triggerPush(String jsonData){
* @param jsonData
* @param eventName
* @return
+ * @throws IOException
+ * @throws ClientProtocolException
*/
- public HTTPResponse triggerPush(String jsonData, String eventName){
+ public String triggerPush(String jsonData, String eventName) throws ClientProtocolException, IOException{
return Pusher.triggerPush(channelName, eventName, jsonData);
}
@@ -65,8 +73,10 @@ public HTTPResponse triggerPush(String jsonData, String eventName){
* @param eventName
* @param socketId
* @return
+ * @throws IOException
+ * @throws ClientProtocolException
*/
- public HTTPResponse triggerPush(String jsonData, String eventName, String socketId){
+ public String triggerPush(String jsonData, String eventName, String socketId) throws ClientProtocolException, IOException{
return Pusher.triggerPush(channelName, eventName, jsonData, socketId);
}
@@ -86,4 +96,4 @@ public String getChannelName() {
return channelName;
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.