List of usage examples for org.apache.http.impl.client DefaultHttpClient getCredentialsProvider
public synchronized final CredentialsProvider getCredentialsProvider()
From source file:neembuu.release1.httpclient.NHttpClient.java
public static DefaultHttpClient getNewInstance() { DefaultHttpClient new_httpClient = null; new_httpClient = new DefaultHttpClient(); GlobalTestSettings.ProxySettings proxySettings = GlobalTestSettings.getGlobalProxySettings(); HttpContext context = new BasicHttpContext(); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", new PlainSocketFactory(), 80)); try {/*from w w w. j ava 2 s.c o m*/ KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keyStore), 8080)); } catch (Exception a) { a.printStackTrace(System.err); } context.setAttribute(ClientContext.SCHEME_REGISTRY, schemeRegistry); context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, new BasicScheme()/*file.httpClient.getAuthSchemes()*/); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, new_httpClient.getCookieSpecs()/*file.httpClient.getCookieSpecs()*/ ); BasicCookieStore basicCookieStore = new BasicCookieStore(); context.setAttribute(ClientContext.COOKIE_STORE, basicCookieStore/*file.httpClient.getCookieStore()*/); context.setAttribute(ClientContext.CREDS_PROVIDER, new BasicCredentialsProvider()/*file.httpClient.getCredentialsProvider()*/); HttpConnection hc = new DefaultHttpClientConnection(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, hc); //System.out.println(file.httpClient.getParams().getParameter("http.useragent")); HttpParams httpParams = new BasicHttpParams(); if (proxySettings != null) { AuthState as = new AuthState(); as.setCredentials(new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password)); as.setAuthScope(AuthScope.ANY); as.setAuthScheme(new BasicScheme()); httpParams.setParameter(ClientContext.PROXY_AUTH_STATE, as); httpParams.setParameter("http.proxy_host", new HttpHost(proxySettings.host, proxySettings.port)); } new_httpClient = new DefaultHttpClient( new SingleClientConnManager(httpParams/*file.httpClient.getParams()*/, schemeRegistry), httpParams/*file.httpClient.getParams()*/); if (proxySettings != null) { new_httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password)); } return new_httpClient; }
From source file:com.norconex.collector.http.client.impl.DefaultHttpClientInitializer.java
@Override public void initializeHTTPClient(DefaultHttpClient httpClient) { // Time out after 30 seconds. //TODO Make configurable. httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT); // Add support for FTP websites (FTP served by HTTP server). Scheme ftp = new Scheme("ftp", FTP_PORT, new PlainSocketFactory()); httpClient.getConnectionManager().getSchemeRegistry().register(ftp); //TODO make charset configurable instead since UTF-8 is not right // charset for URL specifications. It is used here to overcome // so invalid redirect errors, where the redirect target URL is not // URL-Encoded and has non-ascii values, and fails // (e.g. like ja.wikipedia.org). // Can consider a custom RedirectStrategy too if need be. httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "UTF-8"); if (StringUtils.isNotBlank(proxyHost)) { httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort)); if (StringUtils.isNotBlank(proxyUsername)) { httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword)); }/*from w w w .j av a 2 s . c om*/ } if (!cookiesDisabled) { httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); } if (AUTH_METHOD_FORM.equalsIgnoreCase(authMethod)) { authenticateUsingForm(httpClient); } else if (AUTH_METHOD_BASIC.equalsIgnoreCase(authMethod)) { setupBasicDigestAuth(httpClient); } else if (AUTH_METHOD_DIGEST.equalsIgnoreCase(authMethod)) { setupBasicDigestAuth(httpClient); } if (userAgent != null) { httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent); } }
From source file:com.soulgalore.crawler.guice.HttpClientProvider.java
/** * Get the client.//from w ww .j a va 2 s. com * * @return the client */ public HttpClient get() { final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(); cm.setMaxTotal(nrOfThreads); cm.setDefaultMaxPerRoute(maxToRoute); final DefaultHttpClient client = HTTPSFaker.getClientThatAllowAnyHTTPS(cm); client.getParams().setParameter("http.socket.timeout", socketTimeout); client.getParams().setParameter("http.connection.timeout", connectionTimeout); client.addRequestInterceptor(new RequestAcceptEncoding()); client.addResponseInterceptor(new ResponseContentEncoding()); CookieSpecFactory csf = new CookieSpecFactory() { public CookieSpec newInstance(HttpParams params) { return new BestMatchSpecWithURLErrorLog(); } }; client.getCookieSpecs().register("bestmatchwithurl", csf); client.getParams().setParameter(ClientPNames.COOKIE_POLICY, "bestmatchwithurl"); if (!"".equals(proxy)) { StringTokenizer token = new StringTokenizer(proxy, ":"); if (token.countTokens() == 3) { String proxyProtocol = token.nextToken(); String proxyHost = token.nextToken(); int proxyPort = Integer.parseInt(token.nextToken()); HttpHost proxy = new HttpHost(proxyHost, proxyPort, proxyProtocol); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } else System.err.println("Invalid proxy configuration: " + proxy); } if (auths.size() > 0) { for (Auth authObject : auths) { client.getCredentialsProvider().setCredentials( new AuthScope(authObject.getScope(), authObject.getPort()), new UsernamePasswordCredentials(authObject.getUserName(), authObject.getPassword())); } } return client; }
From source file:pl.psnc.synat.wrdz.common.https.HttpsClientHelper.java
/** * Gets HTTPS client that can authenticate in WRDZ modules. * /*from w w w . ja v a 2 s . c o m*/ * @param module * module that wants to be authenticated * @return HTTPS client */ public synchronized HttpClient getHttpsClient(WrdzModule module) { DefaultHttpClient httpClient = httpsClients.get(module); if (httpClient == null) { logger.debug("HTTPS client for module " + module.name() + " is not yet initialized"); try { SSLSocketFactory socketFactory; if (config.getHttpsVerifyHostname()) { socketFactory = new SSLSocketFactory(new TrustAllStrategy()); } else { socketFactory = new SSLSocketFactory(new TrustAllStrategy(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } Scheme scheme = new Scheme("https", 443, socketFactory); PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); connectionManager.getSchemeRegistry().register(scheme); String cipher = config.getModulesPassword(); byte[] key = SECRET.getBytes("utf-8"); Cipher c = Cipher.getInstance("AES"); SecretKeySpec k = new SecretKeySpec(key, "AES"); c.init(Cipher.DECRYPT_MODE, k); byte[] decrypted = c.doFinal(Base64.decodeBase64(cipher)); String password = new String(decrypted, "utf-8"); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(module.name(), password); httpClient = new DefaultHttpClient(connectionManager); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); httpsClients.put(module, httpClient); } catch (Exception e) { throw new WrdzRuntimeException(e.getMessage(), e); } } return httpClient; }
From source file:org.apache.manifoldcf.crawler.connectors.generic.GenericConnector.java
protected DefaultHttpClient getClient() throws ManifoldCFException { DefaultHttpClient cl = new DefaultHttpClient(); if (genericLogin != null && !genericLogin.isEmpty()) { try {/* w ww.j a va 2s. c o m*/ URL url = new URL(genericEntryPoint); Credentials credentials = new UsernamePasswordCredentials(genericLogin, genericPassword); cl.getCredentialsProvider().setCredentials( new AuthScope(url.getHost(), url.getPort() > 0 ? url.getPort() : 80, AuthScope.ANY_REALM), credentials); cl.addRequestInterceptor(new PreemptiveAuth(credentials), 0); } catch (MalformedURLException ex) { throw new ManifoldCFException("getClient exception: " + ex.getMessage(), ex); } } HttpConnectionParams.setConnectionTimeout(cl.getParams(), connectionTimeoutMillis); HttpConnectionParams.setSoTimeout(cl.getParams(), socketTimeoutMillis); return cl; }
From source file:com.ge.research.semtk.sparqlX.SparqlEndpointInterface.java
/** * Execute an auth query using GET (use should be rare - in cases where POST is not supported) * @return a JSONObject wrapping the results. in the event the results were tabular, they can be obtained in the JsonArray "@Table". if the results were a graph, use "@Graph" for json-ld * @throws Exception//from w w w .ja va 2s . c om */ @SuppressWarnings("unused") private JSONObject executeQueryAuthGet(String queryAndUrl, SparqlResultTypes resultType) throws Exception { if (resultType == null) { resultType = getDefaultResultType(); } DefaultHttpClient httpclient = new DefaultHttpClient(); //ResponseHandler<String> responseHandler = new BasicResponseHandler(); System.err.println("the server name was " + this.server); System.err.println("the port id was " + this.port); System.err.println("the user name was " + "SPARQL/" + this.userName); System.err.println("the password was " + this.password); System.err.println(queryAndUrl); httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.userName, this.password)); String[] serverNoProtocol = this.server.split("://"); System.err.println("the new server name is: " + serverNoProtocol[1]); HttpHost targetHost = new HttpHost(serverNoProtocol[1], Integer.valueOf(this.port), "http"); DigestScheme digestAuth = new DigestScheme(); AuthCache authCache = new BasicAuthCache(); digestAuth.overrideParamter("realm", "SPARQL"); // Suppose we already know the expected nonce value digestAuth.overrideParamter("nonce", "whatever"); authCache.put(targetHost, digestAuth); BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet(queryAndUrl); String resultsFormat = this.getContentType(resultType); httpget.addHeader("Accept", resultsFormat); System.out.println("executing request" + httpget.getRequestLine()); // String responseTxt = httpclient.execute(httpget, responseHandler); HttpResponse response_http = httpclient.execute(targetHost, httpget, localcontext); HttpEntity entity = response_http.getEntity(); String responseTxt = EntityUtils.toString(entity, "UTF-8"); // some diagnostic output if (responseTxt == null) { System.err.println("the response text was null!"); } if (responseTxt.trim().isEmpty()) { handleEmptyResponse(); // implementation-specific behavior } if (responseTxt.length() < 100) { System.err.println("SparqlEndpointInterface received: " + responseTxt); } else { System.err.println("SparqlEndpointInterface received: " + responseTxt.substring(0, 99) + "... (" + responseTxt.length() + " chars)"); } JSONObject resp; try { resp = (JSONObject) new JSONParser().parse(responseTxt); } catch (Exception e) { throw new Exception("Cannot parse query result into JSON: " + responseTxt); } if (resp == null) { System.err.println("the response could not be transformed into json"); if (responseTxt.contains("Error")) { throw new Exception(responseTxt); } return null; } else { JSONObject procResp = getResultsFromResponse(resp, resultType); return procResp; } }