Example usage for org.apache.commons.httpclient HostConfiguration HostConfiguration

List of usage examples for org.apache.commons.httpclient HostConfiguration HostConfiguration

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HostConfiguration HostConfiguration.

Prototype

public HostConfiguration(HostConfiguration paramHostConfiguration) 

Source Link

Usage

From source file:lucee.commons.net.http.httpclient3.HttpMethodCloner.java

private static void copyHttpMethodBase(HttpMethodBase m, HttpMethodBase copy) {
    if (m.getHostConfiguration() != null) {
        copy.setHostConfiguration(new HostConfiguration(m.getHostConfiguration()));
    }//ww w .  j  ava 2 s.  c  o  m
    try {
        copy.setParams((HttpMethodParams) m.getParams().clone());
    } catch (CloneNotSupportedException e) {
    }
}

From source file:com.sun.jersey.client.apache.DefaultApacheHttpMethodExecutor.java

private HostConfiguration getHostConfiguration(HttpClient client, Map<String, Object> props) {
    Object proxy = props.get(ApacheHttpClientConfig.PROPERTY_PROXY_URI);
    if (proxy != null) {
        URI proxyUri = getProxyUri(proxy);

        String proxyHost = proxyUri.getHost();
        if (proxyHost == null) {
            proxyHost = "localhost";
        }/*from   w  ww . j av  a2 s.co  m*/

        int proxyPort = proxyUri.getPort();
        if (proxyPort == -1) {
            proxyPort = 8080;
        }

        HostConfiguration hostConfig = new HostConfiguration(client.getHostConfiguration());
        String setHost = hostConfig.getProxyHost();
        int setPort = hostConfig.getProxyPort();

        if ((setHost == null) || (!setHost.equals(proxyHost)) || (setPort == -1) || (setPort != proxyPort)) {
            hostConfig.setProxyHost(new ProxyHost(proxyHost, proxyPort));
        }
        return hostConfig;
    } else {
        return null;
    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 *//* w w w  . j  a v a2 s  .c om*/
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    if (!curi.isPrerequisite()) {
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE,
                CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, CoreAttributeConstants.A_ETAG_HEADER,
                "If-None-Match");
    }

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:nl.nn.adapterframework.http.HttpSender.java

public String sendMessageWithTimeoutGuarded(String correlationID, String message,
        ParameterResolutionContext prc) throws SenderException, TimeOutException {
    ParameterValueList pvl = null;//from  ww  w  .j a  v  a 2s. c om
    try {
        if (prc != null && paramList != null) {
            pvl = prc.getValues(paramList);
        }
    } catch (ParameterException e) {
        throw new SenderException(
                getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
    }
    URI uri;
    HttpMethod httpmethod;
    HostConfiguration hostconfiguration = new HostConfiguration(hostconfigurationBase);
    try {
        if (urlParameter != null) {
            String url = (String) pvl.getParameterValue(getUrlParam()).getValue();
            uri = getURI(url);
        } else {
            uri = staticUri;
        }

        Map<String, String> headersParamsMap = new HashMap<String, String>();
        if (headersParams != null) {
            StringTokenizer st = new StringTokenizer(headersParams, ",");
            while (st.hasMoreElements()) {
                headersParamsMap.put(st.nextToken(), null);
            }
        }

        if (!isParamsInUrl()) {
            httpmethod = getPostMethodWithParamsInBody(uri, message, pvl, headersParamsMap, prc);
        } else {
            httpmethod = getMethod(uri, message, pvl, headersParamsMap);
            if (!"POST".equals(getMethodType()) && !"PUT".equals(getMethodType())
                    && !"REPORT".equals(getMethodType())) {
                httpmethod.setFollowRedirects(isFollowRedirects());
            }
        }

        int port = getPort(uri);

        if (socketfactory != null && "https".equals(uri.getScheme())) {
            Protocol authhttps = new Protocol(uri.getScheme(), socketfactory, port);
            hostconfiguration.setHost(uri.getHost(), port, authhttps);
        } else {
            hostconfiguration.setHost(uri.getHost(), port, uri.getScheme());
        }
        log.info(getLogPrefix() + "configured httpclient for host [" + hostconfiguration.getHostURL() + "]");

        if (credentials != null) {
            httpState.setCredentials(null, uri.getHost(), credentials);
        }
    } catch (URIException e) {
        throw new SenderException(e);
    }

    String result = null;
    int statusCode = -1;
    int count = getMaxExecuteRetries();
    String msg = null;
    while (count-- >= 0 && statusCode == -1) {
        try {
            if (log.isDebugEnabled())
                log.debug(getLogPrefix() + "executing method");
            statusCode = httpclient.executeMethod(hostconfiguration, httpmethod, httpState);
            if (log.isDebugEnabled())
                log.debug(getLogPrefix() + "executed method");

            if (statusCode != HttpServletResponse.SC_OK) {
                StatusLine statusline = httpmethod.getStatusLine();
                if (statusline != null) {
                    log.warn(getLogPrefix() + "status [" + statusline.toString() + "]");
                } else {
                    log.warn(getLogPrefix() + "no statusline found");
                }
            } else {
                if (log.isDebugEnabled())
                    log.debug(getLogPrefix() + "status [" + statusCode + "]");
            }
            HttpServletResponse response = null;
            if (isStreamResultToServlet()) {
                response = (HttpServletResponse) prc.getSession().get("restListenerServletResponse");
            }
            String fileName = null;
            if (StringUtils.isNotEmpty(getStreamResultToFileNameSessionKey())) {
                fileName = (String) prc.getSession().get(getStreamResultToFileNameSessionKey());
            }
            result = extractResult(httpmethod, prc, response, fileName);
            if (log.isDebugEnabled())
                log.debug(getLogPrefix() + "retrieved result [" + result + "]");
        } catch (HttpException e) {
            Throwable throwable = e.getCause();
            String cause = null;
            if (throwable != null) {
                cause = throwable.toString();
            }
            msg = e.getMessage();
            log.warn(getLogPrefix() + "httpException with message [" + msg + "] and cause [" + cause
                    + "], executeRetries left [" + count + "]");
        } catch (IOException e) {
            httpmethod.abort();
            if (e instanceof SocketTimeoutException) {
                throw new TimeOutException(e);
            }
            throw new SenderException(e);
        } finally {
            // In case of storeResultAsStreamInSessionKey release connection
            // is done by ReleaseConnectionAfterReadInputStream.
            if (StringUtils.isEmpty(getStoreResultAsStreamInSessionKey())) {
                httpmethod.releaseConnection();
            }
        }
    }

    if (statusCode == -1) {
        if (StringUtils.contains(msg.toUpperCase(), "TIMEOUTEXCEPTION")) {
            //java.net.SocketTimeoutException: Read timed out
            throw new TimeOutException("Failed to recover from timeout exception");
        }
        throw new SenderException("Failed to recover from exception");
    }

    if (isXhtml() && StringUtils.isNotEmpty(result)) {
        result = XmlUtils.skipDocTypeDeclaration(result.trim());
        if (result.startsWith("<html>") || result.startsWith("<html ")) {
            CleanerProperties props = new CleanerProperties();
            HtmlCleaner cleaner = new HtmlCleaner(props);
            TagNode tagNode = cleaner.clean(result);
            result = new SimpleXmlSerializer(props).getXmlAsString(tagNode);

            if (transformerPool != null) {
                log.debug(getLogPrefix() + " transforming result [" + result + "]");
                ParameterResolutionContext prc_xslt = new ParameterResolutionContext(result, null, true, true);
                try {
                    result = transformerPool.transform(prc_xslt.getInputSource(), null);
                } catch (Exception e) {
                    throw new SenderException("Exception on transforming input", e);
                }
            }
        }
    }
    return result;
}

From source file:org.archive.crawler.fetcher.FetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 */// w  w  w  . ja v  a  2  s .com
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()
            && (curi.getViaContext() == null || !Link.PREREQ_MISC.equals(curi.getViaContext().toString()))) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    if (!curi.isPrerequisite()) {
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE,
                CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
        setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, CoreAttributeConstants.A_ETAG_HEADER,
                "If-None-Match");
    }

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * Configure the HttpMethod setting options and headers.
 *
 * @param curi CrawlURI from which we pull configuration.
 * @param method The Method to configure.
 * @return HostConfiguration copy customized for this CrawlURI
 *///from  w  w  w .j a va  2  s . c  o m
protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) {
    // Don't auto-follow redirects
    method.setFollowRedirects(false);

    //        // set soTimeout
    //        method.getParams().setSoTimeout(
    //                ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS))
    //                        .intValue());

    // Set cookie policy.
    method.getParams()
            .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue())
                    ? CookiePolicy.IGNORE_COOKIES
                    : CookiePolicy.BROWSER_COMPATIBILITY);

    // Use only HTTP/1.0 (to avoid receiving chunked responses)
    method.getParams().setVersion(HttpVersion.HTTP_1_0);

    CrawlOrder order = getSettingsHandler().getOrder();
    String userAgent = curi.getUserAgent();
    if (userAgent == null) {
        userAgent = order.getUserAgent(curi);
    }
    method.setRequestHeader("User-Agent", userAgent);
    method.setRequestHeader("From", order.getFrom(curi));

    // Set retry handler.
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler());

    final long maxLength = getMaxLength(curi);
    if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) {
        method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1)));
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) {
        method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE);
    }

    if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()
            && (curi.getViaContext() == null || !Link.PREREQ_MISC.equals(curi.getViaContext().toString()))) {
        // RFC2616 says no referer header if referer is https and the url
        // is not
        String via = curi.flattenVia();
        if (via != null && via.length() > 0
                && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) {
            method.setRequestHeader(REFERER, via);
        }
    }

    /*if(!curi.isPrerequisite() && curi.containsKey(URLInfo.MODIFY_TIME) &&
       (Boolean)getUncheckedAttribute(curi, ATTR_SEND_IF_MODIFIED_SINCE)) {
      long modifyTime = curi.getLong(URLInfo.MODIFY_TIME);
      if (modifyTime != 0) {
      Date date = new Date(modifyTime);
      method.setRequestHeader("If-Modified-Since", date.toString());
      logger.debug(curi.getUURI().toString() + " send header modifyTime:" + date.toGMTString());
      }
              
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE, 
           CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since");
      setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, 
        CoreAttributeConstants.A_ETAG_HEADER, "If-None-Match");
    }*/

    // TODO: What happens if below method adds a header already
    // added above: e.g. Connection, Range, or Referer?
    setAcceptHeaders(curi, method);

    HttpClient http = getClient();
    HostConfiguration config = new HostConfiguration(http.getHostConfiguration());
    configureProxy(curi, config);
    configureBindAddress(curi, config);
    return config;
}

From source file:org.springframework.security.saml.websso.ArtifactResolutionProfileImpl.java

/**
 * Method is expected to determine hostConfiguration used to send request to the server by back-channel. Configuration
 * should contain URI of the host and used protocol including all security settings.
 * <p>//from ww w  .  j av a2  s .  co  m
 * Default implementation uses either default http protocol for non-SSL requests or constructs a separate
 * TrustManager using trust engine specified in the SAMLMessageContext - based either on MetaIOP (certificates
 * obtained from Metadata and ExtendedMetadata are trusted) or PKIX (certificates from metadata and ExtendedMetadata
 * including specified trust anchors are trusted and verified using PKIX).
 * <p>
 * Used trust engine can be customized as part of the SAMLContextProvider used to process this request.
 * <p>
 * Default values for the HostConfiguration are cloned from the HTTPClient set in this instance, when there are
 * no defaults available a new object is created.
 *
 * @param uri uri the request should be sent to
 * @param context context including the peer address
 * @return host configuration
 * @throws MessageEncodingException in case peer URI can't be parsed
 */
protected HostConfiguration getHostConfiguration(URI uri, SAMLMessageContext context)
        throws MessageEncodingException {

    try {

        HostConfiguration hc = httpClient.getHostConfiguration();

        if (hc != null) {
            // Clone configuration from the HTTP Client object
            hc = new HostConfiguration(hc);
        } else {
            // Create brand new configuration when there are no defaults
            hc = new HostConfiguration();
        }

        if (uri.getScheme().equalsIgnoreCase("http")) {

            log.debug("Using HTTP configuration");
            hc.setHost(uri);

        } else {

            log.debug("Using HTTPS configuration");

            CriteriaSet criteriaSet = new CriteriaSet();
            criteriaSet.add(new EntityIDCriteria(context.getPeerEntityId()));
            criteriaSet
                    .add(new MetadataCriteria(IDPSSODescriptor.DEFAULT_ELEMENT_NAME, SAMLConstants.SAML20P_NS));
            criteriaSet.add(new UsageCriteria(UsageType.UNSPECIFIED));

            X509TrustManager trustManager = new X509TrustManager(criteriaSet, context.getLocalSSLTrustEngine());
            X509KeyManager manager = new X509KeyManager(context.getLocalSSLCredential());
            HostnameVerifier hostnameVerifier = context.getLocalSSLHostnameVerifier();

            ProtocolSocketFactory socketFactory = getSSLSocketFactory(context, manager, trustManager,
                    hostnameVerifier);
            Protocol protocol = new Protocol("https", socketFactory, 443);
            hc.setHost(uri.getHost(), uri.getPort(), protocol);

        }

        return hc;

    } catch (URIException e) {
        throw new MessageEncodingException("Error parsing remote location URI", e);
    }

}

From source file:org.springframework.security.saml.websso.AttributeQueryImpl.java

/**
 * Method is expected to determine hostConfiguration used to send request to the server by back-channel. Configuration
 * should contain URI of the host and used protocol including all security settings.
 * <p/>/* w  ww .j a va  2 s.  co  m*/
 * Default implementation uses either default http protocol for non-SSL requests or constructs a separate
 * TrustManager using trust engine specified in the SAMLMessageContext - based either on MetaIOP (certificates
 * obtained from Metadata and ExtendedMetadata are trusted) or PKIX (certificates from metadata and ExtendedMetadata
 * including specified trust anchors are trusted and verified using PKIX).
 * <p/>
 * Used trust engine can be customized as part of the SAMLContextProvider used to process this request.
 * <p/>
 * Default values for the HostConfiguration are cloned from the HTTPClient set in this instance, when there are
 * no defaults available a new object is created.
 *
 * @param uri uri the request should be sent to
 * @param context context including the peer address
 * @return host configuration
 * @throws MessageEncodingException in case peer URI can't be parsed
 */
protected HostConfiguration getHostConfiguration(URI uri, SAMLMessageContext context)
        throws MessageEncodingException {

    try {

        HostConfiguration hc = httpClient.getHostConfiguration();

        if (hc != null) {
            // Clone configuration from the HTTP Client object
            hc = new HostConfiguration(hc);
        } else {
            // Create brand new configuration when there are no defaults
            hc = new HostConfiguration();
        }

        if (uri.getScheme().equalsIgnoreCase("http")) {

            log.debug("Using HTTP configuration");
            hc.setHost(uri);

        } else {

            log.debug("Using HTTPS configuration");

            CriteriaSet criteriaSet = new CriteriaSet();
            criteriaSet.add(new EntityIDCriteria(context.getPeerEntityId()));
            criteriaSet
                    .add(new MetadataCriteria(IDPSSODescriptor.DEFAULT_ELEMENT_NAME, SAMLConstants.SAML20P_NS));
            criteriaSet.add(new UsageCriteria(UsageType.UNSPECIFIED));

            X509TrustManager trustManager = new X509TrustManager(criteriaSet, context.getLocalSSLTrustEngine());
            X509KeyManager manager = new X509KeyManager(context.getLocalSSLCredential());
            Protocol protocol = new Protocol("https",
                    (ProtocolSocketFactory) new TLSProtocolSocketFactory(manager, trustManager), 443);
            hc.setHost(uri.getHost(), uri.getPort(), protocol);

        }

        return hc;

    } catch (URIException e) {
        throw new MessageEncodingException("Error parsing remote location URI", e);
    }

}