导航菜单

页面标题

页面副标题

Housing v14.7.8 - Query.java 源代码

正在查看: Housing v14.7.8 应用的 Query.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.quickblox.auth.session;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import com.google.firebase.core.settings.RemoteSettings;
import com.quickblox.auth.QBAuth;
import com.quickblox.auth.exception.QBExpirationDateException;
import com.quickblox.auth.model.QBProvider;
import com.quickblox.auth.session.QBSettings;
import com.quickblox.core.PerformProcessor;
import com.quickblox.core.QBEntityCallback;
import com.quickblox.core.RestMethod;
import com.quickblox.core.Utils;
import com.quickblox.core.account.model.QBAccountSettings;
import com.quickblox.core.exception.BaseServiceException;
import com.quickblox.core.exception.QBResponseException;
import com.quickblox.core.helper.Lo;
import com.quickblox.core.parser.QBJsonParser;
import com.quickblox.core.parser.QBResponseParser;
import com.quickblox.core.query.JsonQuery;
import com.quickblox.core.rest.RestRequest;
import com.quickblox.core.rest.RestResponse;
import com.quickblox.core.server.Performer;
import com.quickblox.core.server.RestRequestCallback;
import com.quickblox.users.model.QBUser;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class Query<T> implements Performer<T>, RestRequestCallback {
    protected QBEntityCallback<T> entityCallback;
    private Object originalObject;
    protected QBResponseParser<T> qbResponseParser;
    private RestRequest restRequest;
    protected T result;
    protected RestRequestCallback versionCallback;
    protected boolean isCancel = false;
    private boolean shouldCheckAccountSynchronizing = true;
    protected Bundle bundleResult = new Bundle();
    protected QBSettings qbSettings = QBSettings.getInstance();

    public class VersionEntityCallback implements RestRequestCallback {
        private VersionEntityCallback() {
        }

        private QBResponseException createDefaultException() {
            ArrayList arrayList = new ArrayList();
            arrayList.add("Response parser was not specified");
            return new QBResponseException(arrayList);
        }

        public void completedWithResponse(RestResponse restResponse) {
            Query query = Query.this;
            if (query.isCancel) {
                return;
            }
            Bundle bundle = query.getBundle();
            Query query2 = Query.this;
            QBResponseParser<T> qBResponseParser = query2.qbResponseParser;
            if (qBResponseParser != null) {
                try {
                    query2.result = (T) qBResponseParser.parse(restResponse, bundle);
                    Date parseExpirationDate = Query.this.parseExpirationDate(Query.this.extractTokenExpirationDateHeaderValue(restResponse));
                    QBSessionManager.getInstance().setTokenExpirationDate(parseExpirationDate);
                    if (QBSessionManager.getInstance().isManuallyCreated()) {
                        SessionExpirationTimer.start(Query.this.calculateLeftTimeInMilliseconds(parseExpirationDate));
                    }
                    e = null;
                } catch (QBExpirationDateException e) {
                    e = new QBResponseException(e.getMessage());
                } catch (QBResponseException e2) {
                    e = e2;
                    Query.this.handleException(e);
                }
            } else {
                e = createDefaultException();
            }
            Query query3 = Query.this;
            if (query3.isCancel) {
                return;
            }
            if (e == null) {
                query3.notifySuccess(query3.result, bundle);
            } else {
                query3.notifyErrors(e);
            }
        }
    }

    private boolean IsShouldUpdateAccountSettings() {
        QBSettings.AutoUpdateMode updateMode = this.qbSettings.getUpdateMode();
        boolean isEmpty = TextUtils.isEmpty(QBSettings.getInstance().getAccountKey());
        if (!this.shouldCheckAccountSynchronizing || updateMode == null || isEmpty) {
            return false;
        }
        return updateMode.isUpdatePeriodExpired();
    }

    private Performer<QBSession> createSessionByParameters(QBSessionParameters qBSessionParameters) {
        return qBSessionParameters == null ? QBAuth.createSession() : qBSessionParameters.getSocialProvider() == null ? QBAuth.createSession(new QBUser(qBSessionParameters.getUserLogin(), qBSessionParameters.getUserPassword(), qBSessionParameters.getUserEmail())) : qBSessionParameters.getSocialProvider().equals(QBProvider.TWITTER_DIGITS) ? QBAuth.createSessionUsingTwitterDigits(qBSessionParameters.getAuthServiceProvider(), qBSessionParameters.getVerifyCredentialsAuth()) : qBSessionParameters.getSocialProvider().equals(QBProvider.FIREBASE_PHONE) ? QBAuth.createSessionUsingFirebase(qBSessionParameters.getProjectId(), qBSessionParameters.getAccessToken()) : QBAuth.createSessionUsingSocialProvider(qBSessionParameters.getSocialProvider(), qBSessionParameters.getAccessToken(), qBSessionParameters.getAccessTokenSecret());
    }

    private void createSessionIfNeed() throws QBResponseException {
        if (!QBSessionManager.getInstance().isValidActiveSession() && isShouldCreateSession() && QBSettings.getInstance().isAutoCreateSession()) {
            createSessionByParameters(QBSessionManager.getInstance().getSessionParameters()).perform();
        }
    }

    public void createSessionIfNeedAsync(final QBEntityCallback<T> qBEntityCallback) {
        if (!QBSessionManager.getInstance().isValidActiveSession() && isShouldCreateSession() && QBSettings.getInstance().isAutoCreateSession()) {
            createSessionByParameters(QBSessionManager.getInstance().getSessionParameters()).performAsync(new QBEntityCallback<QBSession>() {
                public void onError(QBResponseException qBResponseException) {
                    QBEntityCallback qBEntityCallback2 = qBEntityCallback;
                    if (qBEntityCallback2 != null) {
                        qBEntityCallback2.onError(qBResponseException);
                    }
                }

                public void onSuccess(QBSession qBSession, Bundle bundle) {
                    Query.this.performInBgAsyncWithDelegate(qBEntityCallback);
                }
            });
        } else {
            performInBgAsyncWithDelegate(qBEntityCallback);
        }
    }

    public void handleException(QBResponseException qBResponseException) {
        boolean isAuthenticationDataError = Utils.isAuthenticationDataError(qBResponseException);
        if ((qBResponseException.getHttpStatusCode() == 401 && Utils.isExactError(qBResponseException, "Required session does not exist")) || isAuthenticationDataError) {
            if (isAuthenticationDataError) {
                QBSessionManager.getInstance().handleAuthDataError(qBResponseException);
            }
            if (QBSessionManager.getInstance().isManuallyCreated()) {
                QBSessionManager.getInstance().handleSessionExpiredError();
            } else {
                QBSessionManager.getInstance().deleteActiveSession();
                QBSessionManager.getInstance().deleteSessionParameters();
            }
        }
    }

    private RestRequest makeRetrieveAccountSettingsRequest() {
        HashMap hashMap = new HashMap();
        hashMap.put("QB-Account-Key", QBSettings.getInstance().getAccountKey());
        return RestRequest.create("https://api.quickblox.com/account_settings.json", hashMap, (Map) null, RestMethod.GET);
    }

    public void parseAccountRequest(RestResponse restResponse) throws QBResponseException {
        if (restResponse.getStatusCode() > 0) {
            Lo.g(restResponse);
        }
        QBJsonParser qBJsonParser = new QBJsonParser((JsonQuery) null);
        qBJsonParser.setDeserializer(QBAccountSettings.class);
        try {
            QBAccountSettings qBAccountSettings = (QBAccountSettings) qBJsonParser.parse(restResponse, (Bundle) null);
            QBSettings.AutoUpdateMode updateMode = this.qbSettings.getUpdateMode();
            if (updateMode != null) {
                updateMode.updateSettings(qBAccountSettings, this.qbSettings);
            }
            Lo.g("Retrieved custom endpoints. ApiEndpoint: " + qBAccountSettings.getApiEndpoint() + ", ChatEndpoint: " + qBAccountSettings.getChatEndpoint());
        } catch (QBResponseException unused) {
            Lo.g("Synchronizing account settings failed");
            throw new QBResponseException("\nSomething wrong with your Account Key. Please check it in QuickBlox Admin Panel (https://admin.quickblox.com/account), Settings tab.");
        }
    }

    private void setApiVersion(RestRequest restRequest) {
        String apiVersion = this.qbSettings.getApiVersion();
        if (apiVersion != null) {
            restRequest.getHeaders().put("QuickBlox-REST-API-Version", apiVersion);
        }
    }

    private void setFrameworkVersion(RestRequest restRequest) {
        restRequest.getHeaders().put("QB-SDK", a.l("Android ", this.qbSettings.getVersionName()));
    }

    public String buildQueryUrl(Object... objArr) {
        return a.r(new StringBuilder(this.qbSettings.getApiEndpoint() + RemoteSettings.FORWARD_SLASH_STRING), TextUtils.join(RemoteSettings.FORWARD_SLASH_STRING, objArr), ".json");
    }

    public long calculateLeftTimeInMilliseconds(Date date) throws QBExpirationDateException {
        long time = date.getTime() - new Date().getTime();
        if (time > 0) {
            return time + 1000;
        }
        throw new QBExpirationDateException(QBExpirationDateException.ERROR_CALCULATE_LEFT_EXPIRATION_TIME);
    }

    public void cancel() {
        this.isCancel = true;
        this.restRequest.cancel();
        QBResponseParser<T> qBResponseParser = this.qbResponseParser;
        if (qBResponseParser != null) {
            qBResponseParser.cancel();
        }
    }

    public void completedWithResponse(RestResponse restResponse) {
        if (restResponse.getStatusCode() > 0) {
            Lo.g(restResponse);
        }
        this.versionCallback.completedWithResponse(restResponse);
    }

    public <R> R convertTo(PerformProcessor<?> performProcessor) {
        return (R) performProcessor.process(this);
    }

    public void doneSuccess() {
    }

    public String extractTokenExpirationDateHeaderValue(RestResponse restResponse) throws QBExpirationDateException {
        try {
            return (String) restResponse.getHeaders().get("QB-Token-ExpirationDate");
        } catch (Exception unused) {
            throw new QBExpirationDateException(QBExpirationDateException.ERROR_EXTRACT_HEADER_EXPIRATION_DATE);
        }
    }

    public Bundle getBundle() {
        return this.bundleResult;
    }

    public Object getOriginalObject() {
        return this.originalObject;
    }

    public QBResponseParser<T> getParser() {
        return this.qbResponseParser;
    }

    public BaseService getService() {
        try {
            return BaseService.getBaseService();
        } catch (BaseServiceException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getUrl() {
        throw new UnsupportedOperationException();
    }

    public boolean isCanceled() {
        return this.isCancel;
    }

    public boolean isDownloadQuery() {
        return false;
    }

    public boolean isShouldCheckAccountSynchronizing() {
        return this.shouldCheckAccountSynchronizing;
    }

    public boolean isShouldCreateSession() {
        return !QBSessionManager.getInstance().isManuallyCreated();
    }

    public void notifyErrors(QBResponseException qBResponseException) {
        QBEntityCallback<T> qBEntityCallback = this.entityCallback;
        if (qBEntityCallback != null) {
            qBEntityCallback.onError(qBResponseException);
        }
    }

    public void notifySuccess(T t, Bundle bundle) {
        doneSuccess();
        QBEntityCallback<T> qBEntityCallback = this.entityCallback;
        if (qBEntityCallback != null) {
            qBEntityCallback.onSuccess(t, bundle);
        }
    }

    public Date parseExpirationDate(String str) throws QBExpirationDateException {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.ENGLISH).parse(str);
        } catch (ParseException unused) {
            throw new QBExpirationDateException(QBExpirationDateException.ERROR_PARSE_EXPIRATION_DATE);
        }
    }

    public T perform() throws QBResponseException {
        if (IsShouldUpdateAccountSettings()) {
            RestRequest makeRetrieveAccountSettingsRequest = makeRetrieveAccountSettingsRequest();
            Lo.g(makeRetrieveAccountSettingsRequest);
            parseAccountRequest(makeRetrieveAccountSettingsRequest.syncRequest());
        }
        createSessionIfNeed();
        RestRequest restRequest = new RestRequest();
        this.restRequest = restRequest;
        setupRequest(restRequest);
        Lo.g(this.restRequest);
        RestResponse syncRequest = this.restRequest.syncRequest();
        if (syncRequest.getStatusCode() > 0) {
            Lo.g(syncRequest);
        }
        try {
            this.result = (T) this.qbResponseParser.parse(syncRequest, getBundle());
            Date parseExpirationDate = parseExpirationDate(extractTokenExpirationDateHeaderValue(syncRequest));
            QBSessionManager.getInstance().setTokenExpirationDate(parseExpirationDate);
            if (QBSessionManager.getInstance().isManuallyCreated()) {
                SessionExpirationTimer.start(calculateLeftTimeInMilliseconds(parseExpirationDate));
            }
            doneSuccess();
            return this.result;
        } catch (QBExpirationDateException e) {
            throw new QBResponseException(e.getMessage());
        } catch (QBResponseException e2) {
            handleException(e2);
            throw e2;
        }
    }

    public void performAsync(final QBEntityCallback<T> qBEntityCallback) {
        if (IsShouldUpdateAccountSettings()) {
            RestRequest makeRetrieveAccountSettingsRequest = makeRetrieveAccountSettingsRequest();
            Lo.g(makeRetrieveAccountSettingsRequest);
            makeRetrieveAccountSettingsRequest.asyncRequestWithCallback(new RestRequestCallback() {
                public void completedWithResponse(RestResponse restResponse) {
                    Query query = Query.this;
                    if (query.isCancel) {
                        return;
                    }
                    try {
                        query.parseAccountRequest(restResponse);
                        Query.this.createSessionIfNeedAsync(qBEntityCallback);
                    } catch (QBResponseException e) {
                        QBEntityCallback qBEntityCallback2 = qBEntityCallback;
                        if (qBEntityCallback2 != null) {
                            qBEntityCallback2.onError(e);
                        }
                    }
                }
            });
        } else {
            createSessionIfNeedAsync(qBEntityCallback);
        }
        this.versionCallback = new VersionEntityCallback();
    }

    public void performInBgAsyncWithDelegate(QBEntityCallback<T> qBEntityCallback) {
        this.entityCallback = qBEntityCallback;
        RestRequest restRequest = new RestRequest();
        this.restRequest = restRequest;
        try {
            setupRequest(restRequest);
            Lo.g(this.restRequest);
            this.restRequest.asyncRequestWithCallback(this);
        } catch (QBResponseException e) {
            notifyErrors(e);
        }
    }

    public void putIdenticalValue(List<Pair<String, Object>> list, String str, Object obj) {
        if (obj != null) {
            list.add(new Pair<>(str, String.valueOf(obj)));
        }
    }

    public void putValue(Map<String, Object> map, String str, Object obj) {
        if (obj != null) {
            map.put(str, String.valueOf(obj));
        }
    }

    public void setAuthentication(RestRequest restRequest) {
        String token = QBSessionManager.getInstance().getToken();
        if (token != null) {
            restRequest.getHeaders().put("QB-Token", token);
        }
    }

    public void setBody(RestRequest restRequest) {
    }

    public void setBundle(Bundle bundle) {
        if (bundle != null) {
            this.bundleResult = bundle;
        }
    }

    public void setFiles(RestRequest restRequest) {
    }

    public void setHeaders(RestRequest restRequest) {
    }

    public void setMethod(RestRequest restRequest) {
    }

    public void setOriginalObject(Object obj) {
        this.originalObject = obj;
    }

    public void setParams(RestRequest restRequest) {
    }

    public void setParser(QBResponseParser qBResponseParser) {
        this.qbResponseParser = qBResponseParser;
    }

    public void setShouldCheckAccountSynchronizing(boolean z) {
        this.shouldCheckAccountSynchronizing = z;
    }

    public void setShouldRedirect(RestRequest restRequest) {
    }

    public void setUrl(RestRequest restRequest) throws QBResponseException {
        try {
            restRequest.setUrl(new URL(getUrl()));
        } catch (MalformedURLException e) {
            Lo.g("Error occurred while parsing url :" + e.getLocalizedMessage());
            throw new QBResponseException("Incorrect url protocol");
        }
    }

    public void setupRequest(RestRequest restRequest) throws QBResponseException {
        setBody(restRequest);
        setParams(restRequest);
        setHeaders(restRequest);
        setMethod(restRequest);
        setUrl(restRequest);
        setFiles(restRequest);
        setShouldRedirect(restRequest);
        setApiVersion(restRequest);
        setFrameworkVersion(restRequest);
        setAuthentication(restRequest);
        restRequest.setIsDownloadFileRequest(isDownloadQuery());
    }

    public T perform(Bundle bundle) throws QBResponseException {
        return perform();
    }
}