Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *   http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  * ====================================================================
  20.  *
  21.  * This software consists of voluntary contributions made by many
  22.  * individuals on behalf of the Apache Software Foundation.  For more
  23.  * information on the Apache Software Foundation, please see
  24.  * <http://www.apache.org/>.
  25.  *
  26.  */
  27.  
  28. package org.apache.http.examples.conn;
  29.  
  30.  
  31. import org.apache.http.Header;
  32. import org.apache.http.HttpHost;
  33. import org.apache.http.HttpRequest;
  34. import org.apache.http.HttpResponse;
  35. import org.apache.http.HttpVersion;
  36. import org.apache.http.conn.ClientConnectionManager;
  37. import org.apache.http.conn.routing.HttpRoute;
  38. import org.apache.http.conn.scheme.PlainSocketFactory;
  39. import org.apache.http.conn.scheme.Scheme;
  40. import org.apache.http.conn.scheme.SchemeRegistry;
  41. import org.apache.http.conn.scheme.SocketFactory;
  42. import org.apache.http.conn.ClientConnectionRequest;
  43. import org.apache.http.conn.ManagedClientConnection;
  44. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  45. import org.apache.http.message.BasicHttpRequest;
  46. import org.apache.http.params.BasicHttpParams;
  47. import org.apache.http.params.HttpParams;
  48. import org.apache.http.params.HttpProtocolParams;
  49. import org.apache.http.protocol.HttpContext;
  50. import org.apache.http.protocol.BasicHttpContext;
  51.  
  52.  
  53.  
  54. /**
  55.  * How to open a direct connection using
  56.  * {@link ClientConnectionManager ClientConnectionManager}.
  57.  * This exemplifies the <i>opening</i> of the connection only.
  58.  * The subsequent message exchange in this example should not
  59.  * be used as a template.
  60.  *
  61.  *
  62.  *
  63.  * @since 4.0
  64.  */
  65. public class ManagerConnectDirect {
  66.  
  67.     /**
  68.      * The default parameters.
  69.      * Instantiated in {@link #setup setup}.
  70.      */
  71.     private static HttpParams defaultParameters = null;
  72.  
  73.     /**
  74.      * The scheme registry.
  75.      * Instantiated in {@link #setup setup}.
  76.      */
  77.     private static SchemeRegistry supportedSchemes;
  78.  
  79.  
  80.     /**
  81.      * Main entry point to this example.
  82.      *
  83.      * @param args      ignored
  84.      */
  85.     public final static void main(String[] args)
  86.         throws Exception {
  87.  
  88.         final HttpHost target = new HttpHost("jakarta.apache.org", 80, "http");
  89.  
  90.         setup(); // some general setup
  91.  
  92.         ClientConnectionManager clcm = createManager();
  93.  
  94.         HttpRequest req = createRequest(target);
  95.         HttpContext ctx = createContext();
  96.  
  97.         System.out.println("preparing route to " + target);
  98.         HttpRoute route = new HttpRoute
  99.             (target, null, supportedSchemes.getScheme(target).isLayered());
  100.  
  101.         System.out.println("requesting connection for " + route);
  102.         ClientConnectionRequest connRequest = clcm.requestConnection(route, null);
  103.         ManagedClientConnection conn = connRequest.getConnection(0, null);
  104.         try {
  105.             System.out.println("opening connection");
  106.             conn.open(route, ctx, getParams());
  107.  
  108.             System.out.println("sending request");
  109.             conn.sendRequestHeader(req);
  110.             // there is no request entity
  111.             conn.flush();
  112.  
  113.             System.out.println("receiving response header");
  114.             HttpResponse rsp = conn.receiveResponseHeader();
  115.  
  116.             System.out.println("----------------------------------------");
  117.             System.out.println(rsp.getStatusLine());
  118.             Header[] headers = rsp.getAllHeaders();
  119.             for (int i=0; i<headers.length; i++) {
  120.                 System.out.println(headers[i]);
  121.             }
  122.             System.out.println("----------------------------------------");
  123.  
  124.             System.out.println("closing connection");
  125.             conn.close();
  126.  
  127.         } finally {
  128.  
  129.             if (conn.isOpen()) {
  130.                 System.out.println("shutting down connection");
  131.                 try {
  132.                     conn.shutdown();
  133.                 } catch (Exception x) {
  134.                     System.out.println("problem during shutdown");
  135.                     x.printStackTrace(System.out);
  136.                 }
  137.             }
  138.  
  139.             System.out.println("releasing connection");
  140.             clcm.releaseConnection(conn, -1, null);
  141.         }
  142.  
  143.     } // main
  144.  
  145.  
  146.     private final static ClientConnectionManager createManager() {
  147.  
  148.         return new ThreadSafeClientConnManager(getParams(), supportedSchemes);
  149.     }
  150.  
  151.  
  152.     /**
  153.      * Performs general setup.
  154.      * This should be called only once.
  155.      */
  156.     private final static void setup() {
  157.  
  158.         // Register the "http" protocol scheme, it is required
  159.         // by the default operator to look up socket factories.
  160.         supportedSchemes = new SchemeRegistry();
  161.         SocketFactory sf = PlainSocketFactory.getSocketFactory();
  162.         supportedSchemes.register(new Scheme("http", sf, 80));
  163.  
  164.         // Prepare parameters.
  165.         // Since this example doesn't use the full core framework,
  166.         // only few parameters are actually required.
  167.         HttpParams params = new BasicHttpParams();
  168.         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  169.         HttpProtocolParams.setUseExpectContinue(params, false);
  170.         defaultParameters = params;
  171.  
  172.     } // setup
  173.  
  174.  
  175.     private final static HttpParams getParams() {
  176.         return defaultParameters;
  177.     }
  178.  
  179.  
  180.     /**
  181.      * Creates a request to execute in this example.
  182.      * In a real application, request interceptors should be used
  183.      * to add the required headers.
  184.      *
  185.      * @param target    the target server for the request
  186.      *
  187.      * @return  a request without an entity
  188.      */
  189.     private final static HttpRequest createRequest(HttpHost target) {
  190.  
  191.         HttpRequest req = new BasicHttpRequest
  192.             ("OPTIONS", "*", HttpVersion.HTTP_1_1);
  193.  
  194.         req.addHeader("Host", target.getHostName());
  195.  
  196.         return req;
  197.     }
  198.  
  199.  
  200.     /**
  201.      * Creates a context for executing a request.
  202.      * Since this example doesn't really use the execution framework,
  203.      * the context can be left empty.
  204.      *
  205.      * @return  a new, empty context
  206.      */
  207.     private final static HttpContext createContext() {
  208.         return new BasicHttpContext(null);
  209.     }
  210.  
  211. } // class ManagerConnectDirect
  212.  
  213.