replace : iotivity -> iotivity-sec
[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)
30     {
31         return nullptr;
32     }
33     for (size_t i = 0; i < vector.size(); ++i)
34     {
35         jstring jStr = env->NewStringUTF(vector[i].c_str());
36         if (!jStr)
37         {
38             return nullptr;
39         }
40         env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr);
41         if (env->ExceptionCheck())
42         {
43             return nullptr;
44         }
45         env->DeleteLocalRef(jStr);
46     }
47     return jList;
48 }
49
50 void JniUtils::convertJavaStrArrToStrVector(JNIEnv *env, jobjectArray jStrArr, std::vector<std::string> &vector)
51 {
52     if (!jStrArr)
53     {
54         return;
55     }
56
57     jsize len = env->GetArrayLength(jStrArr);
58     for (jsize i = 0; i < len; ++i)
59     {
60         jstring jStr = (jstring)env->GetObjectArrayElement(jStrArr, i);
61         if (!jStr)
62         {
63             return;
64         }
65         vector.push_back(env->GetStringUTFChars(jStr, nullptr));
66         if (env->ExceptionCheck())
67         {
68             return;
69         }
70         env->DeleteLocalRef(jStr);
71     }
72 }
73
74 void JniUtils::convertJavaHeaderOptionsArrToVector(JNIEnv *env, jobjectArray jHeaderOptions,
75     OC::HeaderOptions &headerOptions)
76 {
77     if (!jHeaderOptions)
78     {
79         return;
80     }
81
82     jsize len = env->GetArrayLength(jHeaderOptions);
83     for (jsize i = 0; i < len; ++i)
84     {
85         jobject header = env->GetObjectArrayElement(jHeaderOptions, i);
86         if (!header)
87         {
88             return;
89         }
90         jint jId = env->CallIntMethod(header, g_mid_OcHeaderOption_get_id);
91         jstring jData = (jstring)env->CallObjectMethod(header, g_mid_OcHeaderOption_get_data);
92         OC::HeaderOption::OCHeaderOption hopt(
93             static_cast<int>(jId),
94             env->GetStringUTFChars(jData, nullptr));
95
96         headerOptions.push_back(hopt);
97
98         if (env->ExceptionCheck())
99         {
100             return;
101         }
102         env->DeleteLocalRef(header);
103         env->DeleteLocalRef(jData);
104     }
105 }
106
107 jobject JniUtils::convertHeaderOptionsVectorToJavaList(JNIEnv *env, const OC::HeaderOptions& headerOptions)
108 {
109     jobject jHeaderOptionList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
110     if (!jHeaderOptionList)
111     {
112         return nullptr;
113     }
114
115     for (size_t i = 0; i < headerOptions.size(); ++i)
116     {
117         jobject jHeaderOption = env->NewObject(
118             g_cls_OcHeaderOption,
119             g_mid_OcHeaderOption_ctor,
120             static_cast<jint>(headerOptions[i].getOptionID()),
121             env->NewStringUTF(headerOptions[i].getOptionData().c_str())
122             );
123         if (!jHeaderOption)
124         {
125             return nullptr;
126         }
127
128         env->CallBooleanMethod(jHeaderOptionList, g_mid_LinkedList_add_object, jHeaderOption);
129         if (env->ExceptionCheck())
130         {
131             return nullptr;
132         }
133         env->DeleteLocalRef(jHeaderOption);
134     }
135
136     return jHeaderOptionList;
137 }
138
139 void JniUtils::convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap, OC::QueryParamsMap &map)
140 {
141     if (!hashMap)
142     {
143         return;
144     }
145
146     jobject jEntrySet = env->CallObjectMethod(hashMap, g_mid_Map_entrySet);
147     jobject jIterator = env->CallObjectMethod(jEntrySet, g_mid_Set_iterator);
148     if (!jEntrySet || !jIterator || env->ExceptionCheck())
149     {
150         return;
151     }
152
153     while (env->CallBooleanMethod(jIterator, g_mid_Iterator_hasNext))
154     {
155         jobject jEntry = env->CallObjectMethod(jIterator, g_mid_Iterator_next);
156         if (!jEntry)
157         {
158             return;
159         }
160         jstring jKey = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getKey);
161         if (!jKey)
162         {
163             return;
164         }
165         jstring jValue = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getValue);
166         if (!jValue)
167         {
168             return;
169         }
170
171         map.insert(std::make_pair(env->GetStringUTFChars(jKey, nullptr),
172             env->GetStringUTFChars(jValue, nullptr)));
173
174         if (env->ExceptionCheck())
175         {
176             return;
177         }
178         env->DeleteLocalRef(jEntry);
179         env->DeleteLocalRef(jKey);
180         env->DeleteLocalRef(jValue);
181     }
182 }
183
184 jobject JniUtils::convertQueryParamsMapToJavaMap(JNIEnv *env, const OC::QueryParamsMap &map)
185 {
186     jobject hashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
187     if (!hashMap)
188     {
189         return nullptr;
190     }
191
192     for (auto it = map.begin(); it != map.end(); ++it)
193     {
194         std::string key = it->first;
195         std::string value = it->second;
196
197         env->CallObjectMethod(hashMap,
198             g_mid_HashMap_put,
199             env->NewStringUTF(key.c_str()),
200             env->NewStringUTF(value.c_str()));
201         if (env->ExceptionCheck())
202         {
203             return nullptr;
204         }
205     }
206
207     return hashMap;
208 }
209
210 void JniUtils::convertJavaRepresentationArrToVector(JNIEnv *env,
211     jobjectArray jRepresentationArray,
212     std::vector<OC::OCRepresentation>& representationVector)
213 {
214     if (!jRepresentationArray)
215     {
216         return;
217     }
218     jsize len = env->GetArrayLength(jRepresentationArray);
219
220     for (jsize i = 0; i < len; ++i)
221     {
222         jobject jRep = env->GetObjectArrayElement(jRepresentationArray, i);
223         if (!jRep)
224         {
225             return;
226         }
227         OC::OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep);
228         representationVector.push_back(*rep);
229         if (env->ExceptionCheck())
230         {
231             return;
232         }
233         env->DeleteLocalRef(jRep);
234     }
235 }
236
237 jobjectArray JniUtils::convertRepresentationVectorToJavaArray(JNIEnv *env,
238     const std::vector<OC::OCRepresentation>& representationVector)
239 {
240     jsize len = static_cast<jsize>(representationVector.size());
241     jobjectArray repArr = env->NewObjectArray(len, g_cls_OcRepresentation, nullptr);
242     if (!repArr)
243     {
244         return nullptr;
245     }
246     for (jsize i = 0; i < len; ++i)
247     {
248         OCRepresentation* rep = new OCRepresentation(representationVector[i]);
249         jlong handle = reinterpret_cast<jlong>(rep);
250         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
251             handle, true);
252         if (!jRepresentation)
253         {
254             delete rep;
255             return nullptr;
256         }
257         env->SetObjectArrayElement(repArr, i, jRepresentation);
258         if (env->ExceptionCheck())
259         {
260             return nullptr;
261         }
262         env->DeleteLocalRef(jRepresentation);
263     }
264
265     return repArr;
266 }