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.IOException; |
michael@0 | 8 | import java.net.URI; |
michael@0 | 9 | import java.net.URISyntaxException; |
michael@0 | 10 | import java.security.GeneralSecurityException; |
michael@0 | 11 | import java.util.HashMap; |
michael@0 | 12 | |
michael@0 | 13 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 14 | import org.mozilla.gecko.sync.SyncConstants; |
michael@0 | 15 | |
michael@0 | 16 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 17 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 18 | import ch.boye.httpclientandroidlib.client.ClientProtocolException; |
michael@0 | 19 | import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; |
michael@0 | 20 | import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient; |
michael@0 | 21 | |
michael@0 | 22 | public class SyncStorageRequest implements Resource { |
michael@0 | 23 | public static HashMap<String, String> SERVER_ERROR_MESSAGES; |
michael@0 | 24 | static { |
michael@0 | 25 | HashMap<String, String> errors = new HashMap<String, String>(); |
michael@0 | 26 | |
michael@0 | 27 | // Sync protocol errors. |
michael@0 | 28 | errors.put("1", "Illegal method/protocol"); |
michael@0 | 29 | errors.put("2", "Incorrect/missing CAPTCHA"); |
michael@0 | 30 | errors.put("3", "Invalid/missing username"); |
michael@0 | 31 | errors.put("4", "Attempt to overwrite data that can't be overwritten (such as creating a user ID that already exists)"); |
michael@0 | 32 | errors.put("5", "User ID does not match account in path"); |
michael@0 | 33 | errors.put("6", "JSON parse failure"); |
michael@0 | 34 | errors.put("7", "Missing password field"); |
michael@0 | 35 | errors.put("8", "Invalid Weave Basic Object"); |
michael@0 | 36 | errors.put("9", "Requested password not strong enough"); |
michael@0 | 37 | errors.put("10", "Invalid/missing password reset code"); |
michael@0 | 38 | errors.put("11", "Unsupported function"); |
michael@0 | 39 | errors.put("12", "No email address on file"); |
michael@0 | 40 | errors.put("13", "Invalid collection"); |
michael@0 | 41 | errors.put("14", "User over quota"); |
michael@0 | 42 | errors.put("15", "The email does not match the username"); |
michael@0 | 43 | errors.put("16", "Client upgrade required"); |
michael@0 | 44 | errors.put("255", "An unexpected server error occurred: pool is empty."); |
michael@0 | 45 | |
michael@0 | 46 | // Infrastructure-generated errors. |
michael@0 | 47 | errors.put("\"server issue: getVS failed\"", "server issue: getVS failed"); |
michael@0 | 48 | errors.put("\"server issue: prefix not set\"", "server issue: prefix not set"); |
michael@0 | 49 | errors.put("\"server issue: host header not received from client\"", "server issue: host header not received from client"); |
michael@0 | 50 | errors.put("\"server issue: database lookup failed\"", "server issue: database lookup failed"); |
michael@0 | 51 | errors.put("\"server issue: database is not healthy\"", "server issue: database is not healthy"); |
michael@0 | 52 | errors.put("\"server issue: database not in pool\"", "server issue: database not in pool"); |
michael@0 | 53 | errors.put("\"server issue: database marked as down\"", "server issue: database marked as down"); |
michael@0 | 54 | SERVER_ERROR_MESSAGES = errors; |
michael@0 | 55 | } |
michael@0 | 56 | public static String getServerErrorMessage(String body) { |
michael@0 | 57 | if (SERVER_ERROR_MESSAGES.containsKey(body)) { |
michael@0 | 58 | return SERVER_ERROR_MESSAGES.get(body); |
michael@0 | 59 | } |
michael@0 | 60 | return body; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * @param uri |
michael@0 | 65 | * @throws URISyntaxException |
michael@0 | 66 | */ |
michael@0 | 67 | public SyncStorageRequest(String uri) throws URISyntaxException { |
michael@0 | 68 | this(new URI(uri)); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * @param uri |
michael@0 | 73 | */ |
michael@0 | 74 | public SyncStorageRequest(URI uri) { |
michael@0 | 75 | this.resource = new BaseResource(uri); |
michael@0 | 76 | this.resourceDelegate = this.makeResourceDelegate(this); |
michael@0 | 77 | this.resource.delegate = this.resourceDelegate; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | @Override |
michael@0 | 81 | public URI getURI() { |
michael@0 | 82 | return this.resource.getURI(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | @Override |
michael@0 | 86 | public String getURIString() { |
michael@0 | 87 | return this.resource.getURIString(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | @Override |
michael@0 | 91 | public String getHostname() { |
michael@0 | 92 | return this.resource.getHostname(); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * A ResourceDelegate that mediates between Resource-level notifications and the SyncStorageRequest. |
michael@0 | 97 | */ |
michael@0 | 98 | public class SyncStorageResourceDelegate extends BaseResourceDelegate { |
michael@0 | 99 | private static final String LOG_TAG = "SSResourceDelegate"; |
michael@0 | 100 | protected SyncStorageRequest request; |
michael@0 | 101 | |
michael@0 | 102 | SyncStorageResourceDelegate(SyncStorageRequest request) { |
michael@0 | 103 | super(request); |
michael@0 | 104 | this.request = request; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | @Override |
michael@0 | 108 | public AuthHeaderProvider getAuthHeaderProvider() { |
michael@0 | 109 | return request.delegate.getAuthHeaderProvider(); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | @Override |
michael@0 | 113 | public String getUserAgent() { |
michael@0 | 114 | return SyncConstants.USER_AGENT; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | @Override |
michael@0 | 118 | public void handleHttpResponse(HttpResponse response) { |
michael@0 | 119 | Logger.debug(LOG_TAG, "SyncStorageResourceDelegate handling response: " + response.getStatusLine() + "."); |
michael@0 | 120 | SyncStorageRequestDelegate d = this.request.delegate; |
michael@0 | 121 | SyncStorageResponse res = new SyncStorageResponse(response); |
michael@0 | 122 | // It is the responsibility of the delegate handlers to completely consume the response. |
michael@0 | 123 | if (res.wasSuccessful()) { |
michael@0 | 124 | d.handleRequestSuccess(res); |
michael@0 | 125 | } else { |
michael@0 | 126 | Logger.warn(LOG_TAG, "HTTP request failed."); |
michael@0 | 127 | try { |
michael@0 | 128 | Logger.warn(LOG_TAG, "HTTP response body: " + res.getErrorMessage()); |
michael@0 | 129 | } catch (Exception e) { |
michael@0 | 130 | Logger.error(LOG_TAG, "Can't fetch HTTP response body.", e); |
michael@0 | 131 | } |
michael@0 | 132 | d.handleRequestFailure(res); |
michael@0 | 133 | } |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | @Override |
michael@0 | 137 | public void handleHttpProtocolException(ClientProtocolException e) { |
michael@0 | 138 | this.request.delegate.handleRequestError(e); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | @Override |
michael@0 | 142 | public void handleHttpIOException(IOException e) { |
michael@0 | 143 | this.request.delegate.handleRequestError(e); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | @Override |
michael@0 | 147 | public void handleTransportException(GeneralSecurityException e) { |
michael@0 | 148 | this.request.delegate.handleRequestError(e); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | @Override |
michael@0 | 152 | public void addHeaders(HttpRequestBase request, DefaultHttpClient client) { |
michael@0 | 153 | // Clients can use their delegate interface to specify X-If-Unmodified-Since. |
michael@0 | 154 | String ifUnmodifiedSince = this.request.delegate.ifUnmodifiedSince(); |
michael@0 | 155 | if (ifUnmodifiedSince != null) { |
michael@0 | 156 | Logger.debug(LOG_TAG, "Making request with X-If-Unmodified-Since = " + ifUnmodifiedSince); |
michael@0 | 157 | request.setHeader("x-if-unmodified-since", ifUnmodifiedSince); |
michael@0 | 158 | } |
michael@0 | 159 | if (request.getMethod().equalsIgnoreCase("DELETE")) { |
michael@0 | 160 | request.addHeader("x-confirm-delete", "1"); |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | protected BaseResourceDelegate resourceDelegate; |
michael@0 | 166 | public SyncStorageRequestDelegate delegate; |
michael@0 | 167 | protected BaseResource resource; |
michael@0 | 168 | |
michael@0 | 169 | public SyncStorageRequest() { |
michael@0 | 170 | super(); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | // Default implementation. Override this. |
michael@0 | 174 | protected BaseResourceDelegate makeResourceDelegate(SyncStorageRequest request) { |
michael@0 | 175 | return new SyncStorageResourceDelegate(request); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | @Override |
michael@0 | 179 | public void get() { |
michael@0 | 180 | this.resource.get(); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | @Override |
michael@0 | 184 | public void delete() { |
michael@0 | 185 | this.resource.delete(); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | @Override |
michael@0 | 189 | public void post(HttpEntity body) { |
michael@0 | 190 | this.resource.post(body); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | @Override |
michael@0 | 194 | public void put(HttpEntity body) { |
michael@0 | 195 | this.resource.put(body); |
michael@0 | 196 | } |
michael@0 | 197 | } |