List of usage examples for org.apache.commons.httpclient HostConfiguration HostConfiguration
public HostConfiguration()
From source file:org.apache.cocoon.generation.WebServiceProxyGenerator.java
/** * Create one per client session. /* w w w . ja v a2s . com*/ */ protected HttpClient getHttpClient() throws ProcessingException { URI uri = null; String host = null; Request request = ObjectModelHelper.getRequest(objectModel); Session session = request.getSession(true); HttpClient httpClient = null; if (session != null) { httpClient = (HttpClient) session.getAttribute(HTTP_CLIENT); } if (httpClient == null) { httpClient = new HttpClient(); HostConfiguration config = httpClient.getHostConfiguration(); if (config == null) { config = new HostConfiguration(); } /* TODO: fixme! * When the specified source sent to the wsproxy is not "http" * (e.g. "cocoon:/"), the HttpClient throws an exception. Does the source * here need to be resolved before being set in the HostConfiguration? */ try { uri = new URI(this.source); host = uri.getHost(); config.setHost(uri); } catch (URIException ex) { throw new ProcessingException("URI format error: " + ex, ex); } // Check the http.nonProxyHosts to see whether or not the current // host needs to be served through the proxy server. boolean proxiableHost = true; String nonProxyHosts = System.getProperty("http.nonProxyHosts"); if (nonProxyHosts != null) { StringTokenizer tok = new StringTokenizer(nonProxyHosts, "|"); while (tok.hasMoreTokens()) { String nonProxiableHost = tok.nextToken().trim(); // XXX is there any other characters that need to be // escaped? nonProxiableHost = StringUtils.replace(nonProxiableHost, ".", "\\."); nonProxiableHost = StringUtils.replace(nonProxiableHost, "*", ".*"); // XXX do we want .example.com to match // computer.example.com? it seems to be a very common // idiom for the nonProxyHosts, in that case then we want // to change "^" to "^.*" RE re = null; try { re = new RE("^" + nonProxiableHost + "$"); } catch (Exception ex) { throw new ProcessingException("Regex syntax error: " + ex, ex); } if (re.match(host)) { proxiableHost = false; break; } } } if (proxiableHost && System.getProperty("http.proxyHost") != null) { String proxyHost = System.getProperty("http.proxyHost"); int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort")); config.setProxy(proxyHost, proxyPort); } httpClient.setHostConfiguration(config); session.setAttribute(HTTP_CLIENT, httpClient); } return httpClient; }
From source file:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java
/** * Download link and have it be the response. * @param req the http request// w ww . ja va 2s . com * @param resp the http response * @param link the link to download * @param c the cookie to set if any * @throws IOException on any error. */ private static void proxyLink(HttpServletRequest req, HttpServletResponse resp, URI link, Cookie c, String proxyHost) throws IOException { org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(link.toString(), false); HttpClientParams params = new HttpClientParams(); params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpClient client = new HttpClient(params); // Make sure we send the request from the proxy address in the config // since that is what the AM filter checks against. IP aliasing or // similar could cause issues otherwise. HostConfiguration config = new HostConfiguration(); InetAddress localAddress = InetAddress.getByName(proxyHost); if (LOG.isDebugEnabled()) { LOG.debug("local InetAddress for proxy host: " + localAddress.toString()); } config.setLocalAddress(localAddress); HttpMethod method = new GetMethod(uri.getEscapedURI()); @SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); if (passThroughHeaders.contains(name)) { String value = req.getHeader(name); LOG.debug("REQ HEADER: " + name + " : " + value); method.setRequestHeader(name, value); } } String user = req.getRemoteUser(); if (user != null && !user.isEmpty()) { method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII")); } OutputStream out = resp.getOutputStream(); try { resp.setStatus(client.executeMethod(config, method)); for (Header header : method.getResponseHeaders()) { resp.setHeader(header.getName(), header.getValue()); } if (c != null) { resp.addCookie(c); } InputStream in = method.getResponseBodyAsStream(); if (in != null) { IOUtils.copyBytes(in, out, 4096, true); } } finally { method.releaseConnection(); } }
From source file:org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.java
public RepositoryServiceImpl(String uri, IdFactory idFactory, NameFactory nameFactory, PathFactory pathFactory, QValueFactory qValueFactory) throws RepositoryException { if (uri == null || "".equals(uri)) { throw new RepositoryException("Invalid repository uri '" + uri + "'."); }//from w w w . ja v a 2 s . c om if (idFactory == null || qValueFactory == null) { throw new RepositoryException("IdFactory and QValueFactory may not be null."); } this.idFactory = idFactory; this.nameFactory = nameFactory; this.pathFactory = pathFactory; this.qValueFactory = qValueFactory; try { domFactory = DomUtil.BUILDER_FACTORY.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { throw new RepositoryException(e); } try { URI repositoryUri = new URI((uri.endsWith("/")) ? uri : uri + "/", true); hostConfig = new HostConfiguration(); hostConfig.setHost(repositoryUri); nsCache = new NamespaceCache(); uriResolver = new URIResolverImpl(repositoryUri, this, domFactory); NamePathResolver resolver = new NamePathResolverImpl(nsCache); valueFactory = new ValueFactoryQImpl(qValueFactory, resolver); } catch (URIException e) { throw new RepositoryException(e); } connectionManager = new MultiThreadedHttpConnectionManager(); }
From source file:org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.java
/** * Creates a new instance of this repository service. * * @param uri The server uri.//from ww w. ja v a 2 s . c om * @param idFactory The id factory. * @param nameFactory The name factory. * @param pathFactory The path factory. * @param qValueFactory The value factory. * @param itemInfoCacheSize The size of the item info cache. * @param maximumHttpConnections A int >0 defining the maximum number of * connections per host to be configured on * {@link HttpConnectionManagerParams#setDefaultMaxConnectionsPerHost(int)}. * @throws RepositoryException If an error occurs. */ public RepositoryServiceImpl(String uri, IdFactory idFactory, NameFactory nameFactory, PathFactory pathFactory, QValueFactory qValueFactory, int itemInfoCacheSize, int maximumHttpConnections) throws RepositoryException { if (uri == null || "".equals(uri)) { throw new RepositoryException("Invalid repository uri '" + uri + "'."); } if (idFactory == null || qValueFactory == null) { throw new RepositoryException("IdFactory and QValueFactory may not be null."); } this.idFactory = idFactory; this.nameFactory = nameFactory; this.pathFactory = pathFactory; this.qValueFactory = qValueFactory; this.itemInfoCacheSize = itemInfoCacheSize; try { URI repositoryUri = computeRepositoryUri(uri); hostConfig = new HostConfiguration(); hostConfig.setHost(repositoryUri); nsCache = new NamespaceCache(); uriResolver = new URIResolverImpl(repositoryUri, this, DomUtil.createDocument()); NamePathResolver resolver = new NamePathResolverImpl(nsCache); valueFactory = new ValueFactoryQImpl(qValueFactory, resolver); } catch (URIException e) { throw new RepositoryException(e); } catch (ParserConfigurationException e) { throw new RepositoryException(e); } connectionManager = new MultiThreadedHttpConnectionManager(); if (maximumHttpConnections > 0) { HttpConnectionManagerParams connectionParams = connectionManager.getParams(); connectionParams.setDefaultMaxConnectionsPerHost(maximumHttpConnections); connectionParams.setMaxTotalConnections(maximumHttpConnections); } // This configuration of the clients cache assumes that the level of // concurrency on this map will be equal to the default number of maximum // connections allowed on the httpClient level. // TODO: review again int concurrencyLevel = MAX_CONNECTIONS_DEFAULT; int initialCapacity = MAX_CONNECTIONS_DEFAULT; if (maximumHttpConnections > 0) { concurrencyLevel = maximumHttpConnections; initialCapacity = maximumHttpConnections; } clients = new ConcurrentHashMap<Object, HttpClient>(concurrencyLevel, .75f, initialCapacity); }
From source file:org.apache.jmeter.protocol.http.sampler.HTTPHC3Impl.java
/** * Returns an <code>HttpConnection</code> fully ready to attempt * connection. This means it sets the request method (GET or POST), headers, * cookies, and authorization for the URL request. * <p>/*from w w w . ja v a 2 s .c om*/ * The request infos are saved into the sample result if one is provided. * * @param u * <code>URL</code> of the URL request * @param httpMethod * GET/PUT/HEAD etc * @param res * sample result to save request infos to * @return <code>HttpConnection</code> ready for .connect * @exception IOException * if an I/O Exception occurs */ protected HttpClient setupConnection(URL u, HttpMethodBase httpMethod, HTTPSampleResult res) throws IOException { String urlStr = u.toString(); org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(urlStr, false); String schema = uri.getScheme(); if ((schema == null) || (schema.length() == 0)) { schema = HTTPConstants.PROTOCOL_HTTP; } final boolean isHTTPS = HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(schema); if (isHTTPS) { SSLManager.getInstance(); // ensure the manager is initialised // we don't currently need to do anything further, as this sets the default https protocol } Protocol protocol = Protocol.getProtocol(schema); String host = uri.getHost(); int port = uri.getPort(); /* * We use the HostConfiguration as the key to retrieve the HttpClient, * so need to ensure that any items used in its equals/hashcode methods are * not changed after use, i.e.: * host, port, protocol, localAddress, proxy * */ HostConfiguration hc = new HostConfiguration(); hc.setHost(host, port, protocol); // All needed to ensure re-usablility // Set up the local address if one exists final InetAddress inetAddr = getIpSourceAddress(); if (inetAddr != null) {// Use special field ip source address (for pseudo 'ip spoofing') hc.setLocalAddress(inetAddr); } else { hc.setLocalAddress(localAddress); // null means use the default } final String proxyHost = getProxyHost(); final int proxyPort = getProxyPortInt(); boolean useStaticProxy = isStaticProxy(host); boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort); if (useDynamicProxy) { hc.setProxy(proxyHost, proxyPort); useStaticProxy = false; // Dynamic proxy overrules static proxy } else if (useStaticProxy) { if (log.isDebugEnabled()) { log.debug("Setting proxy: " + PROXY_HOST + ":" + PROXY_PORT); } hc.setProxy(PROXY_HOST, PROXY_PORT); } Map<HostConfiguration, HttpClient> map = httpClients.get(); // N.B. HostConfiguration.equals() includes proxy settings in the compare. HttpClient httpClient = map.get(hc); if (httpClient != null && resetSSLContext && isHTTPS) { httpClient.getHttpConnectionManager().closeIdleConnections(-1000); httpClient = null; JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance(); sslMgr.resetContext(); resetSSLContext = false; } if (httpClient == null) { httpClient = new HttpClient(new SimpleHttpConnectionManager()); httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(RETRY_COUNT, false)); if (log.isDebugEnabled()) { log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient)); } httpClient.setHostConfiguration(hc); map.put(hc, httpClient); } else { if (log.isDebugEnabled()) { log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient)); } } // Set up any required Proxy credentials if (useDynamicProxy) { String user = getProxyUser(); if (user.length() > 0) { httpClient.getState().setProxyCredentials( new AuthScope(proxyHost, proxyPort, null, AuthScope.ANY_SCHEME), new NTCredentials(user, getProxyPass(), localHost, PROXY_DOMAIN)); } else { httpClient.getState().clearProxyCredentials(); } } else { if (useStaticProxy) { if (PROXY_USER.length() > 0) { httpClient.getState().setProxyCredentials( new AuthScope(PROXY_HOST, PROXY_PORT, null, AuthScope.ANY_SCHEME), new NTCredentials(PROXY_USER, PROXY_PASS, localHost, PROXY_DOMAIN)); } } else { httpClient.getState().clearProxyCredentials(); } } int rto = getResponseTimeout(); if (rto > 0) { httpMethod.getParams().setSoTimeout(rto); } int cto = getConnectTimeout(); if (cto > 0) { httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(cto); } // Allow HttpClient to handle the redirects: httpMethod.setFollowRedirects(getAutoRedirects()); // a well-behaved browser is supposed to send 'Connection: close' // with the last request to an HTTP server. Instead, most browsers // leave it to the server to close the connection after their // timeout period. Leave it to the JMeter user to decide. if (getUseKeepAlive()) { httpMethod.setRequestHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.KEEP_ALIVE); } else { httpMethod.setRequestHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.CONNECTION_CLOSE); } setConnectionHeaders(httpMethod, u, getHeaderManager(), getCacheManager()); String cookies = setConnectionCookie(httpMethod, u, getCookieManager()); setConnectionAuthorization(httpClient, u, getAuthManager()); if (res != null) { res.setCookies(cookies); } return httpClient; }
From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java
/** Initialize the HttpClient. */ private void initHttpClient() { LOG.debug("A new HttpClient instance is needed ..."); this.cl = new HttpClient(new MultiThreadedHttpConnectionManager()); // Default params if (this.http.getTimeout() != 0) { this.cl.getHttpConnectionManager().getParams().setConnectionTimeout(this.http.getTimeout()); this.cl.getHttpConnectionManager().getParams().setSoTimeout(this.http.getTimeout()); }/*w ww .j a v a 2 s . c om*/ this.cl.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HostConfiguration hc = new HostConfiguration(); HttpState state = new HttpState(); if (StringUtils.isNotEmpty(this.http.getProxyHost())) { hc.setProxy(this.http.getProxyHost(), this.http.getProxyPort()); if (LOG.isDebugEnabled()) { LOG.debug("Proxy Host:" + this.http.getProxyHost()); LOG.debug("Proxy Port:" + this.http.getProxyPort()); } if (StringUtils.isNotEmpty(this.http.getProxyUser()) && this.http.getProxyPassword() != null) { if (LOG.isDebugEnabled()) { LOG.debug("Proxy User:" + this.http.getProxyUser()); } Credentials credentials; if (StringUtils.isNotEmpty(this.http.getProxyNtlmHost())) { credentials = new NTCredentials(this.http.getProxyUser(), this.http.getProxyPassword(), this.http.getProxyNtlmHost(), this.http.getProxyNtlmDomain()); } else { credentials = new UsernamePasswordCredentials(this.http.getProxyUser(), this.http.getProxyPassword()); } state.setProxyCredentials(AuthScope.ANY, credentials); } } else { LOG.debug("Not using a proxy"); } this.cl.setHostConfiguration(hc); this.cl.setState(state); LOG.debug("New HttpClient instance created."); }
From source file:org.apache.maven.plugin.jira.AbstractJiraDownloader.java
/** * Execute the query on the JIRA server. * * @throws Exception on error/*from ww w. j av a 2s .co m*/ */ public void doExecute() throws Exception { try { HttpClient client = new HttpClient(); // MCHANGES-89 Allow circular redirects HttpClientParams clientParams = client.getParams(); clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); HttpState state = new HttpState(); HostConfiguration hc = new HostConfiguration(); client.setHostConfiguration(hc); client.setState(state); Map<String, String> urlMap = JiraHelper.getJiraUrlAndProjectId(project.getIssueManagement().getUrl()); String jiraUrl = urlMap.get("url"); getLog().debug("JIRA lives at: " + jiraUrl); String jiraId = urlMap.get("id"); determineProxy(jiraUrl, client); prepareBasicAuthentication(client); boolean jiraAuthenticationSuccessful = false; if (isJiraAuthenticationConfigured()) { jiraAuthenticationSuccessful = doJiraAuthentication(client, jiraUrl); } if ((isJiraAuthenticationConfigured() && jiraAuthenticationSuccessful) || !isJiraAuthenticationConfigured()) { if (jiraId == null || jiraId.length() == 0) { log.debug("The JIRA URL " + project.getIssueManagement().getUrl() + " doesn't include a pid, trying to extract it from JIRA."); jiraId = JiraHelper.getPidFromJira(log, project.getIssueManagement().getUrl(), client); } if (jiraId == null) { getLog().error("The issue management URL in the POM does not include a pid," + " and it was not possible to extract it from the page at that URL."); } else { // create the URL for getting the proper issues from JIRA String fullURL = jiraUrl + "/secure/IssueNavigator.jspa?view=rss&pid=" + jiraId; if (getFixFor() != null) { fullURL += "&fixfor=" + getFixFor(); } String createdFilter = createFilter(); if (createdFilter.charAt(0) != '&') { fullURL += "&"; } fullURL += createdFilter; fullURL += ("&tempMax=" + nbEntriesMax + "&reset=true&decorator=none"); if (log.isDebugEnabled()) { log.debug("download jira issues from url " + fullURL); } // execute the GET download(client, fullURL); } } } catch (Exception e) { if (project.getIssueManagement() != null) { getLog().error("Error accessing " + project.getIssueManagement().getUrl(), e); } else { getLog().error("Error accessing mock project issues", e); } } }
From source file:org.apache.maven.plugin.jira.ClassicJiraDownloader.java
/** * Execute the query on the JIRA server. * * @throws Exception on error//from w w w . ja va 2 s. c o m */ public void doExecute() throws Exception { try { HttpClient client = new HttpClient(); // MCHANGES-89 Allow circular redirects HttpClientParams clientParams = client.getParams(); clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); clientParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); //MCHANGES-237 HttpState state = new HttpState(); HostConfiguration hc = new HostConfiguration(); client.setHostConfiguration(hc); client.setState(state); String baseUrl = JiraHelper.getBaseUrl(project.getIssueManagement().getUrl()); getLog().debug("JIRA lives at: " + baseUrl); // Here we only need the host part of the URL determineProxy(baseUrl, client); prepareBasicAuthentication(client); boolean jiraAuthenticationSuccessful = false; if (isJiraAuthenticationConfigured()) { // Here we only need the parts up to and including the host part of the URL jiraAuthenticationSuccessful = doJiraAuthentication(client, baseUrl); } if ((isJiraAuthenticationConfigured() && jiraAuthenticationSuccessful) || !isJiraAuthenticationConfigured()) { String fullUrl; if (useJql) { fullUrl = getJqlQueryURL(); } else { fullUrl = getParameterBasedQueryURL(client); } if (log.isDebugEnabled()) { log.debug("download jira issues from url " + fullUrl); } // execute the GET download(client, fullUrl); } } catch (Exception e) { if (project.getIssueManagement() != null) { getLog().error("Error accessing " + project.getIssueManagement().getUrl(), e); } else { getLog().error("Error accessing mock project issues", e); } } }
From source file:org.apache.maven.proxy.config.HttpRepoConfiguration.java
private HttpClient createHttpClient() { HttpClient client = new HttpClient(); HostConfiguration hostConf = new HostConfiguration(); ProxyConfiguration proxy = getProxy(); if (proxy != null) { hostConf.setProxy(proxy.getHost(), proxy.getPort()); client.setHostConfiguration(hostConf); if (proxy.getUsername() != null) { Credentials creds = new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()); client.getState().setProxyCredentials(null, null, creds); }/*from ww w .j ava 2 s . c o m*/ } return client; }
From source file:org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagon.java
public void openConnectionInternal() { repository.setUrl(getURL(repository)); client = new HttpClient(connectionManager); // WAGON-273: default the cookie-policy to browser compatible client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); String username = null;/* w w w.ja v a2 s . c o m*/ String password = null; String domain = null; if (authenticationInfo != null) { username = authenticationInfo.getUserName(); if (StringUtils.contains(username, "\\")) { String[] domainAndUsername = username.split("\\\\"); domain = domainAndUsername[0]; username = domainAndUsername[1]; } password = authenticationInfo.getPassword(); } String host = getRepository().getHost(); if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) { Credentials creds; if (domain != null) { creds = new NTCredentials(username, password, host, domain); } else { creds = new UsernamePasswordCredentials(username, password); } int port = getRepository().getPort() > -1 ? getRepository().getPort() : AuthScope.ANY_PORT; AuthScope scope = new AuthScope(host, port); client.getState().setCredentials(scope, creds); } HostConfiguration hc = new HostConfiguration(); ProxyInfo proxyInfo = getProxyInfo(getRepository().getProtocol(), getRepository().getHost()); if (proxyInfo != null) { String proxyUsername = proxyInfo.getUserName(); String proxyPassword = proxyInfo.getPassword(); String proxyHost = proxyInfo.getHost(); int proxyPort = proxyInfo.getPort(); String proxyNtlmHost = proxyInfo.getNtlmHost(); String proxyNtlmDomain = proxyInfo.getNtlmDomain(); if (proxyHost != null) { hc.setProxy(proxyHost, proxyPort); if (proxyUsername != null && proxyPassword != null) { Credentials creds; if (proxyNtlmHost != null || proxyNtlmDomain != null) { creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain); } else { creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword); } int port = proxyInfo.getPort() > -1 ? proxyInfo.getPort() : AuthScope.ANY_PORT; AuthScope scope = new AuthScope(proxyHost, port); client.getState().setProxyCredentials(scope, creds); } } } hc.setHost(host); //start a session with the webserver client.setHostConfiguration(hc); }