1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/net/SyncResponse.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,261 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync.net; 1.9 + 1.10 +import java.io.BufferedReader; 1.11 +import java.io.IOException; 1.12 +import java.io.InputStream; 1.13 +import java.io.InputStreamReader; 1.14 +import java.io.Reader; 1.15 +import java.util.Scanner; 1.16 + 1.17 +import org.json.simple.parser.ParseException; 1.18 +import org.mozilla.gecko.background.common.log.Logger; 1.19 +import org.mozilla.gecko.sync.ExtendedJSONObject; 1.20 +import org.mozilla.gecko.sync.NonObjectJSONException; 1.21 +import org.mozilla.gecko.sync.Utils; 1.22 + 1.23 +import ch.boye.httpclientandroidlib.Header; 1.24 +import ch.boye.httpclientandroidlib.HttpEntity; 1.25 +import ch.boye.httpclientandroidlib.HttpResponse; 1.26 +import ch.boye.httpclientandroidlib.impl.cookie.DateParseException; 1.27 +import ch.boye.httpclientandroidlib.impl.cookie.DateUtils; 1.28 + 1.29 +public class SyncResponse { 1.30 + private static final String HEADER_RETRY_AFTER = "retry-after"; 1.31 + private static final String LOG_TAG = "SyncResponse"; 1.32 + 1.33 + protected HttpResponse response; 1.34 + 1.35 + public SyncResponse() { 1.36 + super(); 1.37 + } 1.38 + 1.39 + public SyncResponse(HttpResponse res) { 1.40 + response = res; 1.41 + } 1.42 + 1.43 + public HttpResponse httpResponse() { 1.44 + return this.response; 1.45 + } 1.46 + 1.47 + public int getStatusCode() { 1.48 + return this.response.getStatusLine().getStatusCode(); 1.49 + } 1.50 + 1.51 + public boolean wasSuccessful() { 1.52 + return this.getStatusCode() == 200; 1.53 + } 1.54 + 1.55 + /** 1.56 + * Fetch the content type of the HTTP response body. 1.57 + * 1.58 + * @return a <code>Header</code> instance, or <code>null</code> if there was 1.59 + * no body or no valid Content-Type. 1.60 + */ 1.61 + public Header getContentType() { 1.62 + HttpEntity entity = this.response.getEntity(); 1.63 + if (entity == null) { 1.64 + return null; 1.65 + } 1.66 + return entity.getContentType(); 1.67 + } 1.68 + 1.69 + private String body = null; 1.70 + public String body() throws IllegalStateException, IOException { 1.71 + if (body != null) { 1.72 + return body; 1.73 + } 1.74 + InputStreamReader is = new InputStreamReader(this.response.getEntity().getContent()); 1.75 + // Oh, Java, you are so evil. 1.76 + body = new Scanner(is).useDelimiter("\\A").next(); 1.77 + return body; 1.78 + } 1.79 + 1.80 + /** 1.81 + * Return the body as a <b>non-null</b> <code>ExtendedJSONObject</code>. 1.82 + * 1.83 + * @return A non-null <code>ExtendedJSONObject</code>. 1.84 + * 1.85 + * @throws IllegalStateException 1.86 + * @throws IOException 1.87 + * @throws ParseException 1.88 + * @throws NonObjectJSONException 1.89 + */ 1.90 + public ExtendedJSONObject jsonObjectBody() throws IllegalStateException, 1.91 + IOException, ParseException, 1.92 + NonObjectJSONException { 1.93 + if (body != null) { 1.94 + // Do it from the cached String. 1.95 + return ExtendedJSONObject.parseJSONObject(body); 1.96 + } 1.97 + 1.98 + HttpEntity entity = this.response.getEntity(); 1.99 + if (entity == null) { 1.100 + throw new IOException("no entity"); 1.101 + } 1.102 + 1.103 + InputStream content = entity.getContent(); 1.104 + try { 1.105 + Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8")); 1.106 + return ExtendedJSONObject.parseJSONObject(in); 1.107 + } finally { 1.108 + content.close(); 1.109 + } 1.110 + } 1.111 + 1.112 + private boolean hasHeader(String h) { 1.113 + return this.response.containsHeader(h); 1.114 + } 1.115 + 1.116 + private static boolean missingHeader(String value) { 1.117 + return value == null || 1.118 + value.trim().length() == 0; 1.119 + } 1.120 + 1.121 + private int getIntegerHeader(String h) throws NumberFormatException { 1.122 + if (this.hasHeader(h)) { 1.123 + Header header = this.response.getFirstHeader(h); 1.124 + String value = header.getValue(); 1.125 + if (missingHeader(value)) { 1.126 + Logger.warn(LOG_TAG, h + " header present but empty."); 1.127 + return -1; 1.128 + } 1.129 + return Integer.parseInt(value, 10); 1.130 + } 1.131 + return -1; 1.132 + } 1.133 + 1.134 + /** 1.135 + * @return A number of seconds, or -1 if the 'Retry-After' header was not present. 1.136 + */ 1.137 + public int retryAfterInSeconds() throws NumberFormatException { 1.138 + if (!this.hasHeader(HEADER_RETRY_AFTER)) { 1.139 + return -1; 1.140 + } 1.141 + 1.142 + Header header = this.response.getFirstHeader(HEADER_RETRY_AFTER); 1.143 + String retryAfter = header.getValue(); 1.144 + if (missingHeader(retryAfter)) { 1.145 + Logger.warn(LOG_TAG, "Retry-After header present but empty."); 1.146 + return -1; 1.147 + } 1.148 + 1.149 + try { 1.150 + return Integer.parseInt(retryAfter, 10); 1.151 + } catch (NumberFormatException e) { 1.152 + // Fall through to try date format. 1.153 + } 1.154 + 1.155 + try { 1.156 + final long then = DateUtils.parseDate(retryAfter).getTime(); 1.157 + final long now = System.currentTimeMillis(); 1.158 + return (int)((then - now) / 1000); // Convert milliseconds to seconds. 1.159 + } catch (DateParseException e) { 1.160 + Logger.warn(LOG_TAG, "Retry-After header neither integer nor date: " + retryAfter); 1.161 + return -1; 1.162 + } 1.163 + } 1.164 + 1.165 + /** 1.166 + * @return A number of seconds, or -1 if the 'X-Backoff' header was not 1.167 + * present. 1.168 + */ 1.169 + public int backoffInSeconds() throws NumberFormatException { 1.170 + return this.getIntegerHeader("x-backoff"); 1.171 + } 1.172 + 1.173 + /** 1.174 + * @return A number of seconds, or -1 if the 'X-Weave-Backoff' header was not 1.175 + * present. 1.176 + */ 1.177 + public int weaveBackoffInSeconds() throws NumberFormatException { 1.178 + return this.getIntegerHeader("x-weave-backoff"); 1.179 + } 1.180 + 1.181 + /** 1.182 + * Extract a number of seconds, or -1 if none of the specified headers were present. 1.183 + * 1.184 + * @param includeRetryAfter 1.185 + * if <code>true</code>, the Retry-After header is excluded. This is 1.186 + * useful for processing non-error responses where a Retry-After 1.187 + * header would be unexpected. 1.188 + * @return the maximum of the three possible backoff headers, in seconds. 1.189 + */ 1.190 + public int totalBackoffInSeconds(boolean includeRetryAfter) { 1.191 + int retryAfterInSeconds = -1; 1.192 + if (includeRetryAfter) { 1.193 + try { 1.194 + retryAfterInSeconds = retryAfterInSeconds(); 1.195 + } catch (NumberFormatException e) { 1.196 + } 1.197 + } 1.198 + 1.199 + int weaveBackoffInSeconds = -1; 1.200 + try { 1.201 + weaveBackoffInSeconds = weaveBackoffInSeconds(); 1.202 + } catch (NumberFormatException e) { 1.203 + } 1.204 + 1.205 + int backoffInSeconds = -1; 1.206 + try { 1.207 + backoffInSeconds = backoffInSeconds(); 1.208 + } catch (NumberFormatException e) { 1.209 + } 1.210 + 1.211 + int totalBackoff = Math.max(retryAfterInSeconds, Math.max(backoffInSeconds, weaveBackoffInSeconds)); 1.212 + if (totalBackoff < 0) { 1.213 + return -1; 1.214 + } else { 1.215 + return totalBackoff; 1.216 + } 1.217 + } 1.218 + 1.219 + /** 1.220 + * @return A number of milliseconds, or -1 if neither the 'Retry-After', 1.221 + * 'X-Backoff', or 'X-Weave-Backoff' header were present. 1.222 + */ 1.223 + public long totalBackoffInMilliseconds() { 1.224 + long totalBackoff = totalBackoffInSeconds(true); 1.225 + if (totalBackoff < 0) { 1.226 + return -1; 1.227 + } else { 1.228 + return 1000 * totalBackoff; 1.229 + } 1.230 + } 1.231 + 1.232 + /** 1.233 + * The timestamp returned from a Sync server is a decimal number of seconds, 1.234 + * e.g., 1323393518.04. 1.235 + * 1.236 + * We want milliseconds since epoch. 1.237 + * 1.238 + * @return milliseconds since the epoch, as a long, or -1 if the header 1.239 + * was missing or invalid. 1.240 + */ 1.241 + public long normalizedWeaveTimestamp() { 1.242 + String h = "x-weave-timestamp"; 1.243 + if (!this.hasHeader(h)) { 1.244 + return -1; 1.245 + } 1.246 + 1.247 + return Utils.decimalSecondsToMilliseconds(this.response.getFirstHeader(h).getValue()); 1.248 + } 1.249 + 1.250 + public int weaveRecords() throws NumberFormatException { 1.251 + return this.getIntegerHeader("x-weave-records"); 1.252 + } 1.253 + 1.254 + public int weaveQuotaRemaining() throws NumberFormatException { 1.255 + return this.getIntegerHeader("x-weave-quota-remaining"); 1.256 + } 1.257 + 1.258 + public String weaveAlert() { 1.259 + if (this.hasHeader("x-weave-alert")) { 1.260 + return this.response.getFirstHeader("x-weave-alert").getValue(); 1.261 + } 1.262 + return null; 1.263 + } 1.264 +}