STATUS KinesisVideoClientWrapper::getAuthInfo()

in src/JNI/com/amazonaws/kinesis/video/producer/jni/KinesisVideoClientWrapper.cpp [2319:2431]


STATUS KinesisVideoClientWrapper::getAuthInfo(jmethodID methodId, PBYTE* ppCert, PUINT32 pSize, PUINT64 pExpiration)
{
    // Get the ENV from the JavaVM
    JNIEnv *env;

    BOOL detached = FALSE;
    STATUS retStatus = STATUS_SUCCESS;
    jbyteArray byteArray = NULL;
    jobject jAuthInfoObj = NULL;
    jbyte* bufferPtr = NULL;
    jsize arrayLen = 0;
    jclass authCls = NULL;
    jint authType = 0;
    jlong authExpiration = 0;
    jmethodID authTypeMethodId = NULL;
    jmethodID authDataMethodId = NULL;
    jmethodID authExpirationMethodId = NULL;

    // Store this pointer so we can run the common macros
    KinesisVideoClientWrapper *pWrapper = this;

    INT32 envState = mJvm->GetEnv((PVOID*) &env, JNI_VERSION_1_6);
    if (envState == JNI_EDETACHED) {
        ATTACH_CURRENT_THREAD_TO_JVM(env);

        // Store the detached so we can detach the thread after the call
        detached = TRUE;
    }

    // Call the Java func
    jAuthInfoObj = env->CallObjectMethod(mGlobalJniObjRef, methodId);
    if (jAuthInfoObj == NULL) {
        DLOGE("Failed to get the object for the AuthInfo object. methodId %s", methodId);
        retStatus = STATUS_INVALID_ARG;
        goto CleanUp;
    }

    // Extract the method IDs for the auth object
    authCls = env->GetObjectClass(jAuthInfoObj);
    CHK_JVM_EXCEPTION(env);
    if (authCls == NULL) {
        DLOGE("Failed to get the object class for the AuthInfo object.");
        retStatus = STATUS_INVALID_ARG;
        goto CleanUp;
    }

    // Extract the method ids
    authTypeMethodId = env->GetMethodID(authCls, "getIntAuthType", "()I");
    CHK_JVM_EXCEPTION(env);
    authDataMethodId = env->GetMethodID(authCls, "getData", "()[B");
    CHK_JVM_EXCEPTION(env);
    authExpirationMethodId = env->GetMethodID(authCls, "getExpiration", "()J");
    CHK_JVM_EXCEPTION(env);
    if (authTypeMethodId == NULL || authDataMethodId == NULL || authExpirationMethodId == NULL) {
        DLOGE("Couldn't find methods in AuthType object");
        retStatus = STATUS_INVALID_ARG;
        goto CleanUp;
    }

    authType = env->CallIntMethod(jAuthInfoObj, authTypeMethodId);
    CHK_JVM_EXCEPTION(env);

    authExpiration = env->CallLongMethod(jAuthInfoObj, authExpirationMethodId);
    CHK_JVM_EXCEPTION(env);

    byteArray = (jbyteArray) env->CallObjectMethod(jAuthInfoObj, authDataMethodId);
    CHK_JVM_EXCEPTION(env);

    if (byteArray != NULL) {
        // Extract the bits from the byte buffer
        bufferPtr = env->GetByteArrayElements(byteArray, NULL);
        arrayLen = env->GetArrayLength(byteArray);

        if (arrayLen >= MAX_AUTH_LEN) {
            retStatus = STATUS_INVALID_ARG;
            goto CleanUp;
        }

        // Copy the bits
        MEMCPY(mAuthInfo.data, bufferPtr, arrayLen);
        mAuthInfo.type = authInfoTypeFromInt((UINT32) authType);
        mAuthInfo.expiration = (UINT64) authExpiration;
        mAuthInfo.size = arrayLen;

        // Set the returned values
        *ppCert = (PBYTE) &mAuthInfo.data;
        *pSize = mAuthInfo.size;
        *pExpiration = mAuthInfo.expiration;
    } else {
        mAuthInfo.type = AUTH_INFO_NONE;
        mAuthInfo.size = 0;
        mAuthInfo.expiration = 0;

        // Set the returned values
        *ppCert = NULL;
        *pSize = 0;
        *pExpiration = 0;
    }

CleanUp:

    // Release the array object if allocated
    if (byteArray != NULL) {
        env->ReleaseByteArrayElements(byteArray, bufferPtr, 0);
    }

    // Detach the thread if we have attached it to JVM
    if (detached) {
        mJvm->DetachCurrentThread();
    }

    return retStatus;
}