/fineract-provider/src/main/java/org/apache/fineract/infrastructure/configuration/service/ExternalServicesPropertiesReadPlatformServiceImpl.java

https://gitlab.com/skylabase/incubator-fineract · Java · 150 lines · 110 code · 19 blank · 21 comment · 20 complexity · 543cbc303eb9265e814400882c3b6d0e MD5 · raw file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.fineract.infrastructure.configuration.service;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.util.Collection;
  23. import org.apache.fineract.infrastructure.configuration.data.ExternalServicesPropertiesData;
  24. import org.apache.fineract.infrastructure.configuration.data.S3CredentialsData;
  25. import org.apache.fineract.infrastructure.configuration.data.SMTPCredentialsData;
  26. import org.apache.fineract.infrastructure.configuration.exception.ExternalServiceConfigurationNotFoundException;
  27. import org.apache.fineract.infrastructure.core.service.RoutingDataSource;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.dao.DataAccessException;
  30. import org.springframework.jdbc.core.JdbcTemplate;
  31. import org.springframework.jdbc.core.ResultSetExtractor;
  32. import org.springframework.jdbc.core.RowMapper;
  33. import org.springframework.stereotype.Service;
  34. @Service
  35. public class ExternalServicesPropertiesReadPlatformServiceImpl implements ExternalServicesPropertiesReadPlatformService {
  36. private final JdbcTemplate jdbcTemplate;
  37. @Autowired
  38. public ExternalServicesPropertiesReadPlatformServiceImpl(final RoutingDataSource dataSource) {
  39. this.jdbcTemplate = new JdbcTemplate(dataSource);
  40. }
  41. private static final class S3CredentialsDataExtractor implements ResultSetExtractor<S3CredentialsData> {
  42. @Override
  43. public S3CredentialsData extractData(final ResultSet rs) throws SQLException, DataAccessException {
  44. String accessKey = null;
  45. String bucketName = null;
  46. String secretKey = null;
  47. while (rs.next()) {
  48. if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.S3_ACCESS_KEY)) {
  49. accessKey = rs.getString("value");
  50. } else if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.S3_BUCKET_NAME)) {
  51. bucketName = rs.getString("value");
  52. } else if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.S3_SECRET_KEY)) {
  53. secretKey = rs.getString("value");
  54. }
  55. }
  56. return new S3CredentialsData(bucketName, accessKey, secretKey);
  57. }
  58. }
  59. private static final class SMTPCredentialsDataExtractor implements ResultSetExtractor<SMTPCredentialsData> {
  60. @Override
  61. public SMTPCredentialsData extractData(final ResultSet rs) throws SQLException, DataAccessException {
  62. String username = null;
  63. String password = null;
  64. String host = null;
  65. String port = "25";
  66. boolean useTLS = false;
  67. while (rs.next()) {
  68. if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.SMTP_USERNAME)) {
  69. username = rs.getString("value");
  70. } else if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.SMTP_PASSWORD)) {
  71. password = rs.getString("value");
  72. } else if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.SMTP_HOST)) {
  73. host = rs.getString("value");
  74. } else if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.SMTP_PORT)) {
  75. port = rs.getString("value");
  76. } else if (rs.getString("name").equalsIgnoreCase(ExternalServicesConstants.SMTP_USE_TLS)) {
  77. useTLS = Boolean.parseBoolean(rs.getString("value"));
  78. }
  79. }
  80. return new SMTPCredentialsData(username, password, host, port, useTLS);
  81. }
  82. }
  83. private static final class ExternalServiceMapper implements RowMapper<ExternalServicesPropertiesData> {
  84. @Override
  85. public ExternalServicesPropertiesData mapRow(ResultSet rs, @SuppressWarnings("unused") int rowNum) throws SQLException {
  86. // TODO Auto-generated method stub
  87. final String name = rs.getString("name");
  88. String value = rs.getString("value");
  89. // Masking the password as we should not send the password back
  90. if (name != null && "password".equalsIgnoreCase(name)) {
  91. value = "XXXX";
  92. }
  93. return new ExternalServicesPropertiesData(name, value);
  94. }
  95. }
  96. @Override
  97. public S3CredentialsData getS3Credentials() {
  98. final ResultSetExtractor<S3CredentialsData> resultSetExtractor = new S3CredentialsDataExtractor();
  99. final String sql = "SELECT esp.name, esp.value FROM c_external_service_properties esp inner join c_external_service es on esp.external_service_id = es.id where es.name = '"
  100. + ExternalServicesConstants.S3_SERVICE_NAME + "'";
  101. final S3CredentialsData s3CredentialsData = this.jdbcTemplate.query(sql, resultSetExtractor, new Object[] {});
  102. return s3CredentialsData;
  103. }
  104. @Override
  105. public SMTPCredentialsData getSMTPCredentials() {
  106. // TODO Auto-generated method stub
  107. final ResultSetExtractor<SMTPCredentialsData> resultSetExtractor = new SMTPCredentialsDataExtractor();
  108. final String sql = "SELECT esp.name, esp.value FROM c_external_service_properties esp inner join c_external_service es on esp.external_service_id = es.id where es.name = '"
  109. + ExternalServicesConstants.SMTP_SERVICE_NAME + "'";
  110. final SMTPCredentialsData smtpCredentialsData = this.jdbcTemplate.query(sql, resultSetExtractor, new Object[] {});
  111. return smtpCredentialsData;
  112. }
  113. @Override
  114. public Collection<ExternalServicesPropertiesData> retrieveOne(String serviceName) {
  115. String serviceNameToUse = null;
  116. switch (serviceName) {
  117. case "S3":
  118. serviceNameToUse = ExternalServicesConstants.S3_SERVICE_NAME;
  119. break;
  120. case "SMTP":
  121. serviceNameToUse = ExternalServicesConstants.SMTP_SERVICE_NAME;
  122. break;
  123. default:
  124. throw new ExternalServiceConfigurationNotFoundException(serviceName);
  125. }
  126. final ExternalServiceMapper mapper = new ExternalServiceMapper();
  127. final String sql = "SELECT esp.name, esp.value FROM c_external_service_properties esp inner join c_external_service es on esp.external_service_id = es.id where es.name = '"
  128. + serviceNameToUse + "'";
  129. return this.jdbcTemplate.query(sql, mapper, new Object[] {});
  130. }
  131. }