BitmojiApiResponseInterceptor.java 3.79 KB
package com.bitstrips.imoji.api;

import android.content.Context;
import android.text.TextUtils;
import com.bitstrips.imoji.monouser.api.TokenErrorResponse;
import com.bitstrips.imoji.monouser.controllers.UserLogoutController;
import com.bitstrips.imoji.monouser.oauth2.OAuth2Manager;
import com.bitstrips.imoji.monouser.oauth2.OAuth2Manager.OnOAuth2TokenRefreshCallback;
import com.bitstrips.imoji.ui.BitmojiBaseActivity;
import com.google.gson.Gson;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Interceptor.Chain;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import dagger.Lazy;
import java.io.IOException;
import javax.inject.Inject;

public class BitmojiApiResponseInterceptor
  implements Interceptor
{
  private static final String FORCE_LOGOUT_ERROR = "force_logout";
  private static final String INVALID_TOKEN_ERROR = "invalid_token";
  private static final String TAG = "ApiResponseInterceptor";
  private final Context mContext;
  private final OAuth2Manager mOAuth2Manager;
  private final Lazy<UserLogoutController> mUserLogoutController;
  
  @Inject
  public BitmojiApiResponseInterceptor(Context paramContext, OAuth2Manager paramOAuth2Manager, Lazy<UserLogoutController> paramLazy)
  {
    this.mContext = paramContext;
    this.mOAuth2Manager = paramOAuth2Manager;
    this.mUserLogoutController = paramLazy;
  }
  
  private boolean isForceLogoutError(TokenErrorResponse paramTokenErrorResponse)
  {
    return (paramTokenErrorResponse != null) && (!TextUtils.isEmpty(paramTokenErrorResponse.getMessage())) && (TextUtils.equals(paramTokenErrorResponse.getMessage().toLowerCase(), "force_logout"));
  }
  
  private boolean isInvalidTokenError(TokenErrorResponse paramTokenErrorResponse)
  {
    return (paramTokenErrorResponse != null) && (!TextUtils.isEmpty(paramTokenErrorResponse.getMessage())) && (TextUtils.equals(paramTokenErrorResponse.getMessage().toLowerCase(), "invalid_token"));
  }
  
  public Response intercept(Interceptor.Chain paramChain)
    throws IOException
  {
    Object localObject = paramChain.request();
    paramChain = paramChain.proceed((Request)localObject);
    if (!TextUtils.isEmpty(((Request)localObject).header("authorization")))
    {
      if ((paramChain == null) || (paramChain.body() == null)) {
        return paramChain;
      }
      if (paramChain.code() == 401)
      {
        localObject = (TokenErrorResponse)new Gson().fromJson(paramChain.body().charStream(), TokenErrorResponse.class);
        if (!isInvalidTokenError((TokenErrorResponse)localObject)) {
          break label98;
        }
        this.mOAuth2Manager.forceRefreshToken(new OAuth2Manager.OnOAuth2TokenRefreshCallback()
        {
          public void onTokenRefreshFailed(boolean paramAnonymousBoolean)
          {
            if (paramAnonymousBoolean) {
              BitmojiBaseActivity.sendShowForceLogoutDialogBroadcast(BitmojiApiResponseInterceptor.this.mContext);
            }
          }
          
          public void onTokenRefreshSucceeded(boolean paramAnonymousBoolean) {}
        });
      }
    }
    for (;;)
    {
      return paramChain;
      label98:
      if (isForceLogoutError((TokenErrorResponse)localObject))
      {
        ((UserLogoutController)this.mUserLogoutController.get()).logout();
        BitmojiBaseActivity.sendShowForceLogoutDialogBroadcast(this.mContext);
        continue;
        if (paramChain.code() == 401)
        {
          ((UserLogoutController)this.mUserLogoutController.get()).logout();
          BitmojiBaseActivity.sendShowForceLogoutDialogBroadcast(this.mContext);
        }
      }
    }
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/bitstrips/imoji/api/BitmojiApiResponseInterceptor.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */