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