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.ClientConnectionRequest;
  39. import org.apache.http.conn.ManagedClientConnection;
  40. import org.apache.http.conn.scheme.PlainSocketFactory;
  41. import org.apache.http.conn.scheme.Scheme;
  42. import org.apache.http.conn.scheme.SchemeRegistry;
  43. import org.apache.http.conn.scheme.SocketFactory;
  44. import org.apache.http.conn.ssl.SSLSocketFactory;
  45. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  46. import org.apache.http.message.BasicHttpRequest;
  47. import org.apache.http.params.BasicHttpParams;
  48. import org.apache.http.params.HttpParams;
  49. import org.apache.http.params.HttpProtocolParams;
  50. import org.apache.http.protocol.HttpContext;
  51. import org.apache.http.protocol.BasicHttpContext;
  52.  
  53.  
  54.  
  55. /**
  56.  * How to open a secure connection through a proxy using
  57.  * {@link ClientConnectionManager ClientConnectionManager}.
  58.  * This exemplifies the <i>opening</i> of the connection only.
  59.  * The message exchange, both subsequently and for tunnelling,
  60.  * should not be used as a template.
  61.  *
  62.  *
  63.  *
  64.  * @since 4.0
  65.  */
  66. public class ManagerConnectProxy {
  67.  
  68.     /**
  69.      * The default parameters.
  70.      * Instantiated in {@link #setup setup}.
  71.      */
  72.     private static HttpParams defaultParameters = null;
  73.  
  74.     /**
  75.      * The scheme registry.
  76.      * Instantiated in {@link #setup setup}.
  77.      */
  78.     private static SchemeRegistry supportedSchemes;
  79.  
  80.  
  81.     /**
  82.      * Main entry point to this example.
  83.      *
  84.      * @param args      ignored
  85.      */
  86.     public final static void main(String[] args)
  87.         throws Exception {
  88.  
  89.         // make sure to use a proxy that supports CONNECT
  90.         final HttpHost target =
  91.             new HttpHost("issues.apache.org", 443, "https");
  92.         final HttpHost proxy =
  93.             new HttpHost("127.0.0.1", 8666, "http");
  94.  
  95.         setup(); // some general setup
  96.  
  97.         ClientConnectionManager clcm = createManager();
  98.  
  99.         HttpRequest req = createRequest(target);
  100.         HttpContext ctx = createContext();
  101.  
  102.         System.out.println("preparing route to " + target + " via " + proxy);
  103.         HttpRoute route = new HttpRoute
  104.             (target, null, proxy,
  105.              supportedSchemes.getScheme(target).isLayered());
  106.  
  107.         System.out.println("requesting connection for " + route);
  108.         ClientConnectionRequest connRequest = clcm.requestConnection(route, null);
  109.         ManagedClientConnection conn = connRequest.getConnection(0, null);
  110.         try {
  111.             System.out.println("opening connection");
  112.             conn.open(route, ctx, getParams());
  113.  
  114.             HttpRequest connect = createConnect(target);
  115.             System.out.println("opening tunnel to " + target);
  116.             conn.sendRequestHeader(connect);
  117.             // there is no request entity
  118.             conn.flush();
  119.  
  120.             System.out.println("receiving confirmation for tunnel");
  121.             HttpResponse connected = conn.receiveResponseHeader();
  122.             System.out.println("----------------------------------------");
  123.             printResponseHeader(connected);
  124.             System.out.println("----------------------------------------");
  125.             int status = connected.getStatusLine().getStatusCode();
  126.             if ((status < 200) || (status > 299)) {
  127.                 System.out.println("unexpected status code " + status);
  128.                 System.exit(1);
  129.             }
  130.             System.out.println("receiving response body (ignored)");
  131.             conn.receiveResponseEntity(connected);
  132.  
  133.             conn.tunnelTarget(false, getParams());
  134.  
  135.             System.out.println("layering secure connection");
  136.             conn.layerProtocol(ctx, getParams());
  137.  
  138.             // finally we have the secure connection and can send the request
  139.  
  140.             System.out.println("sending request");
  141.             conn.sendRequestHeader(req);
  142.             // there is no request entity
  143.             conn.flush();
  144.  
  145.             System.out.println("receiving response header");
  146.             HttpResponse rsp = conn.receiveResponseHeader();
  147.  
  148.             System.out.println("----------------------------------------");
  149.             printResponseHeader(rsp);
  150.             System.out.println("----------------------------------------");
  151.  
  152.             System.out.println("closing connection");
  153.             conn.close();
  154.  
  155.         } finally {
  156.  
  157.             if (conn.isOpen()) {
  158.                 System.out.println("shutting down connection");
  159.                 try {
  160.                     conn.shutdown();
  161.                 } catch (Exception x) {
  162.                     System.out.println("problem during shutdown");
  163.                     x.printStackTrace(System.out);
  164.                 }
  165.             }
  166.  
  167.             System.out.println("releasing connection");
  168.             clcm.releaseConnection(conn, -1, null);
  169.         }
  170.  
  171.     } // main
  172.  
  173.  
  174.     private final static ClientConnectionManager createManager() {
  175.  
  176.         return new ThreadSafeClientConnManager(getParams(), supportedSchemes);
  177.     }
  178.  
  179.  
  180.     /**
  181.      * Performs general setup.
  182.      * This should be called only once.
  183.      */
  184.     private final static void setup() {
  185.  
  186.         // Register the "http" and "https" protocol schemes, they are
  187.         // required by the default operator to look up socket factories.
  188.         supportedSchemes = new SchemeRegistry();
  189.         SocketFactory sf = PlainSocketFactory.getSocketFactory();
  190.         supportedSchemes.register(new Scheme("http", sf, 80));
  191.         sf = SSLSocketFactory.getSocketFactory();
  192.         supportedSchemes.register(new Scheme("https", sf, 80));
  193.  
  194.         // Prepare parameters.
  195.         // Since this example doesn't use the full core framework,
  196.         // only few parameters are actually required.
  197.         HttpParams params = new BasicHttpParams();
  198.         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  199.         HttpProtocolParams.setUseExpectContinue(params, false);
  200.         defaultParameters = params;
  201.  
  202.     } // setup
  203.  
  204.  
  205.     private final static HttpParams getParams() {
  206.         return defaultParameters;
  207.     }
  208.  
  209.  
  210.     /**
  211.      * Creates a request to tunnel a connection.
  212.      * In a real application, request interceptors should be used
  213.      * to add the required headers.
  214.      *
  215.      * @param target    the target server for the tunnel
  216.      *
  217.      * @return  a CONNECT request without an entity
  218.      */
  219.     private final static HttpRequest createConnect(HttpHost target) {
  220.  
  221.         // see RFC 2817, section 5.2
  222.         final String authority = target.getHostName()+":"+target.getPort();
  223.  
  224.         HttpRequest req = new BasicHttpRequest
  225.             ("CONNECT", authority, HttpVersion.HTTP_1_1);
  226.  
  227.         req.addHeader("Host", authority);
  228.  
  229.         return req;
  230.     }
  231.  
  232.  
  233.     /**
  234.      * Creates a request to execute in this example.
  235.      * In a real application, request interceptors should be used
  236.      * to add the required headers.
  237.      *
  238.      * @param target    the target server for the request
  239.      *
  240.      * @return  a request without an entity
  241.      */
  242.     private final static HttpRequest createRequest(HttpHost target) {
  243.  
  244.         HttpRequest req = new BasicHttpRequest
  245.             ("OPTIONS", "*", HttpVersion.HTTP_1_1);
  246.  
  247.         req.addHeader("Host", target.getHostName());
  248.  
  249.         return req;
  250.     }
  251.  
  252.  
  253.     /**
  254.      * Creates a context for executing a request.
  255.      * Since this example doesn't really use the execution framework,
  256.      * the context can be left empty.
  257.      *
  258.      * @return  a new, empty context
  259.      */
  260.     private final static HttpContext createContext() {
  261.         return new BasicHttpContext(null);
  262.     }
  263.  
  264.  
  265.     private final static void printResponseHeader(HttpResponse rsp) {
  266.  
  267.         System.out.println(rsp.getStatusLine());
  268.         Header[] headers = rsp.getAllHeaders();
  269.         for (int i=0; i<headers.length; i++) {
  270.             System.out.println(headers[i]);
  271.         }
  272.     }
  273.  
  274. } // class ManagerConnectProxy
  275.  
  276.