mobile/android/base/background/fxa/SkewHandler.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     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.background.fxa;
     7 import java.net.URI;
     8 import java.net.URISyntaxException;
     9 import java.util.HashMap;
    11 import org.mozilla.gecko.background.common.log.Logger;
    12 import org.mozilla.gecko.sync.net.Resource;
    14 import ch.boye.httpclientandroidlib.Header;
    15 import ch.boye.httpclientandroidlib.HttpHeaders;
    16 import ch.boye.httpclientandroidlib.HttpResponse;
    17 import ch.boye.httpclientandroidlib.impl.cookie.DateParseException;
    18 import ch.boye.httpclientandroidlib.impl.cookie.DateUtils;
    20 public class SkewHandler {
    21   private static final String LOG_TAG = "SkewHandler";
    22   protected volatile long skewMillis = 0L;
    23   protected final String hostname;
    25   private static final HashMap<String, SkewHandler> skewHandlers = new HashMap<String, SkewHandler>();
    27   public static SkewHandler getSkewHandlerForResource(final Resource resource) {
    28     return getSkewHandlerForHostname(resource.getHostname());
    29   }
    31   public static SkewHandler getSkewHandlerFromEndpointString(final String url) throws URISyntaxException {
    32     if (url == null) {
    33       throw new IllegalArgumentException("url must not be null.");
    34     }
    35     URI u = new URI(url);
    36     return getSkewHandlerForHostname(u.getHost());
    37   }
    39   public static synchronized SkewHandler getSkewHandlerForHostname(final String hostname) {
    40     SkewHandler handler = skewHandlers.get(hostname);
    41     if (handler == null) {
    42       handler = new SkewHandler(hostname);
    43       skewHandlers.put(hostname, handler);
    44     }
    45     return handler;
    46   }
    48   public static synchronized void clearSkewHandlers() {
    49     skewHandlers.clear();
    50   }
    52   public SkewHandler(final String hostname) {
    53     this.hostname = hostname;
    54   }
    56   public boolean updateSkewFromServerMillis(long millis, long now) {
    57     skewMillis = millis - now;
    58     Logger.debug(LOG_TAG, "Updated skew: " + skewMillis + "ms for hostname " + this.hostname);
    59     return true;
    60   }
    62   public boolean updateSkewFromHTTPDateString(String date, long now) {
    63     try {
    64       final long millis = DateUtils.parseDate(date).getTime();
    65       return updateSkewFromServerMillis(millis, now);
    66     } catch (DateParseException e) {
    67       Logger.warn(LOG_TAG, "Unexpected: invalid Date header from " + this.hostname);
    68       return false;
    69     }
    70   }
    72   public boolean updateSkewFromDateHeader(Header header, long now) {
    73     String date = header.getValue();
    74     if (null == date) {
    75       Logger.warn(LOG_TAG, "Unexpected: null Date header from " + this.hostname);
    76       return false;
    77     }
    78     return updateSkewFromHTTPDateString(date, now);
    79   }
    81   /**
    82    * Update our tracked skew value to account for the local clock differing from
    83    * the server's.
    84    * 
    85    * @param response
    86    *          the received HTTP response.
    87    * @param now
    88    *          the current time in milliseconds.
    89    * @return true if the skew value was updated, false otherwise.
    90    */
    91   public boolean updateSkew(HttpResponse response, long now) {
    92     Header header = response.getFirstHeader(HttpHeaders.DATE);
    93     if (null == header) {
    94       Logger.warn(LOG_TAG, "Unexpected: missing Date header from " + this.hostname);
    95       return false;
    96     }
    97     return updateSkewFromDateHeader(header, now);
    98   }
   100   public long getSkewInMillis() {
   101     return skewMillis;
   102   }
   104   public long getSkewInSeconds() {
   105     return skewMillis / 1000;
   106   }
   108   public void resetSkew() {
   109     skewMillis = 0L;
   110   }
   111 }

mercurial