Example usage for org.apache.commons.httpclient HttpMethodBase getResponseCharSet

List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseCharSet

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase getResponseCharSet.

Prototype

public String getResponseCharSet() 

Source Link

Document

Returns the character encoding of the response from the Content-Type header.

Usage

From source file:com.honnix.cheater.validator.ContentValidator.java

private boolean isValidContent(HttpMethodBase method, String keyString) {
    boolean result = false;
    String charSet = method.getResponseCharSet();

    try {/*from   www  .  j av  a 2 s.c  o  m*/
        BufferedReader br = new BufferedReader(
                new InputStreamReader(method.getResponseBodyAsStream(), charSet));
        String line = null;

        while (!result && (line = br.readLine()) != null) {
            if (line.indexOf(keyString) != -1) {
                result = true;
            }
        }
    } catch (IOException e) {
        result = false;
    }

    return result;
}

From source file:com.sun.faban.harness.util.CLI.java

private void makeRequest(HttpMethodBase method) throws IOException {
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    int status = client.executeMethod(method);
    String enc = method.getResponseCharSet();

    InputStream response = method.getResponseBodyAsStream();

    if (status == HttpStatus.SC_NOT_FOUND) {
        System.err.println("Not found!");
        return;//from w  ww .  j a  v  a  2  s .c  o  m
    } else if (status == HttpStatus.SC_NO_CONTENT) {
        System.err.println("Empty!");
        return;
    } else if (response != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(response, enc));
        String line = null;
        while ((line = reader.readLine()) != null)
            System.out.println(line);
    } else if (status != HttpStatus.SC_OK)
        throw new IOException(HttpStatus.getStatusText(status));
}

From source file:com.sun.faban.harness.util.CLI.java

private String makeStringRequest(HttpMethodBase method) throws IOException {
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    int status = client.executeMethod(method);
    String enc = method.getResponseCharSet();

    InputStream response = method.getResponseBodyAsStream();
    StringBuilder buffer = new StringBuilder();
    if (status == HttpStatus.SC_NOT_FOUND) {
        System.err.println("Not found!");
        return buffer.toString();
    } else if (status == HttpStatus.SC_NO_CONTENT) {
        System.err.println("Empty!");
        return buffer.toString();
    } else if (response != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(response, enc));
        String line = null;//from w w w.j  a  v a 2  s  .c  om
        while ((line = reader.readLine()) != null)
            buffer.append(line).append('\n');
    } else if (status != HttpStatus.SC_OK)
        throw new IOException(HttpStatus.getStatusText(status));
    return buffer.toString();
}

From source file:net.adamcin.granite.client.packman.http3.Http3PackageManagerClient.java

private ListResponse executeListRequest(final HttpMethodBase request) throws IOException {
    int status = getClient().executeMethod(request);
    return parseListResponse(status, request.getStatusText(), request.getResponseBodyAsStream(),
            request.getResponseCharSet());
}

From source file:net.adamcin.granite.client.packman.http3.Http3PackageManagerClient.java

private SimpleResponse executeSimpleRequest(final HttpMethodBase request) throws IOException {
    int status = getClient().executeMethod(request);
    return parseSimpleResponse(status, request.getStatusText(), request.getResponseBodyAsStream(),
            request.getResponseCharSet());
}

From source file:de.michaeltamm.W3cMarkupValidator.java

private String getResponseBody(final HttpMethodBase httpMethod) throws IOException {
    final InputStream in = httpMethod.getResponseBodyAsStream();
    try {/*  w  w  w .  jav  a 2  s. c om*/
        final ByteArrayOutputStream out = new ByteArrayOutputStream(1000000);
        final byte[] buffer = new byte[4096];
        int n;
        while ((n = in.read(buffer)) > 0) {
            out.write(buffer, 0, n);
        }
        return out.toString(httpMethod.getResponseCharSet());
    } finally {
        try {
            in.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:net.adamcin.granite.client.packman.http3.Http3PackageManagerClient.java

private DetailedResponse executeDetailedRequest(final HttpMethodBase request,
        final ResponseProgressListener listener) throws IOException {
    int status = getClient().executeMethod(request);
    return parseDetailedResponse(status, request.getStatusText(), request.getResponseBodyAsStream(),
            request.getResponseCharSet(), listener);
}

From source file:com.github.rnewson.couchdb.lucene.Database.java

private synchronized String execute(final HttpMethodBase method) throws HttpException, IOException {
    try {//from w  ww .ja  va  2  s  .  com

        final int sc = CLIENT.executeMethod(method);
        if (sc == 401) {
            throw new HttpException("Unauthorized.");
        }
        final InputStream in = method.getResponseBodyAsStream();
        try {
            final StringWriter writer = new StringWriter(2048);
            IOUtils.copy(in, writer, method.getResponseCharSet());
            return writer.toString();
        } finally {
            in.close();
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:com.xmlcalabash.library.ApacheHttpRequest.java

private void readBodyContent(TreeWriter tree, InputStream bodyStream, HttpMethodBase method)
        throws SaxonApiException, IOException {
    // Find the content type
    String contentType = getContentType(method);
    String charset = method.getResponseCharSet();
    String boundary = null;//from   w  w w .j  a  v  a  2  s  . c  om

    if (overrideContentType != null) {
        contentType = overrideContentType;
    }

    if (contentType.startsWith("multipart/")) {
        boundary = getContentBoundary(method);

        tree.addStartElement(XProcConstants.c_multipart);
        tree.addAttribute(_content_type, getFullContentType(method));
        tree.addAttribute(_boundary, boundary);
        tree.startContent();

        for (Header header : method.getResponseHeaders()) {
            // FIXME: what about parameters?
            if (header.getName().toLowerCase().equals("transfer-encoding")) {
                // nop;
            } else {
                tree.addStartElement(XProcConstants.c_header);
                tree.addAttribute(_name, header.getName());
                tree.addAttribute(_value, header.getValue());
                tree.startContent();
                tree.addEndElement();
            }
        }

        MIMEReader reader = new MIMEReader(bodyStream, boundary);
        boolean done = false;
        while (reader.readHeaders()) {
            Header pctype = reader.getHeader("Content-Type");
            Header pclen = reader.getHeader("Content-Length");

            contentType = getHeaderValue(pctype);

            charset = getContentCharset(pctype);
            String partType = getHeaderValue(pctype);
            InputStream partStream = null;

            if (pclen != null) {
                int len = Integer.parseInt(getHeaderValue(pclen));
                partStream = reader.readBodyPart(len);
            } else {
                partStream = reader.readBodyPart();
            }

            tree.addStartElement(XProcConstants.c_body);
            tree.addAttribute(_content_type, contentType);
            if (!xmlContentType(contentType) && !textContentType(contentType)) {
                tree.addAttribute(_encoding, "base64");
            }
            tree.startContent();

            if (xmlContentType(partType)) {
                BufferedReader preader = new BufferedReader(new InputStreamReader(partStream, charset));
                // Read it as XML
                SAXSource source = new SAXSource(new InputSource(preader));
                DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder();
                tree.addSubtree(builder.build(source));
            } else if (textContentType(partType)) {
                BufferedReader preader = new BufferedReader(new InputStreamReader(partStream, charset));
                // Read it as text
                char buf[] = new char[bufSize];
                int len = preader.read(buf, 0, bufSize);
                while (len >= 0) {
                    // I'm unsure about this. If I'm reading text and injecting it into XML,
                    // I think I need to change CR/LF pairs (and CR not followed by LF) into
                    // plain LFs.

                    char fbuf[] = new char[bufSize];
                    char flen = 0;
                    for (int pos = 0; pos < len; pos++) {
                        if (buf[pos] == '\r') {
                            if (pos + 1 == len) {
                                // FIXME: Check for CR/LF pairs that cross a buffer boundary!
                                // Assume it's part of a CR/LF pair...
                            } else {
                                if (buf[pos + 1] == '\n') {
                                    // nop
                                } else {
                                    fbuf[flen++] = '\n';
                                }
                            }
                        } else {
                            fbuf[flen++] = buf[pos];
                        }
                    }

                    tree.addText(new String(fbuf, 0, flen));
                    len = preader.read(buf, 0, bufSize);
                }
            } else {
                // Read it as binary
                byte bytes[] = new byte[bufSize];
                int pos = 0;
                int readLen = bufSize;
                int len = partStream.read(bytes, 0, bufSize);
                while (len >= 0) {
                    pos += len;
                    readLen -= len;
                    if (readLen == 0) {
                        tree.addText(Base64.encodeBytes(bytes));
                        pos = 0;
                        readLen = bufSize;
                    }

                    len = partStream.read(bytes, pos, readLen);
                }

                if (pos > 0) {
                    byte lastBytes[] = new byte[pos];
                    System.arraycopy(bytes, 0, lastBytes, 0, pos);
                    tree.addText(Base64.encodeBytes(lastBytes));
                }

                tree.addText("\n"); // FIXME: should we be doing this?
            }

            tree.addEndElement();
        }

        tree.addEndElement();
    } else {
        if (xmlContentType(contentType)) {
            readBodyContentPart(tree, bodyStream, contentType, charset);
        } else {
            tree.addStartElement(XProcConstants.c_body);
            tree.addAttribute(_content_type, getFullContentType(method));
            if (!xmlContentType(contentType) && !textContentType(contentType)) {
                tree.addAttribute(_encoding, "base64");
            }
            tree.startContent();
            readBodyContentPart(tree, bodyStream, contentType, charset);
            tree.addEndElement();
        }
    }
}

From source file:com.xmlcalabash.library.HttpRequest.java

private void readBodyContent(TreeWriter tree, InputStream bodyStream, HttpMethodBase method)
        throws SaxonApiException, IOException {
    String contentType = getFullContentType(method);
    String charset = method.getResponseCharSet();
    String boundary = getContentBoundary(method);

    if (overrideContentType != null) {
        contentType = overrideContentType;
    }/*from  w ww  . j a  v  a2 s.c o  m*/

    if (contentType.startsWith("multipart/")) {
        tree.addStartElement(XProcConstants.c_multipart);
        tree.addAttribute(_content_type, contentType);
        tree.addAttribute(_boundary, boundary);
        tree.startContent();

        readMultipartContent(tree, bodyStream, boundary);

        tree.addEndElement();
    } else {
        if (!detailed && (xmlContentType(contentType) || jsonContentType(contentType))) {
            readBodyContentPart(tree, bodyStream, contentType, charset);
        } else {
            tree.addStartElement(XProcConstants.c_body);
            tree.addAttribute(_content_type, contentType);
            if (!xmlContentType(contentType) && !textContentType(contentType)
                    && !jsonContentType(contentType)) {
                tree.addAttribute(_encoding, "base64");
            }
            tree.startContent();
            readBodyContentPart(tree, bodyStream, contentType, charset);
            tree.addEndElement();
        }
    }
}