hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Extending AbstractHttpClientConnection
Date Tue, 06 Mar 2012 15:28:08 GMT
On Tue, Mar 06, 2012 at 01:20:19PM +0000, Ben Short wrote:
> Hi,
> 
> I have something working as shown below. The input and output streams
> are managed outside of the doHttpRequest. Does this seam like a
> reasonable solution?
> 
> public class InputStreamSessionInputBuffer extends AbstractSessionInputBuffer {
> 
>     public InputStreamSessionInputBuffer(final InputStream is, final
> HttpParams params) {
>         super();
>         init(is, 1024, params);
>     }
> 
>     public boolean isDataAvailable(int timeout) throws IOException {
>         boolean result = hasBufferedData();
>         if (!result) {
>             fillBuffer();
>             result = hasBufferedData();
>         }
>         return result;
>     }
> }
> 
> 
> public class OutputStreamSessionInputBuffer extends
> AbstractSessionOutputBuffer {
> 
>     public OutputStreamSessionInputBuffer(final OutputStream os, final
> HttpParams params) {
>         super();
>         init(os, 1024, params);
>     }
> }
> 
> public class StreamHttpClientConnection extends AbstractHttpClientConnection {
> 
>     private boolean open = false;
>     private InputStream is;
>     private OutputStream os;
>     private SessionInputBuffer inputBuffer;
>     private SessionOutputBuffer outputBuffer;
> 
>     public StreamHttpClientConnection() {
>     }
> 
>     public void init(InputStream is, OutputStream os, HttpParams
> params) throws IOException {
> 
>         this.is = is;
>         this.os = os;
> 
>         this.inputBuffer = new InputStreamSessionInputBuffer(is, params);
>         this.outputBuffer = new OutputStreamSessionInputBuffer(os, params);
> 
>         init(this.inputBuffer, this.outputBuffer, params);
> 
>         this.open = true;
>     }
> 
>     @Override
>     protected void assertOpen() throws IllegalStateException {
>         if (!this.open) {
>             throw new IllegalStateException("Connection is not open");
>         }
>     }
> 
>     public void close() throws IOException {
>         if (!this.open) {
>             return;
>         }
>         this.open = false;
> 
>         this.os.flush();
>     }
> 
>     public boolean isOpen() {
>         return this.open;
>     }
> 
>     public void setSocketTimeout(int i) {
>     }
> 
>     public int getSocketTimeout() {
>         return -1;
>     }
> 
>     public void shutdown() throws IOException {
>         this.close();
>     }
> }
> 
> 
> public static void doHttpRequest(InputStream is, OutputStream os)
> throws IOException, HttpException {
> 
>         HttpParams params = new SyncBasicHttpParams();
>         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
>         HttpProtocolParams.setContentCharset(params, "UTF-8");
>         HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
>         HttpProtocolParams.setUseExpectContinue(params, true);
> 
>         HttpProcessor httpproc = new ImmutableHttpProcessor(new
> HttpRequestInterceptor[] {
>                 // Required protocol interceptors
>                 new RequestContent(),
>                 new RequestTargetHost(),
>                 // Recommended protocol interceptors
>                 new RequestConnControl(),
>                 new RequestUserAgent(),
>                 new RequestExpectContinue()});
> 
>         HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
> 
>         HttpContext context = new BasicHttpContext(null);
>         HttpHost host = new HttpHost("10.10.20.60", 4050);
> 
>         StreamHttpClientConnection conn = new StreamHttpClientConnection();
> 
>         //DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
>         ConnectionReuseStrategy connStrategy = new
> DefaultConnectionReuseStrategy();
> 
>         context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
>         context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
> 
>         conn.init(is, os, params);
> 
>         //conn.bind(new Socket(host.getHostName(), host.getPort()), params);
> 
>         HttpRequest request = new BasicHttpRequest("GET",
> "/messages?validity=active_only");
> 
>         httpexecutor.preProcess(request, httpproc, context);
>         HttpResponse response = httpexecutor.execute(request, conn, context);
>         response.setParams(params);
>         httpexecutor.postProcess(response, httpproc, context);
> 
>         System.out.println("<< Response: " + response.getStatusLine());
>         System.out.println(EntityUtils.toString(response.getEntity()));
> 
>         conn.shutdown();
>     }
> 

Looks all right to me.

Cheers

Oleg

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message