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);
29 if (!jList) return nullptr;
30 for (size_t i = 0; i < vector.size(); ++i)
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);
41 void JniUtils::convertJavaStrArrToStrVector(JNIEnv *env, jobjectArray jStrArr, std::vector<std::string> &vector)
45 jsize len = env->GetArrayLength(jStrArr);
46 for (jsize i = 0; i < len; ++i)
48 jstring jStr = (jstring)env->GetObjectArrayElement(jStrArr, i);
50 vector.push_back(env->GetStringUTFChars(jStr, NULL));
51 if (env->ExceptionCheck()) return;
52 env->DeleteLocalRef(jStr);
56 void JniUtils::convertJavaHeaderOptionsArrToVector(JNIEnv *env, jobjectArray jHeaderOptions,
57 OC::HeaderOptions &headerOptions)
59 if (!jHeaderOptions) return;
61 jsize len = env->GetArrayLength(jHeaderOptions);
62 for (jsize i = 0; i < len; ++i)
64 jobject header = env->GetObjectArrayElement(jHeaderOptions, i);
66 jint jId = env->CallIntMethod(header, g_mid_OcHeaderOption_get_id);
67 jstring jData = (jstring)env->CallObjectMethod(header, g_mid_OcHeaderOption_get_data);
70 OC::HeaderOption::OCHeaderOption hopt(
71 static_cast<int>(jId),
72 env->GetStringUTFChars(jData, NULL));
74 headerOptions.push_back(hopt);
76 if (env->ExceptionCheck()) return;
77 env->DeleteLocalRef(header);
78 env->DeleteLocalRef(jData);
82 jobject JniUtils::convertHeaderOptionsVectorToJavaList(JNIEnv *env, const OC::HeaderOptions& headerOptions)
84 jobject jHeaderOptionList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
85 if (!jHeaderOptionList) return nullptr;
87 for (size_t i = 0; i < headerOptions.size(); ++i)
89 jobject jHeaderOption = env->NewObject(
91 g_mid_OcHeaderOption_ctor,
92 static_cast<jint>(headerOptions[i].getOptionID()),
93 env->NewStringUTF(headerOptions[i].getOptionData().c_str())
95 if (!jHeaderOption) return nullptr;
97 env->CallBooleanMethod(jHeaderOptionList, g_mid_LinkedList_add_object, jHeaderOption);
98 if (env->ExceptionCheck()) return nullptr;
99 env->DeleteLocalRef(jHeaderOption);
102 return jHeaderOptionList;
105 void JniUtils::convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap, OC::QueryParamsMap &map)
107 if (!hashMap) return;
109 jobject jEntrySet = env->CallObjectMethod(hashMap, g_mid_Map_entrySet);
110 jobject jIterator = env->CallObjectMethod(jEntrySet, g_mid_Set_iterator);
111 if (!jEntrySet || !jIterator || env->ExceptionCheck()) return;
113 while (env->CallBooleanMethod(jIterator, g_mid_Iterator_hasNext))
115 jobject jEntry = env->CallObjectMethod(jIterator, g_mid_Iterator_next);
117 jstring jKey = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getKey);
119 jstring jValue = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getValue);
122 map.insert(std::make_pair(env->GetStringUTFChars(jKey, NULL),
123 env->GetStringUTFChars(jValue, NULL)));
125 if (env->ExceptionCheck()) return;
126 env->DeleteLocalRef(jEntry);
127 env->DeleteLocalRef(jKey);
128 env->DeleteLocalRef(jValue);
132 jobject JniUtils::convertQueryParamsMapToJavaMap(JNIEnv *env, const OC::QueryParamsMap &map)
134 jobject hashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
135 if (!hashMap) return nullptr;
137 for (auto it = map.begin(); it != map.end(); ++it)
139 std::string key = it->first;
140 std::string value = it->second;
142 env->CallObjectMethod(hashMap,
144 env->NewStringUTF(key.c_str()),
145 env->NewStringUTF(value.c_str()));
146 if (env->ExceptionCheck()) return nullptr;
152 void JniUtils::convertJavaRepresentationArrToVector(JNIEnv *env,
153 jobjectArray jRepresentationArray,
154 std::vector<OC::OCRepresentation>& representationVector)
156 if (!jRepresentationArray) return;
157 jsize len = env->GetArrayLength(jRepresentationArray);
159 for (jsize i = 0; i < len; ++i)
161 jobject jRep = env->GetObjectArrayElement(jRepresentationArray, i);
163 OC::OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep);
164 representationVector.push_back(*rep);
165 if (env->ExceptionCheck()) return;
166 env->DeleteLocalRef(jRep);
170 jobjectArray JniUtils::convertRepresentationVectorToJavaArray(JNIEnv *env,
171 const std::vector<OC::OCRepresentation>& representationVector)
173 jsize len = static_cast<jsize>(representationVector.size());
174 jobjectArray repArr = env->NewObjectArray(len, g_cls_OcRepresentation, NULL);
175 if (!repArr) return nullptr;
176 for (jsize i = 0; i < len; ++i)
178 OCRepresentation* rep = new OCRepresentation(representationVector[i]);
179 jlong handle = reinterpret_cast<jlong>(rep);
180 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
182 if (!jRepresentation)
187 env->SetObjectArrayElement(repArr, i, jRepresentation);
188 if (env->ExceptionCheck()) return nullptr;
189 env->DeleteLocalRef(jRepresentation);