001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.nio.protocol;
028
029import java.io.IOException;
030
031import org.apache.http.ContentTooLongException;
032import org.apache.http.HttpEntity;
033import org.apache.http.HttpEntityEnclosingRequest;
034import org.apache.http.HttpRequest;
035import org.apache.http.entity.ContentType;
036import org.apache.http.nio.ContentDecoder;
037import org.apache.http.nio.IOControl;
038import org.apache.http.nio.entity.ContentBufferEntity;
039import org.apache.http.nio.util.HeapByteBufferAllocator;
040import org.apache.http.nio.util.SimpleInputBuffer;
041import org.apache.http.protocol.HttpContext;
042import org.apache.http.util.Asserts;
043
044/**
045 * Basic implementation of {@link HttpAsyncRequestConsumer}. Please note that
046 * this consumer buffers request content in memory and should be used for
047 * relatively small request messages.
048 *
049 * @since 4.2
050 */
051public class BasicAsyncRequestConsumer extends AbstractAsyncRequestConsumer<HttpRequest> {
052
053    private static final int MAX_INITIAL_BUFFER_SIZE = 256 * 1024;
054
055    private volatile HttpRequest request;
056    private volatile SimpleInputBuffer buf;
057
058    public BasicAsyncRequestConsumer() {
059        super();
060    }
061
062    @Override
063    protected void onRequestReceived(final HttpRequest request) throws IOException {
064        this.request = request;
065    }
066
067    @Override
068    protected void onEntityEnclosed(
069            final HttpEntity entity, final ContentType contentType) throws IOException {
070        long len = entity.getContentLength();
071        if (len > Integer.MAX_VALUE) {
072            throw new ContentTooLongException("Entity content is too long: " + len);
073        }
074        if (len < 0) {
075            len = 4096;
076        }
077        final int initialBufferSize = Math.min((int) len, MAX_INITIAL_BUFFER_SIZE);
078        this.buf = new SimpleInputBuffer(initialBufferSize, new HeapByteBufferAllocator());
079        ((HttpEntityEnclosingRequest) this.request).setEntity(
080                new ContentBufferEntity(entity, this.buf));
081    }
082
083    @Override
084    protected void onContentReceived(
085            final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
086        Asserts.notNull(this.buf, "Content buffer");
087        this.buf.consumeContent(decoder);
088    }
089
090    @Override
091    protected void releaseResources() {
092        this.request = null;
093        this.buf = null;
094    }
095
096    @Override
097    protected HttpRequest buildResult(final HttpContext context) {
098        return this.request;
099    }
100
101}