Example usage for org.apache.commons.httpclient.params HttpMethodParams HttpMethodParams

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams HttpMethodParams

Introduction

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

Prototype

public HttpMethodParams() 

Source Link

Usage

From source file:com.taobao.diamond.client.processor.ServerAddressProcessor.java

/**
 * diamond//from   w ww .ja  v  a  2s  .  c o m
 * 
 * @param acquireCount
 *            01
 * @return
 */
private boolean acquireServerAddressOnce(int acquireCount) {
    HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration();
    String configServerAddress;
    int port;
    if (null != diamondConfigure.getConfigServerAddress()) {
        configServerAddress = diamondConfigure.getConfigServerAddress();
        port = diamondConfigure.getConfigServerPort();
    } else {
        if (acquireCount == 0) {
            configServerAddress = Constants.DEFAULT_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        } else {
            configServerAddress = Constants.DAILY_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        }
    }
    hostConfiguration.setHost(configServerAddress, port);

    String serverAddressUrl = Constants.CONFIG_HTTP_URI_FILE;

    HttpMethod httpMethod = new GetMethod(serverAddressUrl);
    // HttpMethod
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout(diamondConfigure.getOnceTimeout());
    // ///////////////////////
    httpMethod.setParams(params);

    try {
        if (SC_OK == configHttpClient.executeMethod(httpMethod)) {
            InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream());
            BufferedReader bufferedReader = new BufferedReader(reader);
            String address = null;
            List<String> newDomainNameList = new LinkedList<String>();
            while ((address = bufferedReader.readLine()) != null) {
                address = address.trim();
                if (StringUtils.isNotBlank(address)) {
                    newDomainNameList.add(address);
                }
            }
            if (newDomainNameList.size() > 0) {
                log.debug("");
                this.diamondConfigure.setDomainNameList(newDomainNameList);
                return true;
            }
        } else {
            log.warn("");
        }
    } catch (HttpException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (IOException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (Exception e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } finally {
        httpMethod.releaseConnection();
    }
    return false;
}

From source file:cn.leancloud.diamond.client.processor.ServerAddressProcessor.java

/**
 * ?diamond??/* ww  w . j a va  2  s.  c o  m*/
 * 
 * @param acquireCount
 *            ?01?
 * @return
 */
private boolean acquireServerAddressOnce(int acquireCount) {
    HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration();
    String configServerAddress;
    int port;
    if (null != diamondConfigure.getConfigServerAddress()) {
        configServerAddress = diamondConfigure.getConfigServerAddress();
        port = diamondConfigure.getConfigServerPort();
    } else {
        if (acquireCount == 0) {
            configServerAddress = Constants.DEFAULT_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        } else {
            configServerAddress = Constants.DAILY_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        }
    }
    hostConfiguration.setHost(configServerAddress, port);

    String serverAddressUrl = Constants.CONFIG_HTTP_URI_FILE;

    HttpMethod httpMethod = new GetMethod(serverAddressUrl);
    // HttpMethod?
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout(diamondConfigure.getOnceTimeout());
    // ///////////////////////
    httpMethod.setParams(params);

    try {
        if (SC_OK == configHttpClient.executeMethod(httpMethod)) {
            InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream());
            BufferedReader bufferedReader = new BufferedReader(reader);
            String address = null;
            List<String> newDomainNameList = new LinkedList<String>();
            while ((address = bufferedReader.readLine()) != null) {
                address = address.trim();
                if (StringUtils.isNotBlank(address)) {
                    newDomainNameList.add(address);
                }
            }
            if (newDomainNameList.size() > 0) {
                log.debug("?");
                this.diamondConfigure.setDomainNameList(newDomainNameList);
                return true;
            }
        } else {
            log.warn("??");
        }
    } catch (HttpException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (IOException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (Exception e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } finally {
        httpMethod.releaseConnection();
    }
    return false;
}

From source file:com.mobilefirst.fiberlink.WebServiceRequest.java

/**
 * setMethodParams sets methods parameters
 * @param method: method whose parameters to be set
 * @param params: parameter list to be set
 * @return HttpMethodParams   Object of HttpMethodParams
 *//*from   w ww.  j ava 2  s . c  om*/
public HttpMethodParams setMethodParams(HttpMethod method, Hashtable<String, String> params) {
    HttpMethodParams httpMethodParams = new HttpMethodParams();
    Enumeration<String> keys = params.keys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        if (method instanceof PostMethod) {
            ((PostMethod) method).setParameter(key, params.get(key));
            System.out.println(key + " :: " + params.get(key) + "\n");
        }
    }
    return httpMethodParams;
}

From source file:com.mengka.diamond.client.processor.ServerAddressProcessor.java

/**
 * ?diamond??// ww  w .  jav  a 2  s . c  om
 * 
 * @param acquireCount
 *            ?01?
 * @return
 */
private boolean acquireServerAddressOnce(int acquireCount) {
    HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration();
    String configServerAddress;
    int port;
    if (null != diamondConfigure.getConfigServerAddress()) {
        configServerAddress = diamondConfigure.getConfigServerAddress();
        port = diamondConfigure.getConfigServerPort();
    } else {
        if (acquireCount == 0) {
            configServerAddress = Constants.CONFIG_DEFAULT_DOMAINNAME;
            port = Constants.CONFIG_DEFAULT_PORT;
        } else {
            configServerAddress = Constants.CONFIG_DAILY_DOMAINNAME;
            port = Constants.CONFIG_DEFAULT_PORT;
        }
    }
    hostConfiguration.setHost(configServerAddress, port);

    String serverAddressUrl = Constants.SERVER_ADDRESS_URI;

    HttpMethod httpMethod = new GetMethod(serverAddressUrl);
    // HttpMethod?
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout(diamondConfigure.getOnceTimeout());
    // ///////////////////////
    httpMethod.setParams(params);

    try {
        if (SC_OK == configHttpClient.executeMethod(httpMethod)) {
            InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream());
            BufferedReader bufferedReader = new BufferedReader(reader);
            String address = null;
            List<String> newDomainNameList = new LinkedList<String>();
            while ((address = bufferedReader.readLine()) != null) {
                address = address.trim();
                if (StringUtils.isNotBlank(address) && !address.startsWith("#")) {
                    if (address.contains(":")) {
                        address = address.substring(0, address.indexOf(":"));
                    }
                    if (address.contains("\\")) {
                        address = address.substring(0, address.indexOf("\\"));
                    }
                    if (address.contains("/")) {
                        address = address.substring(0, address.indexOf("/"));
                    }
                    newDomainNameList.add(address);
                }
            }
            if (newDomainNameList.size() > 0) {
                log.debug("?");
                this.diamondConfigure.setDomainNameList(newDomainNameList);
                return true;
            }
        } else {
            log.error("??, hostsconfig.tbj.com");
        }
    } catch (HttpException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (IOException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (Exception e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } finally {
        httpMethod.releaseConnection();
    }
    return false;
}

From source file:com.starit.diamond.client.processor.ServerAddressProcessor.java

/**
 * ?diamond??/* w  w w  .  j a  va  2  s. co  m*/
 * 
 * @param acquireCount
 *            ?01?
 * @return
 */
private boolean acquireServerAddressOnce(int acquireCount) {
    HostConfiguration hostConfiguration = configHttpClient.getHostConfiguration();
    String configServerAddress;
    int port;
    if (null != diamondConfigure.getConfigServerAddress()) {
        configServerAddress = diamondConfigure.getConfigServerAddress();
        port = diamondConfigure.getConfigServerPort();
    } else {
        if (acquireCount == 0) {
            configServerAddress = Constants.DEFAULT_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        } else {
            configServerAddress = Constants.DAILY_DOMAINNAME;
            port = Constants.DEFAULT_PORT;
        }
    }
    hostConfiguration.setHost(configServerAddress, port);

    // ??serverURL by leiwen
    String serverAddressUrl = Constants.BASE_URI + "/" + clusterType;

    HttpMethod httpMethod = new GetMethod(serverAddressUrl);
    // HttpMethod?
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout(diamondConfigure.getOnceTimeout());
    // ///////////////////////
    httpMethod.setParams(params);

    try {
        if (SC_OK == configHttpClient.executeMethod(httpMethod)) {
            InputStreamReader reader = new InputStreamReader(httpMethod.getResponseBodyAsStream());
            BufferedReader bufferedReader = new BufferedReader(reader);
            String address = null;
            List<String> newDomainNameList = new LinkedList<String>();
            while ((address = bufferedReader.readLine()) != null) {
                address = address.trim();
                if (StringUtils.isNotBlank(address)) {
                    newDomainNameList.add(address);
                }
            }
            if (newDomainNameList.size() > 0) {
                log.debug("?");
                this.diamondConfigure.setDomainNameList(newDomainNameList);
                return true;
            }
        } else {
            log.warn("??");
        }
    } catch (HttpException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (IOException e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } catch (Exception e) {
        log.error(getErrorMessage(configServerAddress) + ", " + e);
    } finally {
        httpMethod.releaseConnection();
    }
    return false;
}

From source file:com.worldline.easycukes.rest.client.RestService.java

/**
* Allows to send a PUT request, with parameters using JSON format
*
* @param path path to be used for the PUT request
* @param data paremeters to be used (JSON format) as a string
*//*  ww  w . j  a v a  2s  .  c om*/
@SuppressWarnings("unchecked")
public void sendPut(final String path, final String data) {
    String fullpath = path;

    if (path.startsWith("/"))
        fullpath = baseUrl + path;

    log.info("Sending PUT request to " + fullpath);

    final PutMethod method = new PutMethod(fullpath);

    for (final Map.Entry<String, String> header : requestHeaders.entrySet())
        method.setRequestHeader(header.getKey(), header.getValue());

    if (data != null) {
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONHelper.toJSONObject(data);

            for (final Iterator<String> iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
                final String param = iterator.next();
                HttpMethodParams methodParams = new HttpMethodParams();
                methodParams.setParameter(param,
                        (jsonObject.get(param) != null) ? jsonObject.get(param).toString() : null);
                method.setParams(methodParams);
            }
        } catch (final ParseException e) {
            log.error("Sorry, parameters are not proper JSON...", e);
        }
    }

    try {
        if (nonProxyHost != null && fullpath.contains(nonProxyHost)) {
            httpClient.getHostConfiguration().setProxyHost(null);
        }
        final int statusCode = httpClient.executeMethod(method);
        response = new ResponseWrapper(method.getResponseBodyAsString(), method.getResponseHeaders(),
                statusCode);
    } catch (final IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.worldline.easycukes.rest.client.RestService.java

/**
 * Allows to send a DELETE request, with parameters using JSON format
 *
 * @param path path to be used for the DELETE request
 * @param data paremeters to be used (JSON format) as a string
 *//*from  w  ww  .  j  a v  a  2s  .c o  m*/
@SuppressWarnings("unchecked")
public void sendDelete(final String path, final String data) {
    String fullpath = path;

    if (path.startsWith("/"))
        fullpath = baseUrl + path;

    log.info("Sending DELETE request to " + fullpath);

    final DeleteMethod method = new DeleteMethod(fullpath);

    for (final Map.Entry<String, String> header : requestHeaders.entrySet())
        method.setRequestHeader(header.getKey(), header.getValue());

    if (data != null) {
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONHelper.toJSONObject(data);

            for (final Iterator<String> iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
                final String param = iterator.next();
                HttpMethodParams methodParams = new HttpMethodParams();
                methodParams.setParameter(param,
                        (jsonObject.get(param) != null) ? jsonObject.get(param).toString() : null);
                method.setParams(methodParams);

            }
        } catch (final ParseException e) {
            log.error("Sorry, parameters are not proper JSON...", e);
        }

    }
    try {
        if (nonProxyHost != null && fullpath.contains(nonProxyHost)) {
            httpClient.getHostConfiguration().setProxyHost(null);
        }
        final int statusCode = httpClient.executeMethod(method);
        response = new ResponseWrapper(method.getResponseBodyAsString(), method.getResponseHeaders(),
                statusCode);
    } catch (final IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondPublisher.java

private void configureHttpMethod(HttpMethod method) {
    // socket//from  ww w  .  ja v  a 2  s. c o  m
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout(Constants.ONCE_TIMEOUT);
    method.setParams(params);
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

private void configureHttpMethod(boolean skipContentCache, CacheData cacheData, long onceTimeOut,
        HttpMethod httpMethod) {/*from   w  w w.  j a v a2 s .  c  o  m*/
    if (skipContentCache && null != cacheData) {
        if (null != cacheData.getLastModifiedHeader() && Constants.NULL != cacheData.getLastModifiedHeader()) {
            httpMethod.addRequestHeader(Constants.IF_MODIFIED_SINCE, cacheData.getLastModifiedHeader());
        }
        if (null != cacheData.getMd5() && Constants.NULL != cacheData.getMd5()) {
            httpMethod.addRequestHeader(Constants.CONTENT_MD5, cacheData.getMd5());
        }
    }

    httpMethod.addRequestHeader(Constants.ACCEPT_ENCODING, "gzip,deflate");

    // HttpMethod
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout((int) onceTimeOut);
    // ///////////////////////
    httpMethod.setParams(params);
    httpClient.getHostConfiguration().setHost(
            diamondConfigure.getDomainNameList().get(this.domainNamePos.get()), diamondConfigure.getPort());
}

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

private void configureHttpMethod(boolean skipContentCache, CacheData cacheData, long onceTimeOut,
        HttpMethod httpMethod) {/*from   www.ja v  a 2 s. co  m*/
    if (skipContentCache && null != cacheData) {
        if (null != cacheData.getLastModifiedHeader() && Constants.NULL != cacheData.getLastModifiedHeader()) {
            httpMethod.addRequestHeader(Constants.IF_MODIFIED_SINCE, cacheData.getLastModifiedHeader());
        }
        if (null != cacheData.getMd5() && Constants.NULL != cacheData.getMd5()) {
            httpMethod.addRequestHeader(Constants.CONTENT_MD5, cacheData.getMd5());
        }
    }

    httpMethod.addRequestHeader(Constants.ACCEPT_ENCODING, "gzip,deflate");

    // HttpMethod?
    HttpMethodParams params = new HttpMethodParams();
    params.setSoTimeout((int) onceTimeOut);
    // ///////////////////////
    httpMethod.setParams(params);
    httpClient.getHostConfiguration().setHost(
            diamondConfigure.getDomainNameList().get(this.domainNamePos.get()), diamondConfigure.getPort());
}