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

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

Introduction

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

Prototype

public abstract void setPath(String paramString);

Source Link

Usage

From source file:org.svenk.redmine.core.client.AbstractRedmineClient.java

synchronized protected int performExecuteMethod(HttpMethod method, HostConfiguration hostConfiguration,
        IProgressMonitor monitor) throws RedmineException {
    try {//from w w  w. ja va 2s  .  c  o m
        //complete URL
        String baseUrl = new URL(location.getUrl()).getPath();
        if (!method.getPath().startsWith(baseUrl)) {
            method.setPath(baseUrl + method.getPath());
        }

        return WebUtil.execute(httpClient, hostConfiguration, method, monitor);
    } catch (OperationCanceledException e) {
        monitor.setCanceled(true);
        throw new RedmineException(e.getMessage(), e);
    } catch (RuntimeException e) {
        IStatus status = RedmineCorePlugin.toStatus(e, null,
                Messages.AbstractRedmineClient_UNHANDLED_RUNTIME_EXCEPTION);
        StatusHandler.fail(status);
        throw new RedmineStatusException(status);
    } catch (IOException e) {
        IStatus status = RedmineCorePlugin.toStatus(e, null);
        StatusHandler.log(status);
        throw new RedmineStatusException(status);
    }
}

From source file:org.tuckey.web.filters.urlrewrite.RequestProxy.java

private static HttpMethod setupProxyRequest(final HttpServletRequest hsRequest, final URL targetUrl)
        throws IOException {
    final String methodName = hsRequest.getMethod();
    final HttpMethod method;
    if ("POST".equalsIgnoreCase(methodName)) {
        PostMethod postMethod = new PostMethod();
        InputStreamRequestEntity inputStreamRequestEntity = new InputStreamRequestEntity(
                hsRequest.getInputStream());
        postMethod.setRequestEntity(inputStreamRequestEntity);
        method = postMethod;/*  w ww  .j a v a2  s. com*/
    } else if ("GET".equalsIgnoreCase(methodName)) {
        method = new GetMethod();
    } else if ("PUT".equalsIgnoreCase(methodName)) {
        PutMethod putMethod = new PutMethod();
        InputStreamRequestEntity inputStreamRequestEntity = new InputStreamRequestEntity(
                hsRequest.getInputStream());
        putMethod.setRequestEntity(inputStreamRequestEntity);
        method = putMethod;
    } else if ("DELETE".equalsIgnoreCase(methodName)) {
        method = new DeleteMethod();
    } else {
        log.warn("Unsupported HTTP method requested: " + hsRequest.getMethod());
        return null;
    }

    method.setFollowRedirects(false);
    method.setPath(targetUrl.getPath());
    method.setQueryString(targetUrl.getQuery());

    Enumeration e = hsRequest.getHeaderNames();
    if (e != null) {
        while (e.hasMoreElements()) {
            String headerName = (String) e.nextElement();
            if ("host".equalsIgnoreCase(headerName)) {
                //the host value is set by the http client
                continue;
            } else if ("content-length".equalsIgnoreCase(headerName)) {
                //the content-length is managed by the http client
                continue;
            } else if ("accept-encoding".equalsIgnoreCase(headerName)) {
                //the accepted encoding should only be those accepted by the http client.
                //The response stream should (afaik) be deflated. If our http client does not support
                //gzip then the response can not be unzipped and is delivered wrong.
                continue;
            } else if (headerName.toLowerCase().startsWith("cookie")) {
                //fixme : don't set any cookies in the proxied request, this needs a cleaner solution
                continue;
            }

            Enumeration values = hsRequest.getHeaders(headerName);
            while (values.hasMoreElements()) {
                String headerValue = (String) values.nextElement();
                log.info("setting proxy request parameter:" + headerName + ", value: " + headerValue);
                method.addRequestHeader(headerName, headerValue);
            }
        }
    }

    if (log.isInfoEnabled())
        log.info("proxy query string " + method.getQueryString());
    return method;
}

From source file:org.tuleap.mylyn.task.core.internal.client.rest.RestOperation.java

/**
 * Provides the Method to run./*from w  ww  .j  av  a 2 s  .co  m*/
 *
 * @return The method to run.
 */
public HttpMethod createMethod() {
    HttpMethod m = method.create();
    if (m instanceof EntityEnclosingMethod) {
        StringRequestEntity entity;
        try {
            if (body == null) {
                entity = new StringRequestEntity(EMPTY_BODY, CONTENT_TYPE_JSON, ENCODING_UTF8);
            } else {
                entity = new StringRequestEntity(body, CONTENT_TYPE_JSON, ENCODING_UTF8);
            }
        } catch (UnsupportedEncodingException e) {
            logger.log(new Status(IStatus.ERROR, TuleapCoreActivator.PLUGIN_ID,
                    TuleapCoreMessages.getString(TuleapCoreKeys.encodingUtf8NotSupported)));
            return null;
        }
        ((EntityEnclosingMethod) m).setRequestEntity(entity);
    }
    m.setPath(fullUrl);
    for (Entry<String, String> entry : requestHeaders.entrySet()) {
        m.addRequestHeader(entry.getKey(), entry.getValue());
    }
    NameValuePair[] queryParams = new NameValuePair[requestParameters.size()];
    int i = 0;
    for (Entry<String, String> entry : requestParameters.entries()) {
        queryParams[i++] = new NameValuePair(entry.getKey(), entry.getValue());
    }
    m.setQueryString(queryParams);
    return m;
}