Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniEntityHandler.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Corporation.
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 "JniEntityHandler.h"
22 #include "JniOcResourceRequest.h"
23 #include "JniOcResourceResponse.h"
24 #include "JniUtils.h"
25
26 JniEntityHandler::JniEntityHandler(JNIEnv *env, jobject entityHandler)
27 {
28     m_jListener = env->NewGlobalRef(entityHandler);
29 }
30
31 JniEntityHandler::~JniEntityHandler()
32 {
33     LOGD("~JniEntityHandler");
34     if (m_jListener)
35     {
36         jint ret = JNI_ERR;
37         JNIEnv *env = GetJNIEnv(ret);
38         if (nullptr == env)
39         {
40             return;
41         }
42
43         env->DeleteGlobalRef(m_jListener);
44         m_jListener = nullptr;
45
46         if (JNI_EDETACHED == ret)
47         {
48             g_jvm->DetachCurrentThread();
49         }
50     }
51 }
52
53 OCEntityHandlerResult JniEntityHandler::handleEntity(
54     const std::shared_ptr<OCResourceRequest> request)
55 {
56     LOGD("JniEntityHandler_handleEntity");
57     jint envRet = JNI_ERR;
58     JNIEnv *env = GetJNIEnv(envRet);
59     if (nullptr == env)
60     {
61         return OC_EH_ERROR;
62     }
63
64     JniOcResourceRequest* jniResReq = new JniOcResourceRequest(request);
65     jlong reqHandle = reinterpret_cast<jlong>(jniResReq);
66     jobject jResourceRequest =
67         env->NewObject(g_cls_OcResourceRequest,
68                        g_mid_OcResourceRequest_N_ctor,
69                        reqHandle);
70     if (!jResourceRequest)
71     {
72         LOGE("Failed to create OcResourceRequest");
73         delete jniResReq;
74         if (JNI_EDETACHED == envRet)
75         {
76             g_jvm->DetachCurrentThread();
77         }
78
79         return OC_EH_ERROR;
80     }
81
82     jclass clsL = env->GetObjectClass(m_jListener);
83     if (!clsL)
84     {
85         if (JNI_EDETACHED == envRet)
86         {
87             g_jvm->DetachCurrentThread();
88         }
89
90         return OC_EH_ERROR;
91     }
92     jmethodID midL = env->GetMethodID(clsL, "handleEntity",
93         "(Lorg/iotivity/base/OcResourceRequest;)Lorg/iotivity/base/EntityHandlerResult;");
94     if (!midL)
95     {
96         if (JNI_EDETACHED == envRet)
97         {
98             g_jvm->DetachCurrentThread();
99         }
100
101         return OC_EH_ERROR;
102     }
103     jobject entityHandlerResult = env->CallObjectMethod(m_jListener, midL, jResourceRequest);
104     if (env->ExceptionCheck())
105     {
106         if (JNI_EDETACHED == envRet)
107         {
108             g_jvm->DetachCurrentThread();
109         }
110
111         return OC_EH_ERROR;
112     }
113     if (!entityHandlerResult)
114     {
115         ThrowOcException(JNI_INVALID_VALUE, "EntityHandlerResult cannot be null");
116         if (JNI_EDETACHED == envRet)
117         {
118             g_jvm->DetachCurrentThread();
119         }
120
121         return OC_EH_ERROR;
122     }
123     jclass clsResult = env->GetObjectClass(entityHandlerResult);
124     if (!clsResult)
125     {
126         if (JNI_EDETACHED == envRet)
127         {
128             g_jvm->DetachCurrentThread();
129         }
130
131         return OC_EH_ERROR;
132     }
133     jmethodID getValue_ID = env->GetMethodID(clsResult, "getValue", "()I");
134     if (!getValue_ID)
135     {
136         if (JNI_EDETACHED == envRet)
137         {
138             g_jvm->DetachCurrentThread();
139         }
140         return OC_EH_ERROR;
141     }
142     jint jResult = env->CallIntMethod(entityHandlerResult, getValue_ID);
143     if (env->ExceptionCheck())
144     {
145         LOGE("Java exception is thrown");
146         if (JNI_EDETACHED == envRet)
147         {
148             g_jvm->DetachCurrentThread();
149         }
150
151         return OC_EH_ERROR;
152     }
153
154     if (JNI_EDETACHED == envRet)
155     {
156         g_jvm->DetachCurrentThread();
157     }
158
159     return JniUtils::getOCEntityHandlerResult(env, static_cast<int>(jResult));
160 }