Permalink
Browse files

small refactoring to use cleaner handoff to response handler, uses st…

…atics to reference json handler
  • Loading branch information...
1 parent c6d7f68 commit e885c5dafe9cfb4d00e5fc52558b4642ffbca336 @zznate committed Mar 10, 2013
@@ -17,6 +17,9 @@
import org.apache.commons.lang.exception.ExceptionUtils;
import org.codehaus.jackson.map.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.usergrid.vx.handler.RequestJsonHandler;
import org.vertx.java.core.Handler;
import org.vertx.java.core.Vertx;
import org.vertx.java.core.buffer.Buffer;
@@ -26,13 +29,26 @@
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
+/**
+ * The handler is the main entry point for processing the Intravert
+ * JSON payload.
+ * Specifically, this class:
+ * <ol>
+ * <li>Extracts the request body into a {@link IntraReq} object</li>
+ * <li>Sends the IntraReq on the eventBus to the topic
+ * {@link RequestJsonHandler#IHJSON_HANDLER_TOPIC} with an instance
+ * of {@link IHResponse}</li>
+ * <li>IHResponse with send the response via the end method of HttpServerRequest</li>
+ * </ol>
+ */
public class IntraHandlerJson implements Handler<HttpServerRequest>{
+ private static Logger logger = LoggerFactory.getLogger(IntraHandlerJson.class);
+
+ private static ObjectMapper mapper = new ObjectMapper();
+
+ private final Vertx vertx;
- static ObjectMapper mapper = new ObjectMapper();
- private Vertx vertx;
-
public IntraHandlerJson(Vertx vertx) {
- super();
this.vertx = vertx;
}
@@ -49,16 +65,32 @@ private void handleRequestAsync(final HttpServerRequest request, Buffer buffer)
IntraReq req = null;
try {
req = mapper.readValue(buffer.getBytes(), IntraReq.class);
- vertx.eventBus().send("request.json", req.toJson(), new Handler<Message<JsonObject>>() {
- @Override
- public void handle(Message<JsonObject> event) {
- request.response.end(event.body.toString());
- }
- });
+ vertx.eventBus().send(RequestJsonHandler.IHJSON_HANDLER_TOPIC,
+ req.toJson(),
+ new IHResponse(request));
} catch (Exception e) {
request.response.statusCode = BAD_REQUEST.getCode();
request.response.end(ExceptionUtils.getFullStackTrace(e));
}
}
+ private static class IHResponse implements Handler<Message<JsonObject>> {
+
+ private final HttpServerRequest request;
+
+ IHResponse(HttpServerRequest request) {
+ this.request = request;
+ }
+
+ @Override
+ public void handle(Message<JsonObject> event) {
+ if ( logger.isDebugEnabled()) {
+ logger.debug("in IntraHanlderJson's on handler topic {} with event {}",
+ RequestJsonHandler.IHJSON_HANDLER_TOPIC,
+ event.toString() );
+ }
+ request.response.end(event.body.toString());
+ }
+ }
+
}
@@ -23,6 +23,7 @@
import org.slf4j.LoggerFactory;
import org.usergrid.vx.experimental.IntraHandlerJson;
import org.usergrid.vx.experimental.IntraHandlerJsonSmile;
+import org.usergrid.vx.handler.RequestJsonHandler;
import org.usergrid.vx.handler.http.HelloHandler;
import org.usergrid.vx.handler.http.NoMatchHandler;
import org.usergrid.vx.handler.http.OperationsRequestHandler;
@@ -92,32 +93,9 @@ public boolean isRunning() {
return running.get();
}
- public static void registerRequestHandler(final Vertx x) {
- x.eventBus().registerHandler("request.json", new Handler<Message<JsonObject>>() {
- @Override
- public void handle(Message<JsonObject> event) {
- AtomicInteger idGenerator = new AtomicInteger(0);
- JsonArray operations = event.body.getArray("e");
- JsonObject operation = (JsonObject) operations.get(idGenerator.get());
- Long timeout = HandlerUtils.getOperationTime(operation);
-
- operation.putNumber("id", idGenerator.get());
- operation.putObject("state", new JsonObject()
- .putArray("components", new JsonArray()
- .add("name")
- .add("value")));
- idGenerator.incrementAndGet();
-
- OperationsRequestHandler operationsRequestHandler = new OperationsRequestHandler(idGenerator,
- operations, event, x);
- TimeoutHandler timeoutHandler = new TimeoutHandler(operationsRequestHandler);
- long timerId = x.setTimer(timeout, timeoutHandler);
- operationsRequestHandler.setTimerId(timerId);
-
- x.eventBus().send("request." + operation.getString("type").toLowerCase(), operation,
- operationsRequestHandler);
- }
- });
+ public static void registerRequestHandler(Vertx x) {
+ x.eventBus().registerHandler(RequestJsonHandler.IHJSON_HANDLER_TOPIC,
+ new RequestJsonHandler(vertx));
}
public static void registerOperationHandlers(Vertx x) {

0 comments on commit e885c5d

Please sign in to comment.