Search in sources :

Example 1 with ContentControlUrlConnection

use of com.stanfy.enroscar.net.ContentControlUrlConnection in project enroscar by stanfy.

the class BaseContentHandler method getContent.

@Override
public final Object getContent(final URLConnection uConn) throws IOException {
    final ContentControlUrlConnection connection = ContentControlUrlConnection.from(uConn);
    if (connection == null) {
        throw new IllegalArgumentException("Connection is not wrapped with " + ContentControlUrlConnection.class);
    }
    // try to get input stream
    InputStream responseStream = null;
    try {
        responseStream = connection.getInputStream();
    } catch (final IOException responseStreamException) {
        if (Utils.isDebugRest(context)) {
            Log.v(TAG, "Cannot get input stream, message: " + responseStreamException.getMessage() + ", try to use error stream");
        }
        final URLConnection orig = UrlConnectionWrapper.unwrap(connection);
        if (orig instanceof HttpURLConnection) {
            responseStream = ((HttpURLConnection) orig).getErrorStream();
        }
        // no error stream?
        if (responseStream == null) {
            throw responseStreamException;
        }
    }
    // we have input => wrap it for reading
    InputStream source = IoUtils.getUncompressedInputStream(connection.getContentEncoding(), buffersPool.bufferize(responseStream));
    if (Utils.isDebugRestResponse(context)) {
        // source is now closed, don't worry
        final String responseString = IoUtils.streamToString(source, buffersPool);
        Log.d(TAG, responseString);
        source = new ByteArrayInputStream(responseString.getBytes(IoUtils.UTF_8_NAME));
    }
    try {
        return getContent(connection, source, connection.getEntityType());
    } finally {
        // do not forget to close the source
        IoUtils.closeQuietly(source);
    }
}
Also used : ContentControlUrlConnection(com.stanfy.enroscar.net.ContentControlUrlConnection) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Aggregations

ContentControlUrlConnection (com.stanfy.enroscar.net.ContentControlUrlConnection)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URLConnection (java.net.URLConnection)1