PageRenderTime 59ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/client-async/api/client-async-api.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 243 lines | 186 code | 34 blank | 23 comment | 37 complexity | 52ba89dee78bd61225aa31817e01f3f4 MD5 | raw file
  1. /*
  2. * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file src/client-async/api/client-async-api.cpp
  18. * @author Marcin Niesluchowski <m.niesluchow@samsung.com>
  19. * @author Zofia Abramowska <z.abramowska@samsung.com>
  20. * @author Oskar Świtalski <o.switalski@samsung.com>
  21. * @version 1.0
  22. * @brief Implementation of external libcynara-client-async API
  23. */
  24. #include <new>
  25. #include <common.h>
  26. #include <exceptions/TryCatch.h>
  27. #include <log/log.h>
  28. #include <types/string_validation.h>
  29. #include <api/ApiInterface.h>
  30. #include <configuration/Configuration.h>
  31. #include <cynara-client-async.h>
  32. #include <logic/Logic.h>
  33. struct cynara_async {
  34. Cynara::ApiInterface *impl;
  35. cynara_async(Cynara::ApiInterface *_impl) : impl(_impl) {
  36. }
  37. ~cynara_async() {
  38. delete impl;
  39. }
  40. };
  41. struct cynara_async_configuration {
  42. Cynara::Configuration* impl;
  43. cynara_async_configuration(Cynara::Configuration *_impl) : impl(_impl) {}
  44. ~cynara_async_configuration() {
  45. delete impl;
  46. }
  47. };
  48. CYNARA_API
  49. int cynara_async_configuration_create(cynara_async_configuration **pp_conf) {
  50. if (!pp_conf)
  51. return CYNARA_API_INVALID_PARAM;
  52. return Cynara::tryCatch([&]() {
  53. Cynara::ConfigurationUniquePtr ptr(new Cynara::Configuration());
  54. *pp_conf = new cynara_async_configuration(ptr.get());
  55. ptr.release();
  56. LOGD("Cynara configuration initialized");
  57. return CYNARA_API_SUCCESS;
  58. });
  59. }
  60. CYNARA_API
  61. void cynara_async_configuration_destroy(cynara_async_configuration *p_conf) {
  62. delete p_conf;
  63. }
  64. CYNARA_API
  65. int cynara_async_configuration_set_cache_size(cynara_async_configuration *p_conf,
  66. size_t cache_size) {
  67. if (!p_conf || !p_conf->impl)
  68. return CYNARA_API_INVALID_PARAM;
  69. return Cynara::tryCatch([&]() {
  70. p_conf->impl->setCacheSize(cache_size);
  71. return CYNARA_API_SUCCESS;
  72. });
  73. }
  74. CYNARA_API
  75. int cynara_async_initialize(cynara_async **pp_cynara,
  76. const cynara_async_configuration *p_conf,
  77. cynara_status_callback callback, void *user_status_data) {
  78. if (!pp_cynara)
  79. return CYNARA_API_INVALID_PARAM;
  80. init_log();
  81. return Cynara::tryCatch([&]() {
  82. Cynara::LogicUniquePtr ptr;
  83. if (p_conf && p_conf->impl) {
  84. ptr.reset(new Cynara::Logic(callback, user_status_data, *(p_conf->impl)));
  85. } else {
  86. ptr.reset(new Cynara::Logic(callback, user_status_data));
  87. }
  88. *pp_cynara = new cynara_async(ptr.get());
  89. ptr.release();
  90. LOGD("Cynara client async initialized");
  91. return CYNARA_API_SUCCESS;
  92. });
  93. }
  94. CYNARA_API
  95. void cynara_async_finish(cynara_async *p_cynara) {
  96. if (!p_cynara || !p_cynara->impl->isFinishPermitted())
  97. return;
  98. delete p_cynara;
  99. }
  100. CYNARA_API
  101. int cynara_async_check_cache(cynara_async *p_cynara, const char *client, const char *client_session,
  102. const char *user, const char *privilege) {
  103. if (!p_cynara || !p_cynara->impl)
  104. return CYNARA_API_INVALID_PARAM;
  105. if (!isStringValid(client) || !isStringValid(client_session))
  106. return CYNARA_API_INVALID_PARAM;
  107. if (!isStringValid(user) || !isStringValid(privilege))
  108. return CYNARA_API_INVALID_PARAM;
  109. return Cynara::tryCatch([&]() {
  110. std::string clientStr;
  111. std::string clientSessionStr;
  112. std::string userStr;
  113. std::string privilegeStr;
  114. try {
  115. clientStr = client;
  116. clientSessionStr = client_session;
  117. userStr = user;
  118. privilegeStr = privilege;
  119. } catch (const std::length_error &e) {
  120. LOGE("%s", e.what());
  121. return CYNARA_API_INVALID_PARAM;
  122. }
  123. return p_cynara->impl->checkCache(clientStr, clientSessionStr, userStr, privilegeStr);
  124. });
  125. }
  126. CYNARA_API
  127. int cynara_async_create_request(cynara_async *p_cynara, const char *client,
  128. const char *client_session, const char *user, const char *privilege,
  129. cynara_check_id *p_check_id, cynara_response_callback callback,
  130. void *user_response_data) {
  131. if (!p_cynara || !p_cynara->impl)
  132. return CYNARA_API_INVALID_PARAM;
  133. if (!isStringValid(client) || !isStringValid(client_session))
  134. return CYNARA_API_INVALID_PARAM;
  135. if (!isStringValid(user) || !isStringValid(privilege))
  136. return CYNARA_API_INVALID_PARAM;
  137. return Cynara::tryCatch([&]() {
  138. std::string clientStr;
  139. std::string clientSessionStr;
  140. std::string userStr;
  141. std::string privilegeStr;
  142. try {
  143. clientStr = client;
  144. clientSessionStr = client_session;
  145. userStr = user;
  146. privilegeStr = privilege;
  147. } catch (const std::length_error &e) {
  148. LOGE("%s", e.what());
  149. return CYNARA_API_INVALID_PARAM;
  150. }
  151. cynara_check_id checkId;
  152. int ret = p_cynara->impl->createCheckRequest(clientStr, clientSessionStr, userStr,
  153. privilegeStr, checkId, callback,
  154. user_response_data);
  155. if (p_check_id && ret == CYNARA_API_SUCCESS)
  156. *p_check_id = checkId;
  157. return ret;
  158. });
  159. }
  160. CYNARA_API
  161. int cynara_async_create_simple_request(cynara_async *p_cynara, const char *client,
  162. const char *client_session, const char *user,
  163. const char *privilege, cynara_check_id *p_check_id,
  164. cynara_response_callback callback, void *user_response_data){
  165. if (!p_cynara || !p_cynara->impl)
  166. return CYNARA_API_INVALID_PARAM;
  167. if (!isStringValid(client) || !isStringValid(client_session))
  168. return CYNARA_API_INVALID_PARAM;
  169. if (!isStringValid(user) || !isStringValid(privilege))
  170. return CYNARA_API_INVALID_PARAM;
  171. return Cynara::tryCatch([&]() {
  172. std::string clientStr;
  173. std::string clientSessionStr;
  174. std::string userStr;
  175. std::string privilegeStr;
  176. try {
  177. clientStr = client;
  178. clientSessionStr = client_session;
  179. userStr = user;
  180. privilegeStr = privilege;
  181. } catch (const std::length_error &e) {
  182. LOGE("%s", e.what());
  183. return CYNARA_API_INVALID_PARAM;
  184. }
  185. cynara_check_id checkId;
  186. int ret = p_cynara->impl->createSimpleRequest(clientStr, clientSessionStr, userStr,
  187. privilegeStr, checkId, callback,
  188. user_response_data);
  189. if (p_check_id && ret == CYNARA_API_SUCCESS)
  190. *p_check_id = checkId;
  191. return ret;
  192. });
  193. }
  194. CYNARA_API
  195. int cynara_async_process(cynara_async *p_cynara) {
  196. if (!p_cynara || !p_cynara->impl)
  197. return CYNARA_API_INVALID_PARAM;
  198. return Cynara::tryCatch([&]() {
  199. return p_cynara->impl->process();
  200. });
  201. }
  202. CYNARA_API
  203. int cynara_async_cancel_request(cynara_async *p_cynara, cynara_check_id check_id) {
  204. if (!p_cynara || !p_cynara->impl)
  205. return CYNARA_API_INVALID_PARAM;
  206. return Cynara::tryCatch([&]() {
  207. return p_cynara->impl->cancelRequest(check_id);
  208. });
  209. }