1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/net/SyncStorageRequest.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,197 @@ 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.IOException; 1.11 +import java.net.URI; 1.12 +import java.net.URISyntaxException; 1.13 +import java.security.GeneralSecurityException; 1.14 +import java.util.HashMap; 1.15 + 1.16 +import org.mozilla.gecko.background.common.log.Logger; 1.17 +import org.mozilla.gecko.sync.SyncConstants; 1.18 + 1.19 +import ch.boye.httpclientandroidlib.HttpEntity; 1.20 +import ch.boye.httpclientandroidlib.HttpResponse; 1.21 +import ch.boye.httpclientandroidlib.client.ClientProtocolException; 1.22 +import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; 1.23 +import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient; 1.24 + 1.25 +public class SyncStorageRequest implements Resource { 1.26 + public static HashMap<String, String> SERVER_ERROR_MESSAGES; 1.27 + static { 1.28 + HashMap<String, String> errors = new HashMap<String, String>(); 1.29 + 1.30 + // Sync protocol errors. 1.31 + errors.put("1", "Illegal method/protocol"); 1.32 + errors.put("2", "Incorrect/missing CAPTCHA"); 1.33 + errors.put("3", "Invalid/missing username"); 1.34 + errors.put("4", "Attempt to overwrite data that can't be overwritten (such as creating a user ID that already exists)"); 1.35 + errors.put("5", "User ID does not match account in path"); 1.36 + errors.put("6", "JSON parse failure"); 1.37 + errors.put("7", "Missing password field"); 1.38 + errors.put("8", "Invalid Weave Basic Object"); 1.39 + errors.put("9", "Requested password not strong enough"); 1.40 + errors.put("10", "Invalid/missing password reset code"); 1.41 + errors.put("11", "Unsupported function"); 1.42 + errors.put("12", "No email address on file"); 1.43 + errors.put("13", "Invalid collection"); 1.44 + errors.put("14", "User over quota"); 1.45 + errors.put("15", "The email does not match the username"); 1.46 + errors.put("16", "Client upgrade required"); 1.47 + errors.put("255", "An unexpected server error occurred: pool is empty."); 1.48 + 1.49 + // Infrastructure-generated errors. 1.50 + errors.put("\"server issue: getVS failed\"", "server issue: getVS failed"); 1.51 + errors.put("\"server issue: prefix not set\"", "server issue: prefix not set"); 1.52 + errors.put("\"server issue: host header not received from client\"", "server issue: host header not received from client"); 1.53 + errors.put("\"server issue: database lookup failed\"", "server issue: database lookup failed"); 1.54 + errors.put("\"server issue: database is not healthy\"", "server issue: database is not healthy"); 1.55 + errors.put("\"server issue: database not in pool\"", "server issue: database not in pool"); 1.56 + errors.put("\"server issue: database marked as down\"", "server issue: database marked as down"); 1.57 + SERVER_ERROR_MESSAGES = errors; 1.58 + } 1.59 + public static String getServerErrorMessage(String body) { 1.60 + if (SERVER_ERROR_MESSAGES.containsKey(body)) { 1.61 + return SERVER_ERROR_MESSAGES.get(body); 1.62 + } 1.63 + return body; 1.64 + } 1.65 + 1.66 + /** 1.67 + * @param uri 1.68 + * @throws URISyntaxException 1.69 + */ 1.70 + public SyncStorageRequest(String uri) throws URISyntaxException { 1.71 + this(new URI(uri)); 1.72 + } 1.73 + 1.74 + /** 1.75 + * @param uri 1.76 + */ 1.77 + public SyncStorageRequest(URI uri) { 1.78 + this.resource = new BaseResource(uri); 1.79 + this.resourceDelegate = this.makeResourceDelegate(this); 1.80 + this.resource.delegate = this.resourceDelegate; 1.81 + } 1.82 + 1.83 + @Override 1.84 + public URI getURI() { 1.85 + return this.resource.getURI(); 1.86 + } 1.87 + 1.88 + @Override 1.89 + public String getURIString() { 1.90 + return this.resource.getURIString(); 1.91 + } 1.92 + 1.93 + @Override 1.94 + public String getHostname() { 1.95 + return this.resource.getHostname(); 1.96 + } 1.97 + 1.98 + /** 1.99 + * A ResourceDelegate that mediates between Resource-level notifications and the SyncStorageRequest. 1.100 + */ 1.101 + public class SyncStorageResourceDelegate extends BaseResourceDelegate { 1.102 + private static final String LOG_TAG = "SSResourceDelegate"; 1.103 + protected SyncStorageRequest request; 1.104 + 1.105 + SyncStorageResourceDelegate(SyncStorageRequest request) { 1.106 + super(request); 1.107 + this.request = request; 1.108 + } 1.109 + 1.110 + @Override 1.111 + public AuthHeaderProvider getAuthHeaderProvider() { 1.112 + return request.delegate.getAuthHeaderProvider(); 1.113 + } 1.114 + 1.115 + @Override 1.116 + public String getUserAgent() { 1.117 + return SyncConstants.USER_AGENT; 1.118 + } 1.119 + 1.120 + @Override 1.121 + public void handleHttpResponse(HttpResponse response) { 1.122 + Logger.debug(LOG_TAG, "SyncStorageResourceDelegate handling response: " + response.getStatusLine() + "."); 1.123 + SyncStorageRequestDelegate d = this.request.delegate; 1.124 + SyncStorageResponse res = new SyncStorageResponse(response); 1.125 + // It is the responsibility of the delegate handlers to completely consume the response. 1.126 + if (res.wasSuccessful()) { 1.127 + d.handleRequestSuccess(res); 1.128 + } else { 1.129 + Logger.warn(LOG_TAG, "HTTP request failed."); 1.130 + try { 1.131 + Logger.warn(LOG_TAG, "HTTP response body: " + res.getErrorMessage()); 1.132 + } catch (Exception e) { 1.133 + Logger.error(LOG_TAG, "Can't fetch HTTP response body.", e); 1.134 + } 1.135 + d.handleRequestFailure(res); 1.136 + } 1.137 + } 1.138 + 1.139 + @Override 1.140 + public void handleHttpProtocolException(ClientProtocolException e) { 1.141 + this.request.delegate.handleRequestError(e); 1.142 + } 1.143 + 1.144 + @Override 1.145 + public void handleHttpIOException(IOException e) { 1.146 + this.request.delegate.handleRequestError(e); 1.147 + } 1.148 + 1.149 + @Override 1.150 + public void handleTransportException(GeneralSecurityException e) { 1.151 + this.request.delegate.handleRequestError(e); 1.152 + } 1.153 + 1.154 + @Override 1.155 + public void addHeaders(HttpRequestBase request, DefaultHttpClient client) { 1.156 + // Clients can use their delegate interface to specify X-If-Unmodified-Since. 1.157 + String ifUnmodifiedSince = this.request.delegate.ifUnmodifiedSince(); 1.158 + if (ifUnmodifiedSince != null) { 1.159 + Logger.debug(LOG_TAG, "Making request with X-If-Unmodified-Since = " + ifUnmodifiedSince); 1.160 + request.setHeader("x-if-unmodified-since", ifUnmodifiedSince); 1.161 + } 1.162 + if (request.getMethod().equalsIgnoreCase("DELETE")) { 1.163 + request.addHeader("x-confirm-delete", "1"); 1.164 + } 1.165 + } 1.166 + } 1.167 + 1.168 + protected BaseResourceDelegate resourceDelegate; 1.169 + public SyncStorageRequestDelegate delegate; 1.170 + protected BaseResource resource; 1.171 + 1.172 + public SyncStorageRequest() { 1.173 + super(); 1.174 + } 1.175 + 1.176 + // Default implementation. Override this. 1.177 + protected BaseResourceDelegate makeResourceDelegate(SyncStorageRequest request) { 1.178 + return new SyncStorageResourceDelegate(request); 1.179 + } 1.180 + 1.181 + @Override 1.182 + public void get() { 1.183 + this.resource.get(); 1.184 + } 1.185 + 1.186 + @Override 1.187 + public void delete() { 1.188 + this.resource.delete(); 1.189 + } 1.190 + 1.191 + @Override 1.192 + public void post(HttpEntity body) { 1.193 + this.resource.post(body); 1.194 + } 1.195 + 1.196 + @Override 1.197 + public void put(HttpEntity body) { 1.198 + this.resource.put(body); 1.199 + } 1.200 +}