List of usage examples for org.apache.http.impl.client DefaultHttpClient getParams
public synchronized final HttpParams getParams()
From source file:net.yacy.cora.federate.solr.instance.RemoteInstance.java
/** * @param solraccount eventual user name used to authenticate on the target Solr * @param solraccount eventual password used to authenticate on the target Solr * @param trustSelfSignedCertificates when true, https connections to an host providing a self-signed certificate are accepted * @param maxBytesPerReponse//w w w . j av a2 s. c o m * maximum acceptable decompressed size in bytes for a response from * the remote Solr server. Negative value or Long.MAX_VALUE means no * limit. * @return a new apache HttpClient instance usable as a custom http client by SolrJ */ private static HttpClient buildCustomHttpClient(final int timeout, final MultiProtocolURL u, final String solraccount, final String solrpw, final String host, final boolean trustSelfSignedCertificates, final long maxBytesPerResponse) { /* Important note : use of deprecated Apache classes is required because SolrJ still use them internally (see HttpClientUtil). * Upgrade only when Solr implementation will become compatible */ org.apache.http.impl.client.DefaultHttpClient result = new org.apache.http.impl.client.DefaultHttpClient( CONNECTION_MANAGER) { @Override protected HttpContext createHttpContext() { HttpContext context = super.createHttpContext(); AuthCache authCache = new org.apache.http.impl.client.BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost targetHost = new HttpHost(u.getHost(), u.getPort(), u.getProtocol()); authCache.put(targetHost, basicAuth); context.setAttribute(org.apache.http.client.protocol.HttpClientContext.AUTH_CACHE, authCache); if (trustSelfSignedCertificates && SCHEME_REGISTRY != null) { context.setAttribute(ClientContext.SCHEME_REGISTRY, SCHEME_REGISTRY); } this.setHttpRequestRetryHandler( new org.apache.http.impl.client.DefaultHttpRequestRetryHandler(0, false)); // no retries needed; we expect connections to fail; therefore we should not retry return context; } }; org.apache.http.params.HttpParams params = result.getParams(); /* Set the maximum time to establish a connection to the remote server */ org.apache.http.params.HttpConnectionParams.setConnectionTimeout(params, timeout); /* Set the maximum time between data packets reception one a connection has been established */ org.apache.http.params.HttpConnectionParams.setSoTimeout(params, timeout); /* Set the maximum time to get a connection from the shared connections pool */ HttpClientParams.setConnectionManagerTimeout(params, timeout); result.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(final HttpRequest request, final HttpContext context) throws IOException { if (!request.containsHeader(HeaderFramework.ACCEPT_ENCODING)) request.addHeader(HeaderFramework.ACCEPT_ENCODING, HeaderFramework.CONTENT_ENCODING_GZIP); if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) request.addHeader(HTTP.CONN_DIRECTIVE, "close"); // prevent CLOSE_WAIT } }); result.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext context) throws IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase(HeaderFramework.CONTENT_ENCODING_GZIP)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } } }); if (solraccount != null && !solraccount.isEmpty()) { org.apache.http.impl.client.BasicCredentialsProvider credsProvider = new org.apache.http.impl.client.BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(host, AuthScope.ANY_PORT), new UsernamePasswordCredentials(solraccount, solrpw)); result.setCredentialsProvider(credsProvider); } if (maxBytesPerResponse >= 0 && maxBytesPerResponse < Long.MAX_VALUE) { /* * Add in last position the eventual interceptor limiting the response size, so * that this is the decompressed amount of bytes that is considered */ result.addResponseInterceptor(new StrictSizeLimitResponseInterceptor(maxBytesPerResponse), result.getResponseInterceptorCount()); } return result; }
From source file:gov.nih.nci.nbia.StandaloneDMV1.java
private static List<String> connectAndReadFromURL(URL url, String fileName) { List<String> data = null; DefaultHttpClient httpClient = null; TrustStrategy easyStrategy = new TrustStrategy() { @Override/* w w w. ja va 2 s . c o m*/ public boolean isTrusted(X509Certificate[] certificate, String authType) throws CertificateException { return true; } }; try { // SSLContext sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that trusts everything // sslContext.init(null, new TrustManager[] { new // EasyX509TrustManager(null)}, null); SSLSocketFactory sslsf = new SSLSocketFactory(easyStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme httpsScheme = new Scheme("https", 443, sslsf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); ClientConnectionManager ccm = new ThreadSafeClientConnManager(schemeRegistry); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 50000); HttpConnectionParams.setSoTimeout(httpParams, new Integer(12000)); httpClient = new DefaultHttpClient(ccm, httpParams); httpClient.setRoutePlanner(new ProxySelectorRoutePlanner(schemeRegistry, ProxySelector.getDefault())); // // Additions by lrt for tcia - // // attempt to reduce errors going through a Coyote Point // Equalizer load balance switch httpClient.getParams().setParameter("http.socket.timeout", new Integer(12000)); httpClient.getParams().setParameter("http.socket.receivebuffer", new Integer(16384)); httpClient.getParams().setParameter("http.tcp.nodelay", true); httpClient.getParams().setParameter("http.connection.stalecheck", false); // // end lrt additions HttpPost httpPostMethod = new HttpPost(url.toString()); List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>(); postParams.add(new BasicNameValuePair("serverjnlpfileloc", fileName)); UrlEncodedFormEntity query = new UrlEncodedFormEntity(postParams); httpPostMethod.setEntity(query); HttpResponse response = httpClient.execute(httpPostMethod); // int responseCode = response.getStatusLine().getStatusCode(); // System.out.println("Response code for requesting datda file: " + // responseCode); InputStream inputStream = response.getEntity().getContent(); data = IOUtils.readLines(inputStream); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyStoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnrecoverableKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (httpClient != null) { httpClient.getConnectionManager().shutdown(); } } return data; }
From source file:nl.esciencecenter.octopus.webservice.JobLauncherServiceTest.java
@Test public void testMacifyHttpClient() throws URISyntaxException { // FIXME Waiting for https://github.com/NLeSC/octopus/issues/38 to be resolved JobLauncherConfiguration config = sampleConfiguration(); DefaultHttpClient httpClient = new DefaultHttpClient(); JobLauncherService.macifyHttpClient(httpClient, config.getMacs()); assertTrue("MAC Registered auth scheme", httpClient.getAuthSchemes().getSchemeNames().contains("mac")); MacCredential expected_creds = config.getMacs().get(0); AuthScope authscope = expected_creds.getAuthScope(); Credentials creds = httpClient.getCredentialsProvider().getCredentials(authscope); assertEquals(expected_creds, creds); List<String> authSchemes = Collections.unmodifiableList(Arrays.asList(new String[] { MacScheme.SCHEME_NAME, AuthPolicy.SPNEGO, AuthPolicy.KERBEROS, AuthPolicy.NTLM, AuthPolicy.DIGEST, AuthPolicy.BASIC })); assertEquals(authSchemes, httpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF)); }
From source file:nl.esciencecenter.osmium.JobLauncherServiceTest.java
@Test public void testMacifyHttpClient() throws URISyntaxException { // FIXME Waiting for https://github.com/NLeSC/xenon/issues/38 to be resolved JobLauncherConfiguration config = sampleConfiguration(); DefaultHttpClient httpClient = new DefaultHttpClient(); JobLauncherService.macifyHttpClient(httpClient, config.getMacs()); assertTrue("MAC Registered auth scheme", httpClient.getAuthSchemes().getSchemeNames().contains("mac")); MacCredential expected_creds = config.getMacs().get(0); AuthScope authscope = expected_creds.getAuthScope(); Credentials creds = httpClient.getCredentialsProvider().getCredentials(authscope); assertEquals(expected_creds, creds); List<String> authSchemes = Collections.unmodifiableList(Arrays.asList(new String[] { MacScheme.SCHEME_NAME, AuthPolicy.SPNEGO, AuthPolicy.KERBEROS, AuthPolicy.NTLM, AuthPolicy.DIGEST, AuthPolicy.BASIC })); assertEquals(authSchemes, httpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF)); }
From source file:com.nesscomputing.httpclient.factory.httpclient4.ApacheHttpClient4Factory.java
private <T> void contributeParameters(final DefaultHttpClient httpClient, final HttpRequestBase httpRequest, final HttpClientRequest<T> httpClientRequest) { final Map<String, Object> parameters = httpClientRequest.getParameters(); if (parameters != null && !parameters.isEmpty()) { HttpParams clientParams = httpClient.getParams(); HttpParams requestParams = httpRequest.getParams(); for (Map.Entry<String, Object> entry : parameters.entrySet()) { clientParams.setParameter(entry.getKey(), entry.getValue()); requestParams.setParameter(entry.getKey(), entry.getValue()); }//from w ww . ja va 2 s . c o m } }
From source file:com.swater.meimeng.activity.oomimg.ImageCache.java
/** * Gets an instance of AndroidHttpClient if the devices has it (it was introduced in 2.2), or * falls back on a http client that should work reasonably well. * * @return a working instance of an HttpClient */// w w w. j ava 2 s . co m private HttpClient getHttpClient() { HttpClient ahc; try { final Class<?> ahcClass = Class.forName("android.net.http.AndroidHttpClient"); final Method newInstance = ahcClass.getMethod("newInstance", String.class); ahc = (HttpClient) newInstance.invoke(null, "ImageCache"); } catch (final ClassNotFoundException e) { DefaultHttpClient dhc = new DefaultHttpClient(); final HttpParams params = dhc.getParams(); dhc = null; params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20 * 1000); final SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry); ahc = new DefaultHttpClient(manager, params); } catch (final NoSuchMethodException e) { final RuntimeException re = new RuntimeException("Programming error"); re.initCause(e); throw re; } catch (final IllegalAccessException e) { final RuntimeException re = new RuntimeException("Programming error"); re.initCause(e); throw re; } catch (final InvocationTargetException e) { final RuntimeException re = new RuntimeException("Programming error"); re.initCause(e); throw re; } return ahc; }
From source file:gov.nih.nci.nbia.StandaloneDMV2.java
private static List<String> connectAndReadFromURL(URL url, String fileName, String userId, String passWd) { List<String> data = null; DefaultHttpClient httpClient = null; TrustStrategy easyStrategy = new TrustStrategy() { @Override/* w ww.j a va 2s . c om*/ public boolean isTrusted(X509Certificate[] certificate, String authType) throws CertificateException { return true; } }; try { // SSLContext sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that trusts everything // sslContext.init(null, new TrustManager[] { new // EasyX509TrustManager(null)}, null); SSLSocketFactory sslsf = new SSLSocketFactory(easyStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme httpsScheme = new Scheme("https", 443, sslsf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); ClientConnectionManager ccm = new ThreadSafeClientConnManager(schemeRegistry); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 50000); HttpConnectionParams.setSoTimeout(httpParams, new Integer(12000)); httpClient = new DefaultHttpClient(ccm, httpParams); httpClient.setRoutePlanner(new ProxySelectorRoutePlanner(schemeRegistry, ProxySelector.getDefault())); // // Additions by lrt for tcia - // // attempt to reduce errors going through a Coyote Point // Equalizer load balance switch httpClient.getParams().setParameter("http.socket.timeout", new Integer(12000)); httpClient.getParams().setParameter("http.socket.receivebuffer", new Integer(16384)); httpClient.getParams().setParameter("http.tcp.nodelay", true); httpClient.getParams().setParameter("http.connection.stalecheck", false); // // end lrt additions HttpPost httpPostMethod = new HttpPost(url.toString()); List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>(); postParams.add(new BasicNameValuePair("serverManifestLoc", fileName)); if (userId != null && passWd != null) { postParams.add(new BasicNameValuePair("userId", userId)); httpPostMethod.addHeader("password", passWd); } UrlEncodedFormEntity query = new UrlEncodedFormEntity(postParams); httpPostMethod.setEntity(query); HttpResponse response = httpClient.execute(httpPostMethod); int responseCode = response.getStatusLine().getStatusCode(); // System.out.println("!!!!!Response code for requesting datda file: // " + responseCode); if (responseCode != HttpURLConnection.HTTP_OK) { return null; } else { InputStream inputStream = response.getEntity().getContent(); data = IOUtils.readLines(inputStream); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyStoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnrecoverableKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (httpClient != null) { httpClient.getConnectionManager().shutdown(); } } return data; }
From source file:com.googlecode.noweco.webmail.httpclient.UnsecureHttpClientFactory.java
public DefaultHttpClient createUnsecureHttpClient(final HttpHost proxy) { DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager()); SchemeRegistry schemeRegistry = httpclient.getConnectionManager().getSchemeRegistry(); schemeRegistry.unregister("https"); try {// w ww. j a v a 2 s.co m SSLContext instance = SSLContext.getInstance("TLS"); TrustManager tm = UnsecureX509TrustManager.INSTANCE; instance.init(null, new TrustManager[] { tm }, null); schemeRegistry.register(new Scheme("https", 443, new SSLSocketFactory(instance, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER))); } catch (Exception e) { throw new RuntimeException("TLS issue", e); } httpclient.removeResponseInterceptorByClass(ResponseProcessCookies.class); httpclient.addResponseInterceptor(new UnsecureResponseProcessCookies()); HttpParams params = httpclient.getParams(); if (proxy != null) { ConnRouteParams.setDefaultProxy(params, proxy); } HttpConnectionParams.setSoTimeout(params, 7000); return httpclient; }
From source file:com.android.tools.idea.sdk.remote.internal.UrlOpener.java
@NonNull private static Pair<InputStream, HttpResponse> openWithHttpClient(@NonNull String url, @NonNull ITaskMonitor monitor, Header[] inHeaders) throws IOException, CanceledByUserException { UserCredentials result = null;/*from ww w.j a v a 2 s . c o m*/ String realm = null; HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, sConnectionTimeoutMs); HttpConnectionParams.setSoTimeout(params, sSocketTimeoutMs); // use the simple one final DefaultHttpClient httpClient = new DefaultHttpClient(params); // create local execution context HttpContext localContext = new BasicHttpContext(); final HttpGet httpGet = new HttpGet(url); if (inHeaders != null) { for (Header header : inHeaders) { httpGet.addHeader(header); } } // retrieve local java configured network in case there is the need to // authenticate a proxy ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); httpClient.setRoutePlanner(routePlanner); // Set preference order for authentication options. // In particular, we don't add AuthPolicy.SPNEGO, which is given preference over NTLM in // servers that support both, as it is more secure. However, we don't seem to handle it // very well, so we leave it off the list. // See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html for // more info. List<String> authpref = new ArrayList<String>(); authpref.add(AuthPolicy.BASIC); authpref.add(AuthPolicy.DIGEST); authpref.add(AuthPolicy.NTLM); httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref); httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref); if (DEBUG) { try { URI uri = new URI(url); ProxySelector sel = routePlanner.getProxySelector(); if (sel != null && uri.getScheme().startsWith("httP")) { //$NON-NLS-1$ List<Proxy> list = sel.select(uri); System.out.printf("SdkLib.UrlOpener:\n Connect to: %s\n Proxy List: %s\n", //$NON-NLS-1$ url, list == null ? "(null)" : Arrays.toString(list.toArray()));//$NON-NLS-1$ } } catch (Exception e) { System.out.printf("SdkLib.UrlOpener: Failed to get proxy info for %s: %s\n", //$NON-NLS-1$ url, e.toString()); } } boolean trying = true; // loop while the response is being fetched while (trying) { // connect and get status code HttpResponse response = httpClient.execute(httpGet, localContext); int statusCode = response.getStatusLine().getStatusCode(); if (DEBUG) { System.out.printf(" Status: %d\n", statusCode); //$NON-NLS-1$ } // check whether any authentication is required AuthState authenticationState = null; if (statusCode == HttpStatus.SC_UNAUTHORIZED) { // Target host authentication required authenticationState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE); } if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { // Proxy authentication required authenticationState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE); } if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NOT_MODIFIED) { // in case the status is OK and there is a realm and result, // cache it if (realm != null && result != null) { sRealmCache.put(realm, result); } } // there is the need for authentication if (authenticationState != null) { // get scope and realm AuthScope authScope = authenticationState.getAuthScope(); // If the current realm is different from the last one it means // a pass was performed successfully to the last URL, therefore // cache the last realm if (realm != null && !realm.equals(authScope.getRealm())) { sRealmCache.put(realm, result); } realm = authScope.getRealm(); // in case there is cache for this Realm, use it to authenticate if (sRealmCache.containsKey(realm)) { result = sRealmCache.get(realm); } else { // since there is no cache, request for login and password result = monitor.displayLoginCredentialsPrompt("Site Authentication", "Please login to the following domain: " + realm + "\n\nServer requiring authentication:\n" + authScope.getHost()); if (result == null) { throw new CanceledByUserException("User canceled login dialog."); } } // retrieve authentication data String user = result.getUserName(); String password = result.getPassword(); String workstation = result.getWorkstation(); String domain = result.getDomain(); // proceed in case there is indeed a user if (user != null && user.length() > 0) { Credentials credentials = new NTCredentials(user, password, workstation, domain); httpClient.getCredentialsProvider().setCredentials(authScope, credentials); trying = true; } else { trying = false; } } else { trying = false; } HttpEntity entity = response.getEntity(); if (entity != null) { if (trying) { // in case another pass to the Http Client will be performed, close the entity. entity.getContent().close(); } else { // since no pass to the Http Client is needed, retrieve the // entity's content. // Note: don't use something like a BufferedHttpEntity since it would consume // all content and store it in memory, resulting in an OutOfMemory exception // on a large download. InputStream is = new FilterInputStream(entity.getContent()) { @Override public void close() throws IOException { // Since Http Client is no longer needed, close it. // Bug #21167: we need to tell http client to shutdown // first, otherwise the super.close() would continue // downloading and not return till complete. httpClient.getConnectionManager().shutdown(); super.close(); } }; HttpResponse outResponse = new BasicHttpResponse(response.getStatusLine()); outResponse.setHeaders(response.getAllHeaders()); outResponse.setLocale(response.getLocale()); return Pair.of(is, outResponse); } } else if (statusCode == HttpStatus.SC_NOT_MODIFIED) { // It's ok to not have an entity (e.g. nothing to download) for a 304 HttpResponse outResponse = new BasicHttpResponse(response.getStatusLine()); outResponse.setHeaders(response.getAllHeaders()); outResponse.setLocale(response.getLocale()); return Pair.of(null, outResponse); } } // We get here if we did not succeed. Callers do not expect a null result. httpClient.getConnectionManager().shutdown(); throw new FileNotFoundException(url); }