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