Search in sources :

Example 1 with HttpResponseParser

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");
    }
}
Also used : AbstractSessionInputBuffer(cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer) SessionInputBuffer(cz.msebera.android.httpclient.io.SessionInputBuffer) HttpRequest(cz.msebera.android.httpclient.HttpRequest) BasicHttpRequest(cz.msebera.android.httpclient.message.BasicHttpRequest) HttpResponse(cz.msebera.android.httpclient.HttpResponse) BasicHttpResponse(cz.msebera.android.httpclient.message.BasicHttpResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Uri(android.net.Uri) HttpResponseParser(cz.msebera.android.httpclient.impl.io.HttpResponseParser) HttpException(cz.msebera.android.httpclient.HttpException) RemoteException(android.os.RemoteException)

Aggregations

Uri (android.net.Uri)1 RemoteException (android.os.RemoteException)1 HttpException (cz.msebera.android.httpclient.HttpException)1 HttpRequest (cz.msebera.android.httpclient.HttpRequest)1 HttpResponse (cz.msebera.android.httpclient.HttpResponse)1 AbstractSessionInputBuffer (cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer)1 HttpResponseParser (cz.msebera.android.httpclient.impl.io.HttpResponseParser)1 SessionInputBuffer (cz.msebera.android.httpclient.io.SessionInputBuffer)1 BasicHttpRequest (cz.msebera.android.httpclient.message.BasicHttpRequest)1 BasicHttpResponse (cz.msebera.android.httpclient.message.BasicHttpResponse)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1