Simulator:Fix for static analyzer reported issues
[iotivity.git] / service / simulator / java / jni / simulator_remote_resource_jni.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 "simulator_resource_model_jni.h"
22 #include "simulator_resource_model_schema_jni.h"
23 #include "simulator_exceptions_jni.h"
24 #include "simulator_utils_jni.h"
25 #include "jni_sharedobject_holder.h"
26 #include "jni_listener_holder.h"
27 #include "jni_queryparam.h"
28 #include "jni_string.h"
29 #include "jni_vector.h"
30 #include "jni_map.h"
31
32 #include "simulator_remote_resource.h"
33
34 #define VALIDATE_OBJECT(ENV, OBJECT) if (!OBJECT){ThrowBadObjectException(ENV, "No corresponsing native object!"); return;}
35 #define VALIDATE_OBJECT_RET(ENV, OBJECT, RET) if (!OBJECT){ThrowBadObjectException(ENV, "No corresponsing native object!"); return RET;}
36
37 static SimulatorRemoteResourceSP simulatorRemoteResourceToCpp(
38     JNIEnv *env, jobject object)
39 {
40     JniSharedObjectHolder<SimulatorRemoteResource> *jniResource =
41         getHandle<JniSharedObjectHolder<SimulatorRemoteResource>>(env, object);
42     if (jniResource)
43         return jniResource->get();
44     return nullptr;
45 }
46
47 static void onObserveCallback(jobject listener, const std::string &uid, SimulatorResult /*result*/,
48                               const SimulatorResourceModel &representation, const int seq)
49 {
50     JNIEnv *env = GetEnv();
51     if (!env)
52         return;
53
54     jclass listenerCls = env->GetObjectClass(listener);
55     jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onObserveNotification",
56                                  "(Ljava/lang/String;Lorg/oic/simulator/SimulatorResourceModel;I)V");
57
58     jobject jResModel = SimulatorResourceModelToJava(env, representation);
59     jstring jUid = env->NewStringUTF(uid.c_str());
60     env->CallVoidMethod(listener, listenerMethodId, jUid, jResModel, seq);
61     ReleaseEnv();
62 }
63
64 static void onGetCallback(jobject listener, const std::string &uid, SimulatorResult result,
65                           const SimulatorResourceModel &representation)
66 {
67     JNIEnv *env = GetEnv();
68     if (!env)
69         return;
70
71     jclass listenerCls = env->GetObjectClass(listener);
72     jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onGetResponse",
73                                  "(Ljava/lang/String;Lorg/oic/simulator/SimulatorResult;Lorg/oic/simulator/SimulatorResourceModel;)V");
74
75     jobject jResModel = SimulatorResourceModelToJava(env, representation);
76     jstring jUid = env->NewStringUTF(uid.c_str());
77     env->CallVoidMethod(listener, listenerMethodId, jUid,
78                         SimulatorResultToJava(env, result), jResModel);
79
80     ReleaseEnv();
81 }
82
83 static void onPutCallback(jobject listener, const std::string &uid, SimulatorResult result,
84                           const SimulatorResourceModel &representation)
85 {
86     JNIEnv *env = GetEnv();
87     if (!env)
88         return;
89
90     jclass listenerCls = env->GetObjectClass(listener);
91     jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onPutResponse",
92                                  "(Ljava/lang/String;Lorg/oic/simulator/SimulatorResult;Lorg/oic/simulator/SimulatorResourceModel;)V");
93
94     jobject jResModel = SimulatorResourceModelToJava(env, representation);
95     jstring jUid = env->NewStringUTF(uid.c_str());
96     env->CallVoidMethod(listener, listenerMethodId, jUid,
97                         SimulatorResultToJava(env, result), jResModel);
98
99     ReleaseEnv();
100 }
101
102 static void onPostCallback(jobject listener, const std::string &uid, SimulatorResult result,
103                            const SimulatorResourceModel &representation)
104 {
105     JNIEnv *env = GetEnv();
106     if (!env)
107         return;
108
109     jclass listenerCls = env->GetObjectClass(listener);
110     jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onPostResponse",
111                                  "(Ljava/lang/String;Lorg/oic/simulator/SimulatorResult;Lorg/oic/simulator/SimulatorResourceModel;)V");
112
113     jobject jResModel = SimulatorResourceModelToJava(env, representation);
114     jstring jUid = env->NewStringUTF(uid.c_str());
115     env->CallVoidMethod(listener, listenerMethodId, jUid,
116                         SimulatorResultToJava(env, result), jResModel);
117
118     ReleaseEnv();
119 }
120
121 static void onVerificationCallback(jobject listener, const std::string &uid, int id,
122                                    OperationState opState)
123 {
124     JNIEnv *env = GetEnv();
125     if (!env)
126         return;
127
128     jclass listenerCls = env->GetObjectClass(listener);
129     jmethodID listenerMethodId = nullptr;
130     if (OP_START == opState)
131     {
132         listenerMethodId = env->GetMethodID(listenerCls, "onVerificationStarted", "(Ljava/lang/String;I)V");
133     }
134     else if (OP_COMPLETE == opState)
135     {
136         listenerMethodId = env->GetMethodID(listenerCls, "onVerificationCompleted",
137                                             "(Ljava/lang/String;I)V");
138     }
139     else if (OP_ABORT == opState)
140     {
141         listenerMethodId = env->GetMethodID(listenerCls, "onVerificationAborted", "(Ljava/lang/String;I)V");
142     }
143
144     // Invoke java callback method
145     if (nullptr != listenerMethodId)
146     {
147         jstring jUid = env->NewStringUTF(uid.c_str());
148         env->CallVoidMethod(listener, listenerMethodId, jUid, id);
149     }
150
151     if (OP_COMPLETE == opState || OP_ABORT == opState)
152         env->DeleteGlobalRef(listener);
153     ReleaseEnv();
154 }
155
156 class JniRequestType
157 {
158     public:
159         static jobject toJava(JNIEnv *env, RequestType requestType)
160         {
161             switch (requestType)
162             {
163                 case RequestType::RQ_TYPE_GET:
164                     {
165                         static jfieldID fieldID = env->GetStaticFieldID(gSimulatorClassRefs.requestTypeCls,
166                                                   "GET",
167                                                   "Lorg/oic/simulator/client/SimulatorRemoteResource$RequestType;");
168                         return env->GetStaticObjectField(gSimulatorClassRefs.requestTypeCls, fieldID);
169                     }
170
171                 case RequestType::RQ_TYPE_PUT:
172                     {
173                         static jfieldID fieldID = env->GetStaticFieldID(gSimulatorClassRefs.requestTypeCls,
174                                                   "PUT",
175                                                   "Lorg/oic/simulator/client/SimulatorRemoteResource$RequestType;");
176                         return env->GetStaticObjectField(gSimulatorClassRefs.requestTypeCls, fieldID);
177                     }
178
179                 case RequestType::RQ_TYPE_POST:
180                     {
181                         static jfieldID fieldID = env->GetStaticFieldID(gSimulatorClassRefs.requestTypeCls,
182                                                   "POST",
183                                                   "Lorg/oic/simulator/client/SimulatorRemoteResource$RequestType;");
184                         return env->GetStaticObjectField(gSimulatorClassRefs.requestTypeCls, fieldID);
185                     }
186
187                 case RequestType::RQ_TYPE_DELETE:
188                     {
189                         static jfieldID fieldID = env->GetStaticFieldID(gSimulatorClassRefs.requestTypeCls,
190                                                   "DELETE",
191                                                   "Lorg/oic/simulator/client/SimulatorRemoteResource$RequestType;");
192                         return env->GetStaticObjectField(gSimulatorClassRefs.requestTypeCls, fieldID);
193                     }
194
195                 default:
196                     break;
197             }
198
199             static jfieldID fieldID = env->GetStaticFieldID(gSimulatorClassRefs.requestTypeCls,
200                                       "UKNOWN",
201                                       "Lorg/oic/simulator/client/SimulatorRemoteResource$RequestType;");
202             return env->GetStaticObjectField(gSimulatorClassRefs.requestTypeCls, fieldID);
203         }
204
205         static RequestType toCpp(JNIEnv *env, jobject jRequestType)
206         {
207             static jmethodID ordinalMID = env->GetMethodID(
208                                               gSimulatorClassRefs.requestTypeCls, "ordinal", "()I");
209             int ordinal = env->CallIntMethod(jRequestType, ordinalMID);
210             return RequestType(ordinal);
211         }
212 };
213
214 class JniSimulatorRequestModel
215 {
216     public:
217         static jobject toJava(JNIEnv *env, const SimulatorRequestModel &requestModel)
218         {
219             // Convert RequestType
220             jobject jRequestType = JniRequestType::toJava(env, requestModel.getType());
221
222             // Convert Query Parameters
223             JniMap jQueryParams(env);
224             for (auto &qpEntry : requestModel.getQueryParams())
225             {
226                 jstring jQueryParamName = env->NewStringUTF((qpEntry.first).c_str());
227                 jobject jQueryParamValues = JniVector(env).toJava(qpEntry.second);
228
229                 jQueryParams.put(jQueryParamName, jQueryParamValues);
230             }
231
232             // Convert Request body model
233             jobject jRequestBodyModel  =
234                 AttributePropertyToJava(env, requestModel.getRequestBodySchema());
235
236             // Create Java SimulatorResourceModel object
237             static jmethodID simulatorRequestModelCtor = env->GetMethodID(
238                         gSimulatorClassRefs.simulatorRequestModelCls, "<init>",
239                         "(Lorg/oic/simulator/client/SimulatorRemoteResource$RequestType;"
240                         "Ljava/util/Map;Lorg/oic/simulator/ModelProperty;)V");
241
242             return env->NewObject(gSimulatorClassRefs.simulatorRequestModelCls,
243                                   simulatorRequestModelCtor, jRequestType,
244                                   jQueryParams.get(), jRequestBodyModel);
245         }
246 };
247
248 #ifdef __cplusplus
249 extern "C" {
250 #endif
251
252 JNIEXPORT void JNICALL
253 Java_org_oic_simulator_client_SimulatorRemoteResource_nativeGet
254 (JNIEnv *env, jobject object, jstring jResInterface, jobject jQueryParams, jobject jListener)
255 {
256     VALIDATE_CALLBACK(env, jListener)
257
258     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
259     VALIDATE_OBJECT(env, resource)
260
261     try
262     {
263         JniString jniInterface(env, jResInterface);
264         std::map<std::string, std::string> queryParams =
265             JniQueryParameter(env).toCpp(jQueryParams);
266
267         SimulatorRemoteResource::ResponseCallback callback =  std::bind([](
268                     const std::string & uid, SimulatorResult result,
269                     const SimulatorResourceModel & resModel,
270                     const std::shared_ptr<JniListenerHolder> &listenerRef)
271         {
272             onGetCallback(listenerRef->get(), uid, result, resModel);
273         }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
274         JniListenerHolder::create(env, jListener));
275
276         resource->get(jniInterface.get(), queryParams, callback);
277     }
278     catch (InvalidArgsException &e)
279     {
280         ThrowInvalidArgsException(env, e.code(), e.what());
281     }
282     catch (NoSupportException &e)
283     {
284         ThrowNoSupportException(env, e.what());
285     }
286     catch (SimulatorException &e)
287     {
288         ThrowSimulatorException(env, e.code(), e.what());
289     }
290 }
291
292 JNIEXPORT void JNICALL
293 Java_org_oic_simulator_client_SimulatorRemoteResource_nativePut
294 (JNIEnv *env, jobject object, jstring jResInterface, jobject jQueryParams,
295  jobject jRepresentation, jobject jListener)
296 {
297     VALIDATE_CALLBACK(env, jListener)
298
299     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
300     VALIDATE_OBJECT(env, resource)
301
302     try
303     {
304         JniString jniInterface(env, jResInterface);
305         std::map<std::string, std::string> queryParams =
306             JniQueryParameter(env).toCpp(jQueryParams);
307
308         SimulatorResourceModel resModel;
309         SimulatorResourceModelToCpp(env, jRepresentation, resModel);
310
311         SimulatorRemoteResource::ResponseCallback callback =  std::bind([](
312                     const std::string & uid, SimulatorResult result,
313                     const SimulatorResourceModel & resModel,
314                     const std::shared_ptr<JniListenerHolder> &listenerRef)
315         {
316             onPutCallback(listenerRef->get(), uid, result, resModel);
317         }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
318         JniListenerHolder::create(env, jListener));
319
320         resource->put(jniInterface.get(), queryParams, resModel, callback);
321     }
322     catch (InvalidArgsException &e)
323     {
324         ThrowInvalidArgsException(env, e.code(), e.what());
325     }
326     catch (NoSupportException &e)
327     {
328         ThrowNoSupportException(env, e.what());
329     }
330     catch (SimulatorException &e)
331     {
332         ThrowSimulatorException(env, e.code(), e.what());
333     }
334 }
335
336 JNIEXPORT void JNICALL
337 Java_org_oic_simulator_client_SimulatorRemoteResource_nativePost
338 (JNIEnv *env, jobject object, jstring jResInterface, jobject jQueryParams,
339  jobject jRepresentation, jobject jListener)
340 {
341     VALIDATE_CALLBACK(env, jListener)
342
343     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
344     VALIDATE_OBJECT(env, resource)
345
346     try
347     {
348         JniString jniInterface(env, jResInterface);
349         std::map<std::string, std::string> queryParams =
350             JniQueryParameter(env).toCpp(jQueryParams);
351
352         SimulatorResourceModel resModel;
353         SimulatorResourceModelToCpp(env, jRepresentation, resModel);
354
355         SimulatorRemoteResource::ResponseCallback callback =  std::bind([](
356                     const std::string & uid, SimulatorResult result,
357                     const SimulatorResourceModel & resModel,
358                     const std::shared_ptr<JniListenerHolder> &listenerRef)
359         {
360             onPostCallback(listenerRef->get(), uid, result, resModel);
361         }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
362         JniListenerHolder::create(env, jListener));
363
364         resource->post(jniInterface.get(), queryParams, resModel, callback);
365     }
366     catch (InvalidArgsException &e)
367     {
368         ThrowInvalidArgsException(env, e.code(), e.what());
369     }
370     catch (NoSupportException &e)
371     {
372         ThrowNoSupportException(env, e.what());
373     }
374     catch (SimulatorException &e)
375     {
376         ThrowSimulatorException(env, e.code(), e.what());
377     }
378 }
379
380 JNIEXPORT jobject JNICALL
381 Java_org_oic_simulator_client_SimulatorRemoteResource_nativeSetConfigInfo
382 (JNIEnv *env, jobject object, jstring jConfigPath)
383 {
384     VALIDATE_INPUT_RET(env, !jConfigPath, "Path is null!", nullptr)
385
386     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
387     VALIDATE_OBJECT_RET(env, resource, nullptr)
388
389     try
390     {
391         JniString jniConfigPath(env, jConfigPath);
392         std::map<RequestType, SimulatorRequestModel> requestModels =
393             resource->configure(jniConfigPath.get());
394
395         JniMap jRequestModelsMap(env);
396
397         // Add entry to map
398         for (auto &requestModelEntry : requestModels)
399         {
400             jobject jRequestType = JniRequestType::toJava(env, requestModelEntry.first);
401             jobject jRequestModel = JniSimulatorRequestModel::toJava(env, requestModelEntry.second);
402
403             jRequestModelsMap.put(jRequestType, jRequestModel);
404         }
405
406         return jRequestModelsMap.get();
407     }
408     catch (InvalidArgsException &e)
409     {
410         ThrowInvalidArgsException(env, e.code(), e.what());
411     }
412
413     return nullptr;
414 }
415
416 JNIEXPORT void JNICALL
417 Java_org_oic_simulator_client_SimulatorRemoteResource_nativeStartObserve
418 (JNIEnv *env, jobject object, jobject jQueryParams, jobject jListener)
419 {
420     VALIDATE_CALLBACK(env, jListener)
421
422     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
423     VALIDATE_OBJECT(env, resource)
424
425     try
426     {
427         std::map<std::string, std::string> queryParams =
428             JniQueryParameter(env).toCpp(jQueryParams);
429
430         SimulatorRemoteResource::ObserveNotificationCallback callback =  std::bind([](
431                     const std::string & uid, SimulatorResult result,
432                     const SimulatorResourceModel & representation, const int seq,
433                     const std::shared_ptr<JniListenerHolder> &listenerRef)
434         {
435             onObserveCallback(listenerRef->get(), uid, result, representation, seq);
436         }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
437         std::placeholders::_4, JniListenerHolder::create(env, jListener));
438
439         resource->observe(ObserveType::OBSERVE, queryParams, callback);
440     }
441     catch (InvalidArgsException &e)
442     {
443         ThrowInvalidArgsException(env, e.code(), e.what());
444     }
445     catch (SimulatorException &e)
446     {
447         ThrowSimulatorException(env, e.code(), e.what());
448     }
449 }
450
451 JNIEXPORT void JNICALL
452 Java_org_oic_simulator_client_SimulatorRemoteResource_nativeStopObserve
453 (JNIEnv *env, jobject object)
454 {
455     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
456     VALIDATE_OBJECT(env, resource)
457
458     try
459     {
460         resource->cancelObserve();
461     }
462     catch (SimulatorException &e)
463     {
464         ThrowSimulatorException(env, e.code(), e.what());
465     }
466 }
467
468 JNIEXPORT jint JNICALL
469 Java_org_oic_simulator_client_SimulatorRemoteResource_nativeStartAutoRequesting
470 (JNIEnv *env, jobject object, jobject jRequestType, jobject jListener)
471 {
472     VALIDATE_CALLBACK_RET(env, jListener, -1)
473
474     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
475     VALIDATE_OBJECT_RET(env, resource, -1)
476
477     try
478     {
479         RequestType type = JniRequestType::toCpp(env, jRequestType);
480         SimulatorRemoteResource::AutoRequestGenerationCallback callback =  std::bind([](
481                     const std::string & uid, int id, OperationState opState,
482                     const std::shared_ptr<JniListenerHolder> &listenerRef)
483         {
484             onVerificationCallback(listenerRef->get(), uid, id, opState);
485         }, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
486         JniListenerHolder::create(env, jListener));
487
488         return resource->startAutoRequesting(type, callback);
489     }
490     catch (InvalidArgsException &e)
491     {
492         ThrowInvalidArgsException(env, e.code(), e.what());
493     }
494     catch (NoSupportException &e)
495     {
496         ThrowNoSupportException(env, e.what());
497     }
498     catch (OperationInProgressException &e)
499     {
500         ThrowOperationInProgressException(env, e.what());
501     }
502     catch (SimulatorException &e)
503     {
504         ThrowSimulatorException(env, e.code(), e.what());
505     }
506
507     return -1;
508 }
509
510 JNIEXPORT void JNICALL
511 Java_org_oic_simulator_client_SimulatorRemoteResource_nativeStopAutoRequesting
512 (JNIEnv *env, jobject object, jint id)
513 {
514     SimulatorRemoteResourceSP resource = simulatorRemoteResourceToCpp(env, object);
515     VALIDATE_OBJECT(env, resource)
516
517     try
518     {
519         resource->stopAutoRequesting(id);
520     }
521     catch (InvalidArgsException &e)
522     {
523         ThrowInvalidArgsException(env, e.code(), e.what());
524     }
525     catch (NoSupportException &e)
526     {
527         ThrowNoSupportException(env, e.what());
528     }
529 }
530
531 JNIEXPORT void JNICALL
532 Java_org_oic_simulator_client_SimulatorRemoteResource_nativeDispose
533 (JNIEnv *env, jobject object)
534 {
535     JniSharedObjectHolder<SimulatorRemoteResource> *resource =
536         getHandle<JniSharedObjectHolder<SimulatorRemoteResource>>(env, object);
537     delete resource;
538 }
539
540 #ifdef __cplusplus
541 }
542 #endif