Permalink
Browse files

Initial groundwork for REST handlers imported from virgil

- Created interface and abstract base class for all HttpServerRequest handlers to implement
- Initial utility class for extracting virgil's ConsistencyLevel headers
- Imported mockito for handling smoother testing with HttpServerRequest
  • Loading branch information...
1 parent 466af45 commit e07787f67bfeb1b3e3de923eb939dd23d84c494f @zznate committed Mar 1, 2013
View
@@ -124,6 +124,11 @@
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.0</version>
+ </dependency>
</dependencies>
<build>
<plugins>
@@ -0,0 +1,23 @@
+package org.usergrid.vx.handler;
+
+import org.vertx.java.core.Handler;
+import org.vertx.java.core.buffer.Buffer;
+import org.vertx.java.core.http.HttpServerRequest;
+
+/**
+ * Base Handler interface for Intravert HTTP handlers
+ *
+ * @author zznate
+ */
+public interface IntraHandler extends Handler<HttpServerRequest> {
+
+ /**
+ * Handle the provided request asynchronously
+ * @param req
+ * @param buffer
+ */
+ void handleRequestAsync(HttpServerRequest req, Buffer buffer);
+
+ void registerRequestHandler();
+
+}
@@ -0,0 +1,34 @@
+package org.usergrid.vx.handler;
+
+import org.vertx.java.core.Handler;
+import org.vertx.java.core.Vertx;
+import org.vertx.java.core.buffer.Buffer;
+import org.vertx.java.core.http.HttpServerRequest;
+
+/**
+ * @author zznate
+ */
+public abstract class IntraHandlerBase implements IntraHandler {
+
+
+
+ protected final Vertx vertx;
+
+ public IntraHandlerBase(Vertx vertx) {
+ this.vertx = vertx;
+ registerRequestHandler();
+ }
+
+ @Override
+ public void handle(final HttpServerRequest request) {
+ request.bodyHandler( new Handler<Buffer>() {
+ public void handle(Buffer buffer) {
+ handleRequestAsync(request, buffer);
+ }
+ });
+ }
+
+
+
+
+}
@@ -0,0 +1,40 @@
+package org.usergrid.vx.handler.rest;
+
+import org.usergrid.vx.handler.IntraHandlerBase;
+import org.vertx.java.core.Handler;
+import org.vertx.java.core.Vertx;
+import org.vertx.java.core.buffer.Buffer;
+import org.vertx.java.core.eventbus.Message;
+import org.vertx.java.core.http.HttpServerRequest;
+import org.vertx.java.core.json.JsonObject;
+
+import java.util.Map;
+
+/**
+ * @author zznate
+ */
+public class IntraHandlerRest extends IntraHandlerBase {
+
+ public IntraHandlerRest(Vertx vertx) {
+ super(vertx);
+ }
+
+ public void handleRequestAsync(final HttpServerRequest req, Buffer buffer) {
+ Map<String,String> reqParams = req.params();
+ // TODO extract query, etc? or delegate lower. Probably lower given current delegation in virgil
+
+ // TODO extract keyspace (null is ok as this triggers getKeyspaces()
+
+ // TODO extract consistencyLevel header if available
+
+ // dispatch to handler
+
+ }
+
+ public void registerRequestHandler() {
+ vertx.eventBus().registerHandler("data.keyspaceMeta", new KeyspaceMetaHandler());
+
+ }
+
+
+}
@@ -0,0 +1,53 @@
+package org.usergrid.vx.handler.rest;
+
+import org.apache.cassandra.db.ConsistencyLevel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.vertx.java.core.http.HttpServerRequest;
+
+/**
+ * Utility class for extracting useful information from the request for the
+ * REST API handlers.
+ *
+ * @author zznate
+ */
+public class IntravertRestUtils {
+
+ private static Logger log = LoggerFactory.getLogger(IntravertRestUtils.class);
+
+ /**
+ * Defines the Consistency level header:
+ * "X-Consistency-Level"
+ */
+ public static final String CONSISTENCY_LEVEL_HEADER =
+ "X-Consistency-Level";
+
+ /**
+ * Returns the consistency level from the header if present.
+ * Defaults to {@link ConsistencyLevel#ONE} if:
+ * <ul>
+ * <li>The consistency level header is not found</li>
+ * <li>The consistency level header is found but {@link ConsistencyLevel#valueOf(String)}
+ * throws an IllegalArgumentException (i.e. there was a typo)</li>
+ * </ul>
+ *
+ * See {@link #CONSISTENCY_LEVEL_HEADER} for the header definition
+ *
+ * @param request
+ * @return The level specified by the header or ONE according to the conditions
+ * defined above.
+ */
+ public static ConsistencyLevel fromHeader(HttpServerRequest request) {
+ if ( request.headers().containsKey(CONSISTENCY_LEVEL_HEADER)) {
+ try {
+ return ConsistencyLevel.valueOf(request.headers().get(CONSISTENCY_LEVEL_HEADER));
+ } catch (IllegalArgumentException iae) {
+ log.warn("Unable to deduce value for '{}' Header. Using default {} ",
+ CONSISTENCY_LEVEL_HEADER, ConsistencyLevel.ONE.toString());
+ }
+ }
+ // TODO this should be a configuration option one day
+ return ConsistencyLevel.ONE;
+ }
+
+}
@@ -0,0 +1,16 @@
+package org.usergrid.vx.handler.rest;
+
+import org.vertx.java.core.Handler;
+import org.vertx.java.core.eventbus.Message;
+import org.vertx.java.core.json.JsonObject;
+
+/**
+ * @author zznate
+ */
+public class KeyspaceMetaHandler implements Handler<Message<JsonObject>> {
+
+ @Override
+ public void handle(Message<JsonObject> jsonObjectMessage) {
+
+ }
+}
@@ -0,0 +1,50 @@
+package org.usergrid.vx.handler.rest;
+
+import org.apache.cassandra.db.ConsistencyLevel;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.vertx.java.core.http.HttpServerRequest;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author zznate
+ */
+public class IntravertRestUtilsUnitTest {
+
+ private HttpServerRequest req = Mockito.mock(HttpServerRequest.class);
+ private Map<String,String> headersMap;
+
+ @Before
+ public void setupLocal() {
+ headersMap = new HashMap<>();
+ headersMap.put(IntravertRestUtils.CONSISTENCY_LEVEL_HEADER,"QUORUM");
+ }
+
+ @Test
+ public void extractConsisntecyLevel() {
+ Mockito.when(req.headers()).thenReturn(headersMap);
+ ConsistencyLevel cl = IntravertRestUtils.fromHeader(req);
+ assertEquals(ConsistencyLevel.QUORUM, cl);
+ }
+
+ @Test
+ public void extractDefaultOnEmpty() {
+ Mockito.when(req.headers()).thenReturn(new HashMap());
+ ConsistencyLevel cl = IntravertRestUtils.fromHeader(req);
+ assertEquals(ConsistencyLevel.ONE, cl);
+ }
+
+ @Test
+ public void extractDefaultOnTypo() {
+ headersMap.put(IntravertRestUtils.CONSISTENCY_LEVEL_HEADER, "FORTYTWO");
+ Mockito.when(req.headers()).thenReturn(headersMap);
+ ConsistencyLevel cl = IntravertRestUtils.fromHeader(req);
+ assertEquals(ConsistencyLevel.ONE, cl);
+ }
+}

0 comments on commit e07787f

Please sign in to comment.