fd59abfcf1803146d782e5a8a12b7c3bbd6f1c8d
[platform/upstream/iotivity.git] / service / simulator / java / jni / jni_queryparam.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "jni_queryparam.h"
22 #include "jni_string.h"
23 #include "simulator_utils_jni.h"
24
25 JniQueryParameter::JniQueryParameter(JNIEnv *env) : m_env(env) {}
26
27 std::map<std::string, std::string> JniQueryParameter::toCpp(jobject jQueryParams)
28 {
29     std::map<std::string, std::string> result;
30     if (!jQueryParams)
31         return result;
32
33     static jmethodID entrySetMethodID = m_env->GetMethodID(gSimulatorClassRefs.mapCls, "entrySet",
34                                         "()Ljava/util/Set;");
35     static jmethodID iteratorMethodID = m_env->GetMethodID(gSimulatorClassRefs.setCls, "iterator",
36                                         "()Ljava/util/Iterator;");
37     static jmethodID hasNextMethodID = m_env->GetMethodID(gSimulatorClassRefs.iteratorCls, "hasNext",
38                                        "()Z");
39     static jmethodID nextMethodID = m_env->GetMethodID(gSimulatorClassRefs.iteratorCls, "next",
40                                     "()Ljava/lang/Object;");
41     static jmethodID getKeyMethodID = m_env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getKey",
42                                       "()Ljava/lang/Object;");
43     static jmethodID getValueMethodID = m_env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getValue",
44                                         "()Ljava/lang/Object;");
45
46     jobject entrySet = m_env->CallObjectMethod(jQueryParams, entrySetMethodID);
47     jobject iterator = m_env->CallObjectMethod(entrySet, iteratorMethodID);
48     if (!entrySet || !iterator || m_env->ExceptionCheck())
49         return result;
50
51     while (m_env->CallBooleanMethod(iterator, hasNextMethodID))
52     {
53         jobject entry = m_env->CallObjectMethod(iterator, nextMethodID);
54         jstring key = (jstring) m_env->CallObjectMethod(entry, getKeyMethodID);
55         jstring value = (jstring) m_env->CallObjectMethod(entry, getValueMethodID);
56
57         JniString jniKey(m_env, key);
58         JniString jniValue(m_env, key);
59
60         result[jniKey.get()] = jniValue.get();
61
62         m_env->DeleteLocalRef(entry);
63         m_env->DeleteLocalRef(key);
64         m_env->DeleteLocalRef(value);
65     }
66
67     return result;
68 }