mobile/android/base/sync/net/SyncStorageRequest.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial