Bug 1134454 - Extract common resource code out of Sync. r=nalexander

This commit is contained in:
Richard Newman 2015-02-23 13:33:05 -08:00
parent 9c2406f932
commit b630f36591
4 changed files with 180 additions and 158 deletions

View File

@ -981,6 +981,7 @@ sync_java_files = [
'sync/net/HawkAuthHeaderProvider.java',
'sync/net/HMACAuthHeaderProvider.java',
'sync/net/HttpResponseObserver.java',
'sync/net/MozResponse.java',
'sync/net/Resource.java',
'sync/net/ResourceDelegate.java',
'sync/net/SRPConstants.java',

View File

@ -0,0 +1,175 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;
import org.json.simple.parser.ParseException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonObjectJSONException;
import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpEntity;
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.impl.cookie.DateParseException;
import ch.boye.httpclientandroidlib.impl.cookie.DateUtils;
public class MozResponse {
private static final String LOG_TAG = "MozResponse";
private static final String HEADER_RETRY_AFTER = "retry-after";
protected HttpResponse response;
private String body = null;
public HttpResponse httpResponse() {
return this.response;
}
public int getStatusCode() {
return this.response.getStatusLine().getStatusCode();
}
public boolean wasSuccessful() {
return this.getStatusCode() == 200;
}
/**
* Fetch the content type of the HTTP response body.
*
* @return a <code>Header</code> instance, or <code>null</code> if there was
* no body or no valid Content-Type.
*/
public Header getContentType() {
HttpEntity entity = this.response.getEntity();
if (entity == null) {
return null;
}
return entity.getContentType();
}
private static boolean missingHeader(String value) {
return value == null ||
value.trim().length() == 0;
}
public String body() throws IllegalStateException, IOException {
if (body != null) {
return body;
}
InputStreamReader is = new InputStreamReader(this.response.getEntity().getContent());
// Oh, Java, you are so evil.
body = new Scanner(is).useDelimiter("\\A").next();
return body;
}
/**
* Return the body as a <b>non-null</b> <code>ExtendedJSONObject</code>.
*
* @return A non-null <code>ExtendedJSONObject</code>.
*
* @throws IllegalStateException
* @throws IOException
* @throws ParseException
* @throws NonObjectJSONException
*/
public ExtendedJSONObject jsonObjectBody() throws IllegalStateException, IOException,
ParseException, NonObjectJSONException {
if (body != null) {
// Do it from the cached String.
return ExtendedJSONObject.parseJSONObject(body);
}
HttpEntity entity = this.response.getEntity();
if (entity == null) {
throw new IOException("no entity");
}
InputStream content = entity.getContent();
try {
Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8"));
return ExtendedJSONObject.parseJSONObject(in);
} finally {
content.close();
}
}
protected boolean hasHeader(String h) {
return this.response.containsHeader(h);
}
public MozResponse(HttpResponse res) {
response = res;
}
private String getNonMissingHeader(String h) {
if (!this.hasHeader(h)) {
return null;
}
final Header header = this.response.getFirstHeader(h);
final String value = header.getValue();
if (missingHeader(value)) {
Logger.warn(LOG_TAG, h + " header present but empty.");
return null;
}
return value;
}
protected long getLongHeader(String h) throws NumberFormatException {
final String value = getNonMissingHeader(h);
if (value == null) {
return -1L;
}
return Long.parseLong(value, 10);
}
protected int getIntegerHeader(String h) throws NumberFormatException {
final String value = getNonMissingHeader(h);
if (value == null) {
return -1;
}
return Integer.parseInt(value, 10);
}
/**
* @return A number of seconds, or -1 if the 'Retry-After' header was not present.
*/
public int retryAfterInSeconds() throws NumberFormatException {
final String retryAfter = getNonMissingHeader(HEADER_RETRY_AFTER);
if (retryAfter == null) {
return -1;
}
try {
return Integer.parseInt(retryAfter, 10);
} catch (NumberFormatException e) {
// Fall through to try date format.
}
try {
final long then = DateUtils.parseDate(retryAfter).getTime();
final long now = System.currentTimeMillis();
return (int)((then - now) / 1000); // Convert milliseconds to seconds.
} catch (DateParseException e) {
Logger.warn(LOG_TAG, "Retry-After header neither integer nor date: " + retryAfter);
return -1;
}
}
/**
* @return A number of seconds, or -1 if the 'X-Backoff' header was not
* present.
*/
public int backoffInSeconds() throws NumberFormatException {
return this.getIntegerHeader("x-backoff");
}
}

View File

@ -4,167 +4,13 @@
package org.mozilla.gecko.sync.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;
import org.json.simple.parser.ParseException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonObjectJSONException;
import org.mozilla.gecko.sync.Utils;
import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.HttpEntity;
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.impl.cookie.DateParseException;
import ch.boye.httpclientandroidlib.impl.cookie.DateUtils;
public class SyncResponse {
private static final String HEADER_RETRY_AFTER = "retry-after";
private static final String LOG_TAG = "SyncResponse";
protected HttpResponse response;
public SyncResponse() {
super();
}
public class SyncResponse extends MozResponse {
public SyncResponse(HttpResponse res) {
response = res;
}
public HttpResponse httpResponse() {
return this.response;
}
public int getStatusCode() {
return this.response.getStatusLine().getStatusCode();
}
public boolean wasSuccessful() {
return this.getStatusCode() == 200;
}
/**
* Fetch the content type of the HTTP response body.
*
* @return a <code>Header</code> instance, or <code>null</code> if there was
* no body or no valid Content-Type.
*/
public Header getContentType() {
HttpEntity entity = this.response.getEntity();
if (entity == null) {
return null;
}
return entity.getContentType();
}
private String body = null;
public String body() throws IllegalStateException, IOException {
if (body != null) {
return body;
}
InputStreamReader is = new InputStreamReader(this.response.getEntity().getContent());
// Oh, Java, you are so evil.
body = new Scanner(is).useDelimiter("\\A").next();
return body;
}
/**
* Return the body as a <b>non-null</b> <code>ExtendedJSONObject</code>.
*
* @return A non-null <code>ExtendedJSONObject</code>.
*
* @throws IllegalStateException
* @throws IOException
* @throws ParseException
* @throws NonObjectJSONException
*/
public ExtendedJSONObject jsonObjectBody() throws IllegalStateException,
IOException, ParseException,
NonObjectJSONException {
if (body != null) {
// Do it from the cached String.
return ExtendedJSONObject.parseJSONObject(body);
}
HttpEntity entity = this.response.getEntity();
if (entity == null) {
throw new IOException("no entity");
}
InputStream content = entity.getContent();
try {
Reader in = new BufferedReader(new InputStreamReader(content, "UTF-8"));
return ExtendedJSONObject.parseJSONObject(in);
} finally {
content.close();
}
}
private boolean hasHeader(String h) {
return this.response.containsHeader(h);
}
private static boolean missingHeader(String value) {
return value == null ||
value.trim().length() == 0;
}
private int getIntegerHeader(String h) throws NumberFormatException {
if (this.hasHeader(h)) {
Header header = this.response.getFirstHeader(h);
String value = header.getValue();
if (missingHeader(value)) {
Logger.warn(LOG_TAG, h + " header present but empty.");
return -1;
}
return Integer.parseInt(value, 10);
}
return -1;
}
/**
* @return A number of seconds, or -1 if the 'Retry-After' header was not present.
*/
public int retryAfterInSeconds() throws NumberFormatException {
if (!this.hasHeader(HEADER_RETRY_AFTER)) {
return -1;
}
Header header = this.response.getFirstHeader(HEADER_RETRY_AFTER);
String retryAfter = header.getValue();
if (missingHeader(retryAfter)) {
Logger.warn(LOG_TAG, "Retry-After header present but empty.");
return -1;
}
try {
return Integer.parseInt(retryAfter, 10);
} catch (NumberFormatException e) {
// Fall through to try date format.
}
try {
final long then = DateUtils.parseDate(retryAfter).getTime();
final long now = System.currentTimeMillis();
return (int)((then - now) / 1000); // Convert milliseconds to seconds.
} catch (DateParseException e) {
Logger.warn(LOG_TAG, "Retry-After header neither integer nor date: " + retryAfter);
return -1;
}
}
/**
* @return A number of seconds, or -1 if the 'X-Backoff' header was not
* present.
*/
public int backoffInSeconds() throws NumberFormatException {
return this.getIntegerHeader("x-backoff");
super(res);
}
/**

View File

@ -12,11 +12,11 @@ import org.mozilla.gecko.background.common.log.Logger;
import ch.boye.httpclientandroidlib.HttpResponse;
public class SyncStorageResponse extends SyncResponse {
private static final String LOG_TAG = "SyncStorageResponse";
// Responses that are actionable get constant status codes.
public static final String RESPONSE_CLIENT_UPGRADE_REQUIRED = "16";
private static final String LOG_TAG = "SyncStorageResponse";
public static HashMap<String, String> SERVER_ERROR_MESSAGES;
static {
HashMap<String, String> errors = new HashMap<String, String>();
@ -60,7 +60,7 @@ public class SyncStorageResponse extends SyncResponse {
public SyncStorageResponse(HttpResponse res) {
this.response = res;
super(res);
}
public String getErrorMessage() throws IllegalStateException, IOException {