Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.tectonica.util;
  
  import java.net.URL;
 
Provides REST-based implementation of PubNub's PUBLISH API, as an alternative for using PubNub's Java SDK, which relies on an internal HTTP-requests queue, that may not play well with containers such as JavaEE and GAE. The REST API itself is invoked synchronously by sendText(java.lang.String,java.lang.String) and sendJson(java.lang.String,java.lang.String), so avoid using them in the main thread due to network latency.

While not a requirement, this class is intended to be used as a Singleton, and it is thread-safe by design.

See also:
http://www.pubnub.com/http-rest-push-api/
 
 public class PubnubRestPublisher
 {
 	private static final Logger LOG = LoggerFactory.getLogger(PubnubRestPublisher.class);
 
 	public static interface Listener
 	{
 		void onSuccess(String channelString msg);
 
 		void onError(String channelString msg);
 	}
 
 	private Listener listener = new Listener()
 	{
 		public void onSuccess(String channelString msg)
 		{
 			.info(msg);
 		}
 
 		public void onError(String channelString msg)
 		{
 			.error(msg);
 		}
 	};
 
 	public Listener getListener()
 	{
 		return ;
 	}
 
 	public void setListener(Listener listener)
 	{
 		this. = listener;
 	}
 
 	// //////////////////////////////////////////////////////////////////////////////
 
 	private final String urlFmt;
 
 	public PubnubRestPublisher(String publishKeyString subscribeKey)
 	{
 		 = "http://pubsub.pubnub.com/publish/" + publishKey + "/" + subscribeKey + "/0/%s/0/%s";
 	}
 
 	public void sendText(String channelString txtMsg)
 	{
 		sendJson(channelstringAsJson(txtMsg));
 	}
 
 	public void sendJson(String channelString jsonMsg)
 	{
 		String url = String.format(urlEncoded(channel), urlEncoded(jsonMsg));
 		HttpURLConnection conn = null;
 		try
 		{
 			conn = (HttpURLConnection) (new URL(url)).openConnection();
 			conn.setRequestMethod("GET");
 			conn.setUseCaches(false);
 			conn.setDoInput(true);
 			conn.setDoOutput(false);
 //			Map<String, List<String>> responseHeaders = conn.getHeaderFields();
 			int statusCode = conn.getResponseCode();
 			boolean ok = statusCode / 100 == 2;
 			InputStream is = ok ? conn.getInputStream() : conn.getErrorStream();
 			String content = (is == null) ? "" : streamToString(is);
 			if (ok)
 			{
 				if ( != null)
 					.onSuccess(channelcontent);
 			}
 			else
 			{
 				if ( != null)
 					.onError(channelcontent);
			}
		}
		catch (Exception e)
		{
			if ( != null)
				.onError(channele.toString());
		}
		finally
		{
			if (conn != null)
				conn.disconnect();
		}
	}
	private final Pattern p1 = Pattern.compile("\\\\");
	private final Pattern p2 = Pattern.compile("\"");

Converts a Java string into a valid JSON string literal (by escaping quotation marks and backslashes, and then surrounding with quotes)

NOTE: to be completely accurate, escaping is also required for tabs, newlines, backspaces, etc., but these aren't normally expected in a published text

Parameters:
text the raw string for conversion
Returns:
JSON-compliant string literal
	protected String stringAsJson(String text)
	{
		text = .matcher(text).replaceAll("\\\\\\\\"); // first escape backslashes
		text = .matcher(text).replaceAll("\\\\\\\""); // escape quotation marks
		return "\"" + text + "\"";
	}
	{
		StringBuffer sb = new StringBuffer();
		String line;
		while ((line = in.readLine()) != null)
			sb.append(line).append("\n");
		in.close();
		return (sb.length() == 0) ? "" : sb.substring(0, sb.length() - "\n".length());
	}
	private String urlEncoded(String url)
	{
		try
		{
			return URLEncoder.encode(url"UTF-8").replace("+""%20");
		}
		{
			return null// never happens as "UTF-8" is always a valid encoding
		}
	}
New to GrepCode? Check out our FAQ X