Fix issues of RCSResourceObject
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / src / main / jni / JniRcsResourceObject.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "JniRcsResourceObject.h"
22
23 #include "JniRcsObject.h"
24 #include "JniRcsResourceAttributes.h"
25 #include "JavaClasses.h"
26 #include "JavaExceptions.h"
27 #include "JavaGlobalRef.h"
28 #include "JniRcsValue.h"
29 #include "Log.h"
30 #include "ScopedEnv.h"
31 #include "Verify.h"
32
33 #include "RCSResourceObject.h"
34 #include "RCSResponse.h"
35 #include "RCSRequest.h"
36 #include "RequestHandler.h"
37
38 #define LOG_TAG "JNI-RCSResourceObject"
39
40 #define CLS_NAME_RCS_RESOURCE_OBJECT PACKAGE_NAME "/server/RcsResourceObject"
41 #define CLS_NAME_RCS_REQUEST PACKAGE_NAME "/server/RcsRequest"
42
43 #define CLS_NAME_AUTO_NOTIFY_POLICY CLS_NAME_RCS_RESOURCE_OBJECT "$AutoNotifyPolicy"
44 #define CLS_NAME_SET_REQUEST_HANDLER_POLICY CLS_NAME_RCS_RESOURCE_OBJECT "$SetRequestHandlerPolicy"
45
46 #define CLS_NAME_GET_REQUEST_HANDLER CLS_NAME_RCS_RESOURCE_OBJECT "$GetRequestHandler"
47 #define CLS_NAME_SET_REQUEST_HANDLER CLS_NAME_RCS_RESOURCE_OBJECT "$SetRequestHandler"
48 #define CLS_NAME_ON_ATTRIBUTE_UPDATESD_LISTENER \
49     CLS_NAME_RCS_RESOURCE_OBJECT "$OnAttributeUpdatedListener"
50
51 #define CLS_NAME_RCS_RESPONSE PACKAGE_NAME "/server/RcsResponse"
52
53 #define CLS_NAME_RCS_GET_RESPONSE PACKAGE_NAME "/server/RcsGetResponse"
54 #define CLS_NAME_RCS_SET_RESPONSE PACKAGE_NAME "/server/RcsSetResponse"
55 #define CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD CLS_NAME_RCS_SET_RESPONSE "$AcceptanceMethod"
56
57 using namespace OIC::Service;
58
59 namespace
60 {
61     jclass g_cls_RCSRequest;
62     jclass g_cls_RCSResourceObject;
63
64     jmethodID g_ctor_RCSRequest;
65     jmethodID g_ctor_RCSResourceObject;
66
67     jmethodID g_method_GetRequestHandler_onGetRequested;
68     jmethodID g_method_SetRequestHandler_onSetRequested;
69     jmethodID g_method_OnAttributeUpdatedListener_onAttributeUpdated;
70
71     jfieldID g_field_RCSResponse_mErrorCode;
72     jfieldID g_field_RCSResponse_mAttrs;
73     jfieldID g_field_RCSSetResponse_mAcceptanceMethod;
74
75     jobject g_obj_RCSSetResponse_AcceptanceMethod_DEFAULT;
76     jobject g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT;
77     jobject g_obj_RCSSetResponse_AcceptanceMethod_IGNORE;
78
79     jobject g_obj_AutoNotifyPolicy_NEVER;
80     jobject g_obj_AutoNotifyPolicy_ALWAYS;
81     jobject g_obj_AutoNotifyPolicy_UPDATED;
82
83     jobject g_obj_SetRequestHandlerPolicy_NEVER;
84     jobject g_obj_SetRequestHandlerPolicy_ACCEPT;
85
86     inline RCSResourceObject::Ptr& getResource(JNIEnv* env, jobject obj)
87     {
88         return getNativeHandleAs< RCSResourceObject::Ptr >(env, obj);
89     }
90
91     inline RCSResourceObject::AutoNotifyPolicy convertAutoNotifyPolicy(JNIEnv* env, jobject obj)
92     {
93         if (env->IsSameObject(g_obj_AutoNotifyPolicy_NEVER, obj))
94         {
95             return RCSResourceObject::AutoNotifyPolicy::NEVER;
96         }
97
98         if (env->IsSameObject(g_obj_AutoNotifyPolicy_ALWAYS, obj))
99         {
100             return RCSResourceObject::AutoNotifyPolicy::ALWAYS;
101         }
102
103         if (env->IsSameObject(g_obj_AutoNotifyPolicy_UPDATED, obj))
104         {
105             return RCSResourceObject::AutoNotifyPolicy::UPDATED;
106         }
107
108         throwRCSException(env, "Failed to convert AutoNotifyPolicy");
109         return {};
110     }
111
112     inline jobject convertAutoNotifyPolicy(JNIEnv* env, RCSResourceObject::AutoNotifyPolicy policy)
113     {
114         switch(policy)
115         {
116             case RCSResourceObject::AutoNotifyPolicy::NEVER: return g_obj_AutoNotifyPolicy_NEVER;
117             case RCSResourceObject::AutoNotifyPolicy::ALWAYS: return g_obj_AutoNotifyPolicy_ALWAYS;
118             case RCSResourceObject::AutoNotifyPolicy::UPDATED: return g_obj_AutoNotifyPolicy_UPDATED;
119         }
120
121         throwRCSException(env, "Failed to convert AutoNotifyPolicy");
122         return {};
123     }
124
125     inline RCSResourceObject::SetRequestHandlerPolicy convertSetRequestHandlerPolicy(JNIEnv* env,
126             jobject obj)
127     {
128         if (env->IsSameObject(g_obj_SetRequestHandlerPolicy_NEVER, obj))
129         {
130             return RCSResourceObject::SetRequestHandlerPolicy::NEVER;
131         }
132
133         if (env->IsSameObject(g_obj_SetRequestHandlerPolicy_ACCEPT, obj))
134         {
135             return RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE;
136         }
137
138         throwRCSException(env, "Failed to convert SetRequestHandlerPolicy");
139         return {};
140     }
141
142     inline jobject convertSetRequestHandlerPolicy(JNIEnv* env,
143             RCSResourceObject::SetRequestHandlerPolicy policy)
144     {
145         switch(policy)
146         {
147             case RCSResourceObject::SetRequestHandlerPolicy::NEVER:
148                 return g_obj_SetRequestHandlerPolicy_NEVER;
149             case RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE:
150                 return g_obj_SetRequestHandlerPolicy_ACCEPT;
151         }
152
153         throwRCSException(env, "Failed to convert SetRequestHandlerPolicy");
154         return {};
155     }
156
157
158     template< typename ENV >
159     inline jobject getAttrsObj(ENV& env, jobject responseObj)
160     {
161         return env->GetObjectField(responseObj, g_field_RCSResponse_mAttrs);
162     }
163
164     template< typename ENV >
165     inline int getErrorCode(ENV& env, jobject responseObj)
166     {
167         return env->GetIntField(responseObj, g_field_RCSResponse_mErrorCode);
168     }
169
170     template< typename ENV >
171     inline RCSSetResponse::AcceptanceMethod getAcceptanceMethod(ENV& env, jobject responseObj)
172     {
173         auto methodObj = env->GetObjectField(responseObj, g_field_RCSSetResponse_mAcceptanceMethod);
174
175         if (env->IsSameObject(methodObj, g_obj_RCSSetResponse_AcceptanceMethod_IGNORE))
176         {
177             return RCSSetResponse::AcceptanceMethod::IGNORE;
178         }
179
180         if (env->IsSameObject(methodObj, g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT))
181         {
182             return RCSSetResponse::AcceptanceMethod::ACCEPT;
183         }
184
185         return RCSSetResponse::AcceptanceMethod::DEFAULT;
186     }
187
188     inline jobject callHandler(ScopedEnvWrapper& env, jobject listener, jmethodID methodId,
189             const RCSRequest& request, const RCSResourceAttributes& attrs)
190     {
191         auto requestObj = env->NewObject(g_cls_RCSRequest, g_ctor_RCSRequest,
192                 env->NewStringUTF(request.getResourceUri().c_str()));
193         auto attrsObj = newAttributesObject(env.get(), attrs);
194
195         return env->CallObjectMethod(listener, methodId, requestObj, attrsObj);
196     }
197
198     template< typename RESPONSE >
199     inline RESPONSE createResponse(ScopedEnvWrapper& env, jobject responseObj)
200     {
201         auto errorCode = getErrorCode(env, responseObj);
202         auto responseAttrsObj = getAttrsObj(env, responseObj);
203
204         if (responseAttrsObj)
205         {
206             return RESPONSE::create(toNativeAttributes(env.get(), responseAttrsObj), errorCode);
207         }
208
209         return RESPONSE::create(errorCode);
210     }
211
212     RCSGetResponse onGetRequest(const RCSRequest& request, const RCSResourceAttributes& attrs,
213             const JavaGlobalRef& listener)
214     {
215         ScopedEnvWrapper env;
216         EXPECT_RET(env, "env is null!", RCSGetResponse::create(-1));
217
218         try
219         {
220             auto responseObj = callHandler(env, listener, g_method_GetRequestHandler_onGetRequested,
221                     request, attrs);
222
223             return createResponse< RCSGetResponse >(env, responseObj);
224         }
225         catch (const JavaException&)
226         {
227             env->ExceptionDescribe();
228             env->ExceptionClear();
229         }
230         return RCSGetResponse::create({ }, -1);
231     }
232
233     RCSSetResponse onSetRequest(const RCSRequest& request, const RCSResourceAttributes& attrs,
234              const JavaGlobalRef& listener)
235     {
236         ScopedEnvWrapper env;
237         EXPECT_RET(env, "env is null!", RCSSetResponse::create(-1));
238
239         try
240         {
241             auto responseObj = callHandler(env, listener, g_method_SetRequestHandler_onSetRequested,
242                                request, attrs);
243
244             auto acceptanceMethod = getAcceptanceMethod(env, responseObj);
245
246             return createResponse< RCSSetResponse >(env, responseObj).
247                     setAcceptanceMethod(acceptanceMethod);
248         }
249         catch (const JavaException&)
250         {
251             env->ExceptionDescribe();
252             env->ExceptionClear();
253         }
254         return RCSSetResponse::create(-1);
255     }
256
257
258     void onAttributeUpdated(const RCSResourceAttributes::Value& oldVal,
259             const RCSResourceAttributes::Value& newVal, const JavaGlobalRef& listener)
260     {
261         ScopedEnvWrapper env;
262         EXPECT(env, "env is null!");
263
264         try
265         {
266             env->CallVoidMethod(listener, g_method_OnAttributeUpdatedListener_onAttributeUpdated,
267                     newRCSValueObject(env.get(), oldVal), newRCSValueObject(env.get(), newVal));
268         }
269         catch (const JavaException&)
270         {
271             env->ExceptionDescribe();
272             env->ExceptionClear();
273         }
274     }
275
276     void initRCSResponse(JNIEnvWrapper* env)
277     {
278         auto clsRCSResponse = env->FindClass(CLS_NAME_RCS_RESPONSE);
279
280         g_field_RCSResponse_mErrorCode = env->GetFieldID(clsRCSResponse, "mErrorCode", "I");
281
282         g_field_RCSResponse_mAttrs = env->GetFieldID(clsRCSResponse, "mAttrs",
283                 AS_SIG(CLS_NAME_RESOURCEATTRIBUTES));
284
285         auto clsRCSSetResponse = env->FindClass(CLS_NAME_RCS_SET_RESPONSE);
286
287         g_field_RCSSetResponse_mAcceptanceMethod = env->GetFieldID(clsRCSSetResponse,
288                 "mAcceptanceMethod", AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD));
289
290         auto clsAcceptanceMethod = env->FindClass(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD);
291
292         g_obj_RCSSetResponse_AcceptanceMethod_DEFAULT = env->NewGlobalRef(
293                 env->GetStaticObjectField(clsAcceptanceMethod, "DEFAULT",
294                         AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD)));
295
296         g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT = env->NewGlobalRef(
297                 env->GetStaticObjectField(clsAcceptanceMethod, "ACCEPT",
298                         AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD)));
299
300         g_obj_RCSSetResponse_AcceptanceMethod_IGNORE = env->NewGlobalRef(
301                 env->GetStaticObjectField(clsAcceptanceMethod, "IGNORE",
302                         AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD)));
303     }
304 }
305
306 void initRCSResourceObject(JNIEnvWrapper* env)
307 {
308     g_cls_RCSResourceObject = env->FindClassAsGlobalRef(CLS_NAME_RCS_RESOURCE_OBJECT);
309
310     g_ctor_RCSResourceObject = env->GetMethodID(g_cls_RCSResourceObject, "<init>", "()V");
311
312     g_cls_RCSRequest = env->FindClassAsGlobalRef(CLS_NAME_RCS_REQUEST);
313
314     g_ctor_RCSRequest = env->GetMethodID(g_cls_RCSRequest, "<init>",
315             "(" AS_SIG(CLS_NAME_STRING) ")V");
316
317     auto clsGetRequestHandler = env->FindClass(CLS_NAME_GET_REQUEST_HANDLER);
318
319     g_method_GetRequestHandler_onGetRequested = env->GetMethodID(clsGetRequestHandler,
320             "onGetRequested",
321             "(" AS_SIG(CLS_NAME_RCS_REQUEST) AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) ")"
322                 AS_SIG(CLS_NAME_RCS_GET_RESPONSE));
323
324     auto clsSetRequestHandler = env->FindClass(CLS_NAME_SET_REQUEST_HANDLER);
325
326     g_method_SetRequestHandler_onSetRequested = env->GetMethodID(clsSetRequestHandler,
327             "onSetRequested",
328             "(" AS_SIG(CLS_NAME_RCS_REQUEST) AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) ")"
329                 AS_SIG(CLS_NAME_RCS_SET_RESPONSE));
330
331     auto clsOnAttributeUpdatedListener = env->FindClass(CLS_NAME_ON_ATTRIBUTE_UPDATESD_LISTENER);
332
333     g_method_OnAttributeUpdatedListener_onAttributeUpdated = env->GetMethodID(
334             clsOnAttributeUpdatedListener, "onAttributeUpdated",
335             "(" AS_SIG(CLS_NAME_VALUE) AS_SIG(CLS_NAME_VALUE) ")V");
336
337     auto clsAutoNotifyPolicy = env->FindClass(CLS_NAME_AUTO_NOTIFY_POLICY);
338
339     g_obj_AutoNotifyPolicy_NEVER = env->NewGlobalRef(
340             env->GetStaticObjectField(clsAutoNotifyPolicy, "NEVER",
341                     AS_SIG(CLS_NAME_AUTO_NOTIFY_POLICY)));
342
343     g_obj_AutoNotifyPolicy_ALWAYS = env->NewGlobalRef(
344             env->GetStaticObjectField(clsAutoNotifyPolicy, "ALWAYS",
345                     AS_SIG(CLS_NAME_AUTO_NOTIFY_POLICY)));
346
347
348     g_obj_AutoNotifyPolicy_UPDATED = env->NewGlobalRef(
349             env->GetStaticObjectField(clsAutoNotifyPolicy, "UPDATED",
350                     AS_SIG(CLS_NAME_AUTO_NOTIFY_POLICY)));
351
352     auto clsSetRequestHandlerPolicy = env->FindClass(CLS_NAME_SET_REQUEST_HANDLER_POLICY);
353
354     g_obj_SetRequestHandlerPolicy_NEVER = env->NewGlobalRef(
355             env->GetStaticObjectField(clsSetRequestHandlerPolicy, "NEVER",
356                     AS_SIG(CLS_NAME_SET_REQUEST_HANDLER_POLICY)));
357
358     g_obj_SetRequestHandlerPolicy_ACCEPT = env->NewGlobalRef(
359             env->GetStaticObjectField(clsSetRequestHandlerPolicy, "ACCEPT",
360                     AS_SIG(CLS_NAME_SET_REQUEST_HANDLER_POLICY)));
361
362     initRCSResponse(env);
363 }
364
365 void clearRCSResourceObject(JNIEnvWrapper* env)
366 {
367     env->DeleteGlobalRef(g_cls_RCSRequest);
368
369     env->DeleteGlobalRef(g_obj_RCSSetResponse_AcceptanceMethod_DEFAULT);
370     env->DeleteGlobalRef(g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT);
371     env->DeleteGlobalRef(g_obj_RCSSetResponse_AcceptanceMethod_IGNORE);
372
373     env->DeleteGlobalRef(g_obj_AutoNotifyPolicy_NEVER);
374     env->DeleteGlobalRef(g_obj_AutoNotifyPolicy_ALWAYS);
375     env->DeleteGlobalRef(g_obj_AutoNotifyPolicy_UPDATED);
376
377     env->DeleteGlobalRef(g_obj_SetRequestHandlerPolicy_NEVER);
378     env->DeleteGlobalRef(g_obj_SetRequestHandlerPolicy_ACCEPT);
379 }
380
381 JNIEXPORT jint JNICALL
382 Java_org_iotivity_service_server_RcsResponse_nativeGetDefaultErrorCode
383 (JNIEnv*, jclass)
384 {
385     return RequestHandler::DEFAULT_ERROR_CODE;
386 }
387
388 JNIEXPORT jobject JNICALL
389 Java_org_iotivity_service_server_RcsResourceObject_nativeBuild
390 (JNIEnv* env, jclass, jstring uriObj, jstring typeObj, jstring interfaceObj, jboolean isObservable,
391         jboolean isDiscoverable, jobject attrsObj)
392 {
393     LOGI("nativeBuild");
394
395     EXPECT_RET_DEF(uriObj, "uri is null.");
396
397     auto uri = toStdString(env, uriObj);
398     auto type = toStdString(env, typeObj);
399     auto interface = toStdString(env, interfaceObj);
400
401     RCSResourceAttributes attrs;
402     if (attrsObj)
403     {
404         attrs = toNativeAttributes(env, attrsObj);
405         VERIFY_NO_EXC_RET_DEF(env);
406     }
407
408     try
409     {
410         auto resource = RCSResourceObject::Builder(uri, type,interface).
411                 setDiscoverable(isDiscoverable). setObservable(isObservable).setAttributes(attrs).
412                 build();
413
414         auto resourceObj = env->NewObject(g_cls_RCSResourceObject, g_ctor_RCSResourceObject);
415         VERIFY_NO_EXC_RET_DEF(env);
416
417         setSafeNativeHandle< decltype(resource) >(env, resourceObj, resource);
418
419         return resourceObj;
420     }
421     catch (const RCSPlatformException& e)
422     {
423         LOGE("%s", e.what());
424         throwPlatformException(env, e);
425     }
426
427     return nullptr;
428 }
429
430
431 JNIEXPORT void JNICALL
432 Java_org_iotivity_service_server_RcsResourceObject_nativeSetAttribute
433 (JNIEnv* env, jobject obj, jstring keyObj, jobject valueObj)
434 {
435     LOGD("nativeSetAttributeInteger");
436
437     EXPECT(keyObj, "key is null.");
438     EXPECT(valueObj, "value is null.");
439
440     auto res = getResource(env, obj);
441     VERIFY_NO_EXC(env);
442
443     res->setAttribute(toStdString(env, keyObj), toNativeAttrsValue(env, valueObj));
444 }
445
446 JNIEXPORT jobject JNICALL
447 Java_org_iotivity_service_server_RcsResourceObject_nativeGetAttributeValue
448 (JNIEnv *env, jobject obj, jstring keyObj)
449 {
450     LOGD("nativeGetAttributeValue");
451
452     EXPECT_RET_DEF(keyObj, "key is null.");
453
454     auto res = getResource(env, obj);
455     VERIFY_NO_EXC_RET_DEF(env);
456
457     try
458     {
459         auto valueObj = newRCSValueObject(env, res->getAttributeValue(toStdString(env, keyObj)));
460         VERIFY_NO_EXC_RET_DEF(env);
461
462         return valueObj;
463     }
464     catch(const RCSInvalidKeyException& e)
465     {
466         return nullptr;
467     }
468 }
469
470 JNIEXPORT jboolean JNICALL
471 Java_org_iotivity_service_server_RcsResourceObject_nativeRemoveAttribute
472 (JNIEnv* env, jobject obj, jstring keyObj)
473 {
474     LOGD("nativeRemoveAttribute");
475
476     EXPECT_RET_DEF(keyObj, "key is null.");
477
478     auto res = getResource(env, obj);
479     VERIFY_NO_EXC_RET_DEF(env);
480
481     return res->removeAttribute(toStdString(env, keyObj));
482 }
483
484 JNIEXPORT jboolean JNICALL
485 Java_org_iotivity_service_server_RcsResourceObject_nativeContainsAttribute
486 (JNIEnv* env, jobject obj, jstring keyObj)
487 {
488     LOGD("nativeContainsAttribute");
489
490     EXPECT_RET_DEF(keyObj, "key is null.");
491
492     auto res = getResource(env, obj);
493     VERIFY_NO_EXC_RET_DEF(env);
494
495     return res->containsAttribute(toStdString(env, keyObj));
496 }
497
498 JNIEXPORT jobject JNICALL
499 Java_org_iotivity_service_server_RcsResourceObject_nativeGetAttributes
500 (JNIEnv* env, jobject obj)
501 {
502     LOGD("nativeGetAttributes");
503
504     auto res = getResource(env, obj);
505     VERIFY_NO_EXC_RET_DEF(env);
506
507     RCSResourceObject::LockGuard lock{ res, RCSResourceObject::AutoNotifyPolicy::NEVER };
508     return newAttributesObject(env, res->getAttributes());
509 }
510
511 JNIEXPORT jboolean JNICALL
512 Java_org_iotivity_service_server_RcsResourceObject_nativeIsObservable
513 (JNIEnv* env, jobject obj)
514 {
515     LOGD("nativeIsObservable");
516
517     auto res = getResource(env, obj);
518     VERIFY_NO_EXC_RET_DEF(env);
519
520     return res->isObservable();
521 }
522
523 JNIEXPORT jboolean JNICALL
524 Java_org_iotivity_service_server_RcsResourceObject_nativeIsDiscoverable
525 (JNIEnv *env, jobject obj)
526 {
527     LOGD("nativeIsDiscoverable");
528
529     auto res = getResource(env, obj);
530     VERIFY_NO_EXC_RET_DEF(env);
531
532     return res->isDiscoverable();
533 }
534
535 JNIEXPORT void JNICALL
536 Java_org_iotivity_service_server_RcsResourceObject_nativeSetGetRequestHandler
537 (JNIEnv *env, jobject obj, jobject listenerObj)
538 {
539     LOGD("nativeSetGetRequestHandler");
540
541     auto res = getResource(env, obj);
542     VERIFY_NO_EXC(env);
543
544     if (listenerObj)
545     {
546         res->setGetRequestHandler(std::bind(onGetRequest, std::placeholders::_1, std::placeholders::_2,
547                 JavaGlobalRef{ env, listenerObj }));
548     }
549     else
550     {
551         res->setGetRequestHandler({ });
552     }
553 }
554
555 JNIEXPORT void JNICALL
556 Java_org_iotivity_service_server_RcsResourceObject_nativeSetSetRequestHandler
557 (JNIEnv* env, jobject obj, jobject listenerObj)
558 {
559     LOGD("nativeSetSetRequestHandler");
560
561     auto res = getResource(env, obj);
562     VERIFY_NO_EXC(env);
563
564     if (listenerObj)
565     {
566         res->setSetRequestHandler(std::bind(onSetRequest, std::placeholders::_1,
567                 std::placeholders::_2, JavaGlobalRef{ env, listenerObj }));
568     }
569     else
570     {
571         res->setSetRequestHandler({ });
572     }
573 }
574
575 JNIEXPORT void JNICALL
576 Java_org_iotivity_service_server_RcsResourceObject_nativeAddAttributeUpdatedListener
577 (JNIEnv* env, jobject obj, jstring keyObj, jobject listenerObj)
578 {
579     LOGD("nativeAddAttributeUpdatedListener");
580
581     auto res = getResource(env, obj);
582     VERIFY_NO_EXC(env);
583
584     auto key = toStdString(env, keyObj);
585     VERIFY_NO_EXC(env);
586
587     res->addAttributeUpdatedListener(std::move(key), std::bind(onAttributeUpdated,
588             std::placeholders::_1, std::placeholders::_2, JavaGlobalRef{ env, listenerObj }));
589 }
590
591 JNIEXPORT jboolean JNICALL
592 Java_org_iotivity_service_server_RcsResourceObject_nativeRemoveAttributeUpdatedListener
593 (JNIEnv* env, jobject obj, jstring keyObj)
594 {
595     LOGD("nativeAddAttributeUpdatedListener");
596
597     EXPECT_RET_DEF(keyObj, "key is null");
598
599     auto res = getResource(env, obj);
600     VERIFY_NO_EXC_RET_DEF(env);
601
602     auto key = toStdString(env, keyObj);
603     VERIFY_NO_EXC_RET_DEF(env);
604
605     return res->removeAttributeUpdatedListener(key);
606 }
607
608 JNIEXPORT void JNICALL Java_org_iotivity_service_server_RcsResourceObject_nativeNotify
609 (JNIEnv* env, jobject obj)
610 {
611     LOGD("nativeNotify");
612
613     auto res = getResource(env, obj);
614     VERIFY_NO_EXC(env);
615
616     try
617     {
618         res->notify();
619     }
620     catch (const RCSPlatformException& e) {
621         throwPlatformException(env, e);
622     }
623 }
624
625 JNIEXPORT void JNICALL
626 Java_org_iotivity_service_server_RcsResourceObject_nativeSetAutoNotifyPolicy
627 (JNIEnv* env, jobject obj, jobject policyObj)
628 {
629     LOGD("nativeSetAutoNotifyPolicy");
630
631     EXPECT(policyObj, "policyObj is null");
632
633     auto res = getResource(env, obj);
634     VERIFY_NO_EXC(env);
635
636     auto policy = convertAutoNotifyPolicy(env, policyObj);
637     VERIFY_NO_EXC(env);
638
639     res->setAutoNotifyPolicy(policy);
640 }
641
642 JNIEXPORT jobject JNICALL
643 Java_org_iotivity_service_server_RcsResourceObject_nativeGetAutoNotifyPolicy
644 (JNIEnv* env, jobject obj)
645 {
646     LOGD("nativeGetAutoNotifyPolicy");
647
648     auto res = getResource(env, obj);
649     VERIFY_NO_EXC_RET_DEF(env);
650
651     return convertAutoNotifyPolicy(env, res->getAutoNotifyPolicy());
652 }
653
654 JNIEXPORT void JNICALL
655 Java_org_iotivity_service_server_RcsResourceObject_nativeSetSetRequestHandlerPolicy
656 (JNIEnv* env, jobject obj, jobject policyObj)
657 {
658     LOGD("nativeSetSetRequestHandlerPolicy");
659
660     EXPECT(policyObj, "policyObj is null");
661
662     auto res = getResource(env, obj);
663     VERIFY_NO_EXC(env);
664
665     auto policy = convertSetRequestHandlerPolicy(env, policyObj);
666     VERIFY_NO_EXC(env);
667
668     res->setSetRequestHandlerPolicy(policy);
669 }
670
671 JNIEXPORT jobject JNICALL
672 Java_org_iotivity_service_server_RcsResourceObject_nativeGetSetRequestHandlerPolicy
673 (JNIEnv* env, jobject obj)
674 {
675     LOGD("nativeGetSetRequestHandlerPolicy");
676
677     auto res = getResource(env, obj);
678     VERIFY_NO_EXC_RET_DEF(env);
679
680     return convertSetRequestHandlerPolicy(env, res->getSetRequestHandlerPolicy());
681 }