replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / src / main / jni / JniRcsResourceAttributes.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 "JniRcsResourceAttributes.h"
22
23 #include "JniRcsObject.h"
24 #include "JavaLocalRef.h"
25 #include "JniRcsValue.h"
26 #include "Log.h"
27 #include "Verify.h"
28
29 using namespace OIC::Service;
30
31 #define LOG_TAG "JNI-RCSResourceAttributes"
32
33 namespace
34 {
35     jclass g_cls_RCSResourceAttributes;
36
37     jmethodID g_ctor_RCSResourceAttributes;
38
39     jfieldID g_field_mCache;
40 }
41
42 void initRCSResourceAttributes(JNIEnvWrapper* env)
43 {
44     g_cls_RCSResourceAttributes = env->FindClassAsGlobalRef(CLS_NAME_RESOURCEATTRIBUTES);
45     g_ctor_RCSResourceAttributes = env->GetConstructorID(g_cls_RCSResourceAttributes, "()V");
46
47     g_field_mCache = env->GetFieldID(g_cls_RCSResourceAttributes, "mCache", AS_SIG(CLS_NAME_MAP));
48 }
49
50 void clearRCSResourceAttributes(JNIEnvWrapper* env)
51 {
52     env->DeleteGlobalRef(g_cls_RCSResourceAttributes);
53 }
54
55 jobject newAttributesObject(JNIEnv* env, const RCSResourceAttributes& attrs)
56 {
57     jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
58     VERIFY_NO_EXC_RET_DEF(env);
59
60     setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
61
62     return obj;
63 }
64
65 jobject newAttributesObject(JNIEnvWrapper* env, const RCSResourceAttributes& attrs)
66 {
67     jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
68
69     setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
70
71     return obj;
72 }
73
74 RCSResourceAttributes toNativeAttributes(JNIEnv* env, jobject attrsObj)
75 {
76     EXPECT_RET(attrsObj, "attrsObj is null!", { });
77
78     JNIEnvWrapper envWrapper{ env };
79
80     try
81     {
82         return toNativeAttributes(&envWrapper, attrsObj);
83     }
84     catch (const JavaException&)
85     {
86         return {};
87     }
88 }
89
90 RCSResourceAttributes toNativeAttributes(JNIEnvWrapper* env, jobject attrsObj)
91 {
92     EXPECT_RET(attrsObj, "attrsObj is null!", { });
93
94     RCSResourceAttributes attrs;
95
96     if (hasNativeHandle(env, attrsObj))
97     {
98         attrs = getNativeHandleAs< RCSResourceAttributes >(env, attrsObj);
99     }
100
101     writeNativeAttributesFromMap(env,
102             JavaLocalObject{ env, env->GetObjectField(attrsObj, g_field_mCache) }, attrs);
103
104     return attrs;
105 }
106
107 void writeNativeAttributesFromMap(JNIEnv* env, jobject mapObj, RCSResourceAttributes& targetAttrs)
108 {
109     JNIEnvWrapper envWrapper{ env };
110
111     try
112     {
113         return writeNativeAttributesFromMap(&envWrapper, mapObj, targetAttrs);
114     }
115     catch (const JavaException&)
116     {
117     }
118 }
119
120 void writeNativeAttributesFromMap(JNIEnvWrapper* env, jobject mapObj,
121         RCSResourceAttributes& targetAttrs)
122 {
123     JavaLocalObject setObj{ env, invoke_Map_entrySet(env, mapObj) };
124     JavaLocalObject iterObj{ env, invoke_Set_iterator(env, setObj) };
125
126     while (invoke_Iterator_hasNext(env, iterObj))
127     {
128         JavaLocalObject entryObj{ env, invoke_Iterator_next(env, iterObj) };
129         JavaLocalObject keyObj{ env, invoke_MapEntry_getKey(env, entryObj) };
130         JavaLocalObject valueObj{ env, invoke_MapEntry_getValue(env, entryObj) };
131
132         auto key = toStdString(env, static_cast< jstring >(keyObj.get()));
133
134         targetAttrs[std::move(key)] = toNativeAttrsValue(env, valueObj);
135     }
136 }
137
138 JNIEXPORT jboolean JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeIsEmpty
139 (JNIEnv* env, jobject obj)
140 {
141     LOGD("isEmpty");
142     EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", true);
143
144     auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
145     VERIFY_NO_EXC_RET_DEF(env);
146
147     return attrs.empty();
148 }
149
150 JNIEXPORT jint JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeSize
151 (JNIEnv* env, jobject obj)
152 {
153     LOGD("size");
154     EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", 0);
155
156     auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
157     VERIFY_NO_EXC_RET_DEF(env);
158
159     return attrs.size();
160 }
161
162 JNIEXPORT jboolean JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeRemove
163 (JNIEnv* env, jobject obj, jstring keyObj)
164 {
165     LOGD("remove");
166     EXPECT_RET_DEF(keyObj, "Key is null.");
167     EXPECT_RET_DEF(hasNativeHandle(env, obj), "no native handle.");
168
169     auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
170     VERIFY_NO_EXC_RET_DEF(env);
171     const auto& key = toStdString(env, keyObj);
172     VERIFY_NO_EXC_RET_DEF(env);
173
174     auto ret = attrs.erase(key);
175
176     if (attrs.empty()) releaseNativeHandle(env, obj);
177
178     return ret;
179 }
180
181 JNIEXPORT void JNICALL
182 Java_org_iotivity_service_RcsResourceAttributes_nativeClear
183 (JNIEnv* env, jobject obj)
184 {
185     LOGD("clear");
186
187     releaseNativeHandle(env, obj);
188 }
189
190 JNIEXPORT jboolean JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeContains
191 (JNIEnv *env, jobject obj, jstring keyObj)
192 {
193     LOGD("contains");
194     EXPECT_RET(keyObj, "Key is null.", false);
195     EXPECT_RET(hasNativeHandle(env, obj), "no native handle.", false);
196
197     auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
198     VERIFY_NO_EXC_RET_DEF(env);
199     const auto& key = toStdString(env, keyObj);
200     VERIFY_NO_EXC_RET_DEF(env);
201     return attrs.contains(key);
202 }
203
204 JNIEXPORT void JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeAddKeys
205 (JNIEnv *env, jobject obj, jstring setObj)
206 {
207     LOGD("addKeys");
208     EXPECT(setObj, "set is null.");
209     EXPECT(hasNativeHandle(env, obj), "no native handle.");
210
211     auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
212     VERIFY_NO_EXC(env);
213
214     for (const auto& keyValue : attrs)
215     {
216         JavaLocalString localObj{ env, env->NewStringUTF(keyValue.key().c_str()) };
217         VERIFY_NO_EXC(env);
218
219         invoke_Collection_add(env, setObj, localObj);
220         VERIFY_NO_EXC(env);
221     }
222 }
223
224 JNIEXPORT jobject JNICALL Java_org_iotivity_service_RcsResourceAttributes_nativeExtract
225 (JNIEnv* env, jobject obj, jstring keyObj)
226 {
227     LOGD("extract");
228     EXPECT_RET_DEF(keyObj, "Key is null.");
229     EXPECT_RET_DEF(hasNativeHandle(env, obj), "no native handle.");
230
231     auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
232     VERIFY_NO_EXC_RET_DEF(env);
233
234     const auto& key = toStdString(env, keyObj);
235     VERIFY_NO_EXC_RET_DEF(env);
236
237     EXPECT_RET_DEF(attrs.contains(key), "no matched value");
238
239     jobject valueObj = newRCSValueObject(env, attrs[key]);
240     VERIFY_NO_EXC_RET_DEF(env);
241
242     attrs.erase(key);
243     if (attrs.empty()) releaseNativeHandle(env, obj);
244     return valueObj;
245 }
246
247 JNIEXPORT void JNICALL
248 Java_org_iotivity_service_RcsResourceAttributes_nativeExtractAll
249 (JNIEnv* env, jobject obj, jobject mapObj)
250 {
251     LOGD("extractAll");
252     EXPECT(mapObj, "Map is null.");
253     EXPECT(hasNativeHandle(env, obj), "no native handle.");
254
255     auto& attrs = getNativeHandleAs< RCSResourceAttributes >(env, obj);
256     VERIFY_NO_EXC(env);
257
258     for (const auto& p : attrs) {
259         JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
260         VERIFY_NO_EXC(env);
261
262         JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
263         VERIFY_NO_EXC(env);
264
265         invoke_Map_put(env, mapObj, keyObj, valueObj);
266         VERIFY_NO_EXC(env);
267     }
268
269     attrs.clear();
270     releaseNativeHandle(env, obj);
271 }
272