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.HttpResponse; 034import org.apache.http.entity.ContentType; 035import org.apache.http.nio.ContentDecoder; 036import org.apache.http.nio.IOControl; 037import org.apache.http.nio.entity.ContentBufferEntity; 038import org.apache.http.nio.util.HeapByteBufferAllocator; 039import org.apache.http.nio.util.SimpleInputBuffer; 040import org.apache.http.protocol.HttpContext; 041import org.apache.http.util.Asserts; 042 043/** 044 * Basic implementation of {@link HttpAsyncResponseConsumer}. Please note that 045 * this consumer buffers response content in memory and should be used for 046 * relatively small response messages. 047 * 048 * @since 4.2 049 */ 050public class BasicAsyncResponseConsumer extends AbstractAsyncResponseConsumer<HttpResponse> { 051 052 private static final int MAX_INITIAL_BUFFER_SIZE = 256 * 1024; 053 054 private volatile HttpResponse response; 055 private volatile SimpleInputBuffer buf; 056 057 public BasicAsyncResponseConsumer() { 058 super(); 059 } 060 061 @Override 062 protected void onResponseReceived(final HttpResponse response) throws IOException { 063 this.response = response; 064 } 065 066 @Override 067 protected void onEntityEnclosed( 068 final HttpEntity entity, final ContentType contentType) throws IOException { 069 long len = entity.getContentLength(); 070 if (len > Integer.MAX_VALUE) { 071 throw new ContentTooLongException("Entity content is too long: " + len); 072 } 073 if (len < 0) { 074 len = 4096; 075 } 076 final int initialBufferSize = Math.min((int) len, MAX_INITIAL_BUFFER_SIZE); 077 this.buf = new SimpleInputBuffer(initialBufferSize, new HeapByteBufferAllocator()); 078 this.response.setEntity(new ContentBufferEntity(entity, this.buf)); 079 } 080 081 @Override 082 protected void onContentReceived( 083 final ContentDecoder decoder, final IOControl ioctrl) throws IOException { 084 Asserts.notNull(this.buf, "Content buffer"); 085 this.buf.consumeContent(decoder); 086 } 087 088 @Override 089 protected void releaseResources() { 090 this.response = null; 091 this.buf = null; 092 } 093 094 @Override 095 protected HttpResponse buildResult(final HttpContext context) { 096 return this.response; 097 } 098 099}