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