From dff91540f376f621a8fc1f640ad8e6b89c23df70 Mon Sep 17 00:00:00 2001 From: Roland Weber Date: Sun, 21 Jan 2007 13:28:45 +0000 Subject: [PATCH] connection manager, part 2 - example and a fix git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@498312 13f79535-47bb-0310-9956-ffa450edef68 --- .../examples/conn/ManagerConnectDirect.java | 210 ++++++++++++++++++ .../impl/ThreadSafeClientConnManager.java | 54 +++-- 2 files changed, 245 insertions(+), 19 deletions(-) create mode 100644 src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java diff --git a/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java b/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java new file mode 100644 index 000000000..6b8ac5996 --- /dev/null +++ b/src/examples/org/apache/http/examples/conn/ManagerConnectDirect.java @@ -0,0 +1,210 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.examples.conn; + + +import org.apache.http.HttpHost; +import org.apache.http.Header; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.HttpVersion; +import org.apache.http.message.BasicHttpRequest; +import org.apache.http.params.HttpParams; +import org.apache.http.params.HttpProtocolParams; +import org.apache.http.impl.DefaultHttpParams; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpExecutionContext; + +import org.apache.http.conn.Scheme; +import org.apache.http.conn.SocketFactory; +import org.apache.http.conn.PlainSocketFactory; +import org.apache.http.conn.HostConfiguration; +import org.apache.http.conn.ManagedClientConnection; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.impl.ThreadSafeClientConnManager; + + + +/** + * How to open a direct connection using + * {@link ClientConnectionManager ClientConnectionManager}. + * This exemplifies the opening of the connection only. + * The subsequent message exchange in this example should not + * be used as a template. + * + * @author Roland Weber + * + * + * + * @version $Revision$ $Date$ + * + * @since 4.0 + */ +public class ManagerConnectDirect { + + /** + * The default parameters. + * Instantiated in {@link #setup setup}. + */ + private static HttpParams defaultParameters = null; + + + /** + * Main entry point to this example. + * + * @param args ignored + */ + public final static void main(String[] args) + throws Exception { + + final HttpHost target = new HttpHost("jakarta.apache.org", 80, "http"); + + setup(); // some general setup + + // one operator can be used for many connections + ClientConnectionManager clcm = createManager(); + + HttpRequest req = createRequest(target); + HttpContext ctx = createContext(); + + System.out.println("preparing route to " + target); + HostConfiguration route = new HostConfiguration(target, null, null); + + System.out.println("requesting connection for " + route); + ManagedClientConnection conn = clcm.getConnection(route); + try { + System.out.println("opening connection"); + conn.open(route, ctx, getParams()); + + System.out.println("sending request"); + conn.sendRequestHeader(req); + // there is no request entity + conn.flush(); + + System.out.println("receiving response header"); + HttpResponse rsp = conn.receiveResponseHeader(getParams()); + + System.out.println("----------------------------------------"); + System.out.println(rsp.getStatusLine()); + Header[] headers = rsp.getAllHeaders(); + for (int i=0; i