replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcResource.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26
27 JniOcResource::JniOcResource(std::shared_ptr<OCResource> resource)
28     : m_sharedResource(resource)
29 {
30 }
31
32 JniOcResource::~JniOcResource()
33 {
34     LOGD("~JniOcResource()");
35
36     m_sharedResource = nullptr;
37
38     jint envRet = JNI_ERR;
39     JNIEnv *env = GetJNIEnv(envRet);
40     if (nullptr == env)
41     {
42         return;
43     }
44
45     m_onGetManager.removeAllListeners(env);
46     m_onPutManager.removeAllListeners(env);
47     m_onPostManager.removeAllListeners(env);
48     m_onDeleteManager.removeAllListeners(env);
49     m_onObserveManager.removeAllListeners(env);
50 #ifdef WITH_MQ
51     m_onFoundTopicResourceManager.removeAllListeners(env);
52     m_onSubcribeTopicManager.removeAllListeners(env);
53 #endif
54
55     if (JNI_EDETACHED == envRet)
56     {
57         g_jvm->DetachCurrentThread();
58     }
59 }
60
61 OCStackResult JniOcResource::get(JNIEnv* env, const QueryParamsMap &queryParametersMap, jobject jListener)
62 {
63     JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
64
65     GetCallback getCallback = [onGetListener](
66         const HeaderOptions& opts,
67         const OCRepresentation& rep,
68         const int eCode)
69     {
70         onGetListener->onGetCallback(opts, rep, eCode);
71     };
72
73     return m_sharedResource->get(queryParametersMap, getCallback);
74 }
75
76 OCStackResult JniOcResource::get(JNIEnv* env, const QueryParamsMap &queryParametersMap, jobject jListener,
77     QualityOfService QoS)
78 {
79     JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
80
81     GetCallback getCallback = [onGetListener](const HeaderOptions& opts, const OCRepresentation& rep,
82         const int eCode)
83     {
84         onGetListener->onGetCallback(opts, rep, eCode);
85     };
86
87     return m_sharedResource->get(queryParametersMap, getCallback, QoS);
88 }
89
90 OCStackResult JniOcResource::get(
91     JNIEnv* env,
92     const std::string &resourceType,
93     const std::string &resourceInterface,
94     const QueryParamsMap &queryParametersMap,
95     jobject jListener)
96 {
97     JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
98
99     GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
100         const OCRepresentation& rep, const int eCode)
101     {
102         onGetListener->onGetCallback(opts, rep, eCode);
103     };
104
105     return m_sharedResource->get(resourceType, resourceInterface, queryParametersMap,
106         getCallback);
107 }
108
109 OCStackResult JniOcResource::get(JNIEnv* env, const std::string &resourceType,
110     const std::string &resourceInterface, const QueryParamsMap &queryParametersMap,
111     jobject jListener, QualityOfService QoS)
112 {
113     JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
114
115     GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
116         const OCRepresentation& rep, const int eCode)
117     {
118         onGetListener->onGetCallback(opts, rep, eCode);
119     };
120
121     return m_sharedResource->get(resourceType, resourceInterface, queryParametersMap,
122         getCallback, QoS);
123 }
124
125 OCStackResult JniOcResource::put(JNIEnv* env, const OCRepresentation &representation,
126     const QueryParamsMap &queryParametersMap, jobject jListener)
127 {
128     JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
129
130     PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
131         const OCRepresentation& rep, const int eCode)
132     {
133         onPutListener->onPutCallback(opts, rep, eCode);
134     };
135
136     return m_sharedResource->put(representation, queryParametersMap, putCallback);
137 }
138
139 OCStackResult JniOcResource::put(JNIEnv* env, const OCRepresentation &representation,
140     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
141 {
142     JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
143
144     PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
145         const OCRepresentation& rep, const int eCode)
146     {
147         onPutListener->onPutCallback(opts, rep, eCode);
148     };
149
150     return m_sharedResource->put(representation, queryParametersMap, putCallback, QoS);
151 }
152
153 OCStackResult JniOcResource::put(JNIEnv* env, const std::string &resourceType,
154     const std::string &resourceInterface, const OCRepresentation &representation,
155     const QueryParamsMap &queryParametersMap, jobject jListener)
156 {
157     JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
158
159     PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
160         const OCRepresentation& rep, const int eCode)
161     {
162         onPutListener->onPutCallback(opts, rep, eCode);
163     };
164
165     return m_sharedResource->put(resourceType, resourceInterface, representation,
166         queryParametersMap, putCallback);
167 }
168
169 OCStackResult JniOcResource::put(JNIEnv* env, const std::string &resourceType,
170     const std::string &resourceInterface, const OCRepresentation &representation,
171     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
172 {
173     JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
174
175     PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
176         const OCRepresentation& rep, const int eCode)
177     {
178         onPutListener->onPutCallback(opts, rep, eCode);
179     };
180
181     return m_sharedResource->put(resourceType, resourceInterface, representation,
182         queryParametersMap, putCallback, QoS);
183 }
184
185 OCStackResult JniOcResource::post(JNIEnv* env, const OCRepresentation &representation,
186     const QueryParamsMap &queryParametersMap, jobject jListener)
187 {
188     JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
189
190     PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
191         const OCRepresentation& rep, const int eCode)
192     {
193         onPostListener->onPostCallback(opts, rep, eCode);
194     };
195
196     return m_sharedResource->post(representation, queryParametersMap, postCallback);
197 }
198
199 OCStackResult JniOcResource::post(JNIEnv* env, const OCRepresentation &representation,
200     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
201 {
202     JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
203
204     PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
205         const OCRepresentation& rep, const int eCode)
206     {
207         onPostListener->onPostCallback(opts, rep, eCode);
208     };
209
210     return m_sharedResource->post(representation, queryParametersMap, postCallback, QoS);
211 }
212
213 OCStackResult JniOcResource::post(JNIEnv* env, const std::string &resourceType,
214     const std::string &resourceInterface, const OCRepresentation &representation,
215     const QueryParamsMap &queryParametersMap, jobject jListener)
216 {
217     JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
218
219     PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
220         const OCRepresentation& rep, const int eCode)
221     {
222         onPostListener->onPostCallback(opts, rep, eCode);
223     };
224
225     return m_sharedResource->post(resourceType, resourceInterface, representation,
226         queryParametersMap, postCallback);
227 }
228
229 OCStackResult JniOcResource::post(JNIEnv* env, const std::string &resourceType,
230     const std::string &resourceInterface, const OCRepresentation &representation,
231     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
232 {
233     JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
234
235     PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
236         const OCRepresentation& rep, const int eCode)
237     {
238         onPostListener->onPostCallback(opts, rep, eCode);
239     };
240
241     return m_sharedResource->post(resourceType, resourceInterface, representation,
242         queryParametersMap, postCallback, QoS);
243 }
244
245 OCStackResult JniOcResource::deleteResource(JNIEnv* env, jobject jListener)
246 {
247     JniOnDeleteListener *onDeleteListener = addOnDeleteListener(env, jListener);
248
249     DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts,
250         const int eCode)
251     {
252         onDeleteListener->onDeleteCallback(opts, eCode);
253     };
254
255     return m_sharedResource->deleteResource(deleteCallback);
256 }
257
258 OCStackResult JniOcResource::deleteResource(JNIEnv* env, jobject jListener, QualityOfService QoS)
259 {
260     JniOnDeleteListener *onDeleteListener = addOnDeleteListener(env, jListener);
261
262     DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts, const int eCode)
263     {
264         onDeleteListener->onDeleteCallback(opts, eCode);
265     };
266
267     return m_sharedResource->deleteResource(deleteCallback, QoS);
268 }
269
270 OCStackResult JniOcResource::observe(JNIEnv* env, ObserveType observeType,
271     const QueryParamsMap &queryParametersMap, jobject jListener)
272 {
273     JniOnObserveListener *onObserveListener = addOnObserveListener(env, jListener);
274
275     ObserveCallback observeCallback = [onObserveListener](const HeaderOptions& opts,
276         const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
277     {
278         onObserveListener->onObserveCallback(opts, rep, eCode, sequenceNumber);
279     };
280
281     return m_sharedResource->observe(observeType, queryParametersMap, observeCallback);
282 }
283
284 OCStackResult JniOcResource::observe(JNIEnv* env, ObserveType observeType,
285     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
286 {
287     JniOnObserveListener *onObserveListener = addOnObserveListener(env, jListener);
288
289     ObserveCallback observeCallback = [onObserveListener](const HeaderOptions& opts,
290         const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
291     {
292         onObserveListener->onObserveCallback(opts, rep, eCode, sequenceNumber);
293     };
294
295     return m_sharedResource->observe(observeType, queryParametersMap, observeCallback, QoS);
296 }
297
298 OCStackResult JniOcResource::cancelObserve(JNIEnv* env, QualityOfService qos)
299 {
300     // In Low case, after delete the callback and send empty message when client receive the notify.
301     // But TCP does not support EMPTY message.
302     if ((CT_ADAPTER_IP & connectivityType()) && QualityOfService::HighQos != qos)
303     {
304         this->m_onObserveManager.removeAllListeners(env);
305     }
306     return m_sharedResource->cancelObserve(qos);
307 }
308
309 void JniOcResource::setHeaderOptions(const HeaderOptions &headerOptions)
310 {
311     m_sharedResource->setHeaderOptions(headerOptions);
312 }
313
314 void JniOcResource::unsetHeaderOptions()
315 {
316     m_sharedResource->unsetHeaderOptions();
317 }
318
319 std::string JniOcResource::host()
320 {
321     return m_sharedResource->host();
322 }
323
324 std::string JniOcResource::uri()
325 {
326     return m_sharedResource->uri();
327 }
328
329 OCConnectivityType JniOcResource::connectivityType() const
330 {
331     return m_sharedResource->connectivityType();
332 }
333
334 bool JniOcResource::isObservable()
335 {
336     return m_sharedResource->isObservable();
337 }
338
339 std::vector< std::string > JniOcResource::getResourceTypes() const
340 {
341     return m_sharedResource->getResourceTypes();
342 }
343
344 std::vector< std::string > JniOcResource::getResourceInterfaces(void) const
345 {
346     return m_sharedResource->getResourceInterfaces();
347 }
348
349 OCResourceIdentifier JniOcResource::uniqueIdentifier() const
350 {
351     return m_sharedResource->uniqueIdentifier();
352 }
353
354 std::string JniOcResource::sid() const
355 {
356     return m_sharedResource->sid();
357 }
358
359 std::string JniOcResource::deviceName() const
360 {
361     return m_sharedResource->deviceName();
362 }
363
364 JniOnGetListener* JniOcResource::addOnGetListener(JNIEnv* env, jobject jListener)
365 {
366     return this->m_onGetManager.addListener(env, jListener, this);
367 }
368
369 JniOnPutListener* JniOcResource::addOnPutListener(JNIEnv* env, jobject jListener)
370 {
371     return this->m_onPutManager.addListener(env, jListener, this);
372 }
373
374 JniOnPostListener* JniOcResource::addOnPostListener(JNIEnv* env, jobject jListener)
375 {
376     return this->m_onPostManager.addListener(env, jListener, this);
377 }
378
379 JniOnDeleteListener* JniOcResource::addOnDeleteListener(JNIEnv* env, jobject jListener)
380 {
381     return this->m_onDeleteManager.addListener(env, jListener, this);
382 }
383
384 JniOnObserveListener* JniOcResource::addOnObserveListener(JNIEnv* env, jobject jListener)
385 {
386     return this->m_onObserveManager.addListener(env, jListener, this);
387 }
388
389 void JniOcResource::removeOnGetListener(JNIEnv* env, jobject jListener)
390 {
391     this->m_onGetManager.removeListener(env, jListener);
392 }
393
394 void JniOcResource::removeOnPutListener(JNIEnv* env, jobject jListener)
395 {
396     this->m_onPutManager.removeListener(env, jListener);
397 }
398
399 void JniOcResource::removeOnPostListener(JNIEnv* env, jobject jListener)
400 {
401     this->m_onPostManager.removeListener(env, jListener);
402 }
403
404 void JniOcResource::removeOnDeleteListener(JNIEnv* env, jobject jListener)
405 {
406     this->m_onDeleteManager.removeListener(env, jListener);
407 }
408
409 void JniOcResource::removeOnObserveListener(JNIEnv* env, jobject jListener)
410 {
411     this->m_onObserveManager.removeListener(env, jListener);
412 }
413
414 #ifdef WITH_MQ
415 JniOnMQTopicFoundListener* JniOcResource::addOnTopicFoundListener(JNIEnv* env, jobject jListener)
416 {
417     return this->m_onFoundTopicResourceManager.addListener(env, jListener, this);
418 }
419
420 void JniOcResource::removeOnTopicFoundListener(JNIEnv* env, jobject jListener)
421 {
422     this->m_onFoundTopicResourceManager.removeListener(env, jListener);
423 }
424
425 JniOnMQSubscribeListener* JniOcResource::addOnMQTopicSubscribeListener(JNIEnv* env, jobject jListener)
426 {
427     return this->m_onSubcribeTopicManager.addListener(env, jListener, this);
428 }
429
430 void JniOcResource::removeOnMQTopicSubscribeListener(JNIEnv* env, jobject jListener)
431 {
432     this->m_onSubcribeTopicManager.removeListener(env, jListener);
433 }
434 #endif
435
436 std::shared_ptr<OCResource> JniOcResource::getOCResource()
437 {
438     return this->m_sharedResource;
439 }
440
441 JniOcResource* JniOcResource::getJniOcResourcePtr(JNIEnv *env, jobject thiz)
442 {
443     JniOcResource *resource = GetHandle<JniOcResource>(env, thiz);
444     if (env->ExceptionCheck())
445     {
446         LOGE("Failed to get native handle from OcResource");
447     }
448     if (!resource)
449     {
450         ThrowOcException(JNI_NO_NATIVE_POINTER, "");
451     }
452     return resource;
453 }
454
455 #ifdef WITH_MQ
456 OCStackResult JniOcResource::discoveryMQTopics(JNIEnv* env,
457     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
458 {
459     JniOnMQTopicFoundListener *onTopicFoundListener = addOnTopicFoundListener(env, jListener);
460
461     MQTopicCallback findCallback = [onTopicFoundListener](const int& eCode,
462             const std::string& uri, std::shared_ptr<OCResource> resource)
463     {
464         onTopicFoundListener->foundTopicCallback(eCode, uri, resource);
465     };
466
467     return m_sharedResource->discoveryMQTopics(queryParametersMap, findCallback, QoS);
468 }
469
470 OCStackResult JniOcResource::createMQTopic(JNIEnv* env,
471     const OCRepresentation &representation, const std::string &targetUri,
472     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
473 {
474     JniOnMQTopicFoundListener *onTopicCreatedListener = addOnTopicFoundListener(env, jListener);
475
476     MQTopicCallback createCallback = [onTopicCreatedListener](const int& eCode,
477             const std::string& uri, std::shared_ptr<OCResource> resource)
478     {
479         onTopicCreatedListener->createdTopicCallback(eCode, uri, resource);
480     };
481
482     return m_sharedResource->createMQTopic(representation, targetUri,
483                                            queryParametersMap,
484                                            createCallback, QoS);
485 }
486 #endif
487 #ifdef MQ_SUBSCRIBER
488 OCStackResult JniOcResource::subscribeMQTopic(JNIEnv* env,
489     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
490 {
491     JniOnMQSubscribeListener *onSubscribeListener = addOnMQTopicSubscribeListener(env, jListener);
492
493     ObserveCallback subscribeCallback = [onSubscribeListener](const HeaderOptions& opts,
494         const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
495     {
496         onSubscribeListener->onSubscribeCallback(opts, rep, eCode, sequenceNumber);
497     };
498
499     return m_sharedResource->subscribeMQTopic(ObserveType::Observe, queryParametersMap,
500                                               subscribeCallback, QoS);
501 }
502
503 OCStackResult JniOcResource::unsubscribeMQTopic(QualityOfService QoS)
504 {
505     return m_sharedResource->unsubscribeMQTopic(QoS);
506 }
507
508 OCStackResult JniOcResource::requestMQPublish(JNIEnv* env,
509     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
510 {
511     JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
512
513     PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
514         const OCRepresentation& rep, const int eCode)
515     {
516         onPostListener->onPostCallback(opts, rep, eCode);
517     };
518
519     return m_sharedResource->requestMQPublish(queryParametersMap, postCallback, QoS);
520 }
521 #endif
522 #ifdef MQ_PUBLISHER
523 OCStackResult JniOcResource::publishMQTopic(JNIEnv* env, const OCRepresentation &representation,
524     const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
525 {
526     JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
527
528     PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
529         const OCRepresentation& rep, const int eCode)
530     {
531         onPostListener->onPostCallback(opts, rep, eCode);
532     };
533
534     return m_sharedResource->publishMQTopic(representation, queryParametersMap,
535                                             postCallback, QoS);
536 }
537 #endif
538
539 /*
540 * Class:     org_iotivity_base_OcResource
541 * Method:    get
542 * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnGetListener;)V
543 */
544 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get
545 (JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener)
546 {
547     LOGD("OcResource_get");
548     if (!jQueryParamsMap)
549     {
550         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
551         return;
552     }
553     if (!jListener)
554     {
555         ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
556         return;
557     }
558     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
559     if (!resource)
560     {
561         return;
562     }
563
564     QueryParamsMap qpm;
565     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
566
567     try
568     {
569         OCStackResult result = resource->get(
570             env,
571             qpm,
572             jListener);
573
574         if (OC_STACK_OK != result)
575         {
576             ThrowOcException(result, "OcResource_get");
577         }
578     }
579     catch (OCException& e)
580     {
581         LOGE("%s", e.reason().c_str());
582         ThrowOcException(e.code(), e.reason().c_str());
583     }
584 }
585
586 /*
587 * Class:     org_iotivity_base_OcResource
588 * Method:    get1
589 * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnGetListener;I)V
590 */
591 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get1
592 (JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS)
593 {
594     LOGD("OcResource_get");
595     if (!jQueryParamsMap)
596     {
597         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
598         return;
599     }
600     if (!jListener)
601     {
602         ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
603         return;
604     }
605     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
606     if (!resource)
607     {
608         return;
609     }
610
611     QueryParamsMap qpm;
612     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
613
614     try
615     {
616         OCStackResult result = resource->get(
617             env,
618             qpm,
619             jListener,
620             JniUtils::getQOS(env, static_cast<int>(jQoS)));
621
622         if (OC_STACK_OK != result)
623         {
624             ThrowOcException(result, "OcResource_get");
625         }
626     }
627     catch (OCException& e)
628     {
629         LOGE("%s", e.reason().c_str());
630         ThrowOcException(e.code(), e.reason().c_str());
631     }
632 }
633
634 /*
635 * Class:     org_iotivity_base_OcResource
636 * Method:    get2
637 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;
638 Lorg/iotivity/base/OcResource/OnGetListener;)V
639 */
640 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get2
641 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
642 jobject jQueryParamsMap, jobject jListener)
643 {
644     LOGD("OcResource_get");
645     std::string resourceType;
646     if (jResourceType)
647     {
648         resourceType = env->GetStringUTFChars(jResourceType, nullptr);
649     }
650     std::string resourceInterface;
651     if (jResourceInterface)
652     {
653         resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr);
654     }
655     if (!jQueryParamsMap)
656     {
657         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
658         return;
659     }
660     if (!jListener)
661     {
662         ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
663         return;
664     }
665     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
666     if (!resource)
667     {
668         return;
669     }
670
671     QueryParamsMap qpm;
672     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
673     try
674     {
675         OCStackResult result = resource->get(
676             env,
677             resourceType,
678             resourceInterface,
679             qpm,
680             jListener);
681
682         if (OC_STACK_OK != result)
683         {
684             ThrowOcException(result, "OcResource_get");
685         }
686     }
687     catch (OCException& e)
688     {
689         LOGE("%s", e.reason().c_str());
690         ThrowOcException(e.code(), e.reason().c_str());
691     }
692 }
693
694 /*
695 * Class:     org_iotivity_base_OcResource
696 * Method:    get3
697 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;
698 Lorg/iotivity/base/OcResource/OnGetListener;I)V
699 */
700 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get3
701 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
702 jobject jQueryParamsMap, jobject jListener, jint jQoS)
703 {
704     LOGD("OcResource_get");
705     if (!jQueryParamsMap)
706     {
707         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
708         return;
709     }
710     if (!jListener)
711     {
712         ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
713         return;
714     }
715     std::string resourceType;
716     if (jResourceType)
717     {
718         resourceType = env->GetStringUTFChars(jResourceType, nullptr);
719     }
720     std::string resourceInterface;
721     if (jResourceInterface)
722     {
723         resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr);
724     }
725     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
726     if (!resource)
727     {
728         return;
729     }
730
731     QueryParamsMap qpm;
732     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
733
734     try
735     {
736         OCStackResult result = resource->get(
737             env,
738             resourceType,
739             resourceInterface,
740             qpm,
741             jListener,
742             JniUtils::getQOS(env, static_cast<int>(jQoS)));
743
744         if (OC_STACK_OK != result)
745         {
746             ThrowOcException(result, "OcResource_get");
747         }
748     }
749     catch (OCException& e)
750     {
751         LOGE("%s", e.reason().c_str());
752         ThrowOcException(e.code(), e.reason().c_str());
753     }
754 }
755
756 /*
757 * Class:     org_iotivity_base_OcResource
758 * Method:    put
759 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;
760 Lorg/iotivity/base/OcResource/OnPutListener;)V
761 */
762 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put
763 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
764 {
765     LOGD("OcResource_put");
766     if (!jRepresentation)
767     {
768         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
769         return;
770     }
771     if (!jQueryParamsMap)
772     {
773         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
774         return;
775     }
776     if (!jListener)
777     {
778         ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
779         return;
780     }
781     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
782     if (!resource)
783     {
784         return;
785     }
786
787     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
788     if (!representation)
789     {
790         return;
791     }
792
793     QueryParamsMap qpm;
794     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
795
796     try
797     {
798         OCStackResult result = resource->put(
799             env,
800             *representation,
801             qpm,
802             jListener);
803
804         if (OC_STACK_OK != result)
805         {
806             ThrowOcException(result, "OcResource_put");
807         }
808     }
809     catch (OCException& e)
810     {
811         LOGE("%s", e.reason().c_str());
812         ThrowOcException(e.code(), e.reason().c_str());
813     }
814 }
815
816 /*
817 * Class:     org_iotivity_base_OcResource
818 * Method:    put1
819 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;
820 Lorg/iotivity/base/OcResource/OnPutListener;I)V
821 */
822 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put1
823 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap,
824 jobject jListener, jint jQoS)
825 {
826     LOGD("OcResource_put");
827     if (!jRepresentation)
828     {
829         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
830         return;
831     }
832     if (!jQueryParamsMap)
833     {
834         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
835         return;
836     }
837     if (!jListener)
838     {
839         ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
840         return;
841     }
842     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
843     if (!resource)
844     {
845         return;
846     }
847
848     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
849     if (!representation)
850     {
851         return;
852     }
853
854     QueryParamsMap qpm;
855     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
856
857     try
858     {
859         OCStackResult result = resource->put(
860             env,
861             *representation,
862             qpm,
863             jListener,
864             JniUtils::getQOS(env, static_cast<int>(jQoS)));
865
866         if (OC_STACK_OK != result)
867         {
868             ThrowOcException(result, "OcResource_put");
869         }
870     }
871     catch (OCException& e)
872     {
873         LOGE("%s", e.reason().c_str());
874         ThrowOcException(e.code(), e.reason().c_str());
875     }
876 }
877
878 /*
879 * Class:     org_iotivity_base_OcResource
880 * Method:    put2
881 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
882 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPutListener;)V
883 */
884 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put2
885 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
886 jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
887 {
888     LOGD("OcResource_put");
889     std::string resourceType;
890     if (jResourceType)
891     {
892         resourceType = env->GetStringUTFChars(jResourceType, nullptr);
893     }
894     std::string resourceInterface;
895     if (jResourceInterface)
896     {
897         resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr);
898     }
899     if (!jRepresentation)
900     {
901         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
902         return;
903     }
904     if (!jQueryParamsMap)
905     {
906         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
907         return;
908     }
909     if (!jListener)
910     {
911         ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
912         return;
913     }
914     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
915     if (!resource)
916     {
917         return;
918     }
919
920     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
921     if (!representation)
922     {
923         return;
924     }
925
926     QueryParamsMap qpm;
927     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
928
929     try
930     {
931         OCStackResult result = resource->put(
932             env,
933             resourceType,
934             resourceInterface,
935             *representation,
936             qpm,
937             jListener);
938
939         if (OC_STACK_OK != result)
940         {
941             ThrowOcException(result, "OcResource_put");
942         }
943     }
944     catch (OCException& e)
945     {
946         LOGE("%s", e.reason().c_str());
947         ThrowOcException(e.code(), e.reason().c_str());
948     }
949 }
950
951 /*
952 * Class:     org_iotivity_base_OcResource
953 * Method:    put3
954 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
955 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPutListener;I)V
956 */
957 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put3
958 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, jobject jRepresentation,
959 jobject jQueryParamsMap, jobject jListener, jint jQoS)
960 {
961     LOGD("OcResource_put");
962     if (!jRepresentation)
963     {
964         ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
965         return;
966     }
967     if (!jQueryParamsMap)
968     {
969         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
970         return;
971     }
972     if (!jListener)
973     {
974         ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
975         return;
976     }
977     std::string resourceType;
978     if (jResourceType)
979     {
980         resourceType = env->GetStringUTFChars(jResourceType, nullptr);
981     }
982     std::string resourceInterface;
983     if (jResourceInterface)
984     {
985         resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr);
986     }
987
988     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
989     if (!resource)
990     {
991         return;
992     }
993
994     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
995     if (!representation)
996     {
997         return;
998     }
999
1000     QueryParamsMap qpm;
1001     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1002
1003     try
1004     {
1005         OCStackResult result = resource->put(
1006             env,
1007             resourceType,
1008             resourceInterface,
1009             *representation,
1010             qpm,
1011             jListener,
1012             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1013
1014         if (OC_STACK_OK != result)
1015         {
1016             ThrowOcException(result, "OcResource_put");
1017         }
1018     }
1019     catch (OCException& e)
1020     {
1021         LOGE("%s", e.reason().c_str());
1022         ThrowOcException(e.code(), e.reason().c_str());
1023     }
1024 }
1025
1026 /*
1027 * Class:     org_iotivity_base_OcResource
1028 * Method:    post
1029 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;)V
1030 */
1031 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post
1032 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
1033 {
1034     LOGD("OcResource_post");
1035     if (!jRepresentation)
1036     {
1037         ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
1038         return;
1039     }
1040     if (!jQueryParamsMap)
1041     {
1042         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1043         return;
1044     }
1045     if (!jListener)
1046     {
1047         ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
1048         return;
1049     }
1050     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1051     if (!resource)
1052     {
1053         return;
1054     }
1055
1056     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
1057     if (!representation)
1058     {
1059         return;
1060     }
1061
1062     QueryParamsMap qpm;
1063     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1064
1065     try
1066     {
1067         OCStackResult result = resource->post(
1068             env,
1069             *representation,
1070             qpm,
1071             jListener);
1072
1073         if (OC_STACK_OK != result)
1074         {
1075             ThrowOcException(result, "OcResource_post");
1076         }
1077     }
1078     catch (OCException& e)
1079     {
1080         LOGE("%s", e.reason().c_str());
1081         ThrowOcException(e.code(), e.reason().c_str());
1082     }
1083 }
1084
1085 /*
1086 * Class:     org_iotivity_base_OcResource
1087 * Method:    post1
1088 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;I)V
1089 */
1090 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post1
1091 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener, jint jQoS)
1092 {
1093     LOGD("OcResource_post");
1094     if (!jRepresentation)
1095     {
1096         ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
1097         return;
1098     }
1099     if (!jQueryParamsMap)
1100     {
1101         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1102         return;
1103     }
1104     if (!jListener)
1105     {
1106         ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
1107         return;
1108     }
1109     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1110     if (!resource)
1111     {
1112         return;
1113     }
1114
1115     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
1116     if (!representation)
1117     {
1118         return;
1119     }
1120
1121     QueryParamsMap qpm;
1122     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1123
1124     try
1125     {
1126         OCStackResult result = resource->post(
1127             env,
1128             *representation,
1129             qpm,
1130             jListener,
1131             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1132
1133         if (OC_STACK_OK != result)
1134         {
1135             ThrowOcException(result, "OcResource_post");
1136         }
1137     }
1138     catch (OCException& e)
1139     {
1140         LOGE("%s", e.reason().c_str());
1141         ThrowOcException(e.code(), e.reason().c_str());
1142     }
1143 }
1144
1145 /*
1146 * Class:     org_iotivity_base_OcResource
1147 * Method:    post2
1148 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
1149 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;)V
1150 */
1151 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post2
1152 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
1153 jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
1154 {
1155     LOGD("OcResource_post");
1156     if (!jRepresentation)
1157     {
1158         ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
1159         return;
1160     }
1161     if (!jQueryParamsMap)
1162     {
1163         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1164         return;
1165     }
1166     if (!jListener)
1167     {
1168         ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
1169         return;
1170     }
1171     std::string resourceType;
1172     if (jResourceType)
1173     {
1174         resourceType = env->GetStringUTFChars(jResourceType, nullptr);
1175     }
1176     std::string resourceInterface;
1177     if (jResourceInterface)
1178     {
1179         resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr);
1180     }
1181
1182     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1183     if (!resource)
1184     {
1185         return;
1186     }
1187
1188     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
1189     if (!representation)
1190     {
1191         return;
1192     }
1193
1194     QueryParamsMap qpm;
1195     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1196
1197     try
1198     {
1199         OCStackResult result = resource->post(
1200             env,
1201             resourceType,
1202             resourceInterface,
1203             *representation,
1204             qpm,
1205             jListener);
1206
1207         if (OC_STACK_OK != result)
1208         {
1209             ThrowOcException(result, "OcResource_post");
1210         }
1211     }
1212     catch (OCException& e)
1213     {
1214         LOGE("%s", e.reason().c_str());
1215         ThrowOcException(e.code(), e.reason().c_str());
1216     }
1217 }
1218
1219 /*
1220 * Class:     org_iotivity_base_OcResource
1221 * Method:    post3
1222 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
1223 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;I)V
1224 */
1225 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post3
1226 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
1227 jobject jRepresentation, jobject jQueryParamsMap, jobject jListener, jint jQoS)
1228 {
1229     LOGD("OcResource_post");
1230     if (!jRepresentation)
1231     {
1232         ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
1233         return;
1234     }
1235     if (!jQueryParamsMap)
1236     {
1237         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1238         return;
1239     }
1240     if (!jListener)
1241     {
1242         ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
1243         return;
1244     }
1245     std::string resourceType;
1246     if (jResourceType)
1247     {
1248         resourceType = env->GetStringUTFChars(jResourceType, nullptr);
1249     }
1250     std::string resourceInterface;
1251     if (jResourceInterface)
1252     {
1253         resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr);
1254     }
1255
1256     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1257     if (!resource)
1258     {
1259         return;
1260     }
1261
1262     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
1263     if (!representation)
1264     {
1265         return;
1266     }
1267
1268     QueryParamsMap qpm;
1269     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1270
1271     try
1272     {
1273         OCStackResult result = resource->post(
1274             env,
1275             resourceType,
1276             resourceInterface,
1277             *representation,
1278             qpm,
1279             jListener,
1280             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1281
1282         if (OC_STACK_OK != result)
1283         {
1284             ThrowOcException(result, "OcResource_post");
1285         }
1286     }
1287     catch (OCException& e)
1288     {
1289         LOGE("%s", e.reason().c_str());
1290         ThrowOcException(e.code(), e.reason().c_str());
1291     }
1292 }
1293
1294 /*
1295 * Class:     org_iotivity_base_OcResource
1296 * Method:    deleteResource
1297 * Signature: (Lorg/iotivity/base/OcResource/OnDeleteListener;)V
1298 */
1299 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_deleteResource
1300 (JNIEnv *env, jobject thiz, jobject jListener)
1301 {
1302     LOGD("OcResource_deleteResource");
1303     if (!jListener)
1304     {
1305         ThrowOcException(OC_STACK_INVALID_PARAM, "onDeleteListener cannot be null");
1306         return;
1307     }
1308     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1309     if (!resource)
1310     {
1311         return;
1312     }
1313
1314     try
1315     {
1316         OCStackResult result = resource->deleteResource(
1317             env,
1318             jListener);
1319
1320         if (OC_STACK_OK != result)
1321         {
1322             ThrowOcException(result, "OcResource_deleteResource");
1323         }
1324     }
1325     catch (OCException& e)
1326     {
1327         LOGE("%s", e.reason().c_str());
1328         ThrowOcException(e.code(), e.reason().c_str());
1329     }
1330 }
1331
1332 /*
1333 * Class:     org_iotivity_base_OcResource
1334 * Method:    deleteResource1
1335 * Signature: (Lorg/iotivity/base/OcResource/OnDeleteListener;I)V
1336 */
1337 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_deleteResource1
1338 (JNIEnv *env, jobject thiz, jobject jListener, jint jQoS)
1339 {
1340     LOGD("OcResource_deleteResource");
1341     if (!jListener)
1342     {
1343         ThrowOcException(OC_STACK_INVALID_PARAM, "onDeleteListener cannot be null");
1344         return;
1345     }
1346     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1347     if (!resource)
1348     {
1349         return;
1350     }
1351
1352     try
1353     {
1354         OCStackResult result = resource->deleteResource(
1355             env,
1356             jListener,
1357             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1358
1359         if (OC_STACK_OK != result)
1360         {
1361             ThrowOcException(result, "OcResource_deleteResource");
1362         }
1363     }
1364     catch (OCException& e)
1365     {
1366         LOGE("%s", e.reason().c_str());
1367         ThrowOcException(e.code(), e.reason().c_str());
1368     }
1369 }
1370
1371 /*
1372 * Class:     org_iotivity_base_OcResource
1373 * Method:    observe
1374 * Signature: (Lorg/iotivity/base/ObserveType;Ljava/util/Map;
1375 Lorg/iotivity/base/OcResource/OnObserveListener;)V
1376 */
1377 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_observe
1378 (JNIEnv *env, jobject thiz, jint observeType, jobject jQueryParamsMap, jobject jListener)
1379 {
1380     LOGD("OcResource_observe");
1381     if (!jQueryParamsMap)
1382     {
1383         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1384         return;
1385     }
1386     if (!jListener)
1387     {
1388         ThrowOcException(OC_STACK_INVALID_PARAM, "onObserveListener cannot be null");
1389         return;
1390     }
1391     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1392     if (!resource)
1393     {
1394         return;
1395     }
1396
1397     QueryParamsMap qpm;
1398     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1399
1400     try
1401     {
1402         OCStackResult result = resource->observe(
1403             env,
1404             JniUtils::getObserveType(env, static_cast<int>(observeType)),
1405             qpm,
1406             jListener);
1407
1408         if (OC_STACK_OK != result)
1409         {
1410             ThrowOcException(result, "OcResource_observe");
1411         }
1412     }
1413     catch (OCException& e)
1414     {
1415         LOGE("%s", e.reason().c_str());
1416         ThrowOcException(e.code(), e.reason().c_str());
1417     }
1418 }
1419
1420 /*
1421 * Class:     org_iotivity_base_OcResource
1422 * Method:    observe1
1423 * Signature: (Lorg/iotivity/base/ObserveType;Ljava/util/Map;
1424 Lorg/iotivity/base/OcResource/OnObserveListener;I)V
1425 */
1426 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_observe1
1427 (JNIEnv *env, jobject thiz, jint observeType, jobject jQueryParamsMap,
1428 jobject jListener, jint jQoS)
1429 {
1430     LOGD("OcResource_observe");
1431     if (!jQueryParamsMap)
1432     {
1433         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1434         return;
1435     }
1436     if (!jListener)
1437     {
1438         ThrowOcException(OC_STACK_INVALID_PARAM, "onObserveListener cannot be null");
1439         return;
1440     }
1441     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1442     if (!resource)
1443     {
1444         return;
1445     }
1446
1447     QueryParamsMap qpm;
1448     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1449
1450     try
1451     {
1452         OCStackResult result = resource->observe(
1453             env,
1454             JniUtils::getObserveType(env, static_cast<int>(observeType)),
1455             qpm,
1456             jListener,
1457             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1458
1459         if (OC_STACK_OK != result)
1460         {
1461             ThrowOcException(result, "OcResource_observe");
1462         }
1463     }
1464     catch (OCException& e)
1465     {
1466         LOGE("%s", e.reason().c_str());
1467         ThrowOcException(e.code(), e.reason().c_str());
1468     }
1469 }
1470
1471 /*
1472 * Class:     org_iotivity_base_OcResource
1473 * Method:    cancelObserve1
1474 * Signature: I)V
1475 */
1476 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_cancelObserve1
1477 (JNIEnv *env, jobject thiz, jint jQoS)
1478 {
1479     LOGD("OcResource_cancelObserve1");
1480     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1481     if (!resource)
1482     {
1483         return;
1484     }
1485
1486     try
1487     {
1488         OCStackResult result = resource->cancelObserve(
1489             env,
1490             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1491
1492         if (OC_STACK_OK != result)
1493         {
1494             ThrowOcException(result, "OcResource_cancelObserve");
1495         }
1496     }
1497     catch (OCException& e)
1498     {
1499         LOGE("%s", e.reason().c_str());
1500         ThrowOcException(e.code(), e.reason().c_str());
1501     }
1502 }
1503
1504 /*
1505 * Class:     org_iotivity_base_OcResource
1506 * Method:    setHeaderOptions
1507 * Signature: ([Lorg/iotivity/OcHeaderOption;)V
1508 */
1509 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_setHeaderOptions
1510 (JNIEnv *env, jobject thiz, jobjectArray jheaderOptionArr)
1511 {
1512     LOGD("OcResource_setHeaderOptions");
1513     if (!jheaderOptionArr)
1514     {
1515         ThrowOcException(OC_STACK_INVALID_PARAM, "headerOptionList cannot be null");
1516         return;
1517     }
1518     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1519     if (!resource)
1520     {
1521         return;
1522     }
1523
1524     HeaderOptions headerOptions;
1525     JniUtils::convertJavaHeaderOptionsArrToVector(env, jheaderOptionArr, headerOptions);
1526
1527     resource->setHeaderOptions(headerOptions);
1528 }
1529
1530 /*
1531 * Class:     org_iotivity_base_OcResource
1532 * Method:    unsetHeaderOptions
1533 * Signature: ()V
1534 */
1535 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_unsetHeaderOptions
1536 (JNIEnv *env, jobject thiz)
1537 {
1538     LOGD("OcResource_unsetHeaderOptions");
1539     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1540     if (!resource)
1541     {
1542         return;
1543     }
1544
1545     resource->unsetHeaderOptions();
1546 }
1547
1548 /*
1549 * Class:     org_iotivity_base_OcResource
1550 * Method:    getHost
1551 * Signature: ()Ljava/lang/String;
1552 */
1553 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcResource_getHost
1554 (JNIEnv *env, jobject thiz)
1555 {
1556     LOGD("OcResource_getHost");
1557     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1558     if (!resource)
1559     {
1560         return nullptr;
1561     }
1562
1563     return env->NewStringUTF(resource->host().c_str());
1564 }
1565
1566 /*
1567 * Class:     org_iotivity_base_OcResource
1568 * Method:    getUri
1569 * Signature: ()Ljava/lang/String;
1570 */
1571 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcResource_getUri
1572 (JNIEnv *env, jobject thiz)
1573 {
1574     LOGD("OcResource_getUri");
1575     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1576     if (!resource)
1577     {
1578         return nullptr;
1579     }
1580
1581     return env->NewStringUTF(resource->uri().c_str());
1582 }
1583
1584 /*
1585 * Class:     org_iotivity_base_OcResource
1586 * Method:    getConnectivityTypeN
1587 * Signature: ()I
1588 */
1589 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcResource_getConnectivityTypeN
1590 (JNIEnv *env, jobject thiz)
1591 {
1592     LOGD("OcResource_getConnectivityType");
1593     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1594     if (!resource)
1595     {
1596         return -1;
1597     }
1598
1599     OCConnectivityType connectivityType = resource->connectivityType();
1600     return static_cast<jint>(connectivityType);
1601 }
1602
1603 /*
1604 * Class:     org_iotivity_base_OcResource
1605 * Method:    isObservable
1606 * Signature: ()Z
1607 */
1608 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcResource_isObservable
1609 (JNIEnv *env, jobject thiz)
1610 {
1611     LOGD("OcResource_isObservable");
1612     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1613     if (!resource)
1614     {
1615         return false;
1616     }
1617
1618     return (jboolean)resource->isObservable();
1619 }
1620
1621 /*
1622 * Class:     org_iotivity_base_OcResource
1623 * Method:    getResourceTypes
1624 * Signature: ()Ljava/util/List;
1625 */
1626 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcResource_getResourceTypes
1627 (JNIEnv *env, jobject thiz)
1628 {
1629     LOGD("OcResource_getResourceTypes");
1630     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1631     if (!resource)
1632     {
1633         return nullptr;
1634     }
1635
1636     std::vector<std::string> resourceTypes = resource->getResourceTypes();
1637
1638     return JniUtils::convertStrVectorToJavaStrList(env, resourceTypes);
1639 }
1640
1641 /*
1642 * Class:     org_iotivity_base_OcResource
1643 * Method:    getResourceInterfaces
1644 * Signature: ()Ljava/util/List;
1645 */
1646 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcResource_getResourceInterfaces
1647 (JNIEnv *env, jobject thiz)
1648 {
1649     LOGD("OcResource_getResourceInterfaces");
1650     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1651     if (!resource)
1652     {
1653         return nullptr;
1654     }
1655
1656     std::vector<std::string> resourceInterfaces = resource->getResourceInterfaces();
1657
1658     return JniUtils::convertStrVectorToJavaStrList(env, resourceInterfaces);
1659 }
1660
1661 /*
1662 * Class:     org_iotivity_base_OcResource
1663 * Method:    getUniqueIdentifier
1664 * Signature: ()Lorg/iotivity/base/OcResourceIdentifier;
1665 */
1666 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcResource_getUniqueIdentifier
1667 (JNIEnv *env, jobject thiz)
1668 {
1669     LOGD("OcResource_getUniqueIdentifier");
1670     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1671     if (!resource)
1672     {
1673         return nullptr;
1674     }
1675
1676     JniOcResourceIdentifier *jniResourceIdentifier =
1677         new JniOcResourceIdentifier(resource->uniqueIdentifier());
1678     if (!jniResourceIdentifier)
1679     {
1680         return nullptr;
1681     }
1682
1683     jlong handle = reinterpret_cast<jlong>(jniResourceIdentifier);
1684     jobject jResourceIdentifier = env->NewObject(g_cls_OcResourceIdentifier,
1685         g_mid_OcResourceIdentifier_N_ctor, handle);
1686     if (!jResourceIdentifier)
1687     {
1688         delete jniResourceIdentifier;
1689     }
1690     return jResourceIdentifier;
1691 }
1692
1693 /*
1694 * Class:     org_iotivity_base_OcResource
1695 * Method:    getServerId
1696 * Signature: ()Ljava/lang/String;
1697 */
1698 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcResource_getServerId
1699 (JNIEnv *env, jobject thiz)
1700 {
1701     LOGD("OcResource_getServerId");
1702     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1703     if (!resource)
1704     {
1705         return nullptr;
1706     }
1707
1708     return env->NewStringUTF(resource->sid().c_str());
1709 }
1710
1711 /*
1712 * Class:     org_iotivity_base_OcResource
1713 * Method:    getDeviceName
1714 * Signature: ()Ljava/lang/String;
1715 */
1716 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcResource_getDeviceName
1717 (JNIEnv *env, jobject thiz)
1718 {
1719     LOGD("OcResource_getDeviceName");
1720     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1721     if (!resource)
1722     {
1723         return nullptr;
1724     }
1725
1726     return env->NewStringUTF(resource->deviceName().c_str());
1727 }
1728
1729 /*
1730 * Class:     org_iotivity_base_OcResource
1731 * Method:    dispose
1732 * Signature: ()V
1733 */
1734 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_dispose
1735 (JNIEnv *env, jobject thiz)
1736 {
1737     LOGD("OcResource_dispose");
1738     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1739     delete resource;
1740 }
1741
1742 /*
1743 * Class:     org_iotivity_base_OcResource
1744 * Method:    discoveryMQTopicsImpl
1745 * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcPlatform/OnResourceFoundListener;I)V
1746 */
1747 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_discoveryMQTopicsImpl
1748 (JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS)
1749 {
1750     LOGD("OcResource_discoveryMQTopicsImpl");
1751
1752 #ifdef WITH_MQ
1753     if (!jQueryParamsMap)
1754     {
1755         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1756         return;
1757     }
1758
1759     if (!jListener)
1760     {
1761         ThrowOcException(OC_STACK_INVALID_PARAM, "listener cannot be null");
1762         return;
1763     }
1764
1765     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1766     if (!resource)
1767     {
1768         return;
1769     }
1770
1771     QueryParamsMap qpm;
1772     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1773
1774     try
1775     {
1776         OCStackResult result = resource->discoveryMQTopics(
1777             env,
1778             qpm,
1779             jListener,
1780             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1781
1782         if (OC_STACK_OK != result)
1783         {
1784             ThrowOcException(result, "OcResource_discoveryMQTopicsImpl");
1785         }
1786     }
1787     catch (OCException& e)
1788     {
1789         LOGE("%s", e.reason().c_str());
1790         ThrowOcException(e.code(), e.reason().c_str());
1791     }
1792 #else
1793     ThrowOcException(JNI_NO_SUPPORT, "not support");
1794 #endif
1795 }
1796
1797 /*
1798 * Class:     org_iotivity_base_OcResource
1799 * Method:    createMQTopicImpl
1800 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/String;Ljava/util/Map
1801 *             ;Lorg/iotivity/base/OcPlatform/OnMQTopicCreatedListener;I)V
1802 */
1803 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_createMQTopicImpl
1804 (JNIEnv *env, jobject thiz, jobject jRepresentation, jstring jUri,
1805  jobject jQueryParamsMap, jobject jListener, jint jQoS)
1806 {
1807     LOGD("OcResource_createMQTopicImpl");
1808
1809 #ifdef WITH_MQ
1810     if (!jQueryParamsMap)
1811     {
1812         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1813         return;
1814     }
1815
1816     if (!jListener)
1817     {
1818         ThrowOcException(OC_STACK_INVALID_PARAM, "listener cannot be null");
1819         return;
1820     }
1821
1822     if (!jRepresentation)
1823     {
1824         ThrowOcException(OC_STACK_INVALID_PARAM, "representation null");
1825         return;
1826     }
1827
1828     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1829     if (!resource)
1830     {
1831         return;
1832     }
1833
1834     std::string targetUri;
1835     if (jUri)
1836     {
1837         targetUri = env->GetStringUTFChars(jUri, nullptr);
1838     }
1839
1840     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env,
1841                                                                                    jRepresentation);
1842     if (!representation)
1843     {
1844         return;
1845     }
1846
1847     QueryParamsMap qpm;
1848     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1849
1850     try
1851     {
1852         OCStackResult result = resource->createMQTopic(
1853             env,
1854             *representation,
1855             targetUri,
1856             qpm,
1857             jListener,
1858             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1859
1860         if (OC_STACK_OK != result)
1861         {
1862             ThrowOcException(result, "OcResource_createMQTopicImpl");
1863         }
1864     }
1865     catch (OCException& e)
1866     {
1867         LOGE("%s", e.reason().c_str());
1868         ThrowOcException(e.code(), e.reason().c_str());
1869     }
1870 #else
1871     ThrowOcException(JNI_NO_SUPPORT, "not support");
1872 #endif
1873 }
1874
1875 /*
1876 * Class:     org_iotivity_base_OcResource
1877 * Method:    subscribeMQTopic
1878 * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnObserveListener;I)V
1879 */
1880 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_subscribeMQTopicImpl
1881 (JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS)
1882 {
1883     LOGD("OcResource_subscribeMQTopicImpl");
1884 #ifdef MQ_SUBSCRIBER
1885     if (!jQueryParamsMap)
1886     {
1887         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1888         return;
1889     }
1890     if (!jListener)
1891     {
1892         ThrowOcException(OC_STACK_INVALID_PARAM, "onObserveListener cannot be null");
1893         return;
1894     }
1895     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1896     if (!resource)
1897     {
1898         return;
1899     }
1900
1901     QueryParamsMap qpm;
1902     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1903
1904     try
1905     {
1906         OCStackResult result = resource->subscribeMQTopic(
1907             env,
1908             qpm,
1909             jListener,
1910             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1911
1912         if (OC_STACK_OK != result)
1913         {
1914             ThrowOcException(result, "OcResource_subscribeMQTopicImpl");
1915         }
1916     }
1917     catch (OCException& e)
1918     {
1919         LOGE("%s", e.reason().c_str());
1920         ThrowOcException(e.code(), e.reason().c_str());
1921     }
1922 #else
1923     ThrowOcException(JNI_NO_SUPPORT, "not support");
1924 #endif
1925 }
1926
1927 /*
1928 * Class:     org_iotivity_base_OcResource
1929 * Method:    unsubscribeMQTopicImpl
1930 * Signature: (I)V
1931 */
1932 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_unsubscribeMQTopicImpl
1933 (JNIEnv *env, jobject thiz, jint jQoS)
1934 {
1935     LOGD("OcResource_unsubscribeMQTopicImpl");
1936 #ifdef MQ_SUBSCRIBER
1937     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1938     if (!resource)
1939     {
1940         return;
1941     }
1942
1943     try
1944     {
1945         OCStackResult result = resource->unsubscribeMQTopic(
1946                 JniUtils::getQOS(env, static_cast<int>(jQoS)));
1947         if (OC_STACK_OK != result)
1948         {
1949             ThrowOcException(result, "OcResource_unsubscribeMQTopicImpl");
1950         }
1951     }
1952     catch (OCException& e)
1953     {
1954         LOGE("%s", e.reason().c_str());
1955         ThrowOcException(e.code(), e.reason().c_str());
1956     }
1957 #else
1958     ThrowOcException(JNI_NO_SUPPORT, "not support");
1959 #endif
1960 }
1961
1962 /*
1963 * Class:     org_iotivity_base_OcResource
1964 * Method:    requestMQPublishImpl
1965 * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;I)V
1966 */
1967 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_requestMQPublishImpl
1968 (JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS)
1969 {
1970     LOGD("OcResource_requestMQPublishImpl");
1971 #ifdef MQ_SUBSCRIBER
1972     if (!jQueryParamsMap)
1973     {
1974         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1975         return;
1976     }
1977     if (!jListener)
1978     {
1979         ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
1980         return;
1981     }
1982
1983     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1984     if (!resource)
1985     {
1986         return;
1987     }
1988
1989     QueryParamsMap qpm;
1990     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1991
1992     try
1993     {
1994         OCStackResult result = resource->requestMQPublish(
1995             env,
1996             qpm,
1997             jListener,
1998             JniUtils::getQOS(env, static_cast<int>(jQoS)));
1999
2000         if (OC_STACK_OK != result)
2001         {
2002             ThrowOcException(result, "OcResource_requestMQPublishImpl");
2003         }
2004     }
2005     catch (OCException& e)
2006     {
2007         LOGE("%s", e.reason().c_str());
2008         ThrowOcException(e.code(), e.reason().c_str());
2009     }
2010 #else
2011     ThrowOcException(JNI_NO_SUPPORT, "not support");
2012 #endif
2013 }
2014
2015 /*
2016 * Class:     org_iotivity_base_OcResource
2017 * Method:    publishMQTopicImpl
2018 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;
2019 *             Lorg/iotivity/base/OcResource/OnPostListener;I)V
2020 */
2021 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_publishMQTopicImpl
2022 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap,
2023  jobject jListener, jint jQoS)
2024 {
2025     LOGD("OcResource_publishMQTopicImpl");
2026 #ifdef MQ_PUBLISHER
2027     if (!jRepresentation)
2028     {
2029         ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
2030         return;
2031     }
2032
2033     if (!jQueryParamsMap)
2034     {
2035         ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
2036         return;
2037     }
2038
2039     if (!jListener)
2040     {
2041         ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
2042         return;
2043     }
2044
2045     JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
2046     if (!resource)
2047     {
2048         return;
2049     }
2050
2051     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env,
2052                                                                                    jRepresentation);
2053     if (!representation)
2054     {
2055         return;
2056     }
2057
2058     QueryParamsMap qpm;
2059     JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
2060
2061     try
2062     {
2063         OCStackResult result = resource->publishMQTopic(
2064             env,
2065             *representation,
2066             qpm,
2067             jListener,
2068             JniUtils::getQOS(env, static_cast<int>(jQoS)));
2069
2070         if (OC_STACK_OK != result)
2071         {
2072             ThrowOcException(result, "OcResource_publishMQTopicImpl");
2073         }
2074     }
2075     catch (OCException& e)
2076     {
2077         LOGE("%s", e.reason().c_str());
2078         ThrowOcException(e.code(), e.reason().c_str());
2079     }
2080 #else
2081     ThrowOcException(JNI_NO_SUPPORT, "not support");
2082 #endif
2083 }