[android] Addressed some minor SonarQube issues.
[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;
37         JNIEnv *env = GetJNIEnv(ret);
38         if (NULL == env)
39         {
40             return;
41         }
42         env->DeleteGlobalRef(m_jListener);
43         if (JNI_EDETACHED == ret)
44         {
45             g_jvm->DetachCurrentThread();
46         }
47     }
48 }
49
50 OCEntityHandlerResult JniEntityHandler::handleEntity(
51     const std::shared_ptr<OCResourceRequest> request)
52 {
53     LOGD("JniEntityHandler_handleEntity");
54     jint envRet;
55     JNIEnv *env = GetJNIEnv(envRet);
56     if (NULL == env)
57     {
58         return OC_EH_ERROR;
59     }
60
61     JniOcResourceRequest* jniResReq = new JniOcResourceRequest(request);
62     jlong reqHandle = reinterpret_cast<jlong>(jniResReq);
63     jobject jResourceRequest =
64         env->NewObject(g_cls_OcResourceRequest,
65                        g_mid_OcResourceRequest_N_ctor,
66                        reqHandle);
67     if (!jResourceRequest)
68     {
69         LOGE("Failed to create OcResourceRequest");
70         delete jniResReq;
71         if (JNI_EDETACHED == envRet)
72         {
73             g_jvm->DetachCurrentThread();
74         }
75
76         return OC_EH_ERROR;
77     }
78
79     jclass clsL = env->GetObjectClass(m_jListener);
80     if (!clsL)
81     {
82         if (JNI_EDETACHED == envRet)
83         {
84             g_jvm->DetachCurrentThread();
85         }
86
87         return OC_EH_ERROR;
88     }
89     jmethodID midL = env->GetMethodID(clsL, "handleEntity",
90         "(Lorg/iotivity/base/OcResourceRequest;)Lorg/iotivity/base/EntityHandlerResult;");
91     if (!midL)
92     {
93         if (JNI_EDETACHED == envRet)
94         {
95             g_jvm->DetachCurrentThread();
96         }
97
98         return OC_EH_ERROR;
99     }
100     jobject entityHandlerResult = env->CallObjectMethod(m_jListener, midL, jResourceRequest);
101     if (env->ExceptionCheck())
102     {
103         if (JNI_EDETACHED == envRet)
104         {
105             g_jvm->DetachCurrentThread();
106         }
107
108         return OC_EH_ERROR;
109     }
110     if (!entityHandlerResult)
111     {
112         ThrowOcException(JNI_INVALID_VALUE, "EntityHandlerResult cannot be null");
113         if (JNI_EDETACHED == envRet)
114         {
115             g_jvm->DetachCurrentThread();
116         }
117
118         return OC_EH_ERROR;
119     }
120     jclass clsResult = env->GetObjectClass(entityHandlerResult);
121     if (!clsResult)
122     {
123         if (JNI_EDETACHED == envRet)
124         {
125             g_jvm->DetachCurrentThread();
126         }
127
128         return OC_EH_ERROR;
129     }
130     jmethodID getValue_ID = env->GetMethodID(clsResult, "getValue", "()I");
131     if (!getValue_ID)
132     {
133         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
134         return OC_EH_ERROR;
135     }
136     jint jResult = env->CallIntMethod(entityHandlerResult, getValue_ID);
137     if (env->ExceptionCheck())
138     {
139         LOGE("Java exception is thrown");
140         if (JNI_EDETACHED == envRet)
141         {
142             g_jvm->DetachCurrentThread();
143         }
144
145         return OC_EH_ERROR;
146     }
147
148     if (JNI_EDETACHED == envRet)
149     {
150         g_jvm->DetachCurrentThread();
151     }
152
153     return JniUtils::getOCEntityHandlerResult(env, static_cast<int>(jResult));
154 }