JniOcAccountManager.cpp 51.1 KB
Newer Older
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
/*
 *******************************************************************
 *
 * Copyright 2016 Samsung Electronics All Rights Reserved.
 *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 */

#include "JniOcAccountManager.h"
#include "JniOcRepresentation.h"
#include "JniUtils.h"

Jaewook Jung's avatar
Jaewook Jung committed
27 28 29 30
#define VERIFY_NON_NULL_THROW_EXCEPTION(arg, log_message, exc) \
    if (!(arg)) \
    { \
        ThrowOcException(exc, log_message); \
31
        return; \
Jaewook Jung's avatar
Jaewook Jung committed
32 33
    } \

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
JniOcAccountManager::JniOcAccountManager(std::shared_ptr<OCAccountManager> accountManager)
    : m_sharedAccountManager(accountManager)
{
}

JniOcAccountManager::~JniOcAccountManager()
{
    LOGD("~JniOcAccountManager()");

    m_sharedAccountManager = nullptr;

    jint envRet = JNI_ERR;
    JNIEnv *env = GetJNIEnv(envRet);
    if (nullptr == env)
    {
        return;
    }

    m_onGetManager.removeAllListeners(env);
    m_onPostManager.removeAllListeners(env);
    m_onDeleteManager.removeAllListeners(env);
55
    m_onObserveManager.removeAllListeners(env);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    if (JNI_EDETACHED == envRet)
    {
        g_jvm->DetachCurrentThread();
    }
}

std::string JniOcAccountManager::host()
{
    return m_sharedAccountManager->host();
}

OCConnectivityType JniOcAccountManager::connectivityType() const
{
    return m_sharedAccountManager->connectivityType();
}

73 74 75 76 77
std::shared_ptr<OCAccountManager> JniOcAccountManager::getOcAccountManagerObject() const
{
    return m_sharedAccountManager;
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
JniOcAccountManager* JniOcAccountManager::getJniOcAccountManagerPtr(JNIEnv *env, jobject thiz)
{
    JniOcAccountManager *accountManager = GetHandle<JniOcAccountManager>(env, thiz);
    if (env->ExceptionCheck())
    {
        LOGE("Failed to get native handle from OcAccountManager");
    }
    if (!accountManager)
    {
        ThrowOcException(JNI_NO_NATIVE_POINTER, "");
    }
    return accountManager;
}

JniOnGetListener* JniOcAccountManager::addOnGetListener(JNIEnv* env, jobject jListener)
{
    return this->m_onGetManager.addListener(env, jListener, this);
}

JniOnPostListener* JniOcAccountManager::addOnPostListener(JNIEnv* env, jobject jListener)
{
    return this->m_onPostManager.addListener(env, jListener, this);
}

JniOnDeleteListener* JniOcAccountManager::addOnDeleteListener(JNIEnv* env, jobject jListener)
{
    return this->m_onDeleteManager.addListener(env, jListener, this);
}

107 108 109 110 111
JniOnObserveListener* JniOcAccountManager::addOnObserveListener(JNIEnv* env, jobject jListener)
{
    return this->m_onObserveManager.addListener(env, jListener, this);
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
void JniOcAccountManager::removeOnGetListener(JNIEnv* env, jobject jListener)
{
    this->m_onGetManager.removeListener(env, jListener);
}

void JniOcAccountManager::removeOnPostListener(JNIEnv* env, jobject jListener)
{
    this->m_onPostManager.removeListener(env, jListener);
}

void JniOcAccountManager::removeOnDeleteListener(JNIEnv* env, jobject jListener)
{
    this->m_onDeleteManager.removeListener(env, jListener);
}

127 128 129 130 131
void JniOcAccountManager::removeOnObserveListener(JNIEnv* env, jobject jListener)
{
    this->m_onObserveManager.removeListener(env, jListener);
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    getHost
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcAccountManager_getHost
    (JNIEnv *env, jobject thiz)
{
    LOGD("OcAccountManager_getHost");
    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                         thiz);
    if (!accountManager)
    {
        return nullptr;
    }

    return env->NewStringUTF(accountManager->host().c_str());
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    getConnectivityTypeN
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_iotivity_base_OcAccountManager_getConnectivityTypeN
    (JNIEnv *env, jobject thiz)
{
    LOGD("OcAccountManager_getConnectivityType");
    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                         thiz);
    if (!accountManager)
    {
        return -1;
    }

    OCConnectivityType connectivityType = accountManager->connectivityType();
    return static_cast<jint>(connectivityType);
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    signUp0
Jaewook Jung's avatar
Jaewook Jung committed
174 175
* Signature: (Ljava/lang/String;Ljava/lang/String;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
176 177 178 179 180
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signUp0
    (JNIEnv *env, jobject thiz, jstring jAuthProvider, jstring jAuthCode, jobject jListener)
{
    LOGD("OcAccountManager_signUp");
Jaewook Jung's avatar
Jaewook Jung committed
181 182 183 184 185
    VERIFY_NON_NULL_THROW_EXCEPTION(jAuthProvider, "authProvider cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jAuthCode, "authCode cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
186

187 188
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
189

Jaewook Jung's avatar
Jaewook Jung committed
190 191 192 193 194 195 196 197 198
    const char *charAuthProvider = env->GetStringUTFChars(jAuthProvider, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charAuthProvider, "charAuthProvider is null", JNI_EXCEPTION);
    std::string authProvider(charAuthProvider);
    env->ReleaseStringUTFChars(jAuthProvider, charAuthProvider);

    const char *charAuthCode = env->GetStringUTFChars(jAuthCode, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charAuthCode, "charAuthCode is null", JNI_EXCEPTION);
    std::string authCode(charAuthCode);
    env->ReleaseStringUTFChars(jAuthCode, charAuthCode);
199

200 201 202 203 204 205 206 207 208 209 210 211
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

212 213
    try
    {
214
        OCStackResult result = accountManager->signUp(authProvider,
215
                                                      authCode,
216
                                                      postCallback);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_signUp");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    signUp1
Jaewook Jung's avatar
Jaewook Jung committed
232 233
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
234 235 236 237 238 239
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signUp1
    (JNIEnv *env, jobject thiz, jstring jAuthProvider, jstring jAuthCode, jobject jOptionsMap,
     jobject jListener)
{
    LOGD("OcAccountManager_signUp");
Jaewook Jung's avatar
Jaewook Jung committed
240 241 242 243 244 245
    VERIFY_NON_NULL_THROW_EXCEPTION(jAuthProvider, "authProvider cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jAuthCode, "authCode cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jOptionsMap, "options cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
246

247 248
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
249

Jaewook Jung's avatar
Jaewook Jung committed
250 251 252 253 254 255 256 257 258
    const char *charAuthProvider = env->GetStringUTFChars(jAuthProvider, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charAuthProvider, "charAuthProvider is null", JNI_EXCEPTION);
    std::string authProvider(charAuthProvider);
    env->ReleaseStringUTFChars(jAuthProvider, charAuthProvider);

    const char *charAuthCode = env->GetStringUTFChars(jAuthCode, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charAuthCode, "charAuthCode is null", JNI_EXCEPTION);
    std::string authCode(charAuthCode);
    env->ReleaseStringUTFChars(jAuthCode, charAuthCode);
259 260 261 262

    QueryParamsMap optionsMap;
    JniUtils::convertJavaMapToQueryParamsMap(env, jOptionsMap, optionsMap);

263 264 265 266 267 268 269 270 271 272 273 274
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

275 276
    try
    {
277
        OCStackResult result = accountManager->signUp(authProvider,
278 279
                                                      authCode,
                                                      optionsMap,
280
                                                      postCallback);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_signUp");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    signIn0
Jaewook Jung's avatar
Jaewook Jung committed
296 297
* Signature: (Ljava/lang/String;Ljava/lang/String;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
298 299 300 301 302
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signIn0
    (JNIEnv *env, jobject thiz, jstring jUserUuid, jstring jAccessToken, jobject jListener)
{
    LOGD("OcAccountManager_signIn");
Jaewook Jung's avatar
Jaewook Jung committed
303 304 305 306 307
    VERIFY_NON_NULL_THROW_EXCEPTION(jUserUuid, "userUuid cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jAccessToken, "accessToken cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
308

309 310
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
311

Jaewook Jung's avatar
Jaewook Jung committed
312 313 314 315 316 317 318 319 320
    const char *charUserUuid = env->GetStringUTFChars(jUserUuid, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charUserUuid, "charUserUuid is null", JNI_EXCEPTION);
    std::string userUuid(charUserUuid);
    env->ReleaseStringUTFChars(jUserUuid, charUserUuid);

    const char *charAccessToken = env->GetStringUTFChars(jAccessToken, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charAccessToken, "charAccessToken is null", JNI_EXCEPTION);
    std::string accessToken(charAccessToken);
    env->ReleaseStringUTFChars(jAccessToken, charAccessToken);
321

322 323 324 325 326 327 328 329 330 331 332 333
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

334 335
    try
    {
336
        OCStackResult result = accountManager->signIn(userUuid,
337
                                                      accessToken,
338
                                                      postCallback);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_signIn");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    signOut0
Jaewook Jung's avatar
Jaewook Jung committed
354
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
355 356
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signOut0
Jaewook Jung's avatar
Jaewook Jung committed
357
    (JNIEnv *env, jobject thiz, jstring jAccessToken, jobject jListener)
358 359
{
    LOGD("OcAccountManager_signOut");
Jaewook Jung's avatar
Jaewook Jung committed
360 361 362 363
    VERIFY_NON_NULL_THROW_EXCEPTION(jAccessToken, "accessToken cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
364

365 366
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
367

Jaewook Jung's avatar
Jaewook Jung committed
368 369 370 371 372
    const char *charAccessToken = env->GetStringUTFChars(jAccessToken, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charAccessToken, "charAccessToken is null", JNI_EXCEPTION);
    std::string accessToken(charAccessToken);
    env->ReleaseStringUTFChars(jAccessToken, charAccessToken);

373 374 375 376 377 378 379 380 381 382 383 384
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

385 386
    try
    {
387 388
        OCStackResult result = accountManager->signOut(accessToken,
                                                       postCallback);
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_signOut");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    refreshAccessToken0
Jaewook Jung's avatar
Jaewook Jung committed
404 405
* Signature: (Ljava/lang/String;Ljava/lang/String;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
406 407 408 409 410
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_refreshAccessToken0
    (JNIEnv *env, jobject thiz, jstring jUserUuid, jstring jRefreshAccessToken, jobject jListener)
{
    LOGD("OcAccountManager_refreshAccessToken");
Jaewook Jung's avatar
Jaewook Jung committed
411 412 413 414 415
    VERIFY_NON_NULL_THROW_EXCEPTION(jUserUuid, "userUuid cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jRefreshAccessToken, "refreshAccessToken cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
416

417 418
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
419

Jaewook Jung's avatar
Jaewook Jung committed
420 421 422 423 424 425 426 427 428 429
    const char *charUserUuid = env->GetStringUTFChars(jUserUuid, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charUserUuid, "charUserUuid is null", JNI_EXCEPTION);
    std::string userUuid(charUserUuid);
    env->ReleaseStringUTFChars(jUserUuid, charUserUuid);

    const char *charRefreshAccessToken = env->GetStringUTFChars(jRefreshAccessToken, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charRefreshAccessToken, "charRefreshAccessToken is null",
                                    JNI_EXCEPTION);
    std::string refreshAccessToken(charRefreshAccessToken);
    env->ReleaseStringUTFChars(jRefreshAccessToken, charRefreshAccessToken);
430

431 432 433 434 435 436 437 438 439 440 441 442
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

443 444
    try
    {
445
        OCStackResult result = accountManager->refreshAccessToken(userUuid,
446
                                                                  refreshAccessToken,
447
                                                                  postCallback);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_refreshAccessToken");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    searchUser0
* Signature: (Ljava/util/Map;Lorg/iotivity/base/OcAccountManager/OnGetListener;)V
*/
465
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_searchUser0
466 467 468
    (JNIEnv *env, jobject thiz, jobject jQueryMap, jobject jListener)
{
    LOGD("OcAccountManager_searchUser");
Jaewook Jung's avatar
Jaewook Jung committed
469 470 471
    VERIFY_NON_NULL_THROW_EXCEPTION(jQueryMap, "queryMap cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onGetListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
472

473 474
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
475 476 477 478

    QueryParamsMap queryMap;
    JniUtils::convertJavaMapToQueryParamsMap(env, jQueryMap, queryMap);

479 480 481 482 483 484 485 486 487 488 489 490
    JniOnGetListener *onGetListener = jAccountManager->addOnGetListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onGetListener, "onGetListener is null", OC_STACK_ERROR);

    GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
                                              const OCRepresentation& rep, const int eCode)
    {
        onGetListener->onGetCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

491 492
    try
    {
493 494
        OCStackResult result = accountManager->searchUser(queryMap,
                                                          getCallback);
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_searchUser");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    deleteDevice0
Jaewook Jung's avatar
Jaewook Jung committed
510 511
* Signature: (Ljava/lang/String;Ljava/lang/String;
*             Lorg/iotivity/base/OcAccountManager/onDeleteListener;)V
512 513
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_deleteDevice0
Jaewook Jung's avatar
Jaewook Jung committed
514
    (JNIEnv *env, jobject thiz, jstring jAccessToken, jstring jDeviceId, jobject jListener)
515 516
{
    LOGD("OcAccountManager_deleteDevice");
Jaewook Jung's avatar
Jaewook Jung committed
517 518 519 520 521
    VERIFY_NON_NULL_THROW_EXCEPTION(jAccessToken, "accessToken cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jDeviceId, "deviceId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onDeleteListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
522

523 524
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
525

Jaewook Jung's avatar
Jaewook Jung committed
526 527 528 529 530 531 532 533 534
    const char *charAccessToken = env->GetStringUTFChars(jAccessToken, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charAccessToken, "charAccessToken is null", JNI_EXCEPTION);
    std::string accessToken(charAccessToken);
    env->ReleaseStringUTFChars(jAccessToken, charAccessToken);

    const char *charDeviceId = env->GetStringUTFChars(jDeviceId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charDeviceId, "charDeviceId is null", JNI_EXCEPTION);
    std::string deviceId(charDeviceId);
    env->ReleaseStringUTFChars(jDeviceId, charDeviceId);
535

536 537 538 539 540 541 542 543 544 545 546
    JniOnDeleteListener *onDeleteListener = jAccountManager->addOnDeleteListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onDeleteListener, "onDeleteListener is null", OC_STACK_ERROR);

    DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts, const int eCode)
    {
        onDeleteListener->onDeleteCallback(opts, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

547 548
    try
    {
549
        OCStackResult result = accountManager->deleteDevice(accessToken,
550
                                                            deviceId,
551
                                                            deleteCallback);
552 553 554 555 556 557 558 559 560 561 562
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_deleteDevice");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}
563 564 565 566

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    createGroup0
Jaewook Jung's avatar
Jaewook Jung committed
567
* Signature: (Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
568 569
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_createGroup0
Jaewook Jung's avatar
Jaewook Jung committed
570
    (JNIEnv *env, jobject thiz, jobject jListener)
571 572
{
    LOGD("OcAccountManager_createGroup");
Jaewook Jung's avatar
Jaewook Jung committed
573 574
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
575

576 577 578 579 580 581 582 583
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);

    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
584
    {
585 586 587 588 589
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();
590 591 592

    try
    {
593
        OCStackResult result = accountManager->createGroup(postCallback);
594 595 596 597 598 599 600 601 602 603 604 605 606 607
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_createGroup");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
Jaewook Jung's avatar
Jaewook Jung committed
608 609
* Method:    createGroup1
* Signature: (Ljava/util/Map;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
610
*/
Jaewook Jung's avatar
Jaewook Jung committed
611 612
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_createGroup1
    (JNIEnv *env, jobject thiz, jobject jQueryMap, jobject jListener)
613
{
Jaewook Jung's avatar
Jaewook Jung committed
614 615 616 617
    LOGD("OcAccountManager_createGroup");
    VERIFY_NON_NULL_THROW_EXCEPTION(jQueryMap, "queryMap cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
618

619 620
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
621

Jaewook Jung's avatar
Jaewook Jung committed
622 623 624
    QueryParamsMap queryMap;
    JniUtils::convertJavaMapToQueryParamsMap(env, jQueryMap, queryMap);

625 626 627 628 629 630 631 632 633 634 635 636
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

637 638
    try
    {
639 640
        OCStackResult result = accountManager->createGroup(queryMap,
                                                           postCallback);
641 642
        if (OC_STACK_OK != result)
        {
Jaewook Jung's avatar
Jaewook Jung committed
643
            ThrowOcException(result, "OcAccountManager_createGroup");
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    deleteGroup0
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/onDeleteListener;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_deleteGroup0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jobject jListener)
{
    LOGD("OcAccountManager_deleteGroup");
Jaewook Jung's avatar
Jaewook Jung committed
662 663 664
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onDeleteListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
665

666 667
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
668

Jaewook Jung's avatar
Jaewook Jung committed
669 670 671 672
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);
673

674 675 676 677 678 679 680 681 682 683 684
    JniOnDeleteListener *onDeleteListener = jAccountManager->addOnDeleteListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onDeleteListener, "onDeleteListener is null", OC_STACK_ERROR);

    DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts, const int eCode)
    {
        onDeleteListener->onDeleteCallback(opts, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

685 686
    try
    {
687 688
        OCStackResult result = accountManager->deleteGroup(groupId,
                                                           deleteCallback);
689 690 691 692 693 694 695 696 697 698 699 700 701 702
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_deleteGroup");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
Jaewook Jung's avatar
Jaewook Jung committed
703 704
* Method:    getGroupInfoAll0
* Signature: (Lorg/iotivity/base/OcAccountManager/OnGetListener;)V
705
*/
Jaewook Jung's avatar
Jaewook Jung committed
706 707
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_getGroupInfoAll0
    (JNIEnv *env, jobject thiz, jobject jListener)
708
{
Jaewook Jung's avatar
Jaewook Jung committed
709 710 711
    LOGD("OcAccountManager_getGroupInfoAll");
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onGetListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
712

713 714 715 716 717 718 719 720
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);

    JniOnGetListener *onGetListener = jAccountManager->addOnGetListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onGetListener, "onGetListener is null", OC_STACK_ERROR);

    GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
                                              const OCRepresentation& rep, const int eCode)
721
    {
722 723 724 725 726
        onGetListener->onGetCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();
727 728 729

    try
    {
730
        OCStackResult result = accountManager->getGroupInfoAll(getCallback);
731 732
        if (OC_STACK_OK != result)
        {
Jaewook Jung's avatar
Jaewook Jung committed
733
            ThrowOcException(result, "OcAccountManager_getGroupInfoAll");
734 735 736 737 738 739 740 741 742 743 744
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
Jaewook Jung's avatar
Jaewook Jung committed
745 746
* Method:    getGroupInfo0
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnGetListener;)V
747
*/
Jaewook Jung's avatar
Jaewook Jung committed
748 749
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_getGroupInfo0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jobject jListener)
750
{
Jaewook Jung's avatar
Jaewook Jung committed
751 752 753 754
    LOGD("OcAccountManager_getGroupInfo");
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onGetListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
755

756 757
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
758

Jaewook Jung's avatar
Jaewook Jung committed
759 760 761 762
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);
763

764 765 766 767 768 769 770 771 772 773 774 775
    JniOnGetListener *onGetListener = jAccountManager->addOnGetListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onGetListener, "onGetListener is null", OC_STACK_ERROR);

    GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
                                              const OCRepresentation& rep, const int eCode)
    {
        onGetListener->onGetCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

776 777
    try
    {
778 779
        OCStackResult result = accountManager->getGroupInfo(groupId,
                                                            getCallback);
780 781
        if (OC_STACK_OK != result)
        {
Jaewook Jung's avatar
Jaewook Jung committed
782
            ThrowOcException(result, "OcAccountManager_getGroupInfo");
783 784 785 786 787 788 789 790 791 792 793
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
Jaewook Jung's avatar
Jaewook Jung committed
794 795 796
* Method:    addPropertyValueToGroup0
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
797
*/
Jaewook Jung's avatar
Jaewook Jung committed
798 799
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_addPropertyValueToGroup0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jobject jPropertyValue, jobject jListener)
800
{
Jaewook Jung's avatar
Jaewook Jung committed
801 802 803 804 805 806
    LOGD("OcAccountManager_addPropertyValueToGroup");
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jPropertyValue, "propertyValue cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
807

808 809
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
810

Jaewook Jung's avatar
Jaewook Jung committed
811 812
    OCRepresentation *propertyValue = JniOcRepresentation::getOCRepresentationPtr(env,
                                                                                  jPropertyValue);
813

Jaewook Jung's avatar
Jaewook Jung committed
814 815 816 817 818
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);

819 820 821 822 823 824 825 826 827 828 829 830
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

831 832
    try
    {
833
        OCStackResult result = accountManager->addPropertyValueToGroup(groupId,
Jaewook Jung's avatar
Jaewook Jung committed
834
                                                                       *propertyValue,
835
                                                                       postCallback);
836 837
        if (OC_STACK_OK != result)
        {
Jaewook Jung's avatar
Jaewook Jung committed
838
            ThrowOcException(result, "OcAccountManager_addPropertyValueToGroup");
839 840 841 842 843 844 845 846 847 848 849
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
Jaewook Jung's avatar
Jaewook Jung committed
850 851 852
* Method:    deletePropertyValueFromGroup0
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
853
*/
Jaewook Jung's avatar
Jaewook Jung committed
854 855
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_deletePropertyValueFromGroup0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jobject jPropertyValue, jobject jListener)
856
{
Jaewook Jung's avatar
Jaewook Jung committed
857 858 859 860 861 862
    LOGD("OcAccountManager_deletePropertyValueFromGroup");
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jPropertyValue, "propertyValue cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
863

864 865
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
866

Jaewook Jung's avatar
Jaewook Jung committed
867 868
    OCRepresentation *propertyValue = JniOcRepresentation::getOCRepresentationPtr(env,
                                                                                  jPropertyValue);
869

Jaewook Jung's avatar
Jaewook Jung committed
870 871 872 873 874
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);

875 876 877 878 879 880 881 882 883 884 885 886
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

887 888
    try
    {
889
        OCStackResult result = accountManager->deletePropertyValueFromGroup(groupId,
Jaewook Jung's avatar
Jaewook Jung committed
890
                                                                            *propertyValue,
891
                                                                            postCallback);
892 893
        if (OC_STACK_OK != result)
        {
Jaewook Jung's avatar
Jaewook Jung committed
894
            ThrowOcException(result, "OcAccountManager_deletePropertyValueFromGroup");
895 896 897 898 899 900 901 902 903 904 905
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
Jaewook Jung's avatar
Jaewook Jung committed
906 907 908
* Method:    updatePropertyValueOnGroup0
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
909
*/
Jaewook Jung's avatar
Jaewook Jung committed
910 911
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_updatePropertyValueOnGroup0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jobject jPropertyValue, jobject jListener)
912
{
Jaewook Jung's avatar
Jaewook Jung committed
913 914 915 916 917 918
    LOGD("OcAccountManager_updatePropertyValueOnGroup");
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jPropertyValue, "propertyValue cannot be null",
                                    OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
919

920 921
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
922

Jaewook Jung's avatar
Jaewook Jung committed
923 924
    OCRepresentation *propertyValue = JniOcRepresentation::getOCRepresentationPtr(env,
                                                                                  jPropertyValue);
925

Jaewook Jung's avatar
Jaewook Jung committed
926 927 928 929
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);
930

931 932 933 934 935 936 937 938 939 940 941 942
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

943 944
    try
    {
945
        OCStackResult result = accountManager->updatePropertyValueOnGroup(groupId,
Jaewook Jung's avatar
Jaewook Jung committed
946
                                                                          *propertyValue,
947
                                                                          postCallback);
948 949
        if (OC_STACK_OK != result)
        {
Jaewook Jung's avatar
Jaewook Jung committed
950
            ThrowOcException(result, "OcAccountManager_updatePropertyValueOnGroup");
951 952 953 954 955 956 957 958 959 960 961 962
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    observeGroup0
Jaewook Jung's avatar
Jaewook Jung committed
963
* Signature: (Lorg/iotivity/base/OcResource/OnObserveListener;)V
964 965
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_observeGroup0
Jaewook Jung's avatar
Jaewook Jung committed
966
    (JNIEnv *env, jobject thiz, jobject jListener)
967 968
{
    LOGD("OcAccountManager_observeGroup");
Jaewook Jung's avatar
Jaewook Jung committed
969 970
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onObserveListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
971

972 973 974 975 976 977 978 979 980 981 982 983
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);

    JniOnObserveListener *onObserveListener = jAccountManager->addOnObserveListener(env,
                                                                                    jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onObserveListener, "onObserveListener is null",
                                    OC_STACK_ERROR);

    ObserveCallback observeCallback = [onObserveListener](const HeaderOptions& opts,
                                                          const OCRepresentation& rep,
                                                          const int& eCode,
                                                          const int& sequenceNumber)
984
    {
985 986 987 988 989
        onObserveListener->onObserveCallback(opts, rep, eCode, sequenceNumber);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();
990 991 992

    try
    {
993
        OCStackResult result = accountManager->observeGroup(observeCallback);
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_observeGroup");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    cancelObserveGroup0
Jaewook Jung's avatar
Jaewook Jung committed
1009
* Signature: ()V
1010 1011
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_cancelObserveGroup0
Jaewook Jung's avatar
Jaewook Jung committed
1012
    (JNIEnv *env, jobject thiz)
1013 1014 1015
{
    LOGD("OcAccountManager_cancelObserveGroup");

1016 1017 1018 1019 1020
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();
1021 1022 1023

    try
    {
Jaewook Jung's avatar
Jaewook Jung committed
1024
        OCStackResult result = accountManager->cancelObserveGroup();
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_cancelObserveGroup");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    observeInvitation0
* Signature: (Lorg/iotivity/base/OcResource/OnObserveListener;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_observeInvitation0
    (JNIEnv *env, jobject thiz, jobject jListener)
{
    LOGD("OcAccountManager_observeInvitation");
Jaewook Jung's avatar
Jaewook Jung committed
1046 1047
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onObserveListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
1048

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);

    JniOnObserveListener *onObserveListener = jAccountManager->addOnObserveListener(env,
                                                                                    jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onObserveListener, "onObserveListener is null",
                                    OC_STACK_ERROR);

    ObserveCallback observeCallback = [onObserveListener](const HeaderOptions& opts,
                                                          const OCRepresentation& rep,
                                                          const int& eCode,
                                                          const int& sequenceNumber)
1061
    {
1062 1063 1064 1065 1066
        onObserveListener->onObserveCallback(opts, rep, eCode, sequenceNumber);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();
1067 1068 1069

    try
    {
1070
        OCStackResult result = accountManager->observeInvitation(observeCallback);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_observeInvitation");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    cancelObserveInvitation0
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_cancelObserveInvitation0
    (JNIEnv *env, jobject thiz)
{
    LOGD("OcAccountManager_cancelObserveInvitation");

1093 1094 1095 1096 1097
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

    try
    {
        OCStackResult result = accountManager->cancelObserveInvitation();
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_cancelObserveInvitation");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    sendInvitation0
Jaewook Jung's avatar
Jaewook Jung committed
1117 1118
* Signature: (Ljava/lang/String;Ljava/lang/String;
*             Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
1119 1120 1121 1122 1123
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_sendInvitation0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jstring jUserUuid, jobject jListener)
{
    LOGD("OcAccountManager_sendInvitation");
Jaewook Jung's avatar
Jaewook Jung committed
1124 1125 1126 1127
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jUserUuid, "userUuid cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onPostListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
1128

1129 1130
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
1131

Jaewook Jung's avatar
Jaewook Jung committed
1132 1133 1134 1135 1136 1137 1138 1139 1140
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);

    const char *charUserUuid = env->GetStringUTFChars(jUserUuid, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charUserUuid, "charUserUuid is null", JNI_EXCEPTION);
    std::string userUuid(charUserUuid);
    env->ReleaseStringUTFChars(jUserUuid, charUserUuid);
1141

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    JniOnPostListener *onPostListener = jAccountManager->addOnPostListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onPostListener, "onPostListener is null", OC_STACK_ERROR);

    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
                                                 const OCRepresentation& rep, const int eCode)
    {
        onPostListener->onPostCallback(opts, rep, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

1154 1155
    try
    {
1156
        OCStackResult result = accountManager->sendInvitation(groupId,
1157
                                                              userUuid,
1158
                                                              postCallback);
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_sendInvitation");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
* Method:    cancelInvitation0
Jaewook Jung's avatar
Jaewook Jung committed
1174 1175
* Signature: (Ljava/lang/String;Ljava/lang/String;
*             Lorg/iotivity/base/OcAccountManager/onDeleteListener;)V
1176 1177 1178 1179 1180
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_cancelInvitation0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jstring jUserUuid, jobject jListener)
{
    LOGD("OcAccountManager_cancelInvitation");
Jaewook Jung's avatar
Jaewook Jung committed
1181 1182 1183 1184
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jUserUuid, "userUuid cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onDeleteListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
1185

1186 1187
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
1188

Jaewook Jung's avatar
Jaewook Jung committed
1189 1190 1191 1192 1193 1194 1195 1196 1197
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);

    const char *charUserUuid = env->GetStringUTFChars(jUserUuid, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charUserUuid, "charUserUuid is null", JNI_EXCEPTION);
    std::string userUuid(charUserUuid);
    env->ReleaseStringUTFChars(jUserUuid, charUserUuid);
1198

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
    JniOnDeleteListener *onDeleteListener = jAccountManager->addOnDeleteListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onDeleteListener, "onDeleteListener is null", OC_STACK_ERROR);

    DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts, const int eCode)
    {
        onDeleteListener->onDeleteCallback(opts, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

1210 1211
    try
    {
1212
        OCStackResult result = accountManager->cancelInvitation(groupId,
1213
                                                                userUuid,
1214
                                                                deleteCallback);
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
        if (OC_STACK_OK != result)
        {
            ThrowOcException(result, "OcAccountManager_cancelInvitation");
        }
    }
    catch (OCException& e)
    {
        LOGE("%s", e.reason().c_str());
        ThrowOcException(e.code(), e.reason().c_str());
    }
}

/*
* Class:     org_iotivity_base_OcAccountManager
Jaewook Jung's avatar
Jaewook Jung committed
1229 1230
* Method:    replyToInvitation0
* Signature: (Ljava/lang/String;ZLorg/iotivity/base/OcAccountManager/onDeleteListener;)V
1231
*/
Jaewook Jung's avatar
Jaewook Jung committed
1232 1233
JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_replyToInvitation0
    (JNIEnv *env, jobject thiz, jstring jGroupId, jboolean jAccept, jobject jListener)
1234
{
Jaewook Jung's avatar
Jaewook Jung committed
1235 1236 1237 1238
    LOGD("OcAccountManager_replyToInvitation");
    VERIFY_NON_NULL_THROW_EXCEPTION(jGroupId, "groupId cannot be null", OC_STACK_INVALID_PARAM);
    VERIFY_NON_NULL_THROW_EXCEPTION(jListener, "onDeleteListener cannot be null",
                                    OC_STACK_INVALID_PARAM);
1239

1240 1241
    JniOcAccountManager *jAccountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
                                                                                          thiz);
1242

Jaewook Jung's avatar
Jaewook Jung committed
1243 1244 1245 1246
    const char *charGroupId = env->GetStringUTFChars(jGroupId, nullptr);
    VERIFY_NON_NULL_THROW_EXCEPTION(charGroupId, "charGroupId is null", JNI_EXCEPTION);
    std::string groupId(charGroupId);
    env->ReleaseStringUTFChars(jGroupId, charGroupId);
1247

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
    JniOnDeleteListener *onDeleteListener = jAccountManager->addOnDeleteListener(env, jListener);
    VERIFY_NON_NULL_THROW_EXCEPTION(onDeleteListener, "onDeleteListener is null", OC_STACK_ERROR);

    DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts, const int eCode)
    {
        onDeleteListener->onDeleteCallback(opts, eCode);
    };

    std::shared_ptr<OCAccountManager> accountManager =
                                            jAccountManager->getOcAccountManagerObject();

1259 1260
    try
    {
1261
        OCStackResult result = accountManager->replyToInvitation(groupId,
Jaewook Jung's avatar
Jaewook Jung committed
1262
                                                                 static_cast<bool>(jAccept),
1263
                                                                 deleteCallback);
1264