/*
 * Copyright (c) 2013 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

package org.asynchttpclient.providers.grizzly.bodyhandler;

import org.asynchttpclient.Request;
import org.asynchttpclient.providers.grizzly.GrizzlyAsyncHttpProvider;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.filterchain.FilterChainContext;
import org.glassfish.grizzly.http.HttpContent;
import org.glassfish.grizzly.http.HttpRequestPacket;
import org.glassfish.grizzly.memory.Buffers;
import org.glassfish.grizzly.memory.MemoryManager;
import org.glassfish.grizzly.utils.Charsets;

import java.io.IOException;

public final class ByteArrayBodyHandler implements BodyHandler {

    private final boolean compressionEnabled;

    public ByteArrayBodyHandler(GrizzlyAsyncHttpProvider grizzlyAsyncHttpProvider) {
        compressionEnabled = grizzlyAsyncHttpProvider.getClientConfig().isCompressionEnabled();
    }


    // -------------------------------------------- Methods from BodyHandler

    public boolean handlesBodyType(final Request request) {
        return (request.getByteData() != null);
    }

    @SuppressWarnings({"unchecked"})
    public boolean doHandle(final FilterChainContext ctx,
                         final Request request,
                         final HttpRequestPacket requestPacket)
    throws IOException {

        String charset = request.getBodyEncoding();
        if (charset == null) {
            charset = Charsets.ASCII_CHARSET.name();
        }
        final byte[] data = new String(request.getByteData(), charset).getBytes(charset);
        final MemoryManager mm = ctx.getMemoryManager();
        final Buffer gBuffer = Buffers.wrap(mm, data);
        if (requestPacket.getContentLength() == -1) {
                if (!compressionEnabled) {
                    requestPacket.setContentLengthLong(data.length);
                }
            }
        final HttpContent content = requestPacket.httpContentBuilder().content(gBuffer).build();
        content.setLast(true);
        ctx.write(content, ((!requestPacket.isCommitted()) ? ctx.getTransportContext().getCompletionHandler() : null));
        return true;
    }
}

@@ -1953,154 +1953,8 @@
  %7B%0A%0A
-        String charset = request.getBodyEncoding();%0A        if (charset == null) %7B%0A            charset = Charsets.ASCII_CHARSET.name();%0A        %7D%0A
     
@@ -1981,19 +1981,8 @@
 a = 
-new String(
 requ
@@ -2002,36 +2002,8 @@
 ta()
-, charset).getBytes(charset)
 ;%0A  

Fix for #429.