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>https.proxyHost</li>
63   *  <li>https.proxyPort</li>
64   *  <li>http.nonProxyHosts</li>
65   *  <li>http.keepAlive</li>
66   *  <li>http.maxConnections</li>
67   * </ul>
68   * <p>
69   * <p>
70   * The following parameters can be used to customize the behavior of this
71   * class:
72   * </p>
73   * <ul>
74   *  <li>{@link org.apache.http.params.CoreProtocolPNames#PROTOCOL_VERSION}</li>
75   *  <li>{@link org.apache.http.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
76   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
77   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USE_EXPECT_CONTINUE}</li>
78   *  <li>{@link org.apache.http.params.CoreProtocolPNames#WAIT_FOR_CONTINUE}</li>
79   *  <li>{@link org.apache.http.params.CoreProtocolPNames#USER_AGENT}</li>
80   *  <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
81   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
82   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li>
83   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_REUSEADDR}</li>
84   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
85   *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
86   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
87   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
88   *  <li>{@link org.apache.http.params.CoreConnectionPNames#STALE_CONNECTION_CHECK}</li>
89   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#FORCED_ROUTE}</li>
90   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
91   *  <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
92   *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
93   *  <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
94   *  <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
95   *  <li>{@link org.apache.http.client.params.ClientPNames#COOKIE_POLICY}</li>
96   *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_AUTHENTICATION}</li>
97   *  <li>{@link org.apache.http.client.params.ClientPNames#HANDLE_REDIRECTS}</li>
98   *  <li>{@link org.apache.http.client.params.ClientPNames#MAX_REDIRECTS}</li>
99   *  <li>{@link org.apache.http.client.params.ClientPNames#ALLOW_CIRCULAR_REDIRECTS}</li>
100  *  <li>{@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}</li>
101  *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}</li>
102  *  <li>{@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}</li>
103  *  <li>{@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}</li>
104  * </ul>
105  *
106  * @since 4.2
107  *
108  * @deprecated (4.3) use {@link HttpClientBuilder}
109  */
110 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
111 @Deprecated
112 public class SystemDefaultHttpClient extends DefaultHttpClient {
113 
114     public SystemDefaultHttpClient(final HttpParams params) {
115         super(null, params);
116     }
117 
118     public SystemDefaultHttpClient() {
119         super(null, null);
120     }
121 
122     @Override
123     protected ClientConnectionManager createClientConnectionManager() {
124         final PoolingClientConnectionManager connmgr = new PoolingClientConnectionManager(
125                 SchemeRegistryFactory.createSystemDefault());
126         String s = System.getProperty("http.keepAlive", "true");
127         if ("true".equalsIgnoreCase(s)) {
128             s = System.getProperty("http.maxConnections", "5");
129             final int max = Integer.parseInt(s);
130             connmgr.setDefaultMaxPerRoute(max);
131             connmgr.setMaxTotal(2 * max);
132         }
133         return connmgr;
134     }
135 
136     @Override
137     protected HttpRoutePlanner createHttpRoutePlanner() {
138         return new ProxySelectorRoutePlanner(getConnectionManager().getSchemeRegistry(),
139                 ProxySelector.getDefault());
140     }
141 
142     @Override
143     protected ConnectionReuseStrategy createConnectionReuseStrategy() {
144         final String s = System.getProperty("http.keepAlive", "true");
145         if ("true".equalsIgnoreCase(s)) {
146             return new DefaultConnectionReuseStrategy();
147         } else {
148             return new NoConnectionReuseStrategy();
149         }
150     }
151 
152 }