summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/browserid/verifier/AbstractBrowserIDRemoteVerifierClient.java
blob: aa8db2d481d51e1dec828c1fa147236b147c3404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.browserid.verifier;

import java.io.IOException;
import java.net.URI;
import java.security.GeneralSecurityException;

import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.browserid.verifier.BrowserIDVerifierException.BrowserIDVerifierErrorResponseException;
import org.mozilla.gecko.browserid.verifier.BrowserIDVerifierException.BrowserIDVerifierMalformedResponseException;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.net.BaseResourceDelegate;
import org.mozilla.gecko.sync.net.Resource;
import org.mozilla.gecko.sync.net.SyncResponse;

import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.client.ClientProtocolException;

public abstract class AbstractBrowserIDRemoteVerifierClient implements BrowserIDVerifierClient {
  public static final String LOG_TAG = AbstractBrowserIDRemoteVerifierClient.class.getSimpleName();

  protected static class RemoteVerifierResourceDelegate extends BaseResourceDelegate {
    private final BrowserIDVerifierDelegate delegate;

    protected RemoteVerifierResourceDelegate(Resource resource, BrowserIDVerifierDelegate delegate) {
      super(resource);
      this.delegate = delegate;
    }

    @Override
    public String getUserAgent() {
      return null;
    }

    @Override
    public void handleHttpResponse(HttpResponse response) {
      SyncResponse res = new SyncResponse(response);
      int statusCode = res.getStatusCode();
      Logger.debug(LOG_TAG, "Got response with status code " + statusCode + ".");

      if (statusCode != 200) {
        delegate.handleError(new BrowserIDVerifierErrorResponseException("Expected status code 200."));
        return;
      }

      ExtendedJSONObject o = null;
      try {
        o = res.jsonObjectBody();
      } catch (Exception e) {
        delegate.handleError(new BrowserIDVerifierMalformedResponseException(e));
        return;
      }

      String status = o.getString("status");
      if ("failure".equals(status)) {
        delegate.handleFailure(o);
        return;
      }

      if (!("okay".equals(status))) {
        delegate.handleError(new BrowserIDVerifierMalformedResponseException("Expected status okay, got '" + status + "'."));
        return;
      }

      delegate.handleSuccess(o);
    }

    @Override
    public void handleTransportException(GeneralSecurityException e) {
      Logger.warn(LOG_TAG, "Got transport exception.", e);
      delegate.handleError(e);
    }

    @Override
    public void handleHttpProtocolException(ClientProtocolException e) {
      Logger.warn(LOG_TAG, "Got protocol exception.", e);
      delegate.handleError(e);
    }

    @Override
    public void handleHttpIOException(IOException e) {
      Logger.warn(LOG_TAG, "Got IO exception.", e);
      delegate.handleError(e);
    }
  }

  protected final URI verifierUri;

  public AbstractBrowserIDRemoteVerifierClient(URI verifierUri) {
    this.verifierUri = verifierUri;
  }
}