Example usage for org.apache.commons.httpclient.methods DeleteMethod setParams

List of usage examples for org.apache.commons.httpclient.methods DeleteMethod setParams

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods DeleteMethod setParams.

Prototype

@Override
public void setParams(final HttpMethodParams params) 

Source Link

Document

Assigns HttpMethodParams HTTP protocol parameters for this method.

Usage

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
 *///w  w  w  .j  a v  a2 s .  c  om
@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();
    }
}