@Override
  public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();

    long t1 = System.nanoTime();
    L.i(
        TAG,
        "Sending request="
            + request.url()
            + "   connection="
            + chain.connection()
            + "    head="
            + request.headers()
            + " request=");

    Response response = chain.proceed(request);
    long t2 = System.nanoTime();
    L.i(
        TAG,
        "Received response="
            + response.request().url()
            + "   connect time="
            + ((t2 - t1) / 1e6d)
            + "    head="
            + response.headers()
            + "    tostring"
            + response.toString());

    return response;
  }
  @Override
  public synchronized Request authenticate(Route route, final Response response)
      throws IOException {
    logger.warn(response.toString());

    final AuthResponse currentAuth = loginPrefs.getCurrentAuth();
    if (null == currentAuth || null == currentAuth.refresh_token) {
      return null;
    }

    String errorCode = getErrorCode(response.peekBody(200).string());

    if (errorCode != null) {
      switch (errorCode) {
        case TOKEN_EXPIRED_ERROR_MESSAGE:
          final AuthResponse refreshedAuth;
          try {
            refreshedAuth = refreshAccessToken(currentAuth);
          } catch (HttpResponseStatusException e) {
            return null;
          }
          return response
              .request()
              .newBuilder()
              .header("Authorization", refreshedAuth.token_type + " " + refreshedAuth.access_token)
              .build();
        case TOKEN_NONEXISTENT_ERROR_MESSAGE:
        case TOKEN_INVALID_GRANT_ERROR_MESSAGE:
          // Retry request with the current access_token if the original access_token used in
          // request does not match the current access_token. This case can occur when
          // asynchronous calls are made and are attempting to refresh the access_token where
          // one call succeeds but the other fails. https://github.com/edx/edx-app-android/pull/834
          if (!response
              .request()
              .headers()
              .get("Authorization")
              .split(" ")[1]
              .equals(currentAuth.access_token)) {
            return response
                .request()
                .newBuilder()
                .header("Authorization", currentAuth.token_type + " " + currentAuth.access_token)
                .build();
          }
      }
    }
    return null;
  }
Esempio n. 3
0
    @Override
    public void run() {
      try {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(SO_URL).build();
        Response response = client.newCall(request).execute();

        if (response.isSuccessful()) {
          Reader in = response.body().charStream();
          BufferedReader reader = new BufferedReader(in);

          rawResult = new Gson().fromJson(reader, SOQuestions.class);
          reader.close();

          EventBus.getDefault().post(new ThingsLoadedEvent(QuestionsLoader.this));
        } else {
          Log.e(getClass().getSimpleName(), response.toString());
        }
      } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Exception parsing JSON", e);
      }
    }