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;
60 jsize len = env->GetArrayLength(jHeaderOptions);
61 for (jsize i = 0; i < len; ++i)
63 jobject header = env->GetObjectArrayElement(jHeaderOptions, i);
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, NULL));
71 headerOptions.push_back(hopt);
73 if (env->ExceptionCheck()) return;
74 env->DeleteLocalRef(header);
75 env->DeleteLocalRef(jData);
79 jobject JniUtils::convertHeaderOptionsVectorToJavaList(JNIEnv *env, const OC::HeaderOptions& headerOptions)
81 jobject jHeaderOptionList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
82 if (!jHeaderOptionList) return nullptr;
84 for (size_t i = 0; i < headerOptions.size(); ++i)
86 jobject jHeaderOption = env->NewObject(
88 g_mid_OcHeaderOption_ctor,
89 static_cast<jint>(headerOptions[i].getOptionID()),
90 env->NewStringUTF(headerOptions[i].getOptionData().c_str())
92 if (!jHeaderOption) return nullptr;
94 env->CallBooleanMethod(jHeaderOptionList, g_mid_LinkedList_add_object, jHeaderOption);
95 if (env->ExceptionCheck()) return nullptr;
96 env->DeleteLocalRef(jHeaderOption);
99 return jHeaderOptionList;
102 void JniUtils::convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap, OC::QueryParamsMap &map)
104 if (!hashMap) return;
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;
110 while (env->CallBooleanMethod(jIterator, g_mid_Iterator_hasNext))
112 jobject jEntry = env->CallObjectMethod(jIterator, g_mid_Iterator_next);
114 jstring jKey = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getKey);
116 jstring jValue = (jstring)env->CallObjectMethod(jEntry, g_mid_MapEntry_getValue);
119 map.insert(std::make_pair(env->GetStringUTFChars(jKey, NULL),
120 env->GetStringUTFChars(jValue, NULL)));
122 if (env->ExceptionCheck()) return;
123 env->DeleteLocalRef(jEntry);
124 env->DeleteLocalRef(jKey);
125 env->DeleteLocalRef(jValue);
129 jobject JniUtils::convertQueryParamsMapToJavaMap(JNIEnv *env, const OC::QueryParamsMap &map)
131 jobject hashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
132 if (!hashMap) return nullptr;
134 for (auto it = map.begin(); it != map.end(); ++it)
136 std::string key = it->first;
137 std::string value = it->second;
139 env->CallObjectMethod(hashMap,
141 env->NewStringUTF(key.c_str()),
142 env->NewStringUTF(value.c_str()));
143 if (env->ExceptionCheck()) return nullptr;
149 void JniUtils::convertJavaRepresentationArrToVector(JNIEnv *env,
150 jobjectArray jRepresentationArray,
151 std::vector<OC::OCRepresentation>& representationVector)
153 if (!jRepresentationArray) return;
154 jsize len = env->GetArrayLength(jRepresentationArray);
156 for (jsize i = 0; i < len; ++i)
158 jobject jRep = env->GetObjectArrayElement(jRepresentationArray, i);
160 OC::OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep);
161 representationVector.push_back(*rep);
162 if (env->ExceptionCheck()) return;
163 env->DeleteLocalRef(jRep);
167 jobjectArray JniUtils::convertRepresentationVectorToJavaArray(JNIEnv *env,
168 const std::vector<OC::OCRepresentation>& representationVector)
170 jsize len = static_cast<jsize>(representationVector.size());
171 jobjectArray repArr = env->NewObjectArray(len, g_cls_OcRepresentation, NULL);
172 if (!repArr) return nullptr;
173 for (jsize i = 0; i < len; ++i)
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,
179 if (!jRepresentation)
184 env->SetObjectArrayElement(repArr, i, jRepresentation);
185 if (env->ExceptionCheck()) return nullptr;
186 env->DeleteLocalRef(jRepresentation);