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