Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcRepresentation.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 <map>
24
25 #include "JniOcRepresentation.h"
26 #include "JniUtils.h"
27
28 using namespace OC;
29
30 OCRepresentation* JniOcRepresentation::getOCRepresentationPtr(JNIEnv *env, jobject thiz)
31 {
32     OCRepresentation *rep = GetHandle<OCRepresentation>(env, thiz);
33     if (env->ExceptionCheck())
34     {
35         LOGE("Failed to get native handle from OcRepresentation");
36     }
37     if (!rep)
38     {
39         ThrowOcException(JNI_NO_NATIVE_POINTER, "");
40     }
41     return rep;
42 }
43
44 /*
45 * Class:     org_iotivity_base_OcRepresentation
46 * Method:    getValues
47 * Signature: ()Ljava/util/Map;
48 */
49 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getValues
50 (JNIEnv *env, jobject thiz)
51 {
52     LOGD("OcRepresentation_getValues");
53     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
54     if (!rep)
55     {
56         return nullptr;
57     }
58
59     std::map<std::string, AttributeValue> values = rep->getValues();
60     jobject jHashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
61     if (!jHashMap)
62     {
63         return nullptr;
64     }
65
66     for (std::map<std::string, AttributeValue>::const_iterator it = values.begin(); it != values.end(); it++)
67     {
68         jobject key = static_cast<jobject>(env->NewStringUTF(it->first.c_str()));
69         jobject val = boost::apply_visitor(JObjectConverter(env), it->second);
70         env->CallObjectMethod(jHashMap, g_mid_HashMap_put, key, val);
71     }
72     return jHashMap;
73 }
74
75 /*
76 * Class:     org_iotivity_base_OcRepresentation
77 * Method:    getValueN
78 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
79 */
80 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getValueN
81 (JNIEnv *env, jobject thiz, jstring jKey)
82 {
83     LOGD("OcRepresentation_getValue");
84     if (!jKey)
85     {
86         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
87         return nullptr;
88     }
89     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
90     if (!rep)
91     {
92         return nullptr;
93     }
94
95     std::string key = env->GetStringUTFChars(jKey, nullptr);
96
97     AttributeValue attrValue;
98     if (!rep->getAttributeValue(key.c_str(), attrValue))
99     {
100         ThrowOcException(JNI_NO_SUCH_KEY, " attribute key does not exist");
101         return nullptr;
102     }
103     return boost::apply_visitor(JObjectConverter(env), attrValue);
104 }
105
106 /*
107 * Class:     org_iotivity_base_OcRepresentation
108 * Method:    setValueInteger
109 * Signature: (Ljava/lang/String;I)V
110 */
111 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger
112 (JNIEnv *env, jobject thiz, jstring jKey, jint jValue)
113 {
114     LOGD("OcRepresentation_setValueInteger");
115     if (!jKey)
116     {
117         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
118         return;
119     }
120     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
121     if (!rep)
122     {
123         return;
124     }
125
126     std::string str = env->GetStringUTFChars(jKey, nullptr);
127     rep->setValue(str, static_cast<int>(jValue));
128 }
129
130 /*
131 * Class:     org_iotivity_base_OcRepresentation
132 * Method:    setValueDouble
133 * Signature: (Ljava/lang/String;D)V
134 */
135 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble
136 (JNIEnv *env, jobject thiz, jstring jKey, jdouble jValue)
137 {
138     LOGD("OcRepresentation_setValueDouble");
139     if (!jKey)
140     {
141         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
142         return;
143     }
144     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
145     if (!rep)
146     {
147         return;
148     }
149
150     std::string str = env->GetStringUTFChars(jKey, nullptr);
151     rep->setValue(str, static_cast<double>(jValue));
152 }
153
154 /*
155 * Class:     org_iotivity_base_OcRepresentation
156 * Method:    setValueBoolean
157 * Signature: (Ljava/lang/String;Z)V
158 */
159 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean
160 (JNIEnv *env, jobject thiz, jstring jKey, jboolean jValue)
161 {
162     LOGD("OcRepresentation_setValueBoolean");
163     if (!jKey)
164     {
165         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
166         return;
167     }
168     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
169     if (!rep)
170     {
171         return;
172     }
173
174     std::string str = env->GetStringUTFChars(jKey, nullptr);
175     rep->setValue(str, static_cast<bool>(jValue));
176 }
177
178 /*
179 * Class:     org_iotivity_base_OcRepresentation
180 * Method:    setValueStringN
181 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
182 */
183 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueStringN
184 (JNIEnv *env, jobject thiz, jstring jKey, jstring jValue)
185 {
186     LOGD("OcRepresentation_setValueString");
187     if (!jKey)
188     {
189         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
190         return;
191     }
192     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
193     if (!rep)
194     {
195         return;
196     }
197
198     std::string key = env->GetStringUTFChars(jKey, nullptr);
199     std::string value = env->GetStringUTFChars(jValue, nullptr);
200
201     rep->setValue(key, value);
202 }
203
204 /*
205 * Class:     org_iotivity_base_OcRepresentation
206 * Method:    setValueRepresentation
207 * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;)V
208 */
209 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation
210 (JNIEnv *env, jobject thiz, jstring jKey, jobject jValue)
211 {
212     LOGD("OcRepresentation_setValueRepresentation");
213     if (!jKey)
214     {
215         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
216         return;
217     }
218     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
219     if (!rep)
220     {
221         return;
222     }
223
224     std::string key = env->GetStringUTFChars(jKey, nullptr);
225
226     if (jValue)
227     {
228         OCRepresentation *value = JniOcRepresentation::getOCRepresentationPtr(env, jValue);
229         if (!value)
230         {
231             return;
232         }
233         rep->setValue(key, *value);
234     }
235     else
236     {
237         rep->setNULL(key);
238     }
239 }
240
241 /*
242 * Class:     org_iotivity_base_OcRepresentation
243 * Method:    setValueIntegerArray
244 * Signature: (Ljava/lang/String;[I)V
245 */
246 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueIntegerArray
247 (JNIEnv *env, jobject thiz, jstring jKey, jintArray jValue)
248 {
249     LOGD("OcRepresentation_setValueIntegerArray");
250     if (!jKey)
251     {
252         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
253         return;
254     }
255
256     const jsize len = env->GetArrayLength(jValue);
257     jint* ints = env->GetIntArrayElements(jValue, nullptr);
258
259     std::vector<int> value;
260     for (jsize i = 0; i < len; ++i)
261     {
262         value.push_back(static_cast<int>(ints[i]));
263     }
264     env->ReleaseIntArrayElements(jValue, ints, JNI_ABORT);
265
266     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
267     if (!rep)
268     {
269         return;
270     }
271
272     std::string key = env->GetStringUTFChars(jKey, nullptr);
273     rep->setValue(key, value);
274 }
275
276 /*
277 * Class:     org_iotivity_base_OcRepresentation
278 * Method:    setValueInteger2DArray
279 * Signature: (Ljava/lang/String;[[I)V
280 */
281 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger2DArray
282 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
283 {
284     LOGD("OcRepresentation__setValueInteger2DArray");
285     if (!jKey)
286     {
287         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
288         return;
289     }
290     std::vector<std::vector<int>> value;
291     const jsize lenOuter = env->GetArrayLength(jValue);
292     for (jsize j = 0; j < lenOuter; ++j)
293     {
294         jintArray jInnerArray = static_cast<jintArray>(env->GetObjectArrayElement(jValue, j));
295         jint* ints = env->GetIntArrayElements(jInnerArray, nullptr);
296         std::vector<int> innerVector;
297         const jsize lenInner = env->GetArrayLength(jInnerArray);
298         for (jsize i = 0; i < lenInner; ++i)
299         {
300             innerVector.push_back(static_cast<int>(ints[i]));
301         }
302         env->ReleaseIntArrayElements(jInnerArray, ints, JNI_ABORT);
303         env->DeleteLocalRef(jInnerArray);
304         value.push_back(innerVector);
305     }
306
307     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
308     if (!rep)
309     {
310         return;
311     }
312
313     std::string key = env->GetStringUTFChars(jKey, nullptr);
314     rep->setValue(key, value);
315 }
316
317 /*
318 * Class:     org_iotivity_base_OcRepresentation
319 * Method:    setValueInteger3DArray
320 * Signature: (Ljava/lang/String;[[[I)V
321 */
322 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger3DArray
323 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
324 {
325     LOGD("OcRepresentation_setValueInteger3DArray");
326     if (!jKey)
327     {
328         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
329         return;
330     }
331     std::vector<std::vector<std::vector<int>>> value;
332     const jsize lenOuter = env->GetArrayLength(jValue);
333     for (jsize k = 0; k < lenOuter; ++k)
334     {
335         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
336         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
337         std::vector<std::vector<int>> middleArray;
338         for (jsize j = 0; j < lenMiddle; ++j)
339         {
340             jintArray jInnerArray = static_cast<jintArray>(env->GetObjectArrayElement(jMiddleArray, j));
341             jint* ints = env->GetIntArrayElements(jInnerArray, nullptr);
342             std::vector<int> innerVector;
343             const jsize lenInner = env->GetArrayLength(jInnerArray);
344             for (jsize i = 0; i < lenInner; ++i)
345             {
346                 innerVector.push_back(static_cast<int>(ints[i]));
347             }
348             env->ReleaseIntArrayElements(jInnerArray, ints, JNI_ABORT);
349             env->DeleteLocalRef(jInnerArray);
350             middleArray.push_back(innerVector);
351         }
352         env->DeleteLocalRef(jMiddleArray);
353         value.push_back(middleArray);
354     }
355
356     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
357     if (!rep)
358     {
359         return;
360     }
361
362     std::string key = env->GetStringUTFChars(jKey, nullptr);
363     rep->setValue(key, value);
364 }
365
366 /*
367 * Class:     org_iotivity_base_OcRepresentation
368 * Method:    setValueDoubleArray
369 * Signature: (Ljava/lang/String;[D)V
370 */
371 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDoubleArray
372 (JNIEnv *env, jobject thiz, jstring jKey, jdoubleArray jValue)
373 {
374     LOGD("OcRepresentation_setValueDoubleArray");
375     if (!jKey)
376     {
377         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
378         return;
379     }
380
381     const jsize len = env->GetArrayLength(jValue);
382     jdouble* doubles = env->GetDoubleArrayElements(jValue, nullptr);
383
384     std::vector<double> value;
385     for (jsize i = 0; i < len; ++i)
386     {
387         value.push_back(static_cast<double>(doubles[i]));
388     }
389     env->ReleaseDoubleArrayElements(jValue, doubles, JNI_ABORT);
390
391     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
392     if (!rep)
393     {
394         return;
395     }
396
397     std::string key = env->GetStringUTFChars(jKey, nullptr);
398     rep->setValue(key, value);
399 }
400
401 /*
402 * Class:     org_iotivity_base_OcRepresentation
403 * Method:    setValueDouble2DArray
404 * Signature: (Ljava/lang/String;[[D)V
405 */
406 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble2DArray
407 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
408 {
409     LOGD("OcRepresentation_setValueDouble2DArray");
410     if (!jKey)
411     {
412         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
413         return;
414     }
415     std::vector<std::vector<double>> value;
416     const jsize lenOuter = env->GetArrayLength(jValue);
417     for (jsize j = 0; j < lenOuter; ++j)
418     {
419         jdoubleArray jInnerArray = static_cast<jdoubleArray>(env->GetObjectArrayElement(jValue, j));
420         jdouble* doubles = env->GetDoubleArrayElements(jInnerArray, nullptr);
421         std::vector<double> innerVector;
422         const jsize lenInner = env->GetArrayLength(jInnerArray);
423         for (jsize i = 0; i < lenInner; ++i)
424         {
425             innerVector.push_back(static_cast<double>(doubles[i]));
426         }
427         env->ReleaseDoubleArrayElements(jInnerArray, doubles, JNI_ABORT);
428         env->DeleteLocalRef(jInnerArray);
429         value.push_back(innerVector);
430     }
431
432     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
433     if (!rep)
434     {
435         return;
436     }
437
438     std::string key = env->GetStringUTFChars(jKey, nullptr);
439     rep->setValue(key, value);
440 }
441
442 /*
443 * Class:     org_iotivity_base_OcRepresentation
444 * Method:    setValueDouble3DArray
445 * Signature: (Ljava/lang/String;[[[D)V
446 */
447 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble3DArray
448 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
449 {
450     LOGD("OcRepresentation_setValueDouble3DArray");
451     if (!jKey)
452     {
453         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
454         return;
455     }
456     std::vector<std::vector<std::vector<double>>> value;
457     const jsize lenOuter = env->GetArrayLength(jValue);
458     for (jsize k = 0; k < lenOuter; ++k)
459     {
460         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
461         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
462         std::vector<std::vector<double>> middleArray;
463         for (jsize j = 0; j < lenMiddle; ++j)
464         {
465             jdoubleArray jInnerArray = static_cast<jdoubleArray>(env->GetObjectArrayElement(jMiddleArray, j));
466             jdouble* doubles = env->GetDoubleArrayElements(jInnerArray, nullptr);
467             std::vector<double> innerVector;
468             const jsize lenInner = env->GetArrayLength(jInnerArray);
469             for (jsize i = 0; i < lenInner; ++i)
470             {
471                 innerVector.push_back(static_cast<double>(doubles[i]));
472             }
473             env->ReleaseDoubleArrayElements(jInnerArray, doubles, JNI_ABORT);
474             env->DeleteLocalRef(jInnerArray);
475             middleArray.push_back(innerVector);
476         }
477         env->DeleteLocalRef(jMiddleArray);
478         value.push_back(middleArray);
479     }
480
481     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
482     if (!rep)
483     {
484         return;
485     }
486
487     std::string key = env->GetStringUTFChars(jKey, nullptr);
488     rep->setValue(key, value);
489 }
490
491 /*
492 * Class:     org_iotivity_base_OcRepresentation
493 * Method:    setValueBooleanArray
494 * Signature: (Ljava/lang/String;[Z)V
495 */
496 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBooleanArray
497 (JNIEnv *env, jobject thiz, jstring jKey, jbooleanArray jValue)
498 {
499     LOGD("OcRepresentation_setValueBooleanArray");
500     if (!jKey)
501     {
502         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
503         return;
504     }
505
506     const jsize len = env->GetArrayLength(jValue);
507     jboolean* booleans = env->GetBooleanArrayElements(jValue, nullptr);
508
509     std::vector<bool> value;
510     for (jsize i = 0; i < len; ++i)
511     {
512         value.push_back(static_cast<bool>(booleans[i]));
513     }
514     env->ReleaseBooleanArrayElements(jValue, booleans, JNI_ABORT);
515
516     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
517     if (!rep)
518     {
519         return;
520     }
521
522     std::string key = env->GetStringUTFChars(jKey, nullptr);
523     rep->setValue(key, value);
524 }
525
526 /*
527 * Class:     org_iotivity_base_OcRepresentation
528 * Method:    setValueBoolean2DArray
529 * Signature: (Ljava/lang/String;[[Z)V
530 */
531 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean2DArray
532 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
533 {
534     LOGD("OcRepresentation_setValueBoolean2DArray");
535     if (!jKey)
536     {
537         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
538         return;
539     }
540     std::vector<std::vector<bool>> value;
541     const jsize lenOuter = env->GetArrayLength(jValue);
542     for (jsize j = 0; j < lenOuter; ++j)
543     {
544         jbooleanArray jInnerArray = static_cast<jbooleanArray>(env->GetObjectArrayElement(jValue, j));
545         const jsize lenInner = env->GetArrayLength(jInnerArray);
546         jboolean* booleans = env->GetBooleanArrayElements(jInnerArray, nullptr);
547
548         std::vector<bool> innerVector;
549         for (jsize i = 0; i < lenInner; ++i)
550         {
551             innerVector.push_back(static_cast<bool>(booleans[i]));
552         }
553         env->ReleaseBooleanArrayElements(jInnerArray, booleans, JNI_ABORT);
554         env->DeleteLocalRef(jInnerArray);
555         value.push_back(innerVector);
556     }
557
558     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
559     if (!rep)
560     {
561         return;
562     }
563
564     std::string key = env->GetStringUTFChars(jKey, nullptr);
565     rep->setValue(key, value);
566 }
567
568 /*
569 * Class:     org_iotivity_base_OcRepresentation
570 * Method:    setValueBoolean3DArray
571 * Signature: (Ljava/lang/String;[[[Z)V
572 */
573 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean3DArray
574 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
575 {
576     LOGD("OcRepresentation_setValueBoolean3DArray");
577     if (!jKey)
578     {
579         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
580         return;
581     }
582     std::vector<std::vector<std::vector<bool>>> value;
583     const jsize lenOuter = env->GetArrayLength(jValue);
584     for (jsize k = 0; k < lenOuter; ++k)
585     {
586         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
587         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
588         std::vector<std::vector<bool>> middleArray;
589         for (jsize j = 0; j < lenMiddle; ++j)
590         {
591             jbooleanArray jInnerArray = static_cast<jbooleanArray>(env->GetObjectArrayElement(jMiddleArray, j));
592             const jsize lenInner = env->GetArrayLength(jInnerArray);
593             jboolean* booleans = env->GetBooleanArrayElements(jInnerArray, nullptr);
594
595             std::vector<bool> innerVector;
596             for (jsize i = 0; i < lenInner; ++i)
597             {
598                 innerVector.push_back(static_cast<bool>(booleans[i]));
599             }
600             env->ReleaseBooleanArrayElements(jInnerArray, booleans, JNI_ABORT);
601             env->DeleteLocalRef(jInnerArray);
602             middleArray.push_back(innerVector);
603         }
604         env->DeleteLocalRef(jMiddleArray);
605         value.push_back(middleArray);
606     }
607
608     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
609     if (!rep)
610     {
611         return;
612     }
613
614     std::string key = env->GetStringUTFChars(jKey, nullptr);
615     rep->setValue(key, value);
616 }
617
618 /*
619 * Class:     org_iotivity_base_OcRepresentation
620 * Method:    setValueStringArray
621 * Signature: (Ljava/lang/String;[Ljava/lang/String;)V
622 */
623 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueStringArray
624 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
625 {
626     LOGD("OcRepresentation_setValueStringArray");
627     if (!jKey)
628     {
629         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
630         return;
631     }
632
633     std::vector<std::string> value;
634     JniUtils::convertJavaStrArrToStrVector(env, jValue, value);
635
636     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
637     if (!rep)
638     {
639         return;
640     }
641
642     std::string key = env->GetStringUTFChars(jKey, nullptr);
643     rep->setValue(key, value);
644 }
645
646 /*
647 * Class:     org_iotivity_base_OcRepresentation
648 * Method:    setValueString2DArray
649 * Signature: (Ljava/lang/String;[[Ljava/lang/String;)V
650 */
651 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueString2DArray
652 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
653 {
654     LOGD("OcRepresentation_setValueString2DArray");
655     if (!jKey)
656     {
657         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
658         return;
659     }
660     std::vector<std::vector<std::string>> value;
661     const jsize lenOuter = env->GetArrayLength(jValue);
662     for (jsize j = 0; j < lenOuter; ++j)
663     {
664         jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, j));
665         std::vector<std::string> innerVector;
666         JniUtils::convertJavaStrArrToStrVector(env, jInnerArray, innerVector);
667         env->DeleteLocalRef(jInnerArray);
668         value.push_back(innerVector);
669     }
670
671     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
672     if (!rep)
673     {
674         return;
675     }
676
677     std::string key = env->GetStringUTFChars(jKey, nullptr);
678     rep->setValue(key, value);
679 }
680
681 /*
682 * Class:     org_iotivity_base_OcRepresentation
683 * Method:    setValueString3DArray
684 * Signature: (Ljava/lang/String;[[[Ljava/lang/String;)V
685 */
686 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueString3DArray
687 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
688 {
689     LOGD("OcRepresentation_setValueString3DArray");
690     if (!jKey)
691     {
692         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
693         return;
694     }
695     std::vector<std::vector<std::vector<std::string>>> value;
696     const jsize lenOuter = env->GetArrayLength(jValue);
697     for (jsize k = 0; k < lenOuter; ++k)
698     {
699         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
700         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
701         std::vector<std::vector<std::string>> middleArray;
702         for (jsize j = 0; j < lenMiddle; ++j)
703         {
704             jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jMiddleArray, j));
705             std::vector<std::string> innerVector;
706             JniUtils::convertJavaStrArrToStrVector(env, jInnerArray, innerVector);
707             env->DeleteLocalRef(jInnerArray);
708             middleArray.push_back(innerVector);
709         }
710         env->DeleteLocalRef(jMiddleArray);
711         value.push_back(middleArray);
712     }
713
714     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
715     if (!rep)
716     {
717         return;
718     }
719     std::string key = env->GetStringUTFChars(jKey, nullptr);
720     rep->setValue(key, value);
721 }
722
723 /*
724 * Class:     org_iotivity_base_OcRepresentation
725 * Method:    setValueRepresentationArray
726 * Signature: (Ljava/lang/String;[Lorg/iotivity/base/OcRepresentation;)V
727 */
728 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentationArray
729 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
730 {
731     LOGD("OcRepresentation_setValueRepresentationArray");
732     if (!jKey)
733     {
734         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
735         return;
736     }
737
738     std::vector<OCRepresentation> value;
739     JniUtils::convertJavaRepresentationArrToVector(env, jValue, value);
740
741     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
742     if (!rep)
743     {
744         return;
745     }
746
747     std::string key = env->GetStringUTFChars(jKey, nullptr);
748     rep->setValue(key, value);
749 }
750
751 /*
752 * Class:     org_iotivity_base_OcRepresentation
753 * Method:    setValueRepresentation2DArray
754 * Signature: (Ljava/lang/String;[[Lorg/iotivity/base/OcRepresentation;)V
755 */
756 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation2DArray
757 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
758 {
759     LOGD("OcRepresentation_setValueRepresentation2DArray");
760     if (!jKey)
761     {
762         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
763         return;
764     }
765     std::vector<std::vector<OCRepresentation>> value;
766     const jsize lenOuter = env->GetArrayLength(jValue);
767     for (jsize j = 0; j < lenOuter; ++j)
768     {
769         jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, j));
770         std::vector<OCRepresentation> innerVector;
771         JniUtils::convertJavaRepresentationArrToVector(env, jInnerArray, innerVector);
772         env->DeleteLocalRef(jInnerArray);
773         value.push_back(innerVector);
774     }
775
776     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
777     if (!rep)
778     {
779         return;
780     }
781
782     std::string key = env->GetStringUTFChars(jKey, nullptr);
783     rep->setValue(key, value);
784 }
785
786 /*
787 * Class:     org_iotivity_base_OcRepresentation
788 * Method:    setValueRepresentation3DArray
789 * Signature: (Ljava/lang/String;[[[Lorg/iotivity/base/OcRepresentation;)V
790 */
791 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation3DArray
792 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
793 {
794     LOGD("OcRepresentation_setValueRepresentation3DArray");
795     if (!jKey)
796     {
797         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
798         return;
799     }
800     std::vector<std::vector<std::vector<OCRepresentation>>> value;
801     const jsize lenOuter = env->GetArrayLength(jValue);
802     for (jsize k = 0; k < lenOuter; ++k)
803     {
804         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
805         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
806         std::vector<std::vector<OCRepresentation>> middleArray;
807         for (jsize j = 0; j < lenMiddle; ++j)
808         {
809             jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jMiddleArray, j));
810             std::vector<OCRepresentation> innerVector;
811             JniUtils::convertJavaRepresentationArrToVector(env, jInnerArray, innerVector);
812             env->DeleteLocalRef(jInnerArray);
813             middleArray.push_back(innerVector);
814         }
815         env->DeleteLocalRef(jMiddleArray);
816         value.push_back(middleArray);
817     }
818
819     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
820     if (!rep)
821     {
822         return;
823     }
824
825     std::string key = env->GetStringUTFChars(jKey, nullptr);
826     rep->setValue(key, value);
827 }
828
829 /*
830 * Class:     org_iotivity_base_OcRepresentation
831 * Method:    setValueByteArray
832 * Signature: (Ljava/lang/String;[B)V
833 */
834 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueByteArray
835 (JNIEnv *env, jobject thiz, jstring jKey, jbyteArray jValue)
836 {
837     LOGD("OcRepresentation_setValueByteArray");
838     if (!jKey)
839     {
840         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
841         return;
842     }
843
844     const jsize len = env->GetArrayLength(jValue);
845     jbyte* bytes = env->GetByteArrayElements(jValue, nullptr);
846
847     std::vector<uint8_t> value;
848     for (jsize i = 0; i < len; ++i)
849     {
850         value.push_back(static_cast<uint8_t>(bytes[i]));
851     }
852     env->ReleaseByteArrayElements(jValue, bytes, JNI_ABORT);
853
854     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
855     if (!rep)
856     {
857         return;
858     }
859
860     std::string key = env->GetStringUTFChars(jKey, nullptr);
861     rep->setValue(key, value);
862 }
863
864 /*
865 * Class:     org_iotivity_base_OcRepresentation
866 * Method:    addChild
867 * Signature: (Lorg/iotivity/base/OcRepresentation;)V
868 */
869 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_addChild
870 (JNIEnv *env, jobject thiz, jobject jOcRepresentation)
871 {
872     LOGD("OcRepresentation_addChild");
873     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
874     if (!rep)
875     {
876         return;
877     }
878
879     OCRepresentation *child = JniOcRepresentation::getOCRepresentationPtr(env, jOcRepresentation);
880     if (!child)
881     {
882         return;
883     }
884
885     rep->addChild(*child);
886 }
887
888 /*
889 * Class:     org_iotivity_base_OcRepresentation
890 * Method:    clearChildren
891 * Signature: ()V
892 */
893 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_clearChildren
894 (JNIEnv *env, jobject thiz)
895 {
896     LOGD("OcRepresentation_clearChildren");
897     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
898     if (!rep)
899     {
900         return;
901     }
902
903     rep->clearChildren();
904 }
905
906 /*
907 * Class:     org_iotivity_base_OcRepresentation
908 * Method:    getChildrenArray
909 * Signature: ()[Lorg/iotivity/base/OcRepresentation;
910 */
911 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcRepresentation_getChildrenArray
912 (JNIEnv *env, jobject thiz)
913 {
914     LOGD("OcRepresentation_getChildrenArray");
915     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
916     if (!rep)
917     {
918         return nullptr;
919     }
920
921     return JniUtils::convertRepresentationVectorToJavaArray(env, rep->getChildren());
922 }
923
924 /*
925 * Class:     org_iotivity_base_OcRepresentation
926 * Method:    getUri
927 * Signature: ()Ljava/lang/String;
928 */
929 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcRepresentation_getUri
930 (JNIEnv *env, jobject thiz)
931 {
932     LOGD("OcRepresentation_getUri");
933     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
934     if (!rep)
935     {
936         return nullptr;
937     }
938
939     std::string uri(rep->getUri());
940     return env->NewStringUTF(uri.c_str());
941 }
942
943 /*
944 * Class:     org_iotivity_base_OcRepresentation
945 * Method:    getHost
946 * Signature: ()Ljava/lang/String;
947 */
948 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcRepresentation_getHost
949 (JNIEnv *env, jobject thiz)
950 {
951     LOGD("OcRepresentation_getHost");
952     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
953     if (!rep)
954     {
955         return nullptr;
956     }
957
958     std::string uri(rep->getHost());
959     return env->NewStringUTF(uri.c_str());
960 }
961
962 /*
963 * Class:     org_iotivity_base_OcRepresentation
964 * Method:    setUri
965 * Signature: (Ljava/lang/String;)V
966 */
967 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setUri
968 (JNIEnv *env, jobject thiz, jstring jUri)
969 {
970     LOGD("OcRepresentation_setUri");
971     if (!jUri)
972     {
973         ThrowOcException(OC_STACK_INVALID_PARAM, "uri cannot be null");
974         return;
975     }
976     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
977     if (!rep)
978     {
979         return;
980     }
981
982     rep->setUri(env->GetStringUTFChars(jUri, nullptr));
983 }
984
985 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_hasAttribute
986 (JNIEnv *env, jobject thiz, jstring jstr)
987 {
988     LOGD("OcRepresentation_hasAttribute");
989     if (!jstr)
990     {
991         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
992         return false;
993     }
994     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
995     if (!rep)
996     {
997         return false;
998     }
999
1000     std::string str = env->GetStringUTFChars(jstr, nullptr);
1001     return rep->hasAttribute(str);
1002 }
1003
1004 /*
1005 * Class:     org_iotivity_base_OcRepresentation
1006 * Method:    getResourceTypes
1007 * Signature: ()Ljava/util/List;
1008 */
1009 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getResourceTypes
1010 (JNIEnv *env, jobject thiz)
1011 {
1012     LOGD("OcRepresentation_getResourceTypes");
1013     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1014     if (!rep)
1015     {
1016         return nullptr;
1017     }
1018
1019     std::vector<std::string> resourceTypes = rep->getResourceTypes();
1020     return JniUtils::convertStrVectorToJavaStrList(env, resourceTypes);
1021 }
1022
1023 /*
1024 * Class:     org_iotivity_base_OcRepresentation
1025 * Method:    setResourceTypeArray
1026 * Signature: ([Ljava/lang/String;)V
1027 */
1028 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setResourceTypeArray
1029 (JNIEnv *env, jobject thiz, jobjectArray jResourceTypeArray)
1030 {
1031     LOGD("OcRepresentation_setResourceTypeArray");
1032     if (!jResourceTypeArray)
1033     {
1034         ThrowOcException(OC_STACK_INVALID_PARAM, "resourceTypeList cannot be null");
1035         return;
1036     }
1037     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1038     if (!rep)
1039     {
1040         return;
1041     }
1042
1043     std::vector<std::string> resourceTypes;
1044     JniUtils::convertJavaStrArrToStrVector(env, jResourceTypeArray, resourceTypes);
1045     rep->setResourceTypes(resourceTypes);
1046 }
1047 /*
1048 * Class:     org_iotivity_base_OcRepresentation
1049 * Method:    getResourceInterfaces
1050 * Signature: ()Ljava/util/List;
1051 */
1052 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getResourceInterfaces
1053 (JNIEnv *env, jobject thiz)
1054 {
1055     LOGD("OcRepresentation_getResourceInterfaces");
1056     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1057     if (!rep)
1058     {
1059         return nullptr;
1060     }
1061
1062     std::vector<std::string> resourceInterfaces = rep->getResourceInterfaces();
1063     return JniUtils::convertStrVectorToJavaStrList(env, resourceInterfaces);
1064 }
1065
1066 /*
1067 * Class:     org_iotivity_base_OcRepresentation
1068 * Method:    setResourceInterfaceArray
1069 * Signature: ([Ljava/lang/String;)V
1070 */
1071 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setResourceInterfaceArray
1072 (JNIEnv *env, jobject thiz, jobjectArray jResourceInterfaceArray)
1073 {
1074     LOGD("OcRepresentation_setResourceInterfaceArray");
1075     if (!jResourceInterfaceArray)
1076     {
1077         ThrowOcException(OC_STACK_INVALID_PARAM, "resourceInterfaceList cannot be null");
1078         return;
1079     }
1080     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1081     if (!rep)
1082     {
1083         return;
1084     }
1085
1086     std::vector<std::string> resourceInterfaces;
1087     JniUtils::convertJavaStrArrToStrVector(env, jResourceInterfaceArray, resourceInterfaces);
1088     rep->setResourceInterfaces(resourceInterfaces);
1089 }
1090
1091 /*
1092 * Class:     org_iotivity_base_OcRepresentation
1093 * Method:    isEmpty
1094 * Signature: ()Z
1095 */
1096 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_isEmpty
1097 (JNIEnv *env, jobject thiz)
1098 {
1099     LOGD("OcRepresentation_isEmpty");
1100     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1101     if (!rep)
1102     {
1103         return false;
1104     }
1105
1106     return static_cast<jboolean>(rep->empty());
1107 }
1108
1109 /*
1110 * Class:     org_iotivity_base_OcRepresentation
1111 * Method:    size
1112 * Signature: ()I
1113 */
1114 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcRepresentation_size
1115 (JNIEnv *env, jobject thiz)
1116 {
1117     LOGD("OcRepresentation_size");
1118     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1119     if (!rep)
1120     {
1121         return -1;
1122     }
1123
1124     return static_cast<jint>(rep->numberOfAttributes());
1125 }
1126
1127 /*
1128 * Class:     org_iotivity_base_OcRepresentation
1129 * Method:    remove
1130 * Signature: (Ljava/lang/String;)Z
1131 */
1132 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_remove
1133 (JNIEnv *env, jobject thiz, jstring jAttributeKey)
1134 {
1135     LOGD("OcRepresentation_remove");
1136     if (!jAttributeKey)
1137     {
1138         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
1139         return false;
1140     }
1141     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1142     if (!rep)
1143     {
1144         return false;
1145     }
1146
1147     std::string attributeKey = env->GetStringUTFChars(jAttributeKey, nullptr);
1148     return static_cast<jboolean>(rep->erase(attributeKey));
1149 }
1150
1151 /*
1152 * Class:     org_iotivity_base_OcRepresentation
1153 * Method:    setNull
1154 * Signature: (Ljava/lang/String;)V
1155 */
1156 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setNull
1157 (JNIEnv *env, jobject thiz, jstring jAttributeKey)
1158 {
1159     LOGD("OcRepresentation_setNull");
1160     if (!jAttributeKey)
1161     {
1162         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
1163         return;
1164     }
1165     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1166     if (!rep)
1167     {
1168         return;
1169     }
1170
1171     std::string attributeKey = env->GetStringUTFChars(jAttributeKey, nullptr);
1172     rep->setNULL(attributeKey);
1173 }
1174
1175 /*
1176 * Class:     org_iotivity_base_OcRepresentation
1177 * Method:    isNull
1178 * Signature: (Ljava/lang/String;)Z
1179 */
1180 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_isNull
1181 (JNIEnv *env, jobject thiz, jstring jAttributeKey)
1182 {
1183     LOGD("OcRepresentation_isNull");
1184     if (!jAttributeKey)
1185     {
1186         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
1187         return false;
1188     }
1189     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1190     if (!rep)
1191     {
1192         return false;
1193     }
1194
1195     std::string attributeKey = env->GetStringUTFChars(jAttributeKey, nullptr);
1196     return static_cast<jboolean>(rep->isNULL(attributeKey));
1197 }
1198
1199 /*
1200 * Class:     org_iotivity_base_OcRepresentation
1201 * Method:    create
1202 * Signature: ()V
1203 */
1204 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_create
1205 (JNIEnv *env, jobject thiz)
1206 {
1207     LOGD("OcRepresentation_create");
1208     OCRepresentation *rep = new OCRepresentation();
1209     SetHandle<OCRepresentation>(env, thiz, rep);
1210     if (env->ExceptionCheck())
1211     {
1212         LOGE("Failed to set native handle for OcRepresentation");
1213         delete rep;
1214     }
1215 }
1216
1217 /*
1218 * Class:     org_iotivity_base_OcRepresentation
1219 * Method:    dispose
1220 * Signature: ()V
1221 */
1222 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_dispose
1223 (JNIEnv *env, jobject thiz, jboolean jNeedsDelete)
1224 {
1225     LOGD("OcRepresentation_dispose");
1226     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1227
1228     if (jNeedsDelete)
1229     {
1230         delete rep;
1231     }
1232 }