fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [fineract] vorburger commented on a change in pull request #1235: FINERACT-734 Creditbureau-Integration-phase3
Date Thu, 12 Nov 2020 22:20:41 GMT

vorburger commented on a change in pull request #1235:
URL: https://github.com/apache/fineract/pull/1235#discussion_r522466052



##########
File path: fineract-provider/src/main/java/org/apache/fineract/infrastructure/creditbureau/service/ThitsaWorksCreditBureauIntegrationWritePlatformServiceImpl.java
##########
@@ -0,0 +1,420 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.creditbureau.service;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonNull;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+import org.apache.fineract.commands.domain.CommandWrapper;
+import org.apache.fineract.commands.service.CommandWrapperBuilder;
+import org.apache.fineract.infrastructure.core.api.JsonCommand;
+import org.apache.fineract.infrastructure.core.exception.PlatformDataIntegrityException;
+import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
+import org.apache.fineract.infrastructure.creditbureau.data.CreditReportData;
+import org.apache.fineract.infrastructure.creditbureau.domain.CreditBureauConfiguration;
+import org.apache.fineract.infrastructure.creditbureau.domain.CreditBureauConfigurationRepositoryWrapper;
+import org.apache.fineract.infrastructure.creditbureau.domain.CreditBureauToken;
+import org.apache.fineract.infrastructure.creditbureau.domain.TokenRepositoryWrapper;
+import org.apache.fineract.infrastructure.creditbureau.serialization.CreditBureauTokenCommandFromApiJsonDeserializer;
+import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Component
+@Service
+public class ThitsaWorksCreditBureauIntegrationWritePlatformServiceImpl implements ThitsaWorksCreditBureauIntegrationWritePlatformService
{
+
+    private final PlatformSecurityContext context;
+    private final FromJsonHelper fromApiJsonHelper;
+    private final TokenRepositoryWrapper tokenRepository;
+    private final CreditBureauConfigurationRepositoryWrapper configDataRepository;
+    private final CreditBureauTokenCommandFromApiJsonDeserializer fromApiJsonDeserializer;
+
+    @Autowired
+    public ThitsaWorksCreditBureauIntegrationWritePlatformServiceImpl(final PlatformSecurityContext
context,
+            final FromJsonHelper fromApiJsonHelper, final TokenRepositoryWrapper tokenRepository,
+            final CreditBureauConfigurationRepositoryWrapper configDataRepository,
+            final CreditBureauTokenCommandFromApiJsonDeserializer fromApiJsonDeserializer)
{
+        this.context = context;
+        this.tokenRepository = tokenRepository;
+        this.configDataRepository = configDataRepository;
+        this.fromApiJsonHelper = fromApiJsonHelper;
+        this.fromApiJsonDeserializer = fromApiJsonDeserializer;
+    }
+
+    private static final Logger LOG = LoggerFactory.getLogger(ThitsaWorksCreditBureauIntegrationWritePlatformServiceImpl.class);
+
+    @Transactional
+    @Override
+    public String httpConnectionMethod(String process, String nrcID, String userName, String
password, String subscriptionKey,
+            String subscriptionId, String url, String token, Long uniqueID, File report)
{
+
+        String result = null;
+
+        try {
+            String post_params = null;
+            HttpURLConnection httpConnection = this.process(process, nrcID, userName, password,
subscriptionKey, subscriptionId, url, token,
+                    uniqueID, report);
+
+            if (process.equals("token")) {
+                post_params = "" + "BODY=x-www-form-urlencoded&\r" + "grant_type=password&\r"
+ "userName=" + userName + "&\r" + "password="
+                        + password + "&\r";
+            } else if (process.equals("NRC")) {
+
+                post_params = "BODY=x-www-form-urlencoded&nrc=" + nrcID + "&";
+            } else if (process.equals("UploadCreditReport")) {
+
+                post_params = "BODY=formdata&" + report + "&" + "userName=" + userName
+ "&";
+            }
+
+            // token header not used when creating token i.e. when token will be null
+            if (token != null) {
+                httpConnection.setRequestProperty("Authorization", "Bearer " + token);
+            }
+
+            // this set is required only for (POST METHOD)- fetching uniqueID from NRC/Creating
Token/Add Credit report
+            if (process.equals("NRC") || process.equals("token") || process.equals("UploadCreditReport"))
{
+                httpConnection.setDoOutput(true);
+                OutputStream os = httpConnection.getOutputStream();
+                os.write(post_params.getBytes(StandardCharsets.UTF_8));
+                os.flush();
+                os.close();
+            }
+
+            result = this.httpResponse(httpConnection);
+
+        } catch (IOException e) {
+            LOG.error("Error occured.", e);
+        }
+        return result;
+
+    }
+
+    private HttpURLConnection process(String process, String nrcID, String userName, String
password, String subscriptionKey,

Review comment:
       It would be even better not to code at this low level, but use a higher level ready
made HTTP Client library. Do you know of https://square.github.io/okhttp/ and https://square.github.io/retrofit/?
Both are already used in Fineract, search the existing code for examples how to use it...




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message