Skip to content

Instantly share code, notes, and snippets.

@shin1ogawa
Created July 22, 2010 16:15
package com.shin1ogawa.controller;
import java.io.IOException;
import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.StringUtil;
import com.shin1ogawa.model.BuzzActivity;
public class SubscriberController extends Controller {
static final Logger logger = Logger.getLogger(SubscriberController.class.getName());
@Override
protected Navigation run() throws Exception {
if (isPost()) {
return doPost();
}
return doGet();
}
private Navigation doGet() throws IOException {
String hubMode = asString("hub.mode");
String hubChallenge = asString("hub.challenge");
if (StringUtil.isEmpty(hubMode) || StringUtil.isEmpty(hubChallenge)) {
response.setStatus(400);
return null;
}
String hubTopic = asString("hub.topic");
if (hubMode.equals("subscribe")) {
logger.info("subscribe: hub.challenge=" + hubChallenge
+ ", hub.topic=" + hubTopic);
} else if (hubMode.equals("unsubscribe")) {
logger.info("unsubscribe: hub.challenge=" + hubChallenge
+ ", hub.topic=" + hubTopic);
} else {
response.setStatus(400);
return null;
}
response.setStatus(200);
response.setContentType("text/plain");
response.getWriter().print(hubChallenge);
response.flushBuffer();
return null;
}
private Navigation doPost() throws IOException {
response.setStatus(204);
byte[] buffer = new byte[10 * 1024];
request.getInputStream().read(buffer);
BuzzActivity entity = new BuzzActivity();
entity.setBody(new String(buffer, "utf-8"));
Datastore.put(entity);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment