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.io.Reader; |
michael@0 | 12 | import java.util.Scanner; |
michael@0 | 13 | |
michael@0 | 14 | import org.json.simple.parser.ParseException; |
michael@0 | 15 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 16 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 17 | import org.mozilla.gecko.sync.NonObjectJSONException; |
michael@0 | 18 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 19 | |
michael@0 | 20 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 21 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 22 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 23 | import ch.boye.httpclientandroidlib.impl.cookie.DateParseException; |
michael@0 | 24 | import ch.boye.httpclientandroidlib.impl.cookie.DateUtils; |
michael@0 | 25 | |
michael@0 | 26 | public class SyncResponse { |
michael@0 | 27 | private static final String HEADER_RETRY_AFTER = "retry-after"; |
michael@0 | 28 | private static final String LOG_TAG = "SyncResponse"; |
michael@0 | 29 | |
michael@0 | 30 | protected HttpResponse response; |
michael@0 | 31 | |
michael@0 | 32 | public SyncResponse() { |
michael@0 | 33 | super(); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | public SyncResponse(HttpResponse res) { |
michael@0 | 37 | response = res; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | public HttpResponse httpResponse() { |
michael@0 | 41 | return this.response; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | public int getStatusCode() { |
michael@0 | 45 | return this.response.getStatusLine().getStatusCode(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | public boolean wasSuccessful() { |
michael@0 | 49 | return this.getStatusCode() == 200; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * Fetch the content type of the HTTP response body. |
michael@0 | 54 | * |
michael@0 | 55 | * @return a <code>Header</code> instance, or <code>null</code> if there was |
michael@0 | 56 | * no body or no valid Content-Type. |
michael@0 | 57 | */ |
michael@0 | 58 | public Header getContentType() { |
michael@0 | 59 | HttpEntity entity = this.response.getEntity(); |
michael@0 | 60 | if (entity == null) { |
michael@0 | 61 | return null; |
michael@0 | 62 | } |
michael@0 | 63 | return entity.getContentType(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | private String body = null; |
michael@0 | 67 | public String body() throws IllegalStateException, IOException { |
michael@0 | 68 | if (body != null) { |
michael@0 | 69 | return body; |
michael@0 | 70 | } |
michael@0 | 71 | InputStreamReader is = new InputStreamReader(this.response.getEntity().getContent()); |
michael@0 | 72 | // Oh, Java, you are so evil. |
michael@0 | 73 | body = new Scanner(is).useDelimiter("\\A").next(); |
michael@0 | 74 | return body; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * Return the body as a <b>non-null</b> <code>ExtendedJSONObject</code>. |
michael@0 | 79 | * |
michael@0 | 80 | * @return A non-null <code>ExtendedJSONObject</code>. |
michael@0 | 81 | * |
michael@0 | 82 | * @throws IllegalStateException |
michael@0 | 83 | * @throws IOException |
michael@0 | 84 | * @throws ParseException |
michael@0 | 85 | * @throws NonObjectJSONException |
michael@0 | 86 | */ |
michael@0 | 87 | public ExtendedJSONObject jsonObjectBody() throws IllegalStateException, |
michael@0 | 88 | IOException, ParseException, |
michael@0 | 89 | NonObjectJSONException { |
michael@0 | 90 | if (body != null) { |
michael@0 | 91 | // Do it from the cached String. |
michael@0 | 92 | return ExtendedJSONObject.parseJSONObject(body); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | HttpEntity entity = this.response.getEntity(); |
michael@0 | 96 | if (entity == null) { |
michael@0 | 97 | throw new IOException("no entity"); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | InputStream content = entity.getContent(); |
michael@0 | 101 | try { |
michael@0 | 102 | Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8")); |
michael@0 | 103 | return ExtendedJSONObject.parseJSONObject(in); |
michael@0 | 104 | } finally { |
michael@0 | 105 | content.close(); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | private boolean hasHeader(String h) { |
michael@0 | 110 | return this.response.containsHeader(h); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | private static boolean missingHeader(String value) { |
michael@0 | 114 | return value == null || |
michael@0 | 115 | value.trim().length() == 0; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | private int getIntegerHeader(String h) throws NumberFormatException { |
michael@0 | 119 | if (this.hasHeader(h)) { |
michael@0 | 120 | Header header = this.response.getFirstHeader(h); |
michael@0 | 121 | String value = header.getValue(); |
michael@0 | 122 | if (missingHeader(value)) { |
michael@0 | 123 | Logger.warn(LOG_TAG, h + " header present but empty."); |
michael@0 | 124 | return -1; |
michael@0 | 125 | } |
michael@0 | 126 | return Integer.parseInt(value, 10); |
michael@0 | 127 | } |
michael@0 | 128 | return -1; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * @return A number of seconds, or -1 if the 'Retry-After' header was not present. |
michael@0 | 133 | */ |
michael@0 | 134 | public int retryAfterInSeconds() throws NumberFormatException { |
michael@0 | 135 | if (!this.hasHeader(HEADER_RETRY_AFTER)) { |
michael@0 | 136 | return -1; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | Header header = this.response.getFirstHeader(HEADER_RETRY_AFTER); |
michael@0 | 140 | String retryAfter = header.getValue(); |
michael@0 | 141 | if (missingHeader(retryAfter)) { |
michael@0 | 142 | Logger.warn(LOG_TAG, "Retry-After header present but empty."); |
michael@0 | 143 | return -1; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | try { |
michael@0 | 147 | return Integer.parseInt(retryAfter, 10); |
michael@0 | 148 | } catch (NumberFormatException e) { |
michael@0 | 149 | // Fall through to try date format. |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | try { |
michael@0 | 153 | final long then = DateUtils.parseDate(retryAfter).getTime(); |
michael@0 | 154 | final long now = System.currentTimeMillis(); |
michael@0 | 155 | return (int)((then - now) / 1000); // Convert milliseconds to seconds. |
michael@0 | 156 | } catch (DateParseException e) { |
michael@0 | 157 | Logger.warn(LOG_TAG, "Retry-After header neither integer nor date: " + retryAfter); |
michael@0 | 158 | return -1; |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | /** |
michael@0 | 163 | * @return A number of seconds, or -1 if the 'X-Backoff' header was not |
michael@0 | 164 | * present. |
michael@0 | 165 | */ |
michael@0 | 166 | public int backoffInSeconds() throws NumberFormatException { |
michael@0 | 167 | return this.getIntegerHeader("x-backoff"); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * @return A number of seconds, or -1 if the 'X-Weave-Backoff' header was not |
michael@0 | 172 | * present. |
michael@0 | 173 | */ |
michael@0 | 174 | public int weaveBackoffInSeconds() throws NumberFormatException { |
michael@0 | 175 | return this.getIntegerHeader("x-weave-backoff"); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * Extract a number of seconds, or -1 if none of the specified headers were present. |
michael@0 | 180 | * |
michael@0 | 181 | * @param includeRetryAfter |
michael@0 | 182 | * if <code>true</code>, the Retry-After header is excluded. This is |
michael@0 | 183 | * useful for processing non-error responses where a Retry-After |
michael@0 | 184 | * header would be unexpected. |
michael@0 | 185 | * @return the maximum of the three possible backoff headers, in seconds. |
michael@0 | 186 | */ |
michael@0 | 187 | public int totalBackoffInSeconds(boolean includeRetryAfter) { |
michael@0 | 188 | int retryAfterInSeconds = -1; |
michael@0 | 189 | if (includeRetryAfter) { |
michael@0 | 190 | try { |
michael@0 | 191 | retryAfterInSeconds = retryAfterInSeconds(); |
michael@0 | 192 | } catch (NumberFormatException e) { |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | int weaveBackoffInSeconds = -1; |
michael@0 | 197 | try { |
michael@0 | 198 | weaveBackoffInSeconds = weaveBackoffInSeconds(); |
michael@0 | 199 | } catch (NumberFormatException e) { |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | int backoffInSeconds = -1; |
michael@0 | 203 | try { |
michael@0 | 204 | backoffInSeconds = backoffInSeconds(); |
michael@0 | 205 | } catch (NumberFormatException e) { |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | int totalBackoff = Math.max(retryAfterInSeconds, Math.max(backoffInSeconds, weaveBackoffInSeconds)); |
michael@0 | 209 | if (totalBackoff < 0) { |
michael@0 | 210 | return -1; |
michael@0 | 211 | } else { |
michael@0 | 212 | return totalBackoff; |
michael@0 | 213 | } |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | /** |
michael@0 | 217 | * @return A number of milliseconds, or -1 if neither the 'Retry-After', |
michael@0 | 218 | * 'X-Backoff', or 'X-Weave-Backoff' header were present. |
michael@0 | 219 | */ |
michael@0 | 220 | public long totalBackoffInMilliseconds() { |
michael@0 | 221 | long totalBackoff = totalBackoffInSeconds(true); |
michael@0 | 222 | if (totalBackoff < 0) { |
michael@0 | 223 | return -1; |
michael@0 | 224 | } else { |
michael@0 | 225 | return 1000 * totalBackoff; |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | /** |
michael@0 | 230 | * The timestamp returned from a Sync server is a decimal number of seconds, |
michael@0 | 231 | * e.g., 1323393518.04. |
michael@0 | 232 | * |
michael@0 | 233 | * We want milliseconds since epoch. |
michael@0 | 234 | * |
michael@0 | 235 | * @return milliseconds since the epoch, as a long, or -1 if the header |
michael@0 | 236 | * was missing or invalid. |
michael@0 | 237 | */ |
michael@0 | 238 | public long normalizedWeaveTimestamp() { |
michael@0 | 239 | String h = "x-weave-timestamp"; |
michael@0 | 240 | if (!this.hasHeader(h)) { |
michael@0 | 241 | return -1; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | return Utils.decimalSecondsToMilliseconds(this.response.getFirstHeader(h).getValue()); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | public int weaveRecords() throws NumberFormatException { |
michael@0 | 248 | return this.getIntegerHeader("x-weave-records"); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | public int weaveQuotaRemaining() throws NumberFormatException { |
michael@0 | 252 | return this.getIntegerHeader("x-weave-quota-remaining"); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | public String weaveAlert() { |
michael@0 | 256 | if (this.hasHeader("x-weave-alert")) { |
michael@0 | 257 | return this.response.getFirstHeader("x-weave-alert").getValue(); |
michael@0 | 258 | } |
michael@0 | 259 | return null; |
michael@0 | 260 | } |
michael@0 | 261 | } |