Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniUtils.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 "JniUtils.h"
24 #include "JniOcRepresentation.h"
25
26 jobject JniUtils::convertStrVectorToJavaStrList(JNIEnv *env, std::vector<std::string> &vector)
27 {
28     jobject jList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
29     if (!jList) return nullptr;
30     for (size_t i = 0; i < vector.size(); ++i)
31     {
32         jstring jStr = env->NewStringUTF(vector[i].c_str());
33         if (!jStr) return nullptr;
34         env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr);
35         if (env->ExceptionCheck()) return nullptr;
36         env->DeleteLocalRef(jStr);
37     }
38     return jList;
39 }
40
41 void JniUtils::convertJavaStrArrToStrVector(JNIEnv *env, jobjectArray jStrArr, std::vector<std::string> &vector)
42 {
43     if (!jStrArr) return;
44
45     jsize len = env->GetArrayLength(jStrArr);
46     for (jsize i = 0; i < len; ++i)
47     {
48         jstring jStr = (jstring)env->GetObjectArrayElement(jStrArr, i);
49         if (!jStr) return;
50         vector.push_back(env->GetStringUTFChars(jStr, nullptr));
51         if (env->ExceptionCheck()) return;
52         env->DeleteLocalRef(jStr);
53     }
54 }
55
56 void JniUtils::convertJavaHeaderOptionsArrToVector(JNIEnv *env, jobjectArray jHeaderOptions,
57     OC::HeaderOptions &headerOptions)
58 {
59     if (!jHeaderOptions) return;
60     jsize len = env->GetArrayLength(jHeaderOptions);
61     for (jsize i = 0; i < len; ++i)
62     {
63         jobject header = env->GetObjectArrayElement(jHeaderOptions, i);
64         if (!header) return;
65         jint jId = env->CallIntMethod(header, g_mid_OcHeaderOption_get_id);
66         jstring jData = (jstring)env->CallObjectMethod(header, g_mid_OcHeaderOption_get_data);
67         OC::HeaderOption::OCHeaderOption hopt(
68             static_cast<int>(jId),
69             env->GetStringUTFChars(jData, nullptr));
70
71         headerOptions.push_back(hopt);
72
73         if (env->ExceptionCheck()) return;
74         env->DeleteLocalRef(header);
75         env->DeleteLocalRef(jData);
76     }
77 }
78
79 jobject JniUtils::convertHeaderOptionsVectorToJavaList(JNIEnv *env, const OC::HeaderOptions& headerOptions)
80 {
81     jobject jHeaderOptionList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
82     if (!jHeaderOptionList) return nullptr;
83
84     for (size_t i = 0; i < headerOptions.size(); ++i)
85     {
86         jobject jHeaderOption = env->NewObject(
87             g_cls_OcHeaderOption,
88             g_mid_OcHeaderOption_ctor,
89             static_cast<jint>(headerOptions[i].getOptionID()),
90             env->NewStringUTF(headerOptions[i].getOptionData().c_str())
91             );
92         if (!jHeaderOption) return nullptr;
93
94         env->CallBooleanMethod(jHeaderOptionList, g_mid_LinkedList_add_object, jHeaderOption);
95         if (env->ExceptionCheck()) return nullptr;
96         env->DeleteLocalRef(jHeaderOption);
97     }
98
99     return jHeaderOptionList;
100 }
101
102 void JniUtils::convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap, OC::QueryParamsMap &map)
103 {
104     if (!hashMap) return;
105
106     jobject jEntrySet = env->CallObjectMethod(hashMap, g_mid_Map_entrySet);
107     jobject jIterator = env->CallObjectMethod(jEntrySet, g_mid_Set_iterator);
108     if (!jEntrySet || !jIterator || env->ExceptionCheck()) return;
109
110     while (env->CallBooleanMethod(jIterator, g_mid_Iterator_hasNext))
111     {
112         jobject jEntry = env->CallObjectMethod(jIterator, g_mid_Iterator_next);
113         if (!jEntry) return;
114         jstring jKey = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getKey);
115         if (!jKey) return;
116         jstring jValue = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getValue);
117         if (!jValue) return;
118
119         map.insert(std::make_pair(env->GetStringUTFChars(jKey, nullptr),
120             env->GetStringUTFChars(jValue, nullptr)));
121
122         if (env->ExceptionCheck()) return;
123         env->DeleteLocalRef(jEntry);
124         env->DeleteLocalRef(jKey);
125         env->DeleteLocalRef(jValue);
126     }
127 }
128
129 jobject JniUtils::convertQueryParamsMapToJavaMap(JNIEnv *env, const OC::QueryParamsMap &map)
130 {
131     jobject hashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
132     if (!hashMap) return nullptr;
133
134     for (auto it = map.begin(); it != map.end(); ++it)
135     {
136         std::string key = it->first;
137         std::string value = it->second;
138
139         env->CallObjectMethod(hashMap,
140             g_mid_HashMap_put,
141             env->NewStringUTF(key.c_str()),
142             env->NewStringUTF(value.c_str()));
143         if (env->ExceptionCheck()) return nullptr;
144     }
145
146     return hashMap;
147 }
148
149 void JniUtils::convertJavaRepresentationArrToVector(JNIEnv *env,
150     jobjectArray jRepresentationArray,
151     std::vector<OC::OCRepresentation>& representationVector)
152 {
153     if (!jRepresentationArray) return;
154     jsize len = env->GetArrayLength(jRepresentationArray);
155
156     for (jsize i = 0; i < len; ++i)
157     {
158         jobject jRep = env->GetObjectArrayElement(jRepresentationArray, i);
159         if (!jRep) return;
160         OC::OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep);
161         representationVector.push_back(*rep);
162         if (env->ExceptionCheck()) return;
163         env->DeleteLocalRef(jRep);
164     }
165 }
166
167 jobjectArray JniUtils::convertRepresentationVectorToJavaArray(JNIEnv *env,
168     const std::vector<OC::OCRepresentation>& representationVector)
169 {
170     jsize len = static_cast<jsize>(representationVector.size());
171     jobjectArray repArr = env->NewObjectArray(len, g_cls_OcRepresentation, nullptr);
172     if (!repArr) return nullptr;
173     for (jsize i = 0; i < len; ++i)
174     {
175         OCRepresentation* rep = new OCRepresentation(representationVector[i]);
176         jlong handle = reinterpret_cast<jlong>(rep);
177         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
178             handle, true);
179         if (!jRepresentation)
180         {
181             delete rep;
182             return nullptr;
183         }
184         env->SetObjectArrayElement(repArr, i, jRepresentation);
185         if (env->ExceptionCheck()) return nullptr;
186         env->DeleteLocalRef(jRepresentation);
187     }
188
189     return repArr;
190 }