View Javadoc

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.impl.client;
29  
30  import java.net.ProxySelector;
31  
32  import org.apache.http.ConnectionReuseStrategy;
33  import org.apache.http.annotation.ThreadSafe;
34  import org.apache.http.conn.ClientConnectionManager;
35  import org.apache.http.conn.routing.HttpRoutePlanner;
36  import org.apache.http.impl.DefaultConnectionReuseStrategy;
37  import org.apache.http.impl.NoConnectionReuseStrategy;
38  import org.apache.http.impl.conn.PoolingClientConnectionManager;
39  import org.apache.http.impl.conn.ProxySelectorRoutePlanner;
40  import org.apache.http.impl.conn.SchemeRegistryFactory;
41  import org.apache.http.params.HttpParams;
42  
43  /**
44   * An extension of {@link DefaultHttpClient} pre-configured using system properties.
45   * <p>
46   * The following system properties are taken into account by this class:
47   * <ul>
48   *  <li>ssl.TrustManagerFactory.algorithm</li>
49   *  <li>javax.net.ssl.trustStoreType</li>
50   *  <li>javax.net.ssl.trustStore</li>
51   *  <li>javax.net.ssl.trustStoreProvider</li>
52   *  <li>javax.net.ssl.trustStorePassword</li>
53   *  <li>java.home</li>
54   *  <li>ssl.KeyManagerFactory.algorithm</li>
55   *  <li>javax.net.ssl.keyStoreType</li>
56   *  <li>javax.net.ssl.keyStore</li>
57   *  <li>javax.net.ssl.keyStoreProvider</li>
58   *  <li>javax.net.ssl.keyStorePassword</li>
59   *  <li>http.proxyHost</li>
60   *  <li>http.proxyPort</li>
61   *  <li>http.nonProxyHosts</li>
62   *  <li>http.keepAlive</li>
63   *  <li>http.maxConnections</li>
64   * </ul>
65   * <p>
66   * <p>
67   * The following parameters can be used to customize the behavior of this
68   * class:
69   * <ul>
70   *  <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}</li>
71   *  <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
72   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
73   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}</li>
74   *  <li>{@link org.apache.http.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}</li>
75   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}</li>
76   *  <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
77   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
78   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li>
79   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li>
80   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
81   *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
82   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
83   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
84   *  <li>{@link org.apache.http.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li>
85   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
86   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
87   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
88   *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
89   *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
90   *  <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
91   *  <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY}</li>
92   *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li>
93   *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_REDIRECTS}</li>
94   *  <li>{@link org.apache.http.client.params.ClientPNames#MAX_REDIRECTS}</li>
95   *  <li>{@link org.apache.http.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li>
96   *  <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}</li>
97   *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}</li>
98   *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}</li>
99   *  <li>{@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}</li>
100  * </ul>
101  * </p>
102  *
103  * @since 4.2
104  *
105  * @deprecated (4.3) use {@link HttpClientBuilder}
106  */
107 @ThreadSafe
108 @Deprecated
109 public class SystemDefaultHttpClient extends DefaultHttpClient {
110 
111     public SystemDefaultHttpClient(final HttpParams params) {
112         super(null, params);
113     }
114 
115     public SystemDefaultHttpClient() {
116         super(null, null);
117     }
118 
119     @Override
120     protected ClientConnectionManager createClientConnectionManager() {
121         final PoolingClientConnectionManager connmgr = new PoolingClientConnectionManager(
122                 SchemeRegistryFactory.createSystemDefault());
123         String s = System.getProperty("http.keepAlive", "true");
124         if ("true".equalsIgnoreCase(s)) {
125             s = System.getProperty("http.maxConnections", "5");
126             final int max = Integer.parseInt(s);
127             connmgr.setDefaultMaxPerRoute(max);
128             connmgr.setMaxTotal(2 * max);
129         }
130         return connmgr;
131     }
132 
133     @Override
134     protected HttpRoutePlanner createHttpRoutePlanner() {
135         return new ProxySelectorRoutePlanner(getConnectionManager().getSchemeRegistry(),
136                 ProxySelector.getDefault());
137     }
138 
139     @Override
140     protected ConnectionReuseStrategy createConnectionReuseStrategy() {
141         final String s = System.getProperty("http.keepAlive", "true");
142         if ("true".equalsIgnoreCase(s)) {
143             return new DefaultConnectionReuseStrategy();
144         } else {
145             return new NoConnectionReuseStrategy();
146         }
147     }
148 
149 }