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