List of usage examples for org.apache.commons.httpclient HostConfiguration HostConfiguration
public HostConfiguration()
From source file:org.archive.wayback.resourceindex.ziplines.Http11BlockLoader.java
/** * /* ww w .jav a 2 s . co m*/ */ public Http11BlockLoader() { connectionManager = new MultiThreadedHttpConnectionManager(); hostConfiguration = new HostConfiguration(); HttpClientParams params = new HttpClientParams(); // params.setParameter(HttpClientParams.RETRY_HANDLER, new NoRetryHandler()); http = new HttpClient(params, connectionManager); http.setHostConfiguration(hostConfiguration); }
From source file:org.attribyte.api.http.impl.commons.Commons3Client.java
/** * Creates a client with specified options. * @param options The options.//from ww w.j av a 2 s .c o m */ public Commons3Client(final ClientOptions options) { connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.setParams(fromOptions(options)); httpClient = new HttpClient(connectionManager); HostConfiguration hostConfig = new HostConfiguration(); if (options.proxyHost != null) { hostConfig.setProxy(options.proxyHost, options.proxyPort); } httpClient.setHostConfiguration(hostConfig); userAgent = options.userAgent; }
From source file:org.attribyte.api.http.impl.commons.Commons3Client.java
@Override /**/*from www .j a v a 2s. co m*/ * Initializes the client from properties. * <p> * The following properties are available. <b>Bold</b> properties are required. * * <h2>HTTP Client</h2> * <dl> * <dt><b>User-Agent</b></dt> * <dd>The default User-Agent string sent with requests. Added only if * request has no <code>User-Agent</code> header.</dd> * <dt><b>connectionTimeoutMillis</b></dt> * <dd>The HTTP connection timeout in milliseconds.</dd> * <dt><b>socketTimeoutMillis</b></dt> * <dd>The HTTP client socket timeout in milliseconds.</dd> * <dt>proxyHost</dt> * <dd>The HTTP proxy host. If specified, all client requests will use this proxy.</dd> * <dt>proxyPort</dt> * <dd>The HTTP proxy port. Required when <code>proxyHost</code> is specified</dd> * </dl> * </p> * @param prefix The prefix for all properties (e.g. 'client.'). * @param props The properties. * @param logger The logger. If unspecified, messages are logged to the console. * @throws InitializationException on initialization error. */ public synchronized void init(String prefix, Properties props, Logger logger) throws InitializationException { if (isInit.compareAndSet(false, true)) { ClientOptions options = new ClientOptions(prefix, props); connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.setParams(fromOptions(options)); httpClient = new HttpClient(connectionManager); HostConfiguration hostConfig = new HostConfiguration(); if (options.proxyHost != null) { hostConfig.setProxy(options.proxyHost, options.proxyPort); } httpClient.setHostConfiguration(hostConfig); userAgent = options.userAgent; } }
From source file:org.bbaw.pdr.ae.view.identifiers.internal.ConcurrenceSearchController.java
private String requestWebService(URL url) throws URISyntaxException, UnsupportedEncodingException { String result = null;//from w w w.j a v a2s.c om if (url != null) { HttpClient client = new HttpClient(); // System.out.println("url " + url.toString()); // PostMethod method = null; HttpClient httpclient = null; httpclient = new HttpClient(); String urlString = new String(url.toString()); if (urlString.contains(" ")) { // System.out.println("containts ws"); urlString.replace(" ", "%20"); } Pattern p = Pattern.compile("\\s"); Matcher m = p.matcher(urlString); // while (m.find()) // { // // System.out.println("\\s"); // } urlString = m.replaceAll("%20"); // while (m.find()) // { // System.out.println("2.\\s"); // } // urlString = URLEncoder.encode(urlString, "UTF-8"); // urlString.replace("\\s+", "%20"); GetMethod get = new GetMethod(urlString); HostConfiguration hf = new HostConfiguration(); hf.setHost(urlString, url.getPort()); httpclient.setHostConfiguration(hf); // get = new PostMethod(theURL); // LogHelper.logMessage("Before sending SMS Message: "+message); int respCode; try { respCode = httpclient.executeMethod(get); log = new Status(IStatus.INFO, Activator.PLUGIN_ID, "Response code: " + respCode); iLogger.log(log); // successful. /* send request */ final int status = client.executeMethod(get); // LOG.debug("http status #execute: " + // Integer.toString(status)); switch (status) { case HttpStatus.SC_NOT_IMPLEMENTED: get.releaseConnection(); // throw new IOException("Solr Query #GET (" + // get.getURI().toString() + ") returned 501"); default: result = get.getResponseBodyAsString(); get.releaseConnection(); } } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; }
From source file:org.bonitasoft.connectors.webdav.exo.common.ExoConnector.java
/** * initiate client/*from w w w.ja v a2 s .co m*/ * * @param host * @param port * @param username * @param password */ public void initClient(final String host, final int port, final String username, final String password) { if (LOGGER.isLoggable(Level.INFO)) { LOGGER.info("WebDAVConnector {host=" + host + ", port=" + port + ", username=" + username + ", password=******}"); } final HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(host, port); final HttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); final HttpConnectionManagerParams params = new HttpConnectionManagerParams(); final int maxHostConnections = 20; params.setMaxConnectionsPerHost(hostConfig, maxHostConnections); connectionManager.setParams(params); client = new HttpClient(connectionManager); final Credentials creds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, creds); client.setHostConfiguration(hostConfig); }
From source file:org.cauldron.tasks.HttpCall.java
/** * Compiling an HttpTask validates the get/post method setting and the * presence of parameters appropriate for the method. *///w ww . j a v a 2 s . com public void compile() throws TaskException { if (host == null) throw new TaskException("must specify host name"); if (path == null) throw new TaskException("must specify get or post"); config = new HostConfiguration(); config.setHost(host, port); if (proxyHost != null) config.setProxy(proxyHost, proxyPort); }
From source file:org.codehaus.httpcache4j.client.HTTPClientResponseResolver.java
/** * Determines the HttpClient's request method from the HTTPMethod enum. * * @param method the HTTPCache enum that determines * @param requestURI the request URI./* www . j a v a2s . com*/ * @return a new HttpMethod subclass. */ protected HttpMethod getMethod(HTTPMethod method, URI requestURI) { if (CONNECT.equals(method)) { HostConfiguration config = new HostConfiguration(); config.setHost(requestURI.getHost(), requestURI.getPort(), requestURI.getScheme()); return new ConnectMethod(config); } else if (DELETE.equals(method)) { return new DeleteMethod(requestURI.toString()); } else if (GET.equals(method)) { return new GetMethod(requestURI.toString()); } else if (HEAD.equals(method)) { return new HeadMethod(requestURI.toString()); } else if (OPTIONS.equals(method)) { return new OptionsMethod(requestURI.toString()); } else if (POST.equals(method)) { return new PostMethod(requestURI.toString()); } else if (PUT.equals(method)) { return new PutMethod(requestURI.toString()); } else if (TRACE.equals(method)) { return new TraceMethod(requestURI.toString()); } else { throw new IllegalArgumentException("Cannot handle method: " + method); } }
From source file:org.codehaus.wadi.web.impl.CommonsHttpProxy.java
protected void doProxy(URI uri, WebInvocation context) throws ProxyingException { HttpServletRequest hreq = context.getHreq(); HttpServletResponse hres = context.getHres(); long startTime = System.currentTimeMillis(); String m = hreq.getMethod();//from w w w . java 2s . c o m Class clazz = (Class) _methods.get(m); if (clazz == null) { throw new IrrecoverableException("unsupported http method: " + m); } HttpMethod hm = null; try { hm = (HttpMethod) clazz.newInstance(); } catch (Exception e) { throw new IrrecoverableException("could not create HttpMethod instance", e); // should never happen } String requestURI = getRequestURI(hreq); hm.setPath(requestURI); String queryString = hreq.getQueryString(); if (queryString != null) { hm.setQueryString(queryString); requestURI += queryString; } hm.setFollowRedirects(false); //hm.setURI(new URI(uri)); hm.setStrictMode(false); // check connection header String connectionHdr = hreq.getHeader("Connection"); // TODO - what if there are multiple values ? if (connectionHdr != null) { connectionHdr = connectionHdr.toLowerCase(); if (connectionHdr.equals("keep-alive") || connectionHdr.equals("close")) connectionHdr = null; // TODO ?? } // copy headers boolean xForwardedFor = false; boolean hasContent = false; int contentLength = 0; Enumeration enm = hreq.getHeaderNames(); while (enm.hasMoreElements()) { // TODO could be better than this! - using javax.servlet ? String hdr = (String) enm.nextElement(); String lhdr = hdr.toLowerCase(); if (_DontProxyHeaders.contains(lhdr)) continue; if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0) continue; if ("content-length".equals(lhdr)) { try { contentLength = hreq.getIntHeader(hdr); hasContent = contentLength > 0; } catch (NumberFormatException e) { if (_log.isWarnEnabled()) _log.warn("bad Content-Length header value: " + hreq.getHeader(hdr), e); } } if ("content-type".equals(lhdr)) { hasContent = true; } Enumeration vals = hreq.getHeaders(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { hm.addRequestHeader(hdr, val); // if (_log.isInfoEnabled()) _log.info("Request " + hdr + ": " + val); xForwardedFor |= "X-Forwarded-For".equalsIgnoreCase(hdr); // why is this not in the outer loop ? } } } // cookies... // although we copy cookie headers into the request abover - commons-httpclient thinks it knows better and strips them out before sending. // we have to explicitly use their interface to add the cookies - painful... // DOH! - an org.apache.commons.httpclient.Cookie is NOT a // javax.servlet.http.Cookie - and it looks like the two don't // map onto each other without data loss... HttpState state = new HttpState(); javax.servlet.http.Cookie[] cookies = hreq.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { javax.servlet.http.Cookie c = cookies[i]; String domain = c.getDomain(); if (domain == null) { domain = hreq.getServerName(); // TODO - tmp test // _log.warn("defaulting cookie domain"); } // domain=null; String cpath = c.getPath(); if (cpath == null) { cpath = hreq.getContextPath(); // fix for Jetty // _log.warn("defaulting cookie path"); } //if (_log.isTraceEnabled()) _log.trace("PATH: value="+path+" length="+(path==null?0:path.length())); Cookie cookie = new Cookie(domain, c.getName(), c.getValue(), cpath, c.getMaxAge(), c.getSecure()); // TODO - sort out domain //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.getDomain()+","+ cookie.getName()+","+ cookie.getValue()+","+ cookie.getPath()+","+ cookie.getExpiryDate()+","+ cookie.getSecure()); state.addCookie(cookie); //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.toString()); } } // Proxy headers hm.addRequestHeader("Via", "1.1 " + hreq.getLocalName() + ":" + hreq.getLocalPort() + " \"WADI\""); if (!xForwardedFor) hm.addRequestHeader("X-Forwarded-For", hreq.getRemoteAddr()); // Max-Forwards... // a little bit of cache control // String cache_control = hreq.getHeader("Cache-Control"); // if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0)) // httpMethod.setUseCaches(false); // customize Connection // uc.setDoInput(true); int client2ServerTotal = 0; if (hasContent) { // uc.setDoOutput(true); try { if (hm instanceof EntityEnclosingMethod) ((EntityEnclosingMethod) hm).setRequestBody(hreq.getInputStream()); // TODO - do we need to close response stream at end... ? } catch (IOException e) { throw new IrrecoverableException("could not pss request input across proxy", e); } } try { HttpClient client = new HttpClient(); HostConfiguration hc = new HostConfiguration(); //String host=location.getAddress().getHostAddress(); // inefficient - but stops httpclient from rejecting half our cookies... String host = uri.getHost(); hc.setHost(host, uri.getPort()); client.executeMethod(hc, hm, state); } catch (IOException e) // TODO { _log.warn("problem proxying connection:", e); } InputStream fromServer = null; // handler status codes etc. int code = 502; // String message="Bad Gateway: could not read server response code or message"; code = hm.getStatusCode(); // IOException // message=hm.getStatusText(); // IOException hres.setStatus(code); // hres.setStatus(code, message); - deprecated... try { fromServer = hm.getResponseBodyAsStream(); // IOException } catch (IOException e) { _log.warn("problem acquiring http client output", e); } // clear response defaults. hres.setHeader("Date", null); hres.setHeader("Server", null); // set response headers // TODO - is it a bug in Jetty that I have to start my loop at 1 ? or that key[0]==null ? // Try this inside Tomcat... Header[] headers = hm.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { String h = headers[i].toExternalForm(); int index = h.indexOf(':'); String key = h.substring(0, index).trim().toLowerCase(); String val = h.substring(index + 1, h.length()).trim(); if (val != null && !_DontProxyHeaders.contains(key)) { hres.addHeader(key, val); // if (_log.isInfoEnabled()) _log.info("Response: "+key+" - "+val); } } hres.addHeader("Via", "1.1 (WADI)"); // copy server->client int server2ClientTotal = 0; if (fromServer != null) { try { OutputStream toClient = hres.getOutputStream();// IOException server2ClientTotal += copy(fromServer, toClient, 8192);// IOException } catch (IOException e) { _log.warn("problem proxying server response back to client", e); } finally { try { fromServer.close(); } catch (IOException e) { // well - we did our best... _log.warn("problem closing server response stream", e); } } } long endTime = System.currentTimeMillis(); long elapsed = endTime - startTime; if (_log.isDebugEnabled()) { _log.debug("in:" + client2ServerTotal + ", out:" + server2ClientTotal + ", status:" + code + ", time:" + elapsed + ", uri:" + uri); } }
From source file:org.eclipse.mylyn.internal.provisional.commons.soap.CommonsHttpSender.java
protected HostConfiguration getHostConfiguration(HttpClient client, MessageContext context, URL targetURL) { TransportClientProperties tcp = TransportClientPropertiesFactory.create(targetURL.getProtocol()); // http or https int port = targetURL.getPort(); boolean hostInNonProxyList = isHostInNonProxyList(targetURL.getHost(), tcp.getNonProxyHosts()); HostConfiguration config = new HostConfiguration(); if (port == -1) { if (targetURL.getProtocol().equalsIgnoreCase("https")) { //$NON-NLS-1$ port = 443; // default port for https being 443 } else { // it must be http port = 80; // default port for http being 80 }//from w ww.j av a2s . c om } if (hostInNonProxyList) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyHost().length() == 0 || tcp.getProxyPort().length() == 0) { config.setHost(targetURL.getHost(), port, targetURL.getProtocol()); } else { if (tcp.getProxyUser().length() != 0) { Credentials proxyCred = new UsernamePasswordCredentials(tcp.getProxyUser(), tcp.getProxyPassword()); // if the username is in the form "user\domain" // then use NTCredentials instead. int domainIndex = tcp.getProxyUser().indexOf("\\"); //$NON-NLS-1$ if (domainIndex > 0) { String domain = tcp.getProxyUser().substring(0, domainIndex); if (tcp.getProxyUser().length() > domainIndex + 1) { String user = tcp.getProxyUser().substring(domainIndex + 1); proxyCred = new NTCredentials(user, tcp.getProxyPassword(), tcp.getProxyHost(), domain); } } client.getState().setProxyCredentials(AuthScope.ANY, proxyCred); } int proxyPort = new Integer(tcp.getProxyPort()).intValue(); config.setProxy(tcp.getProxyHost(), proxyPort); } } return config; }
From source file:org.eclipse.virgo.ide.runtime.internal.core.utils.WebDownloadUtils.java
public static Date getLastModifiedDate(String url, IProgressMonitor monitor) { GetMethod method = null;/*from www.j a va 2 s . c o m*/ BufferedInputStream bis = null; FileOutputStream fos = null; try { if (monitor.isCanceled()) { return null; } HttpClient client = new HttpClient(); method = new GetMethod(url); method.setFollowRedirects(true); HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setHost(getHost(url), HTTP_PORT, "http"); configureHttpClientProxy(url, client, hostConfiguration); client.getHttpConnectionManager().getParams().setSoTimeout(SOCKET_TIMEOUT); client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNNECT_TIMEOUT); // Execute the GET method int statusCode = client.executeMethod(hostConfiguration, method); if (statusCode == 200) { if (monitor.isCanceled()) { return null; } Header lastModified = method.getResponseHeader("Last-Modified"); if (lastModified != null) { return DateUtil.parseDate(lastModified.getValue()); } } } catch (Exception e) { } finally { try { if (method != null) { method.releaseConnection(); } } catch (Exception e2) { } try { if (bis != null) { bis.close(); } } catch (Exception e2) { } try { if (fos != null) { fos.close(); } } catch (Exception e2) { } } return null; }