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

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

Introduction

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

Prototype

public abstract StatusLine getStatusLine();

Source Link

Usage

From source file:edu.utah.further.core.ws.HttpResponseTo.java

/**
 * Copy-constructor from an {@link HttpMethod}.
 * /*  www .j a v a 2s . c  om*/
 * @param method
 *            method to copy fields from
 */
public HttpResponseTo(final HttpMethod method) throws IOException {
    this.httpMethod = edu.utah.further.core.api.ws.HttpMethod.valueOf(method.getName());
    this.statusLine = method.getStatusLine();

    for (final Header header : method.getResponseHeaders()) {
        this.responseHeaders.addHeader(new Header(header.getName(), header.getValue()));
    }

    this.path = method.getPath();
    this.queryString = method.getQueryString();
    this.responseBody = method.getResponseBody();
    setParams(method.getParams());
}

From source file:ehu.ned.DBpediaSpotlightClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//  ww  w .  j av a2 s . co m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        // // Deal with the response.
        // // Use caution: ensure correct character encoding and is not binary data
        InputStream responseBody = method.getResponseBodyAsStream();
        response = IOUtils.toString(responseBody, "UTF-8");

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:CertStreamCallback.java

private static void invokeActions(String url, String params, byte[] data, Part[] parts, String accept,
        String contentType, String sessionID, String language, String method, Callback callback) {

    if (params != null && !"".equals(params)) {
        if (url.contains("?")) {
            url = url + "&" + params;
        } else {/*from w ww  .j  a v  a  2s.  c  o  m*/
            url = url + "?" + params;
        }
    }

    if (language != null && !"".equals(language)) {
        if (url.contains("?")) {
            url = url + "&language=" + language;
        } else {
            url = url + "?language=" + language;
        }
    }

    HttpMethod httpMethod = null;

    try {
        HttpClient httpClient = new HttpClient(new HttpClientParams(), new SimpleHttpConnectionManager(true));
        httpMethod = getHttpMethod(url, method);
        if ((httpMethod instanceof PostMethod) || (httpMethod instanceof PutMethod)) {
            if (null != data)
                ((EntityEnclosingMethod) httpMethod).setRequestEntity(new ByteArrayRequestEntity(data));
            if (httpMethod instanceof PostMethod && null != parts)
                ((PostMethod) httpMethod)
                        .setRequestEntity(new MultipartRequestEntity(parts, httpMethod.getParams()));
        }

        if (sessionID != null) {
            httpMethod.addRequestHeader("Cookie", "JSESSIONID=" + sessionID);
        }

        if (!(httpMethod instanceof PostMethod && null != parts)) {
            if (null != accept && !"".equals(accept))
                httpMethod.addRequestHeader("Accept", accept);
            if (null != contentType && !"".equals(contentType))
                httpMethod.addRequestHeader("Content-Type", contentType);
        }

        int statusCode = httpClient.executeMethod(httpMethod);
        if (statusCode != HttpStatus.SC_OK) {
            throw ActionException.create(ClientMessages.CON_ERROR1, httpMethod.getStatusLine());
        }

        contentType = null != httpMethod.getResponseHeader("Content-Type")
                ? httpMethod.getResponseHeader("Content-Type").getValue()
                : accept;

        InputStream o = httpMethod.getResponseBodyAsStream();
        if (callback != null) {
            callback.execute(o, contentType, sessionID);
        }

    } catch (Exception e) {
        String result = BizClientUtils.doError(e, contentType);
        ByteArrayInputStream in = null;
        try {
            in = new ByteArrayInputStream(result.getBytes("UTF-8"));
            if (callback != null) {
                callback.execute(in, contentType, null);
            }

        } catch (UnsupportedEncodingException e1) {
            throw new RuntimeException(e1.getMessage() + "", e1);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    } finally {
        // 
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
}

From source file:AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;//from w  w w  .jav  a 2  s  .  co  m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:com.mythesis.userbehaviouranalysis.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;// w w w  .j  av a2 s  .c  o m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        //byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        StringWriter writer = new StringWriter();
        IOUtils.copy(responseBodyAsStream, writer, "utf-8");
        response = writer.toString();

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:ixa.pipe.ned.DBpediaSpotlightClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from w w  w  .j a v  a2 s . co  m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        // // Deal with the response.
        // // Use caution: ensure correct character encoding and is not binary data
        InputStream responseBody = method.getResponseBodyAsStream();
        response = StreamUtils.copyToString(responseBody, Charset.forName("UTF-8"));

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:colt.nicity.performance.latent.http.ApacheHttpClient.java

HttpResponse execute(HttpMethod method) throws IOException {

    applyHeadersCommonToAllRequests(method);

    byte[] responseBody;
    StatusLine statusLine;/* ww  w .ja v  a  2 s  . co m*/
    try {
        client.executeMethod(method);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        if (responseBodyAsStream != null) {
            copyLarge(responseBodyAsStream, outputStream, new byte[1024 * 4]);
        }

        responseBody = outputStream.toByteArray();
        statusLine = method.getStatusLine();

        // Catch exception and log error here? or silently fail?
    } finally {
        method.releaseConnection();
    }

    return new HttpResponse(statusLine.getStatusCode(), statusLine.getReasonPhrase(), responseBody);
}

From source file:net.sourceforge.jwbf.actions.HttpActionClient.java

/**
 * Process a GET Message.//from  ww w . ja v a2s . c  o  m
 * 
 * @param authgets
 *            a
 * @param cp
 *            a
 * @return a returning message, not null
 * @throws IOException on problems
 * @throws CookieException on problems
 * @throws ProcessException on problems
 */
public byte[] get(HttpMethod authgets) throws IOException, CookieException, ProcessException {
    showCookies(client);
    byte[] out = null;
    authgets.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET);
    //      System.err.println(authgets.getParams().getParameter("http.protocol.content-charset"));

    client.executeMethod(authgets);
    LOG.debug(authgets.getURI());
    LOG.debug("GET: " + authgets.getStatusLine().toString());

    out = authgets.getResponseBody();

    // release any connection resources used by the method
    authgets.releaseConnection();
    int statuscode = authgets.getStatusCode();

    if (statuscode == HttpStatus.SC_NOT_FOUND) {
        LOG.warn("Not Found: " + authgets.getQueryString());

        throw new FileNotFoundException(authgets.getQueryString());
    }

    return out;
}

From source file:it.haefelinger.flaka.util.HttpUpload.java

protected boolean exec(HttpMethod meth) {
    HttpClient client;//from   w w  w .  ja  va2s.c  o m
    boolean rc = false;

    /* exec http method */
    try {
        client = new HttpClient();
        settimeout(client);
        setcred(client);
        client.executeMethod(meth);
        rc = (meth.getStatusCode() / 100) == 2;
        if (rc == false) {
            setError(meth.getStatusLine().toString());
        }
    } catch (java.net.UnknownHostException ex) {
        set("errmsg", "unable to resolve host `" + ex.getMessage() + "'.");
    } catch (Exception ex) {
        setError(ex.getClass().getName() + " " + ex.getMessage());
        if (this.debug) {
            System.err.println("*** excepting seen while uploading ..");
            ex.printStackTrace(System.err);
            System.err.println("<<*>>");
        }
    }
    return rc;
}

From source file:com.hp.alm.ali.rest.client.AliRestClient.java

private void writeResponse(ResultInfo result, HttpMethod method, boolean writeBodyAndHeaders) {
    OutputStream bodyStream = result.getBodyStream();

    StatusLine statusLine = method.getStatusLine();
    if (statusLine != null) {
        result.setReasonPhrase(statusLine.getReasonPhrase());
    }/* ww  w .  j  a v a  2  s . c om*/
    try {
        result.setLocation(method.getURI().toString());
    } catch (URIException e) {
        throw new RuntimeException(e);
    }
    if (writeBodyAndHeaders) {
        Map<String, String> headersMap = result.getHeaders();
        Header[] headers = method.getResponseHeaders();
        for (Header header : headers) {
            headersMap.put(header.getName(), header.getValue());
        }
    }
    result.setHttpStatus(method.getStatusCode());
    Filter filter = new IdentityFilter(result);
    for (ResponseFilter responseFilter : responseFilters) {
        filter = responseFilter.applyFilter(filter, method, result);
    }
    if (writeBodyAndHeaders && bodyStream != null && method.getStatusCode() != 204) {
        try {
            InputStream responseBody = method.getResponseBodyAsStream();
            if (responseBody != null) {
                IOUtils.copy(responseBody, filter.getOutputStream());
                bodyStream.flush();
                bodyStream.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}