📄 httpclient.java
字号:
else { return new ByteArrayBuffer(size); } } /* ------------------------------------------------------------ */ public int getMaxConnectionsPerAddress() { return _maxConnectionsPerAddress; } /* ------------------------------------------------------------ */ public void setMaxConnectionsPerAddress(int maxConnectionsPerAddress) { _maxConnectionsPerAddress=maxConnectionsPerAddress; } /* ------------------------------------------------------------ */ protected void doStart() throws Exception { super.doStart(); _timeoutQ.setNow(); _timeoutQ.setDuration(_timeout); if(_threadPool==null) { QueuedThreadPool pool = new QueuedThreadPool(); pool.setMaxThreads(16); pool.setDaemon(true); pool.setName("HttpClient"); _threadPool=pool; } if (_threadPool instanceof LifeCycle) { ((LifeCycle)_threadPool).start(); } if (_connectorType==CONNECTOR_SELECT_CHANNEL) { _connector=new SelectConnector(this); } else { _connector=new SocketConnector(this); } _connector.start(); _threadPool.dispatch(new Runnable() { public void run() { while (isRunning()) { _timeoutQ.setNow(); _timeoutQ.tick(); try { Thread.sleep(1000); } catch (InterruptedException e) { Log.ignore(e); } } } }); } /* ------------------------------------------------------------ */ protected void doStop() throws Exception { _connector.stop(); _connector=null; if (_threadPool instanceof LifeCycle) { ((LifeCycle)_threadPool).stop(); } for (HttpDestination destination : _destinations.values()) { destination.close(); } _timeoutQ.cancelAll(); super.doStop(); } /* ------------------------------------------------------------ */ interface Connector extends LifeCycle { public void startConnection(HttpDestination destination) throws IOException; } /** * if a keystore location has been provided then client will attempt to use it as the keystore, * otherwise we simply ignore certificates and run with a loose ssl context. * * @return * @throws IOException */ protected SSLContext getSSLContext() throws IOException { if (_sslContext == null) { if (_keyStoreLocation == null) { _sslContext = getLooseSSLContext(); } else { _sslContext = getStrictSSLContext(); } } return _sslContext; } protected SSLContext getStrictSSLContext() throws IOException { try { if (_trustStoreLocation==null) { _trustStoreLocation=_keyStoreLocation; _trustStoreType=_keyStoreType; } KeyManager[] keyManagers=null; InputStream keystoreInputStream = null; keystoreInputStream= Resource.newResource(_keyStoreLocation).getInputStream(); KeyStore keyStore=KeyStore.getInstance(_keyStoreType); keyStore.load(keystoreInputStream,_keyStorePassword==null?null:_keyStorePassword.toString().toCharArray()); KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(_keyManagerAlgorithm); keyManagerFactory.init(keyStore,_keyManagerPassword==null?null:_keyManagerPassword.toString().toCharArray()); keyManagers=keyManagerFactory.getKeyManagers(); TrustManager[] trustManagers=null; InputStream truststoreInputStream = null; truststoreInputStream = Resource.newResource(_trustStoreLocation).getInputStream(); KeyStore trustStore=KeyStore.getInstance(_trustStoreType); trustStore.load(truststoreInputStream,_trustStorePassword==null?null:_trustStorePassword.toString().toCharArray()); TrustManagerFactory trustManagerFactory=TrustManagerFactory.getInstance(_trustManagerAlgorithm); trustManagerFactory.init(trustStore); trustManagers=trustManagerFactory.getTrustManagers(); SecureRandom secureRandom=_secureRandomAlgorithm==null?null:SecureRandom.getInstance(_secureRandomAlgorithm); SSLContext context=_provider==null?SSLContext.getInstance(_protocol):SSLContext.getInstance(_protocol,_provider); context.init(keyManagers,trustManagers,secureRandom); return context; } catch ( Exception e ) { Log.debug(e); throw new IOException( "error generating ssl context for " + _keyStoreLocation + " " + e.getMessage() ); } } protected SSLContext getLooseSSLContext() throws IOException { // Create a trust manager that does not validate certificate // chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType ) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType ) { } } }; HostnameVerifier hostnameVerifier = new HostnameVerifier() { public boolean verify( String urlHostName, SSLSession session ) { Log.warn( "Warning: URL Host: " + urlHostName + " vs." + session.getPeerHost() ); return true; } }; // Install the all-trusting trust manager try { // TODO real trust manager SSLContext sslContext = SSLContext.getInstance( "SSL" ); sslContext.init( null, trustAllCerts, new java.security.SecureRandom() ); return sslContext; } catch ( Exception e ) { Log.debug(e); throw new IOException( "issue ignoring certs" ); } } /* ------------------------------------------------------------ */ /** * @return the period in milliseconds a {@link HttpConnection} can be idle for before it is closed. */ public long getIdleTimeout() { return _idleTimeout; } /* ------------------------------------------------------------ */ /** * @param ms the period in milliseconds a {@link HttpConnection} can be idle for before it is closed. */ public void setIdleTimeout(long ms) { _idleTimeout=ms; } /* ------------------------------------------------------------ */ public int getSoTimeout() { return _soTimeout; } /* ------------------------------------------------------------ */ public void setSoTimeout(int so) { _soTimeout = so; } /* ------------------------------------------------------------ */ /** * @return the period in ms that an exchange will wait for a response from the server. */ public long getTimeout() { return _timeout; } /* ------------------------------------------------------------ */ /** * @param ms the period in ms that an exchange will wait for a response from the server. */ public void setTimeout(long ms) { _timeout=ms; } /* ------------------------------------------------------------ */ public Address getProxy() { return _proxy; } /* ------------------------------------------------------------ */ public void setProxy(Address proxy) { this._proxy = proxy; } /* ------------------------------------------------------------ */ public Authorization getProxyAuthentication() { return _proxyAuthentication; } /* ------------------------------------------------------------ */ public void setProxyAuthentication(Authorization authentication) { _proxyAuthentication = authentication; } /* ------------------------------------------------------------ */ public boolean isProxied() { return this._proxy!=null; } /* ------------------------------------------------------------ */ public Set<String> getNoProxy() { return _noProxy; } /* ------------------------------------------------------------ */ public void setNoProxy(Set<String> noProxyAddresses) { _noProxy = noProxyAddresses; } /* ------------------------------------------------------------ */ public int maxRetries() { return _maxRetries; } /* ------------------------------------------------------------ */ public void setMaxRetries( int retries ) { _maxRetries = retries; } /* ------------------------------------------------------------ */ public String getTrustStoreLocation() { return _trustStoreLocation; } /* ------------------------------------------------------------ */ public void setTrustStoreLocation(String trustStoreLocation) { this._trustStoreLocation = trustStoreLocation; } /* ------------------------------------------------------------ */ public String getKeyStoreLocation() { return _keyStoreLocation; } /* ------------------------------------------------------------ */ public void setKeyStoreLocation(String keyStoreLocation) { this._keyStoreLocation = keyStoreLocation; } /* ------------------------------------------------------------ */ public void setKeyStorePassword(String _keyStorePassword) { this._keyStorePassword = _keyStorePassword; } /* ------------------------------------------------------------ */ public void setKeyManagerPassword(String _keyManagerPassword) { this._keyManagerPassword = _keyManagerPassword; } /* ------------------------------------------------------------ */ public void setTrustStorePassword(String _trustStorePassword) { this._trustStorePassword = _trustStorePassword; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -