/*
 * Copyright 2013 LinkedIn, Inc
 *
 * 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 voldemort.rest.coordinator.admin;

import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static org.jboss.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;

import org.apache.log4j.Logger;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;

import voldemort.rest.RestErrorHandler;
import voldemort.rest.RestMessageHeaders;

public class CoordinatorAdminRequestHandler extends SimpleChannelHandler {

    public HttpRequest request;
    private boolean readingChunks;
    private final Logger logger = Logger.getLogger(CoordinatorAdminRequestHandler.class);

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
        if (!readingChunks) {
            HttpRequest request = this.request = (HttpRequest) messageEvent.getMessage();
            String requestURI = this.request.getUri();
            if (logger.isDebugEnabled()) {
                logger.debug("Request URI: " + requestURI);
            }

            if (request.isChunked()) {
                readingChunks = true;
            } else {
                // Instantiate the appropriate error handler
                HttpMethod httpMethod = request.getMethod();
                if (httpMethod.equals(HttpMethod.GET)) {
                    if(logger.isDebugEnabled()) {
                        logger.debug("Received a Http GET request at " + System.currentTimeMillis() + " ms");
                    }
                    // handleGet()
                } else if (httpMethod.equals(HttpMethod.POST)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Recieved a Http POST request at " + System.currentTimeMillis() + " ms");
                    }
                    // handlePut
                } else if (httpMethod.equals(HttpMethod.DELETE)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Received a Http DELETE request at " + System.currentTimeMillis() + " ms");
                    }
                    // handleDelete
                } else {
                    String errorMessage = "Illegal Http request received at " + System.currentTimeMillis() + " ms";
                    logger.error(errorMessage);
                    RestErrorHandler.writeErrorResponse(messageEvent, BAD_REQUEST, errorMessage);
                    return;
                }
            }
        } else {
            HttpChunk chunk = (HttpChunk) messageEvent.getMessage();
            if (chunk.isLast()) {
                readingChunks = false;
            }
        }
        sendResponse(messageEvent);
    }

    public void sendResponse(MessageEvent messageEvent) {
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NO_CONTENT);
        response.setHeader(CONTENT_LENGTH, "0");
        response.setHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, "Sid");
        messageEvent.getChannel().write(response);
        logger.info("Sent");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        e.getCause().printStackTrace();
        e.getChannel().close();
    }
}

@@ -949,39 +949,269 @@
 _1;%0A
-%0Aimport org.apache.log4j.Logger
+import static org.jboss.netty.handler.codec.http.HttpResponseStatus.OK;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0A%0Aimport org.apache.log4j.Logger;%0Aimport org.jboss.netty.buffer.ChannelBuffer;%0Aimport org.jboss.netty.buffer.ChannelBuffers
 ;%0Aim
@@ -2137,2269 +2137,1004 @@
     
-if (!readingChunks) %7B%0A            HttpRequest request = this.request = (HttpRequest) messageEvent.getMessage();%0A            String requestURI = this.request.getUri();%0A            if (logger.isDebugEnabled()) %7B%0A                logger.debug(%22Request URI: %22 + requestURI);%0A            %7D%0A%0A            if (request.isChunked()) %7B%0A                readingChunks = true;%0A            %7D else %7B%0A                // Instantiate the appropriate error handler%0A                HttpMethod httpMethod = request.getMethod();%0A                if (httpMethod.equals(HttpMethod.GET)) %7B%0A                    if(logger.isDebugEnabled()) %7B%0A                        logger.debug(%22Received a Http GET request at %22 + System.currentTimeMillis() + %22 ms%22);%0A                    %7D%0A                    // handleGet()%0A                %7D else if (httpMethod.equals(HttpMethod.POST)) %7B%0A                    if (logger.isDebugEnabled()) %7B%0A                        logger.debug(%22Recieved a Http POST request at %22 + System.currentTimeMillis() + %22 ms%22);%0A                    %7D%0A                    // handlePut%0A                %7D else if (httpMethod.equals(HttpMethod.DELETE)) %7B%0A                    if (logger.isDebugEnabled()) %7B%0A                        logger.debug(%22Received a Http DELETE request at %22 + System.currentTimeMillis() + %22 ms%22);%0A                    %7D%0A                    // handleDelete%0A                %7D else %7B%0A                    String errorMessage = %22Illegal Http request received at %22 + System.currentTimeMillis() + %22 ms%22;%0A                    logger.error(errorMessage);%0A                    RestErrorHandler.writeErrorResponse(messageEvent, BAD_REQUEST, errorMessage);%0A                    return;%0A                %7D%0A            %7D%0A        %7D else %7B%0A            HttpChunk chunk = (HttpChunk) messageEvent.getMessage();%0A            if (chunk.isLast()) %7B%0A                readingChunks = false;%0A            %7D%0A        %7D%0A        sendResponse(messageEvent);%0A    %7D%0A%0A    public void sendResponse(MessageEvent messageEvent) %7B%0A        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NO_CONTENT);%0A        response.setHeader(CONTENT_LENGTH, %220%22);%0A        response.setHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, %22Sid%22);%0A        messageEvent.getChannel().write(response);%0A        logger.info(%22S
+// TODO handle message here. Depending on the type of request, different handlers will be called.%0A        // This is just for testing purposes.%0A        sendResponse(messageEvent);%0A    %7D%0A%0A    public void sendResponse(MessageEvent messageEvent) %7B%0A        // TODO Remove the code below with actual code for the response. This was for testing purposes.%0A        // Fill in the actual response that you want to send.%0A        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);%0A        response.setHeader(CONTENT_LENGTH, %224%22);%0A        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();%0A        try %7B%0A            outputStream.write(%22allo%22.getBytes());%0A        %7D catch (IOException e) %7B%0A        e.printStackTrace(); //To change body of catch statement use File %7C Settings %7C File Templates.%0A        %7D%0A        ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();%0A        responseContent.writeBytes(outputStream.toByteArray());%0A        response.setContent(responseCont
 ent
-%22
 );%0A 

Remove unneeded code and add add TODO for send response