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);
}
}
Aggregations