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