Fix a logic to filter out an exception in createRemoteEnrollee API for Android
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / jni / JniEasySetup.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 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 #include <memory>
21 #include <vector>
22
23 #include "OCPlatform.h"
24 #include "OCResource.h"
25 #include "octypes.h"
26 #include "ESRichCommon.h"
27
28 #include "JniOcResource.h"
29 #include "JniEasySetup.h"
30
31 using namespace OC;
32 using namespace OIC::Service;
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 JNIEXPORT jobject JNICALL
38 Java_org_iotivity_service_easysetup_mediator_EasySetup_nativeCreateRemoteEnrollee
39 (JNIEnv *env, jobject thiz, jobject jResource)
40 {
41     ES_LOGI("JniEasySetup::nativeCreateRemoteEnrollee enter");
42
43     std::shared_ptr<RemoteEnrollee> nativeRemoteEnrollee;
44     jobject jRemoteEnrollee;
45
46     JniOcResource* jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
47
48     if (!jniOcResource)
49     {
50         ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee getJniOcResourcePtr returns nullptr.");
51         return nullptr;
52     }
53
54     try
55     {
56         nativeRemoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(jniOcResource->getOCResource());
57         if (!nativeRemoteEnrollee)
58         {
59             ES_LOGE("Failed to create RemoteEnrollee object.");
60             return NULL;
61         }
62
63         //create the java object
64         jRemoteEnrollee = env->NewObject(g_cls_RemoteEnrollee, g_mid_RemoteEnrollee_ctor);
65         if (!jRemoteEnrollee)
66         {
67             ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee Unable to create the java object");
68             return NULL;
69         }
70         JniRemoteEnrollee *jniRemoteEnrollee = new JniRemoteEnrollee(nativeRemoteEnrollee);
71         ESSetHandle<JniRemoteEnrollee>(env, jRemoteEnrollee, jniRemoteEnrollee);
72     }
73     catch (ESBadRequestException exception)
74     {
75         ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee Unable to create the Native EnrolleeDevice");
76         //throw the exception to java layer
77         throwESException( env,  exception.what());
78     }
79
80     ES_LOGI("JniEasySetup::nativeCreateRemoteEnrollee exit");
81
82     return jRemoteEnrollee;
83 }
84 #ifdef __cplusplus
85 }
86 #endif
87