2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
27 JniOcResource::JniOcResource(std::shared_ptr<OCResource> resource)
28 : m_sharedResource(resource)
31 JniOcResource::~JniOcResource()
33 LOGD("~JniOcResource()");
34 m_sharedResource = NULL;
37 JNIEnv *env = GetJNIEnv(envRet);
38 if (NULL == env) return;
40 m_onGetManager.removeAllListeners(env);
41 m_onPutManager.removeAllListeners(env);
42 m_onPostManager.removeAllListeners(env);
43 m_onDeleteManager.removeAllListeners(env);
44 m_onObserveManager.removeAllListeners(env);
46 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
49 OCStackResult JniOcResource::get(JNIEnv* env, const QueryParamsMap &queryParametersMap, jobject jListener)
51 JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
53 GetCallback getCallback = [onGetListener](
54 const HeaderOptions& opts,
55 const OCRepresentation& rep,
58 onGetListener->onGetCallback(opts, rep, eCode);
61 return m_sharedResource->get(queryParametersMap, getCallback);
64 OCStackResult JniOcResource::get(JNIEnv* env, const QueryParamsMap &queryParametersMap, jobject jListener,
67 JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
69 GetCallback getCallback = [onGetListener](const HeaderOptions& opts, const OCRepresentation& rep,
72 onGetListener->onGetCallback(opts, rep, eCode);
75 return m_sharedResource->get(queryParametersMap, getCallback, QoS);
78 OCStackResult JniOcResource::get(
80 const std::string &resourceType,
81 const std::string &resourceInterface,
82 const QueryParamsMap &queryParametersMap,
85 JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
87 GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
88 const OCRepresentation& rep, const int eCode)
90 onGetListener->onGetCallback(opts, rep, eCode);
93 return m_sharedResource->get(resourceType, resourceInterface, queryParametersMap,
97 OCStackResult JniOcResource::get(JNIEnv* env, const std::string &resourceType,
98 const std::string &resourceInterface, const QueryParamsMap &queryParametersMap,
99 jobject jListener, QualityOfService QoS)
101 JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
103 GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
104 const OCRepresentation& rep, const int eCode)
106 onGetListener->onGetCallback(opts, rep, eCode);
109 return m_sharedResource->get(resourceType, resourceInterface, queryParametersMap,
113 OCStackResult JniOcResource::put(JNIEnv* env, const OCRepresentation &representation,
114 const QueryParamsMap &queryParametersMap, jobject jListener)
116 JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
118 PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
119 const OCRepresentation& rep, const int eCode)
121 onPutListener->onPutCallback(opts, rep, eCode);
124 return m_sharedResource->put(representation, queryParametersMap, putCallback);
127 OCStackResult JniOcResource::put(JNIEnv* env, const OCRepresentation &representation,
128 const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
130 JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
132 PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
133 const OCRepresentation& rep, const int eCode)
135 onPutListener->onPutCallback(opts, rep, eCode);
138 return m_sharedResource->put(representation, queryParametersMap, putCallback, QoS);
141 OCStackResult JniOcResource::put(JNIEnv* env, const std::string &resourceType,
142 const std::string &resourceInterface, const OCRepresentation &representation,
143 const QueryParamsMap &queryParametersMap, jobject jListener)
145 JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
147 PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
148 const OCRepresentation& rep, const int eCode)
150 onPutListener->onPutCallback(opts, rep, eCode);
153 return m_sharedResource->put(resourceType, resourceInterface, representation,
154 queryParametersMap, putCallback);
157 OCStackResult JniOcResource::put(JNIEnv* env, const std::string &resourceType,
158 const std::string &resourceInterface, const OCRepresentation &representation,
159 const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
161 JniOnPutListener *onPutListener = addOnPutListener(env, jListener);
163 PutCallback putCallback = [onPutListener](const HeaderOptions& opts,
164 const OCRepresentation& rep, const int eCode)
166 onPutListener->onPutCallback(opts, rep, eCode);
169 return m_sharedResource->put(resourceType, resourceInterface, representation,
170 queryParametersMap, putCallback, QoS);
173 OCStackResult JniOcResource::post(JNIEnv* env, const OCRepresentation &representation,
174 const QueryParamsMap &queryParametersMap, jobject jListener)
176 JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
178 PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
179 const OCRepresentation& rep, const int eCode)
181 onPostListener->onPostCallback(opts, rep, eCode);
184 return m_sharedResource->post(representation, queryParametersMap, postCallback);
187 OCStackResult JniOcResource::post(JNIEnv* env, const OCRepresentation &representation,
188 const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
190 JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
192 PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
193 const OCRepresentation& rep, const int eCode)
195 onPostListener->onPostCallback(opts, rep, eCode);
198 return m_sharedResource->post(representation, queryParametersMap, postCallback, QoS);
201 OCStackResult JniOcResource::post(JNIEnv* env, const std::string &resourceType,
202 const std::string &resourceInterface, const OCRepresentation &representation,
203 const QueryParamsMap &queryParametersMap, jobject jListener)
205 JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
207 PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
208 const OCRepresentation& rep, const int eCode)
210 onPostListener->onPostCallback(opts, rep, eCode);
213 return m_sharedResource->post(resourceType, resourceInterface, representation,
214 queryParametersMap, postCallback);
217 OCStackResult JniOcResource::post(JNIEnv* env, const std::string &resourceType,
218 const std::string &resourceInterface, const OCRepresentation &representation,
219 const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
221 JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
223 PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
224 const OCRepresentation& rep, const int eCode)
226 onPostListener->onPostCallback(opts, rep, eCode);
229 return m_sharedResource->post(resourceType, resourceInterface, representation,
230 queryParametersMap, postCallback, QoS);
233 OCStackResult JniOcResource::deleteResource(JNIEnv* env, jobject jListener)
235 JniOnDeleteListener *onDeleteListener = addOnDeleteListener(env, jListener);
237 DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts,
240 onDeleteListener->onDeleteCallback(opts, eCode);
243 return m_sharedResource->deleteResource(deleteCallback);
246 OCStackResult JniOcResource::deleteResource(JNIEnv* env, jobject jListener, QualityOfService QoS)
248 JniOnDeleteListener *onDeleteListener = addOnDeleteListener(env, jListener);
250 DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts, const int eCode)
252 onDeleteListener->onDeleteCallback(opts, eCode);
255 return m_sharedResource->deleteResource(deleteCallback, QoS);
258 OCStackResult JniOcResource::observe(JNIEnv* env, ObserveType observeType,
259 const QueryParamsMap &queryParametersMap, jobject jListener)
261 JniOnObserveListener *onObserveListener = addOnObserveListener(env, jListener);
263 ObserveCallback observeCallback = [onObserveListener](const HeaderOptions& opts,
264 const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
266 onObserveListener->onObserveCallback(opts, rep, eCode, sequenceNumber);
269 return m_sharedResource->observe(observeType, queryParametersMap, observeCallback);
272 OCStackResult JniOcResource::observe(JNIEnv* env, ObserveType observeType,
273 const QueryParamsMap &queryParametersMap, jobject jListener, QualityOfService QoS)
275 JniOnObserveListener *onObserveListener = addOnObserveListener(env, jListener);
277 ObserveCallback observeCallback = [onObserveListener](const HeaderOptions& opts,
278 const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
280 onObserveListener->onObserveCallback(opts, rep, eCode, sequenceNumber);
283 return m_sharedResource->observe(observeType, queryParametersMap, observeCallback, QoS);
286 OCStackResult JniOcResource::cancelObserve(JNIEnv* env)
288 this->m_onObserveManager.removeAllListeners(env);
289 return m_sharedResource->cancelObserve();
292 OCStackResult JniOcResource::cancelObserve(JNIEnv* env, QualityOfService qos)
294 //TODO confirm behavior
295 //add removal of java listeners by qos
296 this->m_onObserveManager.removeAllListeners(env);
297 return m_sharedResource->cancelObserve(qos);
300 void JniOcResource::setHeaderOptions(const HeaderOptions &headerOptions)
302 m_sharedResource->setHeaderOptions(headerOptions);
305 void JniOcResource::unsetHeaderOptions()
307 m_sharedResource->unsetHeaderOptions();
310 std::string JniOcResource::host()
312 return m_sharedResource->host();
315 std::string JniOcResource::uri()
317 return m_sharedResource->uri();
320 OCConnectivityType JniOcResource::connectivityType() const
322 return m_sharedResource->connectivityType();
325 bool JniOcResource::isObservable()
327 return m_sharedResource->isObservable();
330 std::vector< std::string > JniOcResource::getResourceTypes() const
332 return m_sharedResource->getResourceTypes();
335 std::vector< std::string > JniOcResource::getResourceInterfaces(void) const
337 return m_sharedResource->getResourceInterfaces();
340 OCResourceIdentifier JniOcResource::uniqueIdentifier() const
342 return m_sharedResource->uniqueIdentifier();
345 std::string JniOcResource::sid() const
347 return m_sharedResource->sid();
350 JniOnGetListener* JniOcResource::addOnGetListener(JNIEnv* env, jobject jListener)
352 return this->m_onGetManager.addListener(env, jListener, this);
355 JniOnPutListener* JniOcResource::addOnPutListener(JNIEnv* env, jobject jListener)
357 return this->m_onPutManager.addListener(env, jListener, this);
360 JniOnPostListener* JniOcResource::addOnPostListener(JNIEnv* env, jobject jListener)
362 return this->m_onPostManager.addListener(env, jListener, this);
365 JniOnDeleteListener* JniOcResource::addOnDeleteListener(JNIEnv* env, jobject jListener)
367 return this->m_onDeleteManager.addListener(env, jListener, this);
370 JniOnObserveListener* JniOcResource::addOnObserveListener(JNIEnv* env, jobject jListener)
372 return this->m_onObserveManager.addListener(env, jListener, this);
375 void JniOcResource::removeOnGetListener(JNIEnv* env, jobject jListener)
377 this->m_onGetManager.removeListener(env, jListener);
380 void JniOcResource::removeOnPutListener(JNIEnv* env, jobject jListener)
382 this->m_onPutManager.removeListener(env, jListener);
385 void JniOcResource::removeOnPostListener(JNIEnv* env, jobject jListener)
387 this->m_onPostManager.removeListener(env, jListener);
390 void JniOcResource::removeOnDeleteListener(JNIEnv* env, jobject jListener)
392 this->m_onDeleteManager.removeListener(env, jListener);
395 void JniOcResource::removeOnObserveListener(JNIEnv* env, jobject jListener)
397 this->m_onObserveManager.removeListener(env, jListener);
400 std::shared_ptr<OCResource> JniOcResource::getOCResource()
402 return this->m_sharedResource;
405 JniOcResource* JniOcResource::getJniOcResourcePtr(JNIEnv *env, jobject thiz)
407 JniOcResource *resource = GetHandle<JniOcResource>(env, thiz);
408 if (env->ExceptionCheck())
410 LOGE("Failed to get native handle from OcResource");
414 ThrowOcException(JNI_NO_NATIVE_POINTER, "");
420 * Class: org_iotivity_base_OcResource
422 * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnGetListener;)V
424 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get
425 (JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener)
427 LOGD("OcResource_get");
428 if (!jQueryParamsMap)
430 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
435 ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
438 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
439 if (!resource) return;
442 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
446 OCStackResult result = resource->get(
451 if (OC_STACK_OK != result)
453 ThrowOcException(result, "OcResource_get");
456 catch (OCException& e)
458 LOGE("%s", e.reason().c_str());
459 ThrowOcException(e.code(), e.reason().c_str());
464 * Class: org_iotivity_base_OcResource
466 * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcResource/OnGetListener;I)V
468 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get1
469 (JNIEnv *env, jobject thiz, jobject jQueryParamsMap, jobject jListener, jint jQoS)
471 LOGD("OcResource_get");
472 if (!jQueryParamsMap)
474 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
479 ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
482 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
483 if (!resource) return;
486 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
490 OCStackResult result = resource->get(
494 JniUtils::getQOS(env, static_cast<int>(jQoS)));
496 if (OC_STACK_OK != result)
498 ThrowOcException(result, "OcResource_get");
501 catch (OCException& e)
503 LOGE("%s", e.reason().c_str());
504 ThrowOcException(e.code(), e.reason().c_str());
509 * Class: org_iotivity_base_OcResource
511 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;
512 Lorg/iotivity/base/OcResource/OnGetListener;)V
514 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get2
515 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
516 jobject jQueryParamsMap, jobject jListener)
518 LOGD("OcResource_get");
519 std::string resourceType;
522 resourceType = env->GetStringUTFChars(jResourceType, NULL);
524 std::string resourceInterface;
525 if (jResourceInterface)
527 resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL);
529 if (!jQueryParamsMap)
531 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
536 ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
539 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
540 if (!resource) return;
543 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
546 OCStackResult result = resource->get(
553 if (OC_STACK_OK != result)
555 ThrowOcException(result, "OcResource_get");
558 catch (OCException& e)
560 LOGE("%s", e.reason().c_str());
561 ThrowOcException(e.code(), e.reason().c_str());
566 * Class: org_iotivity_base_OcResource
568 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;
569 Lorg/iotivity/base/OcResource/OnGetListener;I)V
571 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_get3
572 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
573 jobject jQueryParamsMap, jobject jListener, jint jQoS)
575 LOGD("OcResource_get");
576 if (!jQueryParamsMap)
578 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
583 ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
586 std::string resourceType;
589 resourceType = env->GetStringUTFChars(jResourceType, NULL);
591 std::string resourceInterface;
592 if (jResourceInterface)
594 resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL);
596 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
597 if (!resource) return;
600 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
604 OCStackResult result = resource->get(
610 JniUtils::getQOS(env, static_cast<int>(jQoS)));
612 if (OC_STACK_OK != result)
614 ThrowOcException(result, "OcResource_get");
617 catch (OCException& e)
619 LOGE("%s", e.reason().c_str());
620 ThrowOcException(e.code(), e.reason().c_str());
625 * Class: org_iotivity_base_OcResource
627 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;
628 Lorg/iotivity/base/OcResource/OnPutListener;)V
630 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put
631 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
633 LOGD("OcResource_put");
634 if (!jRepresentation)
636 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
639 if (!jQueryParamsMap)
641 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
646 ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
649 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
650 if (!resource) return;
652 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
653 if (!representation) return;
656 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
660 OCStackResult result = resource->put(
666 if (OC_STACK_OK != result)
668 ThrowOcException(result, "OcResource_put");
671 catch (OCException& e)
673 LOGE("%s", e.reason().c_str());
674 ThrowOcException(e.code(), e.reason().c_str());
679 * Class: org_iotivity_base_OcResource
681 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;
682 Lorg/iotivity/base/OcResource/OnPutListener;I)V
684 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put1
685 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap,
686 jobject jListener, jint jQoS)
688 LOGD("OcResource_put");
689 if (!jRepresentation)
691 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
694 if (!jQueryParamsMap)
696 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
701 ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
704 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
705 if (!resource) return;
707 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
708 if (!representation) return;
711 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
715 OCStackResult result = resource->put(
720 JniUtils::getQOS(env, static_cast<int>(jQoS)));
722 if (OC_STACK_OK != result)
724 ThrowOcException(result, "OcResource_put");
727 catch (OCException& e)
729 LOGE("%s", e.reason().c_str());
730 ThrowOcException(e.code(), e.reason().c_str());
735 * Class: org_iotivity_base_OcResource
737 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
738 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPutListener;)V
740 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put2
741 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
742 jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
744 LOGD("OcResource_put");
745 std::string resourceType;
748 resourceType = env->GetStringUTFChars(jResourceType, NULL);
750 std::string resourceInterface;
751 if (jResourceInterface)
753 resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL);
755 if (!jRepresentation)
757 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
760 if (!jQueryParamsMap)
762 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
767 ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
770 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
771 if (!resource) return;
773 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
774 if (!representation) return;
777 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
781 OCStackResult result = resource->put(
789 if (OC_STACK_OK != result)
791 ThrowOcException(result, "OcResource_put");
794 catch (OCException& e)
796 LOGE("%s", e.reason().c_str());
797 ThrowOcException(e.code(), e.reason().c_str());
802 * Class: org_iotivity_base_OcResource
804 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
805 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPutListener;I)V
807 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_put3
808 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface, jobject jRepresentation,
809 jobject jQueryParamsMap, jobject jListener, jint jQoS)
811 LOGD("OcResource_put");
812 if (!jRepresentation)
814 ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
817 if (!jQueryParamsMap)
819 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
824 ThrowOcException(OC_STACK_INVALID_PARAM, "onPutListener cannot be null");
827 std::string resourceType;
830 resourceType = env->GetStringUTFChars(jResourceType, NULL);
832 std::string resourceInterface;
833 if (jResourceInterface)
835 resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL);
838 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
839 if (!resource) return;
841 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
842 if (!representation) return;
845 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
849 OCStackResult result = resource->put(
856 JniUtils::getQOS(env, static_cast<int>(jQoS)));
858 if (OC_STACK_OK != result)
860 ThrowOcException(result, "OcResource_put");
863 catch (OCException& e)
865 LOGE("%s", e.reason().c_str());
866 ThrowOcException(e.code(), e.reason().c_str());
871 * Class: org_iotivity_base_OcResource
873 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;)V
875 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post
876 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
878 LOGD("OcResource_post");
879 if (!jRepresentation)
881 ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
884 if (!jQueryParamsMap)
886 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
891 ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
894 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
895 if (!resource) return;
897 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
898 if (!representation) return;
901 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
905 OCStackResult result = resource->post(
911 if (OC_STACK_OK != result)
913 ThrowOcException(result, "OcResource_post");
916 catch (OCException& e)
918 LOGE("%s", e.reason().c_str());
919 ThrowOcException(e.code(), e.reason().c_str());
924 * Class: org_iotivity_base_OcResource
926 * Signature: (Lorg/iotivity/base/OcRepresentation;Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;I)V
928 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post1
929 (JNIEnv *env, jobject thiz, jobject jRepresentation, jobject jQueryParamsMap, jobject jListener, jint jQoS)
931 LOGD("OcResource_post");
932 if (!jRepresentation)
934 ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
937 if (!jQueryParamsMap)
939 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
944 ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
947 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
948 if (!resource) return;
950 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
951 if (!representation) return;
954 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
958 OCStackResult result = resource->post(
963 JniUtils::getQOS(env, static_cast<int>(jQoS)));
965 if (OC_STACK_OK != result)
967 ThrowOcException(result, "OcResource_post");
970 catch (OCException& e)
972 LOGE("%s", e.reason().c_str());
973 ThrowOcException(e.code(), e.reason().c_str());
978 * Class: org_iotivity_base_OcResource
980 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
981 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;)V
983 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post2
984 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
985 jobject jRepresentation, jobject jQueryParamsMap, jobject jListener)
987 LOGD("OcResource_post");
988 if (!jRepresentation)
990 ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
993 if (!jQueryParamsMap)
995 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1000 ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
1003 std::string resourceType;
1006 resourceType = env->GetStringUTFChars(jResourceType, NULL);
1008 std::string resourceInterface;
1009 if (jResourceInterface)
1011 resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL);
1014 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1015 if (!resource) return;
1017 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
1018 if (!representation) return;
1021 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1025 OCStackResult result = resource->post(
1033 if (OC_STACK_OK != result)
1035 ThrowOcException(result, "OcResource_post");
1038 catch (OCException& e)
1040 LOGE("%s", e.reason().c_str());
1041 ThrowOcException(e.code(), e.reason().c_str());
1046 * Class: org_iotivity_base_OcResource
1048 * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;
1049 Ljava/util/Map;Lorg/iotivity/base/OcResource/OnPostListener;I)V
1051 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_post3
1052 (JNIEnv *env, jobject thiz, jstring jResourceType, jstring jResourceInterface,
1053 jobject jRepresentation, jobject jQueryParamsMap, jobject jListener, jint jQoS)
1055 LOGD("OcResource_post");
1056 if (!jRepresentation)
1058 ThrowOcException(OC_STACK_INVALID_PARAM, "representation cannot be null");
1061 if (!jQueryParamsMap)
1063 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1068 ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
1071 std::string resourceType;
1074 resourceType = env->GetStringUTFChars(jResourceType, NULL);
1076 std::string resourceInterface;
1077 if (jResourceInterface)
1079 resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL);
1082 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1083 if (!resource) return;
1085 OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
1086 if (!representation) return;
1089 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1093 OCStackResult result = resource->post(
1100 JniUtils::getQOS(env, static_cast<int>(jQoS)));
1102 if (OC_STACK_OK != result)
1104 ThrowOcException(result, "OcResource_post");
1107 catch (OCException& e)
1109 LOGE("%s", e.reason().c_str());
1110 ThrowOcException(e.code(), e.reason().c_str());
1115 * Class: org_iotivity_base_OcResource
1116 * Method: deleteResource
1117 * Signature: (Lorg/iotivity/base/OcResource/OnDeleteListener;)V
1119 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_deleteResource
1120 (JNIEnv *env, jobject thiz, jobject jListener)
1122 LOGD("OcResource_deleteResource");
1125 ThrowOcException(OC_STACK_INVALID_PARAM, "onDeleteListener cannot be null");
1128 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1129 if (!resource) return;
1133 OCStackResult result = resource->deleteResource(
1137 if (OC_STACK_OK != result)
1139 ThrowOcException(result, "OcResource_deleteResource");
1142 catch (OCException& e)
1144 LOGE("%s", e.reason().c_str());
1145 ThrowOcException(e.code(), e.reason().c_str());
1150 * Class: org_iotivity_base_OcResource
1151 * Method: deleteResource1
1152 * Signature: (Lorg/iotivity/base/OcResource/OnDeleteListener;I)V
1154 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_deleteResource1
1155 (JNIEnv *env, jobject thiz, jobject jListener, jint jQoS)
1157 LOGD("OcResource_deleteResource");
1160 ThrowOcException(OC_STACK_INVALID_PARAM, "onDeleteListener cannot be null");
1163 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1164 if (!resource) return;
1168 OCStackResult result = resource->deleteResource(
1171 JniUtils::getQOS(env, static_cast<int>(jQoS)));
1173 if (OC_STACK_OK != result)
1175 ThrowOcException(result, "OcResource_deleteResource");
1178 catch (OCException& e)
1180 LOGE("%s", e.reason().c_str());
1181 ThrowOcException(e.code(), e.reason().c_str());
1186 * Class: org_iotivity_base_OcResource
1188 * Signature: (Lorg/iotivity/base/ObserveType;Ljava/util/Map;
1189 Lorg/iotivity/base/OcResource/OnObserveListener;)V
1191 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_observe
1192 (JNIEnv *env, jobject thiz, jint observeType, jobject jQueryParamsMap, jobject jListener)
1194 LOGD("OcResource_observe");
1195 if (!jQueryParamsMap)
1197 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1202 ThrowOcException(OC_STACK_INVALID_PARAM, "onObserveListener cannot be null");
1205 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1206 if (!resource) return;
1209 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1213 OCStackResult result = resource->observe(
1215 JniUtils::getObserveType(env, static_cast<int>(observeType)),
1219 if (OC_STACK_OK != result)
1221 ThrowOcException(result, "OcResource_observe");
1224 catch (OCException& e)
1226 LOGE("%s", e.reason().c_str());
1227 ThrowOcException(e.code(), e.reason().c_str());
1232 * Class: org_iotivity_base_OcResource
1234 * Signature: (Lorg/iotivity/base/ObserveType;Ljava/util/Map;
1235 Lorg/iotivity/base/OcResource/OnObserveListener;I)V
1237 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_observe1
1238 (JNIEnv *env, jobject thiz, jint observeType, jobject jQueryParamsMap,
1239 jobject jListener, jint jQoS)
1241 LOGD("OcResource_observe");
1242 if (!jQueryParamsMap)
1244 ThrowOcException(OC_STACK_INVALID_PARAM, "queryParamsMap cannot be null");
1249 ThrowOcException(OC_STACK_INVALID_PARAM, "onObserveListener cannot be null");
1252 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1253 if (!resource) return;
1256 JniUtils::convertJavaMapToQueryParamsMap(env, jQueryParamsMap, qpm);
1260 OCStackResult result = resource->observe(
1262 JniUtils::getObserveType(env, static_cast<int>(observeType)),
1265 JniUtils::getQOS(env, static_cast<int>(jQoS)));
1267 if (OC_STACK_OK != result)
1269 ThrowOcException(result, "OcResource_observe");
1272 catch (OCException& e)
1274 LOGE("%s", e.reason().c_str());
1275 ThrowOcException(e.code(), e.reason().c_str());
1280 * Class: org_iotivity_base_OcResource
1281 * Method: cancelObserve
1284 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_cancelObserve
1285 (JNIEnv *env, jobject thiz)
1287 LOGD("OcResource_cancelObserve");
1288 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1289 if (!resource) return;
1293 OCStackResult result = resource->cancelObserve(env);
1295 if (OC_STACK_OK != result)
1297 ThrowOcException(result, "OcResource_cancelObserve");
1300 catch (OCException& e)
1302 LOGE("%s", e.reason().c_str());
1303 ThrowOcException(e.code(), e.reason().c_str());
1308 * Class: org_iotivity_base_OcResource
1309 * Method: cancelObserve1
1312 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_cancelObserve1
1313 (JNIEnv *env, jobject thiz, jint jQoS)
1315 LOGD("OcResource_cancelObserve1");
1316 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1317 if (!resource) return;
1321 OCStackResult result = resource->cancelObserve(
1323 JniUtils::getQOS(env, static_cast<int>(jQoS)));
1325 if (OC_STACK_OK != result)
1327 ThrowOcException(result, "OcResource_cancelObserve");
1330 catch (OCException& e)
1332 LOGE("%s", e.reason().c_str());
1333 ThrowOcException(e.code(), e.reason().c_str());
1338 * Class: org_iotivity_base_OcResource
1339 * Method: setHeaderOptions
1340 * Signature: ([Lorg/iotivity/OcHeaderOption;)V
1342 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_setHeaderOptions
1343 (JNIEnv *env, jobject thiz, jobjectArray jheaderOptionArr)
1345 LOGD("OcResource_setHeaderOptions");
1346 if (!jheaderOptionArr)
1348 ThrowOcException(OC_STACK_INVALID_PARAM, "headerOptionList cannot be null");
1351 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1352 if (!resource) return;
1354 HeaderOptions headerOptions;
1355 JniUtils::convertJavaHeaderOptionsArrToVector(env, jheaderOptionArr, headerOptions);
1357 resource->setHeaderOptions(headerOptions);
1361 * Class: org_iotivity_base_OcResource
1362 * Method: unsetHeaderOptions
1365 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_unsetHeaderOptions
1366 (JNIEnv *env, jobject thiz)
1368 LOGD("OcResource_unsetHeaderOptions");
1369 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1370 if (!resource) return;
1372 resource->unsetHeaderOptions();
1376 * Class: org_iotivity_base_OcResource
1378 * Signature: ()Ljava/lang/String;
1380 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcResource_getHost
1381 (JNIEnv *env, jobject thiz)
1383 LOGD("OcResource_getHost");
1384 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1385 if (!resource) return nullptr;
1387 return env->NewStringUTF(resource->host().c_str());
1391 * Class: org_iotivity_base_OcResource
1393 * Signature: ()Ljava/lang/String;
1395 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcResource_getUri
1396 (JNIEnv *env, jobject thiz)
1398 LOGD("OcResource_getUri");
1399 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1400 if (!resource) return nullptr;
1402 return env->NewStringUTF(resource->uri().c_str());
1406 * Class: org_iotivity_base_OcResource
1407 * Method: getConnectivityTypeN
1410 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcResource_getConnectivityTypeN
1411 (JNIEnv *env, jobject thiz)
1413 LOGD("OcResource_getConnectivityType");
1414 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1415 if (!resource) return -1;
1417 OCConnectivityType connectivityType = resource->connectivityType();
1418 return static_cast<jint>(connectivityType);
1422 * Class: org_iotivity_base_OcResource
1423 * Method: isObservable
1426 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcResource_isObservable
1427 (JNIEnv *env, jobject thiz)
1429 LOGD("OcResource_isObservable");
1430 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1431 return (jboolean)resource->isObservable();
1435 * Class: org_iotivity_base_OcResource
1436 * Method: getResourceTypes
1437 * Signature: ()Ljava/util/List;
1439 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcResource_getResourceTypes
1440 (JNIEnv *env, jobject thiz)
1442 LOGD("OcResource_getResourceTypes");
1443 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1444 if (!resource) return nullptr;
1446 std::vector<std::string> resourceTypes = resource->getResourceTypes();
1448 return JniUtils::convertStrVectorToJavaStrList(env, resourceTypes);
1452 * Class: org_iotivity_base_OcResource
1453 * Method: getResourceInterfaces
1454 * Signature: ()Ljava/util/List;
1456 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcResource_getResourceInterfaces
1457 (JNIEnv *env, jobject thiz)
1459 LOGD("OcResource_getResourceInterfaces");
1460 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1461 if (!resource) return nullptr;
1463 std::vector<std::string> resourceInterfaces = resource->getResourceInterfaces();
1465 return JniUtils::convertStrVectorToJavaStrList(env, resourceInterfaces);
1469 * Class: org_iotivity_base_OcResource
1470 * Method: getUniqueIdentifier
1471 * Signature: ()Lorg/iotivity/base/OcResourceIdentifier;
1473 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcResource_getUniqueIdentifier
1474 (JNIEnv *env, jobject thiz)
1476 LOGD("OcResource_getUniqueIdentifier");
1477 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1478 if (!resource) return nullptr;
1480 JniOcResourceIdentifier *jniResourceIdentifier =
1481 new JniOcResourceIdentifier(resource->uniqueIdentifier());
1482 if (!jniResourceIdentifier) return nullptr;
1484 jlong handle = reinterpret_cast<jlong>(jniResourceIdentifier);
1485 jobject jResourceIdentifier = env->NewObject(g_cls_OcResourceIdentifier,
1486 g_mid_OcResourceIdentifier_N_ctor, handle);
1487 if (!jResourceIdentifier)
1489 delete jniResourceIdentifier;
1491 return jResourceIdentifier;
1495 * Class: org_iotivity_base_OcResource
1496 * Method: getServerId
1497 * Signature: ()Ljava/lang/String;
1499 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcResource_getServerId
1500 (JNIEnv *env, jobject thiz)
1502 LOGD("OcResource_getServerId");
1503 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);
1504 if (!resource) return nullptr;
1506 return env->NewStringUTF(resource->sid().c_str());
1510 * Class: org_iotivity_base_OcResource
1514 JNIEXPORT void JNICALL Java_org_iotivity_base_OcResource_dispose
1515 (JNIEnv *env, jobject thiz)
1517 LOGD("OcResource_dispose");
1518 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, thiz);