Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,25 @@ This Java classes can be used to communicate very easily with the Pusher REST AP

Get Started
-----------
Simply replace the Pusher specific constants in Pusher.java:
Use the PushApi as seen in ch.mbae.pusher.PushApi.

private final static String pusherApplicationId = "";

private final static String pusherApplicationKey = "";

private final static String pusherApplicationSecret = "";

Call one of the two available static methods called "triggerPush" and pass channel name, event name and the message body (JSON encoded data) as parameters:

Pusher.triggerPush("test_channel", "my_event", jsonData);

The second "triggerPush" method provides an additional parameter for the socket_id:

PushApi api = new PushImpl();

Pusher.triggerPush("test_channel", "my_event", jsonData, socketId);

That's it.
api.setGAECredentials(pusherApplicationId, pusherApplicationKey, pusherApplicationSecret);

then call the following, where all arguments are strings.

PusherResponse resp = api.pushEvent( channelName, eventName, jsonData, socketId) ;

Default values
--------------
Sometimes it can be very convenient to prepulate a PusherRequest instance with default channel and/or event name:
The implementation caches your channels, therefore when you have finished, eg a disconnect happens, then

api.disposeOfChannel(channelName);

PusherRequest p = new PusherRequest("test_channel","my_event");

To send a request to the Pusher API just call "triggerPush" on this instance:
That's it.

p.triggerPush(jsondata);

License
-------
Copyright 2010, Stephan Scheuermann. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
Copyright 2010, Stephan Scheuermann. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/ch/mbae/pusher/PushApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ch.mbae.pusher;

import java.util.Collection;

/**
* Author: wge
* Date: 09/02/2013
* Time: 18:55
*/

public interface PushApi
{
/**
* The implementation must keep these secret.
*
* @param pusherApplicationId
* @param pusherApplicationKey
* @param pusherApplicationSecret
*/
void setGAECredentials(String pusherApplicationId, String pusherApplicationKey, String pusherApplicationSecret);

/**
*
* @param channelName the particular channel
* @param eventName anything that describes your event.
* @param jsonData data to be pushed
* @param socketId
* @return A response object encapsulating all the headers from PusherApp.com
* @throws PusherTransportException
*/
PusherResponse pushEvent( String channelName, String eventName, String jsonData, String socketId) throws PusherTransportException;

/**
* Should be called when we have finished using a channel.
* For example, a disconnection has occurred.
* @param channelName
*/
void disposeOfChannel(String channelName);

/**
* Show all the channels currently being used
* @return
*/
Collection<String> listLiveChannels();
}
38 changes: 38 additions & 0 deletions src/main/java/ch/mbae/pusher/impl/CredentialHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ch.mbae.pusher.impl;/**
* Author: wge
* Date: 15/02/2013
* Time: 12:54
*/

public class CredentialHolder
{
private static final CredentialHolder instance = new CredentialHolder();

private String APPLICATION_ID;
private String APPLICATION_KEY;
private String APPLICATION_SECRET;

public static String getAPPLICATION_ID()
{
return instance.APPLICATION_ID;
}

public static String getAPPLICATION_KEY()
{
return instance.APPLICATION_KEY;
}

public static String getAPPLICATION_SECRET()
{
return instance.APPLICATION_SECRET;
}

public static synchronized void build (String application_id, String application_key, String application_secret)
{
instance.APPLICATION_ID = application_id;
instance.APPLICATION_KEY = application_key;
instance.APPLICATION_SECRET = application_secret;
}


}
68 changes: 68 additions & 0 deletions src/main/java/ch/mbae/pusher/impl/PushImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ch.mbae.pusher.impl;/**
* Author: wge
* Date: 14/02/2013
* Time: 22:17
*/

import ch.mbae.pusher.*;
import ch.mbae.pusher.transport.HttpClientPusherTransport;
import org.apache.log4j.Logger;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;

public class PushImpl implements PushApi
{
private static final Logger log = Logger.getLogger(PushImpl.class);
private ConcurrentHashMap<String,PusherChannel> channelMap = new ConcurrentHashMap<String,PusherChannel>();

@Override
public void setGAECredentials(String pusherApplicationId, String pusherApplicationKey, String pusherApplicationSecret)
{
CredentialHolder.build(pusherApplicationId,pusherApplicationKey,pusherApplicationSecret);
}

@Override
public PusherResponse pushEvent(String channelName, String eventName, String jsonData, String socketId) throws PusherTransportException
{
PusherChannel channel = findOrCreateHttpChannel(channelName);

return channel.pushEvent(eventName, jsonData, socketId);

}

@Override
public void disposeOfChannel(String channelName)
{
PusherChannel channel = channelMap.get(channelName);

if(channel != null)
{
channelMap.remove(channelName);
log.info("channel " + channelName + " successfully removed ");
}
else
{
log.warn("channel " + channelName + " not found ");
}
}

@Override
public Collection<String> listLiveChannels()
{
return channelMap.keySet();
}

private PusherChannel findOrCreateHttpChannel(String channelName)
{
PusherChannel channel = channelMap.get(channelName);
if(channel == null)
{
channel = new PusherChannel(channelName,new HttpClientPusherTransport());
channelMap.putIfAbsent(channelName,channel);
}
return channel;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
* Author: marcbaechinger
* Copyright 2011. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/
package ch.mbae.pusher;
package ch.mbae.pusher.impl;

import ch.mbae.pusher.PusherResponse;
import ch.mbae.pusher.PusherTransport;
import ch.mbae.pusher.PusherTransportException;
import ch.mbae.pusher.util.PusherUtil;

import java.net.URL;

/**
Expand All @@ -15,46 +19,37 @@ public class PusherChannel {

private PusherTransport transport;
private String channelName;
private String pusherApplicationId;
private String pusherApplicationSecret;
private String pusherApplicationKey;

public PusherChannel(String channelName, String pusherApplicationId, String pusherApplicationKey,
String pusherApplicationSecret, PusherTransport transport) {


public PusherChannel(String channelName, PusherTransport transport) {
this.channelName = channelName;
this.pusherApplicationKey = pusherApplicationKey;
this.pusherApplicationId = pusherApplicationId;
this.pusherApplicationSecret = pusherApplicationSecret;
this.transport = transport;
}

/**
* Delivers a message to the Pusher API without providing a socket_id
* @param channel
* @param event
* @param jsonData
* @return
*/
public PusherResponse pushEvent(String event, String jsonData) throws PusherTransportException{
public PusherResponse pushEvent(String event, String jsonData) throws PusherTransportException
{
return pushEvent(event, jsonData, "");
}

/**
* Delivers a message to the Pusher API
* @param channel
* @param event
* @param jsonData
* @param socketId
* @return
*/
public PusherResponse pushEvent(String event, String jsonData, String socketId) throws PusherTransportException{
//Build URI path
String uriPath = PusherUtil.buildURIPath(this.channelName, this.pusherApplicationId);
String uriPath = PusherUtil.buildURIPath(this.channelName, CredentialHolder.getAPPLICATION_ID());
//Build query
String query = PusherUtil.buildQuery(event, jsonData, socketId, this.pusherApplicationKey);
String query = PusherUtil.buildQuery(event, jsonData, socketId, CredentialHolder.getAPPLICATION_KEY());
//Generate signature
String signature = PusherUtil.buildAuthenticationSignature(uriPath, query, this.pusherApplicationSecret);
String signature = PusherUtil.buildAuthenticationSignature(uriPath, query, CredentialHolder.getAPPLICATION_SECRET());
//Build URI
URL url = PusherUtil.buildURI(uriPath, query, signature);

Expand Down
46 changes: 13 additions & 33 deletions Pusher.java → ...ava/ch/mbae/pusher/impl/PusherHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package ch.mbae.pusher.impl;

import com.google.appengine.api.urlfetch.*;
import org.apache.log4j.Logger;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
Expand All @@ -9,18 +14,6 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

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;

/**
* Static class to send messages to Pusher's REST API.
*
Expand All @@ -30,28 +23,15 @@
* @author Stephan Scheuermann
* Copyright 2010. Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/
public class Pusher {
class PusherHelper
{

private static final Logger log = Logger.getLogger(PusherHelper.class);
/**
* Pusher Host name
*/
private final static String pusherHost = "api.pusherapp.com";

/**
* Pusher Application Identifier
*/
private final static String pusherApplicationId = "";

/**
* Pusher Application Key
*/
private final static String pusherApplicationKey = "";

/**
* Pusher Secret
*/
private final static String pusherApplicationSecret = "";

/**
* Converts a byte array to a string representation
* @param data
Expand Down Expand Up @@ -92,10 +72,10 @@ private static String md5Representation(String data) {
* @param data
* @return
*/
private static String hmacsha256Representation(String data) {
public static String hmacsha256Representation(String data) {
try {
// Create the HMAC/SHA256 key from application secret
final SecretKeySpec signingKey = new SecretKeySpec( pusherApplicationSecret.getBytes(), "HmacSHA256");
final SecretKeySpec signingKey = new SecretKeySpec( CredentialHolder.getAPPLICATION_SECRET().getBytes(), "HmacSHA256");

// Create the message authentication code (MAC)
final Mac mac = Mac.getInstance("HmacSHA256");
Expand Down Expand Up @@ -128,7 +108,7 @@ private static String buildQuery(String eventName, String jsonData, String socke
StringBuffer buffer = new StringBuffer();
//Auth_Key
buffer.append("auth_key=");
buffer.append(pusherApplicationKey);
buffer.append(CredentialHolder.getAPPLICATION_KEY());
//Timestamp
buffer.append("&auth_timestamp=");
buffer.append(System.currentTimeMillis() / 1000);
Expand Down Expand Up @@ -157,7 +137,7 @@ private static String buildURIPath(String channelName){
StringBuffer buffer = new StringBuffer();
//Application ID
buffer.append("/apps/");
buffer.append(pusherApplicationId);
buffer.append(CredentialHolder.getAPPLICATION_ID());
//Channel name
buffer.append("/channels/");
buffer.append(channelName);
Expand Down Expand Up @@ -256,7 +236,7 @@ public static HTTPResponse triggerPush(String channel, String event, String json
return urlFetchService.fetch(request);
} catch (IOException e) {
//Log warning
Log.warn("Pusher request could not be send to the following URI " + url.toString());
log.warn("Pusher request could not be send to the following URI " + url.toString());
return null;
}
}
Expand Down
Loading