Java Code Examples for org.apache.commons.httpclient.util.EncodingUtil#getString()

The following examples show how to use org.apache.commons.httpclient.util.EncodingUtil#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpResponse.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String getResponse()
{
	if (responseBytes != null)
	{
		if (method instanceof HttpMethodBase)
		{
			// mimic method.getResponseBodyAsString
			return EncodingUtil.getString(responseBytes, ((HttpMethodBase)method).getResponseCharSet());
		}
		else
		{
			return new String(responseBytes);
		}
	}
	else
	{
		return null;
	}
}
 
Example 2
Source File: LaxHttpParser.java    From webarchive-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Read up to <tt>"\n"</tt> from an (unchunked) input stream.
 * If the stream ends before the line terminator is found,
 * the last part of the string will still be returned.
 * If no input data available, <code>null</code> is returned.
 *
 * @param inputStream the stream to read from
 * @param charset charset of HTTP protocol elements
 *
 * @throws IOException if an I/O problem occurs
 * @return a line from the stream
 * 
 * @since 3.0
 */
public static String readLine(InputStream inputStream, String charset) throws IOException {
    LOG.trace("enter LaxHttpParser.readLine(InputStream, String)");
    byte[] rawdata = readRawLine(inputStream);
    if (rawdata == null) {
        return null;
    }
    // strip CR and LF from the end
    int len = rawdata.length;
    int offset = 0;
    if (len > 0) {
        if (rawdata[len - 1] == '\n') {
            offset++;
            if (len > 1) {
                if (rawdata[len - 2] == '\r') {
                    offset++;
                }
            }
        }
    }
    return EncodingUtil.getString(rawdata, 0, len - offset, charset);
}
 
Example 3
Source File: HttpParser.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read up to <tt>"\n"</tt> from an (unchunked) input stream.
 * If the stream ends before the line terminator is found,
 * the last part of the string will still be returned.
 * If no input data available, <code>null</code> is returned.
 *
 * @param inputStream the stream to read from
 * @param charset charset of HTTP protocol elements
 *
 * @throws IOException if an I/O problem occurs
 * @return a line from the stream
 * 
 * @since 3.0
 */
public static String readLine(InputStream inputStream, String charset) throws IOException {
    LOG.trace("enter HttpParser.readLine(InputStream, String)");
    byte[] rawdata = readRawLine(inputStream);
    if (rawdata == null) {
        return null;
    }
    // strip CR and LF from the end
    int len = rawdata.length;
    int offset = 0;
    if (len > 0) {
        if (rawdata[len - 1] == '\n') {
            offset++;
            if (len > 1) {
                if (rawdata[len - 2] == '\r') {
                    offset++;
                }
            }
        }
    }
    final String result =
        EncodingUtil.getString(rawdata, 0, len - offset, charset);
    if (Wire.HEADER_WIRE.enabled()) {
        String logoutput = result;
        if (offset == 2)
            logoutput = result + "\r\n";
        else if (offset == 1)
            logoutput = result + "\n";
        Wire.HEADER_WIRE.input(logoutput);
    }
    return result;
}
 
Example 4
Source File: LaxURI.java    From webarchive-commons with Apache License 2.0 5 votes vote down vote up
protected static String decode(String component, String charset)
        throws URIException {
    if (component == null) {
        throw new IllegalArgumentException(
                "Component array of chars may not be null");
    }
    byte[] rawdata = null;
    //     try {
    rawdata = LaxURLCodec.decodeUrlLoose(EncodingUtil
            .getAsciiBytes(component));
    //     } catch (DecoderException e) {
    //         throw new URIException(e.getMessage());
    //     }
    return EncodingUtil.getString(rawdata, charset);
}
 
Example 5
Source File: HeaderedArchiveRecord.java    From webarchive-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Read header if present. Technique borrowed from HttpClient HttpParse
 * class. Using http parser code for now. Later move to more generic header
 * parsing code if there proves a need.
 * 
 * @return ByteArrayInputStream with the http header in it or null if no
 *         http header.
 * @throws IOException
 */
private InputStream readContentHeaders() throws IOException {
    // If judged a record that doesn't have an http header, return
    // immediately.
    if (!hasContentHeaders()) {
        return null;
    }
    byte [] statusBytes = LaxHttpParser.readRawLine(getIn());
    int eolCharCount = getEolCharsCount(statusBytes);
    if (eolCharCount <= 0) {
        throw new IOException("Failed to read raw lie where one " +
            " was expected: " + new String(statusBytes));
    }
    String statusLine = EncodingUtil.getString(statusBytes, 0,
        statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING);
    if (statusLine == null) {
        throw new NullPointerException("Expected status line is null");
    }
    // TODO: Tighten up this test.
    boolean isHttpResponse = StatusLine.startsWithHTTP(statusLine);
    boolean isHttpRequest = false;
    if (!isHttpResponse) {
        isHttpRequest = statusLine.toUpperCase().startsWith("GET") ||
            !statusLine.toUpperCase().startsWith("POST");
    }
    if (!isHttpResponse && !isHttpRequest) {
        throw new UnexpectedStartLineIOException("Failed parse of " +
            "status line: " + statusLine);
    }
    this.statusCode = isHttpResponse?
        (new StatusLine(statusLine)).getStatusCode(): -1;
    
    // Save off all bytes read.  Keep them as bytes rather than
    // convert to strings so we don't have to worry about encodings
    // though this should never be a problem doing http headers since
    // its all supposed to be ascii.
    ByteArrayOutputStream baos =
        new ByteArrayOutputStream(statusBytes.length + 4 * 1024);
    baos.write(statusBytes);
    
    // Now read rest of the header lines looking for the separation
    // between header and body.
    for (byte [] lineBytes = null; true;) {
        lineBytes = LaxHttpParser.readRawLine(getIn());
        eolCharCount = getEolCharsCount(lineBytes);
        if (eolCharCount <= 0) {
            throw new IOException("Failed reading headers: " +
                ((lineBytes != null)? new String(lineBytes): null));
        }
        // Save the bytes read.
        baos.write(lineBytes);
        if ((lineBytes.length - eolCharCount) <= 0) {
            // We've finished reading the http header.
            break;
        }
    }
    
    byte [] headerBytes = baos.toByteArray();
    // Save off where content body, post content headers, starts.
    this.contentHeadersLength = headerBytes.length;
    ByteArrayInputStream bais =
        new ByteArrayInputStream(headerBytes);
    if (!bais.markSupported()) {
        throw new IOException("ByteArrayInputStream does not support mark");
    }
    bais.mark(headerBytes.length);
    // Read the status line.  Don't let it into the parseHeaders function.
    // It doesn't know what to do with it.
    bais.read(statusBytes, 0, statusBytes.length);
    this.contentHeaders = LaxHttpParser.parseHeaders(bais,
        ARCConstants.DEFAULT_ENCODING);
    bais.reset();
    return bais;
}
 
Example 6
Source File: HttpMethodBase.java    From http4e with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header.
 * 
 * Note: This will cause the entire response body to be buffered in memory. A
 * malicious server may easily exhaust all the VM memory. It is strongly
 * recommended, to use getResponseAsStream if the content length of the response
 * is unknown or resonably large.
 * 
 * @return The response body.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString() throws IOException {
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody();
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}
 
Example 7
Source File: HttpMethodBase.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header. Buffers the response and this method can be 
 * called several times yielding the same result each time.
 * 
 * Note: This will cause the entire response body to be buffered in memory. A
 * malicious server may easily exhaust all the VM memory. It is strongly
 * recommended, to use getResponseAsStream if the content length of the response
 * is unknown or resonably large.
 * 
 * @return The response body or <code>null</code>.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString() throws IOException {
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody();
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}
 
Example 8
Source File: HttpMethodBase.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Returns the response body of the HTTP method, if any, as a {@link String}. 
 * If response body is not available or cannot be read, returns <tt>null</tt>
 * The string conversion on the data is done using the character encoding specified
 * in <tt>Content-Type</tt> header. Buffers the response and this method can be 
 * called several times yielding the same result each time.</p>
 * 
 * Note: This will cause the entire response body to be buffered in memory. This method is
 * safe if the content length of the response is unknown, because the amount of memory used
 * is limited.<p>
 * 
 * If the response is large this method involves lots of array copying and many object 
 * allocations, which makes it unsuitable for high-performance / low-footprint applications.
 * Those applications should use {@link #getResponseBodyAsStream()}.
 * 
 * @param maxlen the maximum content length to accept (number of bytes). Note that,
 * depending on the encoding, this is not equal to the number of characters.
 * @return The response body or <code>null</code>.
 * 
 * @throws IOException If an I/O (transport) problem occurs while obtaining the 
 * response body.
 */
public String getResponseBodyAsString(int maxlen) throws IOException {
    if (maxlen < 0) throw new IllegalArgumentException("maxlen must be positive");
    byte[] rawdata = null;
    if (responseAvailable()) {
        rawdata = getResponseBody(maxlen);
    }
    if (rawdata != null) {
        return EncodingUtil.getString(rawdata, getResponseCharSet());
    } else {
        return null;
    }
}
 
Example 9
Source File: URI.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Decodes URI encoded string.
 *
 * This is a two mapping, one from URI characters to octets, and
 * subsequently a second from octets to original characters:
 * <p><blockquote><pre>
 *   URI character sequence->octet sequence->original character sequence
 * </pre></blockquote><p>
 *
 * A URI must be separated into its components before the escaped
 * characters within those components can be allowedly decoded.
 * <p>
 * Notice that there is a chance that URI characters that are non UTF-8
 * may be parsed as valid UTF-8.  A recent non-scientific analysis found
 * that EUC encoded Japanese words had a 2.7% false reading; SJIS had a
 * 0.0005% false reading; other encoding such as ASCII or KOI-8 have a 0%
 * false reading.
 * <p>
 * The percent "%" character always has the reserved purpose of being
 * the escape indicator, it must be escaped as "%25" in order to be used
 * as data within a URI.
 * <p>
 * The unescape method is internally performed within this method.
 *
 * @param component the URI character sequence
 * @param charset the protocol charset
 * @return original character sequence
 * @throws URIException incomplete trailing escape pattern or unsupported
 * character encoding
 * 
 * @since 3.0
 */
protected static String decode(String component, String charset) 
    throws URIException {
    if (component == null) {
        throw new IllegalArgumentException("Component array of chars may not be null");
    }
    byte[] rawdata = null;
    try { 
        rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(component));
    } catch (DecoderException e) {
        throw new URIException(e.getMessage());
    }
    return EncodingUtil.getString(rawdata, charset);
}