Example usage for org.apache.commons.httpclient StatusLine getReasonPhrase

List of usage examples for org.apache.commons.httpclient StatusLine getReasonPhrase

Introduction

In this page you can find the example usage for org.apache.commons.httpclient StatusLine getReasonPhrase.

Prototype

public final String getReasonPhrase() 

Source Link

Usage

From source file:com.springsource.insight.plugin.apache.http.hc3.HttpPlaceholderMethod.java

@Override
public String getStatusText() {
    StatusLine st = getStatusLine();
    return st.getReasonPhrase();
}

From source file:com.tasktop.c2c.server.ssh.server.commands.AbstractInteractiveProxyCommand.java

private void readHttpResponse(InputStream proxyInput) throws IOException, CommandException {
    String statusLineText = HttpParser.readLine(proxyInput, HTTP_ENTITY_CHARSET);
    StatusLine statusLine = new StatusLine(statusLineText);
    if (statusLine.getStatusCode() != HttpServletResponse.SC_OK) {
        String message = Integer.toString(statusLine.getStatusCode());
        String reasonPhrase = statusLine.getReasonPhrase();
        if (reasonPhrase != null && !reasonPhrase.isEmpty()) {
            message += ": " + reasonPhrase;
        }/*from  w ww  .j a  v a 2s  .c  o m*/
        throw new CommandException(-1, message);
    }
    Header[] parsedHeaders = HttpParser.parseHeaders(proxyInput, HTTP_ENTITY_CHARSET);
    HeaderGroup headerGroup = new HeaderGroup();
    headerGroup.setHeaders(parsedHeaders);

    Header transferEncoding = headerGroup.getFirstHeader("Transfer-Encoding");
    if (transferEncoding == null || !transferEncoding.getValue().equals("chunked")) {
        throw new IOException("Expected Transfer-Encoding of \"chunked\" but received " + transferEncoding);
    }
    Header contentType = headerGroup.getFirstHeader("Content-Type");
    if (contentType == null || !contentType.getValue().equals(MIME_TYPE_APPLICATION_OCTET_STREAM)) {
        throw new IOException("Unexpected Content-Type " + contentType);
    }
}

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  ava  2s  . c  o m*/
    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);
        }
    }
}

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

HttpResponse execute(HttpMethod method) throws IOException {

    applyHeadersCommonToAllRequests(method);

    byte[] responseBody;
    StatusLine statusLine;
    try {//from  w w w.  j  a v a 2  s.  c  o m
        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:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java

private HttpStreamResponse createStreamResponse(HttpMethod method) throws IOException {

    StatusLine statusLine = method.getStatusLine();
    String filename = getFileName(method);
    long length = getContentLength(method);
    String contentType = getContentType(method);

    HttpStreamResponse streamResponse = new HttpStreamResponse(statusLine.getStatusCode(),
            statusLine.getReasonPhrase(), method.getResponseBodyAsStream(), filename, contentType, length);
    return streamResponse;
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java

private HttpResponse execute(HttpMethod method) throws IOException {
    if (isOauthEnabled) {
        signWithOAuth(method);//from ww  w .  j a  v a  2s  .  com
    }

    applyHeadersCommonToAllRequests(method);

    byte[] responseBody;
    StatusLine statusLine = null;
    LOG.startTimer(TIMER_NAME);
    try {

        client.executeMethod(method);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        if (responseBodyAsStream != null) {
            IOUtils.copy(responseBodyAsStream, outputStream);
        }

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

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

    } finally {
        method.releaseConnection();
        LOG.stopTimer(TIMER_NAME);
    }
}

From source file:org.apache.ode.axis2.httpbinding.HttpClientHelper.java

/**
 * Convert a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1">HTTP status line</a> into an xml element like this:
 * <p/>//from   www.ja v a  2  s .co m
 * < Status-line>
 * < HTTP-Version>HTTP/1.1< /HTTP-Version>
 * < Status-Code>200< /Status-Code>
 * < Reason-Phrase>Success - The action was successfully received, understood, and accepted< /Reason-Phrase>
 * < /Status-line></br>
 *
 * @param statusLine - the {@link org.apache.commons.httpclient.StatusLine} instance to be converted
 * @param doc        - the document to use to create new nodes
 * @return an Element
 */
public static Element statusLineToElement(Document doc, StatusLine statusLine) {
    Element statusLineEl = doc.createElementNS(null, "Status-Line");
    Element versionEl = doc.createElementNS(null, "HTTP-Version");
    Element codeEl = doc.createElementNS(null, "Status-Code");
    Element reasonEl = doc.createElementNS(null, "Reason-Phrase");

    // wiring
    doc.appendChild(statusLineEl);
    statusLineEl.appendChild(versionEl);
    statusLineEl.appendChild(codeEl);
    statusLineEl.appendChild(reasonEl);

    // values
    versionEl.setTextContent(statusLine.getHttpVersion());
    codeEl.setTextContent(String.valueOf(statusLine.getStatusCode()));
    reasonEl.setTextContent(statusLine.getReasonPhrase());

    return statusLineEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpHelper.java

/**
 * Convert a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1">HTTP status line</a> into an xml element like this:
 * <p/>/*from   www.  j a  va  2s .c o m*/
 * < Status-line>
 * < HTTP-Version>HTTP/1.1< /HTTP-Version>
 * < Status-Code>200< /Status-Code>
 * < Reason-Phrase>Success - The action was successfully received, understood, and accepted< /Reason-Phrase>
 * < /Status-line></br>
 *
 * @param statusLine - the {@link org.apache.commons.httpclient.StatusLine} instance to be converted
 * @param doc        - the document to use to create new nodes
 * @return an Element
 */
public static Element statusLineToElement(Document doc, StatusLine statusLine) {
    Element statusLineEl = doc.createElementNS(null, "Status-Line");
    Element versionEl = doc.createElementNS(null, "HTTP-Version");
    Element codeEl = doc.createElementNS(null, "Status-Code");
    Element reasonEl = doc.createElementNS(null, "Reason-Phrase");
    Element originalEl = doc.createElementNS(null, "original");

    // wiring
    doc.appendChild(statusLineEl);
    statusLineEl.appendChild(versionEl);
    statusLineEl.appendChild(codeEl);
    statusLineEl.appendChild(reasonEl);
    statusLineEl.appendChild(originalEl);

    // values
    versionEl.setTextContent(statusLine.getHttpVersion());
    codeEl.setTextContent(String.valueOf(statusLine.getStatusCode()));
    reasonEl.setTextContent(statusLine.getReasonPhrase());
    // the line as received, not parsed
    originalEl.setTextContent(statusLine.toString());

    return statusLineEl;
}

From source file:org.eclipse.mylyn.github.internal.GitHubServiceException.java

protected GitHubServiceException(StatusLine statusLine) {
    this(String.format("HTTP %s: %s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
    httpStatusCode = statusLine.getStatusCode();
}

From source file:org.mule.transport.http.HttpResponse.java

public HttpResponse(final StatusLine statusline, final Header[] headers, final InputStream content)
        throws IOException {
    super();//  w ww. j ava2s  .c  o m
    if (statusline == null) {
        throw new IllegalArgumentException("Status line may not be null");
    }
    setStatusLine(HttpVersion.parse(statusline.getHttpVersion()), statusline.getStatusCode(),
            statusline.getReasonPhrase());
    setHeaders(headers);
    if (content != null) {
        InputStream in = content;
        Header contentLength = this.headers.getFirstHeader(HttpConstants.HEADER_CONTENT_LENGTH);
        Header transferEncoding = this.headers.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);

        if (transferEncoding != null) {
            if (transferEncoding.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) {
                in = new ChunkedInputStream(in);
            }
        } else if (contentLength != null) {
            long len = getContentLength();
            if (len >= 0) {
                in = new ContentLengthInputStream(in, len);
            }
        }
    }
}