michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.net; michael@0: michael@0: import java.io.IOException; michael@0: import java.net.URI; michael@0: import java.net.URISyntaxException; michael@0: import java.security.GeneralSecurityException; michael@0: import java.util.HashMap; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.SyncConstants; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpEntity; michael@0: import ch.boye.httpclientandroidlib.HttpResponse; michael@0: import ch.boye.httpclientandroidlib.client.ClientProtocolException; michael@0: import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; michael@0: import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient; michael@0: michael@0: public class SyncStorageRequest implements Resource { michael@0: public static HashMap SERVER_ERROR_MESSAGES; michael@0: static { michael@0: HashMap errors = new HashMap(); michael@0: michael@0: // Sync protocol errors. michael@0: errors.put("1", "Illegal method/protocol"); michael@0: errors.put("2", "Incorrect/missing CAPTCHA"); michael@0: errors.put("3", "Invalid/missing username"); michael@0: errors.put("4", "Attempt to overwrite data that can't be overwritten (such as creating a user ID that already exists)"); michael@0: errors.put("5", "User ID does not match account in path"); michael@0: errors.put("6", "JSON parse failure"); michael@0: errors.put("7", "Missing password field"); michael@0: errors.put("8", "Invalid Weave Basic Object"); michael@0: errors.put("9", "Requested password not strong enough"); michael@0: errors.put("10", "Invalid/missing password reset code"); michael@0: errors.put("11", "Unsupported function"); michael@0: errors.put("12", "No email address on file"); michael@0: errors.put("13", "Invalid collection"); michael@0: errors.put("14", "User over quota"); michael@0: errors.put("15", "The email does not match the username"); michael@0: errors.put("16", "Client upgrade required"); michael@0: errors.put("255", "An unexpected server error occurred: pool is empty."); michael@0: michael@0: // Infrastructure-generated errors. michael@0: errors.put("\"server issue: getVS failed\"", "server issue: getVS failed"); michael@0: errors.put("\"server issue: prefix not set\"", "server issue: prefix not set"); michael@0: errors.put("\"server issue: host header not received from client\"", "server issue: host header not received from client"); michael@0: errors.put("\"server issue: database lookup failed\"", "server issue: database lookup failed"); michael@0: errors.put("\"server issue: database is not healthy\"", "server issue: database is not healthy"); michael@0: errors.put("\"server issue: database not in pool\"", "server issue: database not in pool"); michael@0: errors.put("\"server issue: database marked as down\"", "server issue: database marked as down"); michael@0: SERVER_ERROR_MESSAGES = errors; michael@0: } michael@0: public static String getServerErrorMessage(String body) { michael@0: if (SERVER_ERROR_MESSAGES.containsKey(body)) { michael@0: return SERVER_ERROR_MESSAGES.get(body); michael@0: } michael@0: return body; michael@0: } michael@0: michael@0: /** michael@0: * @param uri michael@0: * @throws URISyntaxException michael@0: */ michael@0: public SyncStorageRequest(String uri) throws URISyntaxException { michael@0: this(new URI(uri)); michael@0: } michael@0: michael@0: /** michael@0: * @param uri michael@0: */ michael@0: public SyncStorageRequest(URI uri) { michael@0: this.resource = new BaseResource(uri); michael@0: this.resourceDelegate = this.makeResourceDelegate(this); michael@0: this.resource.delegate = this.resourceDelegate; michael@0: } michael@0: michael@0: @Override michael@0: public URI getURI() { michael@0: return this.resource.getURI(); michael@0: } michael@0: michael@0: @Override michael@0: public String getURIString() { michael@0: return this.resource.getURIString(); michael@0: } michael@0: michael@0: @Override michael@0: public String getHostname() { michael@0: return this.resource.getHostname(); michael@0: } michael@0: michael@0: /** michael@0: * A ResourceDelegate that mediates between Resource-level notifications and the SyncStorageRequest. michael@0: */ michael@0: public class SyncStorageResourceDelegate extends BaseResourceDelegate { michael@0: private static final String LOG_TAG = "SSResourceDelegate"; michael@0: protected SyncStorageRequest request; michael@0: michael@0: SyncStorageResourceDelegate(SyncStorageRequest request) { michael@0: super(request); michael@0: this.request = request; michael@0: } michael@0: michael@0: @Override michael@0: public AuthHeaderProvider getAuthHeaderProvider() { michael@0: return request.delegate.getAuthHeaderProvider(); michael@0: } michael@0: michael@0: @Override michael@0: public String getUserAgent() { michael@0: return SyncConstants.USER_AGENT; michael@0: } michael@0: michael@0: @Override michael@0: public void handleHttpResponse(HttpResponse response) { michael@0: Logger.debug(LOG_TAG, "SyncStorageResourceDelegate handling response: " + response.getStatusLine() + "."); michael@0: SyncStorageRequestDelegate d = this.request.delegate; michael@0: SyncStorageResponse res = new SyncStorageResponse(response); michael@0: // It is the responsibility of the delegate handlers to completely consume the response. michael@0: if (res.wasSuccessful()) { michael@0: d.handleRequestSuccess(res); michael@0: } else { michael@0: Logger.warn(LOG_TAG, "HTTP request failed."); michael@0: try { michael@0: Logger.warn(LOG_TAG, "HTTP response body: " + res.getErrorMessage()); michael@0: } catch (Exception e) { michael@0: Logger.error(LOG_TAG, "Can't fetch HTTP response body.", e); michael@0: } michael@0: d.handleRequestFailure(res); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void handleHttpProtocolException(ClientProtocolException e) { michael@0: this.request.delegate.handleRequestError(e); michael@0: } michael@0: michael@0: @Override michael@0: public void handleHttpIOException(IOException e) { michael@0: this.request.delegate.handleRequestError(e); michael@0: } michael@0: michael@0: @Override michael@0: public void handleTransportException(GeneralSecurityException e) { michael@0: this.request.delegate.handleRequestError(e); michael@0: } michael@0: michael@0: @Override michael@0: public void addHeaders(HttpRequestBase request, DefaultHttpClient client) { michael@0: // Clients can use their delegate interface to specify X-If-Unmodified-Since. michael@0: String ifUnmodifiedSince = this.request.delegate.ifUnmodifiedSince(); michael@0: if (ifUnmodifiedSince != null) { michael@0: Logger.debug(LOG_TAG, "Making request with X-If-Unmodified-Since = " + ifUnmodifiedSince); michael@0: request.setHeader("x-if-unmodified-since", ifUnmodifiedSince); michael@0: } michael@0: if (request.getMethod().equalsIgnoreCase("DELETE")) { michael@0: request.addHeader("x-confirm-delete", "1"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: protected BaseResourceDelegate resourceDelegate; michael@0: public SyncStorageRequestDelegate delegate; michael@0: protected BaseResource resource; michael@0: michael@0: public SyncStorageRequest() { michael@0: super(); michael@0: } michael@0: michael@0: // Default implementation. Override this. michael@0: protected BaseResourceDelegate makeResourceDelegate(SyncStorageRequest request) { michael@0: return new SyncStorageResourceDelegate(request); michael@0: } michael@0: michael@0: @Override michael@0: public void get() { michael@0: this.resource.get(); michael@0: } michael@0: michael@0: @Override michael@0: public void delete() { michael@0: this.resource.delete(); michael@0: } michael@0: michael@0: @Override michael@0: public void post(HttpEntity body) { michael@0: this.resource.post(body); michael@0: } michael@0: michael@0: @Override michael@0: public void put(HttpEntity body) { michael@0: this.resource.put(body); michael@0: } michael@0: }