2 * //******************************************************************
4 * // Copyright 2015 Intel Corporation.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "JniOcRepresentation.h"
26 jobject JniUtils::convertStrVectorToJavaStrList(JNIEnv *env, std::vector<std::string> &vector)
28 jobject jList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
33 for (size_t i = 0; i < vector.size(); ++i)
35 jstring jStr = env->NewStringUTF(vector[i].c_str());
40 env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr);
41 if (env->ExceptionCheck())
45 env->DeleteLocalRef(jStr);
50 void JniUtils::convertJavaStrArrToStrVector(JNIEnv *env, jobjectArray jStrArr, std::vector<std::string> &vector)
57 jsize len = env->GetArrayLength(jStrArr);
58 for (jsize i = 0; i < len; ++i)
60 jstring jStr = (jstring)env->GetObjectArrayElement(jStrArr, i);
65 vector.push_back(env->GetStringUTFChars(jStr, nullptr));
66 if (env->ExceptionCheck())
70 env->DeleteLocalRef(jStr);
74 void JniUtils::convertJavaHeaderOptionsArrToVector(JNIEnv *env, jobjectArray jHeaderOptions,
75 OC::HeaderOptions &headerOptions)
82 jsize len = env->GetArrayLength(jHeaderOptions);
83 for (jsize i = 0; i < len; ++i)
85 jobject header = env->GetObjectArrayElement(jHeaderOptions, i);
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));
96 headerOptions.push_back(hopt);
98 if (env->ExceptionCheck())
102 env->DeleteLocalRef(header);
103 env->DeleteLocalRef(jData);
107 jobject JniUtils::convertHeaderOptionsVectorToJavaList(JNIEnv *env, const OC::HeaderOptions& headerOptions)
109 jobject jHeaderOptionList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
110 if (!jHeaderOptionList)
115 for (size_t i = 0; i < headerOptions.size(); ++i)
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())
128 env->CallBooleanMethod(jHeaderOptionList, g_mid_LinkedList_add_object, jHeaderOption);
129 if (env->ExceptionCheck())
133 env->DeleteLocalRef(jHeaderOption);
136 return jHeaderOptionList;
139 void JniUtils::convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap, OC::QueryParamsMap &map)
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())
153 while (env->CallBooleanMethod(jIterator, g_mid_Iterator_hasNext))
155 jobject jEntry = env->CallObjectMethod(jIterator, g_mid_Iterator_next);
160 jstring jKey = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getKey);
165 jstring jValue = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getValue);
171 map.insert(std::make_pair(env->GetStringUTFChars(jKey, nullptr),
172 env->GetStringUTFChars(jValue, nullptr)));
174 if (env->ExceptionCheck())
178 env->DeleteLocalRef(jEntry);
179 env->DeleteLocalRef(jKey);
180 env->DeleteLocalRef(jValue);
184 jobject JniUtils::convertQueryParamsMapToJavaMap(JNIEnv *env, const OC::QueryParamsMap &map)
186 jobject hashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
192 for (auto it = map.begin(); it != map.end(); ++it)
194 std::string key = it->first;
195 std::string value = it->second;
197 env->CallObjectMethod(hashMap,
199 env->NewStringUTF(key.c_str()),
200 env->NewStringUTF(value.c_str()));
201 if (env->ExceptionCheck())
210 void JniUtils::convertJavaRepresentationArrToVector(JNIEnv *env,
211 jobjectArray jRepresentationArray,
212 std::vector<OC::OCRepresentation>& representationVector)
214 if (!jRepresentationArray)
218 jsize len = env->GetArrayLength(jRepresentationArray);
220 for (jsize i = 0; i < len; ++i)
222 jobject jRep = env->GetObjectArrayElement(jRepresentationArray, i);
227 OC::OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep);
228 representationVector.push_back(*rep);
229 if (env->ExceptionCheck())
233 env->DeleteLocalRef(jRep);
237 jobjectArray JniUtils::convertRepresentationVectorToJavaArray(JNIEnv *env,
238 const std::vector<OC::OCRepresentation>& representationVector)
240 jsize len = static_cast<jsize>(representationVector.size());
241 jobjectArray repArr = env->NewObjectArray(len, g_cls_OcRepresentation, nullptr);
246 for (jsize i = 0; i < len; ++i)
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,
252 if (!jRepresentation)
257 env->SetObjectArrayElement(repArr, i, jRepresentation);
258 if (env->ExceptionCheck())
262 env->DeleteLocalRef(jRepresentation);