Example usage for org.apache.commons.httpclient.methods TraceMethod TraceMethod

List of usage examples for org.apache.commons.httpclient.methods TraceMethod TraceMethod

Introduction

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

Prototype

public TraceMethod(String paramString) 

Source Link

Usage

From source file:org.mozilla.zest.impl.ZestBasicRunner.java

private ZestResponse send(HttpClient httpclient, ZestRequest req) throws IOException {
    HttpMethod method;//  ww  w .  ja va2s  . c  om
    URI uri = new URI(req.getUrl().toString(), false);

    switch (req.getMethod()) {
    case "GET":
        method = new GetMethod(uri.toString());
        // Can only redirect on GETs
        method.setFollowRedirects(req.isFollowRedirects());
        break;
    case "POST":
        method = new PostMethod(uri.toString());
        break;
    case "OPTIONS":
        method = new OptionsMethod(uri.toString());
        break;
    case "HEAD":
        method = new HeadMethod(uri.toString());
        break;
    case "PUT":
        method = new PutMethod(uri.toString());
        break;
    case "DELETE":
        method = new DeleteMethod(uri.toString());
        break;
    case "TRACE":
        method = new TraceMethod(uri.toString());
        break;
    default:
        throw new IllegalArgumentException("Method not supported: " + req.getMethod());
    }

    setHeaders(method, req.getHeaders());

    for (Cookie cookie : req.getCookies()) {
        // Replace any Zest variables in the value
        cookie.setValue(this.replaceVariablesInString(cookie.getValue(), false));
        httpclient.getState().addCookie(cookie);
    }

    if (req.getMethod().equals("POST")) {
        // The setRequestEntity call trashes any Content-Type specified, so record it and reapply it after
        Header contentType = method.getRequestHeader("Content-Type");
        RequestEntity requestEntity = new StringRequestEntity(req.getData(), null, null);

        ((PostMethod) method).setRequestEntity(requestEntity);

        if (contentType != null) {
            method.setRequestHeader(contentType);
        }
    }

    int code = 0;
    String responseHeader = null;
    String responseBody = null;
    Date start = new Date();
    try {
        this.debug(req.getMethod() + " : " + req.getUrl());
        code = httpclient.executeMethod(method);

        responseHeader = method.getStatusLine().toString() + "\r\n" + arrayToStr(method.getResponseHeaders());
        responseBody = method.getResponseBodyAsString();

    } finally {
        method.releaseConnection();
    }
    // Update the headers with the ones actually sent
    req.setHeaders(arrayToStr(method.getRequestHeaders()));

    if (method.getStatusCode() == 302 && req.isFollowRedirects() && !req.getMethod().equals("GET")) {
        // Follow the redirect 'manually' as the httpclient lib only supports them for GET requests
        method = new GetMethod(method.getResponseHeader("Location").getValue());
        // Just in case there are multiple redirects
        method.setFollowRedirects(req.isFollowRedirects());

        try {
            this.debug(req.getMethod() + " : " + req.getUrl());
            code = httpclient.executeMethod(method);

            responseHeader = method.getStatusLine().toString() + "\r\n"
                    + arrayToStr(method.getResponseHeaders());
            responseBody = method.getResponseBodyAsString();

        } finally {
            method.releaseConnection();
        }
    }

    return new ZestResponse(req.getUrl(), responseHeader, responseBody, code,
            new Date().getTime() - start.getTime());
}

From source file:org.mule.transport.http.functional.HttpMethodTestCase.java

@Test
public void testTrace() throws Exception {
    TraceMethod method = new TraceMethod(getHttpEndpointAddress());
    int statusCode = client.executeMethod(method);
    assertEquals(HttpStatus.SC_OK, statusCode);
}

From source file:org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest.java

protected HttpMethod createTraceMethod(MuleMessage message) throws Exception {
    URI uri = getURI(message);//  w ww .ja v a 2s. c om
    return new TraceMethod(uri.toString());
}

From source file:org.owtf.runtime.core.ZestBasicRunner.java

private ZestResponse send(HttpClient httpclient, ZestRequest req) throws IOException {
    HttpMethod method;//w  ww . j  a  va  2 s.  c om
    URI uri = new URI(req.getUrl().toString(), false);

    switch (req.getMethod()) {
    case "GET":
        method = new GetMethod(uri.toString());
        // Can only redirect on GETs
        method.setFollowRedirects(req.isFollowRedirects());
        break;
    case "POST":
        method = new PostMethod(uri.toString());
        break;
    case "OPTIONS":
        method = new OptionsMethod(uri.toString());
        break;
    case "HEAD":
        method = new HeadMethod(uri.toString());
        break;
    case "PUT":
        method = new PutMethod(uri.toString());
        break;
    case "DELETE":
        method = new DeleteMethod(uri.toString());
        break;
    case "TRACE":
        method = new TraceMethod(uri.toString());
        break;
    default:
        throw new IllegalArgumentException("Method not supported: " + req.getMethod());
    }

    setHeaders(method, req.getHeaders());

    for (Cookie cookie : req.getCookies()) {
        // Replace any Zest variables in the value
        cookie.setValue(this.replaceVariablesInString(cookie.getValue(), false));
        httpclient.getState().addCookie(cookie);
    }

    if (req.getMethod().equals("POST")) {
        // Do this after setting the headers so the length is corrected
        RequestEntity requestEntity = new StringRequestEntity(req.getData(), null, null);
        ((PostMethod) method).setRequestEntity(requestEntity);
    }

    int code = 0;
    String responseHeader = null;
    String responseBody = null;
    Date start = new Date();
    try {
        this.debug(req.getMethod() + " : " + req.getUrl());
        code = httpclient.executeMethod(method);
        responseHeader = method.getStatusLine().toString() + "\n" + arrayToStr(method.getResponseHeaders());
        //   httpclient.getParams().setParameter("http.method.response.buffer.warnlimit",new Integer(1000000000));
        responseBody = method.getResponseBodyAsString();

    } finally {
        method.releaseConnection();
    }
    // Update the headers with the ones actually sent
    req.setHeaders(arrayToStr(method.getRequestHeaders()));

    return new ZestResponse(req.getUrl(), responseHeader, responseBody, code,
            new Date().getTime() - start.getTime());
}

From source file:org.springfield.mojo.http.HttpHelper.java

/**
 * Sends a standard HTTP request to the specified URI using the determined method.
 * Attaches the content, uses the specified content type, sets cookies, timeout and
 * request headers//w w  w  .  j  av  a 2s .co  m
 *  
 * @param method - the request method
 * @param uri - the uri to request
 * @param body - the content  
 * @param contentType - the content type
 * @param cookies - cookies
 * @param timeout - timeout in milliseconds
 * @param charSet - the character set
 * @param requestHeaders - extra user defined headers
 * @return response
 */
public static Response sendRequest(String method, String uri, String body, String contentType, String cookies,
        int timeout, String charSet, Map<String, String> requestHeaders) {
    // http client
    HttpClient client = new HttpClient();

    // method
    HttpMethodBase reqMethod = null;
    if (method.equals(HttpMethods.PUT)) {
        reqMethod = new PutMethod(uri);
    } else if (method.equals(HttpMethods.POST)) {
        reqMethod = new PostMethod(uri);
    } else if (method.equals(HttpMethods.GET)) {
        if (body != null) {
            // hack to be able to send a request body with a get (only if required)
            reqMethod = new PostMethod(uri) {
                public String getName() {
                    return "GET";
                }
            };
        } else {
            reqMethod = new GetMethod(uri);
        }
    } else if (method.equals(HttpMethods.DELETE)) {
        if (body != null) {
            // hack to be able to send a request body with a delete (only if required)
            reqMethod = new PostMethod(uri) {
                public String getName() {
                    return "DELETE";
                }
            };
        } else {
            reqMethod = new DeleteMethod(uri);
        }
    } else if (method.equals(HttpMethods.HEAD)) {
        reqMethod = new HeadMethod(uri);
    } else if (method.equals(HttpMethods.TRACE)) {
        reqMethod = new TraceMethod(uri);
    } else if (method.equals(HttpMethods.OPTIONS)) {
        reqMethod = new OptionsMethod(uri);
    }

    // add request body
    if (body != null) {
        try {
            RequestEntity entity = new StringRequestEntity(body, contentType, charSet);
            ((EntityEnclosingMethod) reqMethod).setRequestEntity(entity);
            reqMethod.setRequestHeader("Content-type", contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // add cookies
    if (cookies != null) {
        reqMethod.addRequestHeader("Cookie", cookies);
    }

    // add custom headers
    if (requestHeaders != null) {
        for (Map.Entry<String, String> header : requestHeaders.entrySet()) {
            String name = header.getKey();
            String value = header.getValue();

            reqMethod.addRequestHeader(name, value);
        }
    }

    Response response = new Response();

    // do request
    try {
        if (timeout != -1) {
            client.getParams().setSoTimeout(timeout);
        }
        int statusCode = client.executeMethod(reqMethod);
        response.setStatusCode(statusCode);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // read response
    try {
        InputStream instream = reqMethod.getResponseBodyAsStream();
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len;
        while ((len = instream.read(buffer)) > 0) {
            outstream.write(buffer, 0, len);
        }
        String resp = new String(outstream.toByteArray(), reqMethod.getResponseCharSet());
        response.setResponse(resp);

        //set content length
        long contentLength = reqMethod.getResponseContentLength();
        response.setContentLength(contentLength);
        //set character set
        String respCharSet = reqMethod.getResponseCharSet();
        response.setCharSet(respCharSet);
        //set all headers
        Header[] headers = reqMethod.getResponseHeaders();
        response.setHeaders(headers);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // release connection
    reqMethod.releaseConnection();

    // return
    return response;
}

From source file:org.springframework.http.client.CommonsClientHttpRequestFactory.java

/**
 * Create a Commons HttpMethodBase object for the given HTTP method
 * and URI specification./*from  w ww  . j  a  v a2  s.c o  m*/
 * @param httpMethod the HTTP method
 * @param uri the URI
 * @return the Commons HttpMethodBase object
 */
protected HttpMethodBase createCommonsHttpMethod(HttpMethod httpMethod, String uri) {
    switch (httpMethod) {
    case GET:
        return new GetMethod(uri);
    case DELETE:
        return new DeleteMethod(uri);
    case HEAD:
        return new HeadMethod(uri);
    case OPTIONS:
        return new OptionsMethod(uri);
    case POST:
        return new PostMethod(uri);
    case PUT:
        return new PutMethod(uri);
    case TRACE:
        return new TraceMethod(uri);
    case PATCH:
        throw new IllegalArgumentException(
                "HTTP method PATCH not available before Apache HttpComponents HttpClient 4.2");
    default:
        throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
    }
}