Skip to content

Instantly share code, notes, and snippets.

@Narigo
Created August 18, 2012 12:13
Show Gist options
  • Save Narigo/3386443 to your computer and use it in GitHub Desktop.
Save Narigo/3386443 to your computer and use it in GitHub Desktop.
Vert.x Session Manager Helper class
package com.campudus.gorilla;
import java.util.Set;
import org.jboss.netty.handler.codec.http.Cookie;
import org.jboss.netty.handler.codec.http.CookieDecoder;
import org.jboss.netty.handler.codec.http.CookieEncoder;
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;
public class SessionHelper {
private static String sessionManagerAddress = "session";
public static void withSessionData(final EventBus eb, final HttpServerRequest req,
final String requiredField, final Handler<JsonObject> handler) {
withSessionData(eb, req, new JsonArray().addString(requiredField), handler);
}
public static void withSessionData(final EventBus eb, final HttpServerRequest req,
final JsonArray requiredFields, final Handler<JsonObject> handler) {
withSessionId(eb, req, new Handler<String>() {
@Override
public void handle(String sessionId) {
withSessionData(eb, sessionId, requiredFields, handler);
}
});
}
public static void withSessionData(EventBus eb, String sessionId, String requiredField,
final Handler<JsonObject> handler) {
withSessionData(eb, sessionId, new JsonArray().addString(requiredField), handler);
}
public static void withSessionData(EventBus eb, String sessionId, JsonArray requiredFields,
final Handler<JsonObject> handler) {
final JsonObject json = new JsonObject().putString("action", "get")
.putString("sessionId", sessionId).putArray("fields", requiredFields);
eb.send(sessionManagerAddress, json, new Handler<Message<JsonObject>>() {
public void handle(Message<JsonObject> event) {
System.out.println("sessiondata of " + json.encode() + ": " + event.body.encode());
if (event.body.getField("error") == null) {
handler.handle(event.body.getObject("data"));
}
};
});
}
public static void putSessionData(EventBus eb, HttpServerRequest req, JsonObject obj) {
sendPutData(eb, req, obj, null);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, JsonArray obj) {
sendPutData(eb, req, new JsonObject().putArray(key, obj), null);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, byte[] obj) {
sendPutData(eb, req, new JsonObject().putBinary(key, obj), null);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, boolean obj) {
sendPutData(eb, req, new JsonObject().putBoolean(key, obj), null);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, Number obj) {
sendPutData(eb, req, new JsonObject().putNumber(key, obj), null);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, JsonObject obj) {
sendPutData(eb, req, new JsonObject().putObject(key, obj), null);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, String obj) {
sendPutData(eb, req, new JsonObject().putString(key, obj), null);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, JsonObject obj,
Handler<Message<JsonObject>> doneHandler) {
sendPutData(eb, req, obj, doneHandler);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key,
JsonArray obj, Handler<Message<JsonObject>> doneHandler) {
sendPutData(eb, req, new JsonObject().putArray(key, obj), doneHandler);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, byte[] obj,
Handler<Message<JsonObject>> doneHandler) {
sendPutData(eb, req, new JsonObject().putBinary(key, obj), doneHandler);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, boolean obj,
Handler<Message<JsonObject>> doneHandler) {
sendPutData(eb, req, new JsonObject().putBoolean(key, obj), doneHandler);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, Number obj,
Handler<Message<JsonObject>> doneHandler) {
sendPutData(eb, req, new JsonObject().putNumber(key, obj), doneHandler);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key,
JsonObject obj, Handler<Message<JsonObject>> doneHandler) {
sendPutData(eb, req, new JsonObject().putObject(key, obj), doneHandler);
}
public static void putSessionData(EventBus eb, HttpServerRequest req, String key, String obj,
Handler<Message<JsonObject>> doneHandler) {
sendPutData(eb, req, new JsonObject().putString(key, obj), doneHandler);
}
public static void withSessionId(final EventBus eb, final HttpServerRequest req,
final Handler<String> handler) {
String value = req.headers().get("Cookie");
if (value != null) {
Set<Cookie> cookies = new CookieDecoder().decode(value);
for (final Cookie cookie : cookies) {
if ("sessionId".equals(cookie.getName())) {
handler.handle(cookie.getValue());
return;
}
}
}
startSession(eb, req, handler);
}
public static void startSession(final EventBus eb, final HttpServerRequest req,
final Handler<String> handlerWithSessionId) {
// Create a new session and use that
eb.send(sessionManagerAddress, new JsonObject().putString("action", "start"),
new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> event) {
String sessionId = event.body.getString("sessionId");
CookieEncoder ce = new CookieEncoder(true);
ce.addCookie("sessionId", sessionId);
req.response.putHeader("Set-Cookie", ce.encode());
if (handlerWithSessionId != null) {
handlerWithSessionId.handle(sessionId);
}
}
});
}
private static void sendPutData(final EventBus eb, final HttpServerRequest req,
final JsonObject obj, final Handler<Message<JsonObject>> doneHandler) {
withSessionId(eb, req, new Handler<String>() {
@Override
public void handle(String sessionId) {
sendPutData(eb, sessionId, obj, doneHandler);
}
});
}
public static void putSessionData(EventBus eb, String sessionId, String key, JsonArray obj) {
sendPutData(eb, sessionId, new JsonObject().putArray(key, obj), null);
}
public static void putSessionData(EventBus eb, String sessionId, String key, byte[] obj) {
sendPutData(eb, sessionId, new JsonObject().putBinary(key, obj), null);
}
public static void putSessionData(EventBus eb, String sessionId, String key, boolean obj) {
sendPutData(eb, sessionId, new JsonObject().putBoolean(key, obj), null);
}
public static void putSessionData(EventBus eb, String sessionId, String key, Number obj) {
sendPutData(eb, sessionId, new JsonObject().putNumber(key, obj), null);
}
public static void putSessionData(EventBus eb, String sessionId, String key, JsonObject obj) {
sendPutData(eb, sessionId, new JsonObject().putObject(key, obj), null);
}
public static void putSessionData(EventBus eb, String sessionId, String key, String obj) {
sendPutData(eb, sessionId, new JsonObject().putString(key, obj), null);
}
private static void sendPutData(EventBus eb, String sessionId, JsonObject putObject,
Handler<Message<JsonObject>> doneHandler) {
JsonObject json = new JsonObject().putString("action", "put")
.putString("sessionId", sessionId).putObject("data", putObject);
if (doneHandler != null) {
eb.send(sessionManagerAddress, json, doneHandler);
} else {
eb.send(sessionManagerAddress, json);
}
}
public static void destroySession(EventBus eb, String sessionId,
Handler<Message<JsonObject>> doneHandler) {
eb.send(sessionManagerAddress,
new JsonObject().putString("action", "destroy").putString("sessionId", sessionId),
doneHandler);
}
public static void checkAllSessionsForMatch(EventBus eb, JsonObject json,
final Handler<JsonObject> doneHandler) {
eb.send(sessionManagerAddress,
new JsonObject().putString("action", "status").putString("report", "matches")
.putObject("data", json), new Handler<Message<JsonObject>>() {
public void handle(Message<JsonObject> arg0) {
doneHandler.handle(arg0.body);
};
});
}
public static void withStatistics(EventBus eb, final Handler<JsonObject> handler) {
eb.send(sessionManagerAddress,
new JsonObject().putString("action", "status").putString("report", "connections"),
new Handler<Message<JsonObject>>() {
@Override
public void handle(Message<JsonObject> event) {
handler.handle(event.body);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment