Example usage for org.apache.commons.httpclient HttpMethod getHostConfiguration

List of usage examples for org.apache.commons.httpclient HttpMethod getHostConfiguration

Introduction

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

Prototype

public abstract HostConfiguration getHostConfiguration();

Source Link

Usage

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

/**
 * FUNKTIONIERT NICHT, HOST WIRD NICHT UEBERNOMMEN
 * Clones a http method and sets a new url
 * @param src/*  ww  w  . j  av  a2s.  c o m*/
 * @param url
 * @return
 */
private static HttpMethod clone(HttpMethod src, URL url) {
    HttpMethod trg = HttpMethodCloner.clone(src);
    HostConfiguration trgConfig = trg.getHostConfiguration();
    trgConfig.setHost(url.getHost(), url.getPort(), url.getProtocol());
    trg.setPath(url.getPath());
    trg.setQueryString(url.getQuery());

    return trg;
}

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

/**
 * rewrite request method//  w  w  w  . j a va 2  s  .  c o m
 * @param method
 * @return
 * @throws MalformedURLException
 */
private static HttpMethod rewrite(HttpMethod method) throws MalformedURLException {
    org.apache.commons.httpclient.Header location = method.getResponseHeader("location");
    if (location == null)
        return method;

    HostConfiguration config = method.getHostConfiguration();
    URL url;
    try {
        url = new URL(location.getValue());
    } catch (MalformedURLException e) {

        url = new URL(config.getProtocol().getScheme(), config.getHost(), config.getPort(),
                mergePath(method.getPath(), location.getValue()));
    }

    method = clone(method, url);

    return method;
}

From source file:com.yufei.dataget.utils.BaiheHtmlClientRetryHandler.java

@Override
public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
    String targetServer = method.getHostConfiguration().getHost();
    if (executionCount > maxRetryCount) {
        // Do not retry if over max retry count
        return false;
    }/*from   w w w  .java2s  . c  om*/
    if (exception instanceof NoHttpResponseException) {
        // Retry if the server dropped connection on us
        mLog.info("Exception that can not connect to the target server " + targetServer
                + " for it too busy  to accept current connecton!");
        return true;
    }
    if (exception instanceof SocketTimeoutException) {
        // Retry if the client  unable to establish a connection with the target server or proxy server within the given period of time.
        mLog.info("Expection that can not get response from  target server " + targetServer
                + " within fixed amount time! ");
        return true;
    }
    //        if (exception instanceof ConnectionPoolTimeoutException) {
    //            // Retry if the client  fails to obtain a free connection from the connection pool within the given period of time.
    //            mLog.info("Exception that can not obtain a free connection from current connection pool, current host is: " + targetServer + ", please check the connection config for this host! ");
    //            return true;
    //        }
    if (exception instanceof ConnectTimeoutException) {
        // Retry if the client  unable to establish a connection with the target server or proxy server within the given period of time.
        mLog.info("Expection that can not establish a connection with the target server " + targetServer
                + " within fixed amount time! ");
        return true;
    }

    if (exception instanceof ProtocolException) {
        mLog.info("Exception that violation of the HTTP specification against the target server: "
                + targetServer + "!");
        return false;

    }
    if (exception instanceof UnknownHostException) {
        mLog.info("UnknowHost:" + targetServer + "");
        return false;

    }
    if (exception instanceof ConnectException) {
        mLog.info(targetServer + " maybe have shutdown!");
        return false;

    }

    if (!method.isRequestSent()) {
        // Retry if the request has not been sent fully or
        // if it's OK to retry methods that have been sent
        return true;
    }
    // otherwise do not retry
    return false;
}

From source file:org.apache.camel.component.http.HttpProducer.java

/**
 * Creates the HttpMethod to use to call the remote server, either its GET or POST.
 *
 * @param exchange the exchange// w  w  w  .  j  a va  2  s  .  co m
 * @return the created method as either GET or POST
 * @throws CamelExchangeException is thrown if error creating RequestEntity
 */
@SuppressWarnings("deprecation")
protected HttpMethod createMethod(Exchange exchange) throws Exception {
    // creating the url to use takes 2-steps
    String url = HttpHelper.createURL(exchange, getEndpoint());
    URI uri = HttpHelper.createURI(exchange, url, getEndpoint());
    // get the url and query string from the uri
    url = uri.toASCIIString();
    String queryString = uri.getRawQuery();

    // execute any custom url rewrite
    String rewriteUrl = HttpHelper.urlRewrite(exchange, url, getEndpoint(), this);
    if (rewriteUrl != null) {
        // update url and query string from the rewritten url
        url = rewriteUrl;
        uri = new URI(url);
        // use raw query to have uri decimal encoded which http client requires
        queryString = uri.getRawQuery();
    }

    // remove query string as http client does not accept that
    if (url.indexOf('?') != -1) {
        url = url.substring(0, url.indexOf('?'));
    }

    // create http holder objects for the request
    RequestEntity requestEntity = createRequestEntity(exchange);
    HttpMethods methodToUse = HttpHelper.createMethod(exchange, getEndpoint(), requestEntity != null);
    HttpMethod method = methodToUse.createMethod(url);
    if (queryString != null) {
        // need to encode query string
        queryString = UnsafeUriCharactersEncoder.encode(queryString);
        method.setQueryString(queryString);
    }

    LOG.trace("Using URL: {} with method: {}", url, method);

    if (methodToUse.isEntityEnclosing()) {
        ((EntityEnclosingMethod) method).setRequestEntity(requestEntity);
        if (requestEntity != null && requestEntity.getContentType() == null) {
            LOG.debug("No Content-Type provided for URL: {} with exchange: {}", url, exchange);
        }
    }

    // there must be a host on the method
    if (method.getHostConfiguration().getHost() == null) {
        throw new IllegalArgumentException("Invalid uri: " + url
                + ". If you are forwarding/bridging http endpoints, then enable the bridgeEndpoint option on the endpoint: "
                + getEndpoint());
    }

    return method;
}

From source file:org.jboss.web.loadbalancer.Loadbalancer.java

protected HttpClient prepareServerRequest(HttpServletRequest request, HttpServletResponse response,
        HttpMethod method) {
    // clear state
    HttpClient client = new HttpClient(connectionManager);
    client.setStrictMode(false);/*  ww w .j  a  va2s  .c  o  m*/
    client.setTimeout(connectionTimeout);
    method.setFollowRedirects(false);
    method.setDoAuthentication(false);
    client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);

    Enumeration reqHeaders = request.getHeaderNames();

    while (reqHeaders.hasMoreElements()) {
        String headerName = (String) reqHeaders.nextElement();
        String headerValue = request.getHeader(headerName);

        if (!ignorableHeader.contains(headerName.toLowerCase())) {
            method.setRequestHeader(headerName, headerValue);
        }
    }

    //Cookies
    Cookie[] cookies = request.getCookies();
    HttpState state = client.getState();

    for (int i = 0; cookies != null && i < cookies.length; ++i) {
        Cookie cookie = cookies[i];

        org.apache.commons.httpclient.Cookie reqCookie = new org.apache.commons.httpclient.Cookie();

        reqCookie.setName(cookie.getName());
        reqCookie.setValue(cookie.getValue());

        if (cookie.getPath() != null) {
            reqCookie.setPath(cookie.getPath());
        } else {
            reqCookie.setPath("/");
        }

        reqCookie.setSecure(cookie.getSecure());

        reqCookie.setDomain(method.getHostConfiguration().getHost());
        state.addCookie(reqCookie);
    }
    return client;
}

From source file:org.jboss.web.loadbalancer.scheduler.SchedulerBase.java

public void setNodeDown(HttpServletRequest request, HttpServletResponse response, HttpClient client,
        HttpMethod method) {
    log.warn("Marking host with URL " + method.getHostConfiguration().getHostURL() + "/ as DOWN");
    try {//from  w  w w. j ava 2  s  . co  m
        URL methodURL = new URL(method.getHostConfiguration().getHostURL() + "/");
        synchronized (hostsUp) {
            int index = hostsUp.indexOf(methodURL);
            if (index == -1) {
                return;
            }
            hostsUp.remove(index);
        }
        synchronized (hostsDown) {
            hostsDown.add(methodURL);
        }
    } catch (MalformedURLException ex) {
        log.error("Could not mark host " + method.getHostConfiguration().getHostURL() + " down", ex);
    }
}