Skip to content

Instantly share code, notes, and snippets.

@TomTasche
Created September 12, 2011 20:57
Show Gist options
  • Save TomTasche/1212398 to your computer and use it in GitHub Desktop.
Save TomTasche/1212398 to your computer and use it in GitHub Desktop.
SuperfeedrServlet for accessing private (aka authenticated) feeds
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
@SuppressWarnings("serial")
public class SuperfeedrServlet extends HttpServlet {
static URI hubbubUri;
public static URI getHubbubUri() throws URISyntaxException {
if (hubbubUri == null) hubbubUri = new URI("http://superfeedr.com/hubbub");
return hubbubUri;
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("superfeedr calls via GET");
// the credentials you're using to sign into superfeedr.
UsernamePasswordCredentials credentials; // new UsernamePasswordCredentials("USERNAME", "PASSWORD");
// the URL superfeedr should poll every now and then. This URL should point to a servlet you're hosting, which fetches and returns a private feed / website.
String feedUrl; // "http://tomtasche.at/gmail/feed"
// the URL superfeedr should call when a new "notification" is ready for you.
String yourUrl; // "http://tomtasche.at/superfeedr/"
printRequest(request, response);
String challenge = request.getParameter("hub.challenge");
if (challenge != null) {
response.getWriter().println(challenge);
response.setStatus(HttpServletResponse.SC_OK);
} else {
try {
subscribeToFeed(feedUrl, yourUrl, credentials);
} catch (URISyntaxException e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("superfeedr calls via POST");
printRequest(request, response);
// parse the superfeedr notification...
}
private static void subscribeToFeed(String topic, String callback, UsernamePasswordCredentials credentials) throws IOException, URISyntaxException {
DefaultHttpClient client = Http.getHttpClient();
CredentialsProvider credentialProvider = client.getCredentialsProvider();
AuthScope scope = new AuthScope("superfeedr.com", AuthScope.ANY_PORT);
if (credentialProvider.getCredentials(scope) == null) {
credentialProvider.setCredentials(scope, credentials);
}
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("hub.mode", "subscribe"));
formparams.add(new BasicNameValuePair("hub.callback", callback));
formparams.add(new BasicNameValuePair("hub.topic", topic));
formparams.add(new BasicNameValuePair("hub.verify", "async"));
formparams.add(new BasicNameValuePair("superfeedr.private", "true"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost post = new HttpPost(getHubbubUri());
// comment this line out if you want to receive XML notifications.
// P.S. XML is evil.
post.addHeader("Accept", "application/json");
post.setEntity(entity);
client.execute(post);
}
private static void printRequest(HttpServletRequest request) throws IOException {
System.out.println("---");
Map<String, Object> parameters = request.getParameterMap();
for (String s : parameters.keySet()) {
String line = s + " => " + request.getParameter(s);
System.out.println(line);
}
System.out.println("---");
InputStreamReader reader = new InputStreamReader(request.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
for (String s = bufferedReader.readLine(); s != null; s = bufferedReader.readLine()) {
builder.append(s);
}
System.out.println(builder.toString());
System.out.println("---");
}
}
@julien51
Copy link

Beware, the endpoint at http://superfeedr.com/hubbub as been deprecated in favor of https://push.superfeedr.com!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment