Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync.net; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.BufferedReader; |
michael@0 | 8 | import java.io.IOException; |
michael@0 | 9 | import java.io.InputStream; |
michael@0 | 10 | import java.io.InputStreamReader; |
michael@0 | 11 | import java.net.URI; |
michael@0 | 12 | |
michael@0 | 13 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 14 | |
michael@0 | 15 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 16 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 17 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 18 | import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; |
michael@0 | 19 | import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient; |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * A request class that handles line-by-line responses. Eventually this will |
michael@0 | 23 | * handle real stream processing; for now, just parse the returned body |
michael@0 | 24 | * line-by-line. |
michael@0 | 25 | * |
michael@0 | 26 | * @author rnewman |
michael@0 | 27 | * |
michael@0 | 28 | */ |
michael@0 | 29 | public class SyncStorageCollectionRequest extends SyncStorageRequest { |
michael@0 | 30 | private static final String LOG_TAG = "CollectionRequest"; |
michael@0 | 31 | |
michael@0 | 32 | public SyncStorageCollectionRequest(URI uri) { |
michael@0 | 33 | super(uri); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | protected volatile boolean aborting = false; |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Instruct the request that it should process no more records, |
michael@0 | 40 | * and decline to notify any more delegate callbacks. |
michael@0 | 41 | */ |
michael@0 | 42 | public void abort() { |
michael@0 | 43 | aborting = true; |
michael@0 | 44 | try { |
michael@0 | 45 | this.resource.request.abort(); |
michael@0 | 46 | } catch (Exception e) { |
michael@0 | 47 | // Just in case. |
michael@0 | 48 | Logger.warn(LOG_TAG, "Got exception in abort: " + e); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | @Override |
michael@0 | 53 | protected BaseResourceDelegate makeResourceDelegate(SyncStorageRequest request) { |
michael@0 | 54 | return new SyncCollectionResourceDelegate((SyncStorageCollectionRequest) request); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // TODO: this is awful. |
michael@0 | 58 | public class SyncCollectionResourceDelegate extends |
michael@0 | 59 | SyncStorageResourceDelegate { |
michael@0 | 60 | |
michael@0 | 61 | private static final String CONTENT_TYPE_INCREMENTAL = "application/newlines"; |
michael@0 | 62 | private static final int FETCH_BUFFER_SIZE = 16 * 1024; // 16K chars. |
michael@0 | 63 | |
michael@0 | 64 | SyncCollectionResourceDelegate(SyncStorageCollectionRequest request) { |
michael@0 | 65 | super(request); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | @Override |
michael@0 | 69 | public void addHeaders(HttpRequestBase request, DefaultHttpClient client) { |
michael@0 | 70 | super.addHeaders(request, client); |
michael@0 | 71 | request.setHeader("Accept", CONTENT_TYPE_INCREMENTAL); |
michael@0 | 72 | // Caller is responsible for setting full=1. |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | @Override |
michael@0 | 76 | public void handleHttpResponse(HttpResponse response) { |
michael@0 | 77 | if (aborting) { |
michael@0 | 78 | return; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | if (response.getStatusLine().getStatusCode() != 200) { |
michael@0 | 82 | super.handleHttpResponse(response); |
michael@0 | 83 | return; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | HttpEntity entity = response.getEntity(); |
michael@0 | 87 | Header contentType = entity.getContentType(); |
michael@0 | 88 | if (!contentType.getValue().startsWith(CONTENT_TYPE_INCREMENTAL)) { |
michael@0 | 89 | // Not incremental! |
michael@0 | 90 | super.handleHttpResponse(response); |
michael@0 | 91 | return; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // TODO: at this point we can access X-Weave-Timestamp, compare |
michael@0 | 95 | // that to our local timestamp, and compute an estimate of clock |
michael@0 | 96 | // skew. We can provide this to the incremental delegate, which |
michael@0 | 97 | // will allow it to seamlessly correct timestamps on the records |
michael@0 | 98 | // it processes. Bug 721887. |
michael@0 | 99 | |
michael@0 | 100 | // Line-by-line processing, then invoke success. |
michael@0 | 101 | SyncStorageCollectionRequestDelegate delegate = (SyncStorageCollectionRequestDelegate) this.request.delegate; |
michael@0 | 102 | InputStream content = null; |
michael@0 | 103 | BufferedReader br = null; |
michael@0 | 104 | try { |
michael@0 | 105 | content = entity.getContent(); |
michael@0 | 106 | br = new BufferedReader(new InputStreamReader(content), FETCH_BUFFER_SIZE); |
michael@0 | 107 | String line; |
michael@0 | 108 | |
michael@0 | 109 | // This relies on connection timeouts at the HTTP layer. |
michael@0 | 110 | while (!aborting && |
michael@0 | 111 | null != (line = br.readLine())) { |
michael@0 | 112 | try { |
michael@0 | 113 | delegate.handleRequestProgress(line); |
michael@0 | 114 | } catch (Exception ex) { |
michael@0 | 115 | delegate.handleRequestError(new HandleProgressException(ex)); |
michael@0 | 116 | BaseResource.consumeEntity(entity); |
michael@0 | 117 | return; |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | if (aborting) { |
michael@0 | 121 | // So we don't hit the success case below. |
michael@0 | 122 | return; |
michael@0 | 123 | } |
michael@0 | 124 | } catch (IOException ex) { |
michael@0 | 125 | if (!aborting) { |
michael@0 | 126 | delegate.handleRequestError(ex); |
michael@0 | 127 | } |
michael@0 | 128 | BaseResource.consumeEntity(entity); |
michael@0 | 129 | return; |
michael@0 | 130 | } finally { |
michael@0 | 131 | // Attempt to close the stream and reader. |
michael@0 | 132 | if (br != null) { |
michael@0 | 133 | try { |
michael@0 | 134 | br.close(); |
michael@0 | 135 | } catch (IOException e) { |
michael@0 | 136 | // We don't care if this fails. |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | // We're done processing the entity. Don't let fetching the body succeed! |
michael@0 | 141 | BaseResource.consumeEntity(entity); |
michael@0 | 142 | delegate.handleRequestSuccess(new SyncStorageResponse(response)); |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | } |