use of cz.msebera.android.httpclient.impl.io.HttpResponseParser in project Zom-Android by zom.
the class OtrDataHandler method onIncomingResponse.
public void onIncomingResponse(Address from, Address to, byte[] value) {
// Log.e( TAG, "onIncomingResponse:" + value.length);
SessionInputBuffer buffer = new MemorySessionInputBuffer(value);
HttpResponseParser parser = new HttpResponseParser(buffer, lineParser, responseFactory, params);
HttpResponse res;
try {
res = (HttpResponse) parser.parse();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (HttpException e) {
e.printStackTrace();
return;
}
String uid = res.getFirstHeader("Request-Id").getValue();
Request request = requestCache.get(uid);
if (request == null) {
debug("Unknown request ID " + uid);
return;
}
if (request.isSeen()) {
debug("Already seen request ID " + uid);
return;
}
request.seen();
int statusCode = res.getStatusLine().getStatusCode();
if (statusCode != 200) {
debug("got status " + statusCode + ": " + res.getStatusLine().getReasonPhrase());
// TODO handle error
return;
}
// TODO handle success
try {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
readIntoByteBuffer(byteBuffer, buffer);
// debug("Received sha1 @" + request.start + " is " + sha1sum(byteBuffer.toByteArray()));
if (request.method.equals("GET")) {
Transfer transfer = transferCache.get(request.url);
if (transfer == null) {
debug("Transfer expired for url " + request.url);
return;
}
transfer.chunkReceived(request, byteBuffer.toByteArray());
if (transfer.isDone()) {
// Log.e( TAG, "onIncomingResponse: isDone");
debug("Transfer complete for " + request.url);
String filename = transfer.closeFile();
Uri vfsUri = SecureMediaStore.vfsUri(filename);
if (transfer.checkSum()) {
// Log.e( TAG, "onIncomingResponse: writing");
if (mDataListener != null)
mDataListener.onTransferComplete(false, null, from.getAddress(), transfer.url, transfer.type, vfsUri.toString());
} else {
if (mDataListener != null)
mDataListener.onTransferFailed(false, null, to.getAddress(), transfer.url, "checksum");
debug("Wrong checksum for file");
}
} else {
if (mDataListener != null)
mDataListener.onTransferProgress(true, null, to.getAddress(), transfer.url, ((float) transfer.chunksReceived) / transfer.chunks);
transfer.perform();
debug("Progress " + transfer.chunksReceived + " / " + transfer.chunks);
}
}
} catch (IOException e) {
debug("Could not read line from response");
} catch (RemoteException e) {
debug("Could not read remote exception");
}
}
Aggregations