/* 
 *   Copyright 2013 Nate McCall and Edward Capriolo
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
*/
package org.usergrid.vx.experimental;

import org.codehaus.jackson.map.ObjectMapper;
import org.usergrid.vx.handler.http.OperationsRequestHandler;
import org.usergrid.vx.handler.http.TimeoutHandler;
import org.usergrid.vx.server.IntravertCassandraServer;
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.JsonArray;
import org.vertx.java.core.json.JsonObject;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public class IntraHandlerJson implements Handler<HttpServerRequest>{

	static ObjectMapper mapper = new ObjectMapper();
  private Vertx vertx;
  
  public IntraHandlerJson(Vertx vertx) {
    super();
    this.vertx = vertx;
  }
	
	@Override
	public void handle(final HttpServerRequest request) {
		request.bodyHandler( new Handler<Buffer>() {
			public void handle(Buffer buffer) {
			     handleRequestAsync(request, buffer);
			}
		});
	}

  private void handleRequestAsync(final HttpServerRequest request, Buffer buffer) {
    IntraReq req = null;
    try {
      req = mapper.readValue(buffer.getBytes(), IntraReq.class);
    } catch (IOException e) {
      //TODO we need to return something here.
      e.printStackTrace();
    }
    vertx.eventBus().send("request.json", req.toJson(), new Handler<Message<JsonObject>>() {
      @Override
      public void handle(Message<JsonObject> event) {
        request.response.end(event.body.toString());
      }
    });
  }

}

@@ -682,209 +682,96 @@
 org.
-codehaus.jackson.map.ObjectMapper;%0Aimport org.usergrid.vx.handler.http.OperationsRequestHandler;%0Aimport org.usergrid.vx.handler.http.TimeoutHandler;%0Aimport org.usergrid.vx.server.IntravertCassandraServ
+apache.commons.lang.exception.ExceptionUtils;%0Aimport org.codehaus.jackson.map.ObjectMapp
 er;%0A
@@ -982,51 +982,8 @@
 st;%0A
-import org.vertx.java.core.json.JsonArray;%0A
 impo
@@ -1034,77 +1034,80 @@
 ort 
-java.io.IOException;%0Aimport java.util.concurrent.atomic.AtomicInteger
+static org.jboss.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST
 ;%0A%0Ap
@@ -1423,17 +1423,16 @@
 Handler(
- 
 new Hand
@@ -1447,19 +1447,22 @@
 er%3E() %7B%0A
-%09%09%09
+      
 public v
@@ -1489,19 +1489,19 @@
 ffer) %7B%0A
-%09%09%09
+   
      han
@@ -1538,15 +1538,20 @@
 r);%0A
-%09%09%09%7D%0A%09%09
+      %7D%0A    
 %7D);%0A
@@ -1744,116 +1744,8 @@
 ;%0A  
-  %7D catch (IOException e) %7B%0A      //TODO we need to return something here.%0A      e.printStackTrace();%0A    %7D%0A
     
@@ -1839,16 +1839,18 @@
 %7B%0A      
+  
 @Overrid
@@ -1851,16 +1851,18 @@
 verride%0A
+  
       pu
@@ -1915,16 +1915,18 @@
         
+ 
+ 
 request.
@@ -1968,25 +1968,29 @@
 ;%0A      
-%7D%0A
+  %7D%0A  
     %7D);%0A
   %7D%0A%0A%7D%0A
@@ -1982,15 +1982,173 @@
     %7D);%0A
+    %7D catch (Exception e) %7B%0A      request.response.statusCode = BAD_REQUEST.getCode();%0A      request.response.end(ExceptionUtils.getFullStackTrace(e));%0A    %7D%0A
   %7D%0A%0A%7D%0A

Send a 400 response back to the client if there is a mapping exception