From a1f12be4b7d16f825b8c0574fa76f2bc16d2b428 Mon Sep 17 00:00:00 2001
From: Abhishek Patil <abpatil@microsoft.com>
Date: Wed, 20 Jan 2021 00:19:57 -0500
Subject: [PATCH] Code refactoring for using BlobStore

---
 .../countries/StorageReaderFactoryImpl.java   | 13 ++---
 .../azure/countries/StorageReaderImpl.java    | 50 ++++---------------
 .../src/main/resources/application.properties |  1 +
 3 files changed, 17 insertions(+), 47 deletions(-)

diff --git a/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderFactoryImpl.java b/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderFactoryImpl.java
index 64ecb945c..48789eba2 100644
--- a/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderFactoryImpl.java
+++ b/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderFactoryImpl.java
@@ -17,20 +17,17 @@ package org.opengroup.osdu.legal.azure.countries;
 import javax.inject.Inject;
 import javax.inject.Named;
 
-import org.opengroup.osdu.azure.blobstorage.IBlobContainerClientFactory;
+import org.opengroup.osdu.azure.blobstorage.BlobStore;
 import org.opengroup.osdu.core.common.model.http.DpsHeaders;
 import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
-import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
 import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
 import org.opengroup.osdu.legal.provider.interfaces.IStorageReaderFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 @Component
 public class StorageReaderFactoryImpl implements IStorageReaderFactory {
 
-    @Inject
-    private IBlobContainerClientFactory blobContainerClientFactory;
-
     @Inject
     private DpsHeaders headers;
 
@@ -38,11 +35,11 @@ public class StorageReaderFactoryImpl implements IStorageReaderFactory {
     @Named("STORAGE_CONTAINER_NAME")
     private String containerName;
 
-    @Inject
-    private JaxRsDpsLog logger;
+    @Autowired
+    private BlobStore blobStore;
 
     @Override
     public IStorageReader getReader(TenantInfo tenant, String projectRegion) {
-        return new StorageReaderImpl(tenant, projectRegion, blobContainerClientFactory.getClient(headers.getPartitionId(), containerName), logger);
+        return new StorageReaderImpl(headers.getPartitionId(), containerName, blobStore);
     }
 }
diff --git a/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderImpl.java b/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderImpl.java
index 910e34e93..7f55be34d 100644
--- a/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderImpl.java
+++ b/provider/legal-azure/src/main/java/org/opengroup/osdu/legal/azure/countries/StorageReaderImpl.java
@@ -14,57 +14,29 @@
 
 package org.opengroup.osdu.legal.azure.countries;
 
-import com.azure.storage.blob.BlobContainerClient;
-import com.azure.storage.blob.specialized.BlockBlobClient;
-import org.opengroup.osdu.core.common.model.http.AppException;
-import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
-import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
+import org.opengroup.osdu.azure.blobstorage.BlobStore;
 import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
 
-import java.io.ByteArrayOutputStream;
 import java.nio.charset.StandardCharsets;
 
 public class StorageReaderImpl implements IStorageReader {
-    private final TenantInfo tenantInfo;
-    private final String cloudRegion;
-    private BlobContainerClient blobContainerClient;
-    private JaxRsDpsLog logger;
+
+    private final BlobStore blobStore;
+
+    private final String dataPartitionId;
+    private final String containerName;
 
     private static final String fileName = "Legal_COO.json";
 
-    public StorageReaderImpl(TenantInfo tenantInfo, String cloudRegion, BlobContainerClient blobContainerClient, JaxRsDpsLog logger) {
-        this.tenantInfo = tenantInfo;
-        this.cloudRegion = cloudRegion;
-        this.blobContainerClient = blobContainerClient;
-        this.logger = logger;
+    public StorageReaderImpl(String dataPartitionId, String containerName, BlobStore blobStore) {
+        this.dataPartitionId = dataPartitionId;
+        this.containerName = containerName;
+        this.blobStore = blobStore;
     }
 
     @Override
     public byte[] readAllBytes() {
-        return  readFromBlobStorage().getBytes(StandardCharsets.UTF_8); //should return a json format of an array of Country class
+        return  blobStore.readFromStorageContainer(dataPartitionId, fileName, containerName).getBytes(StandardCharsets.UTF_8); //should return a json format of an array of Country class
     }
 
-    /**
-     * This function should be fail close. It reads the pre-configuration file for the partition. As a security requirement, we should
-     * fail the corresponding request instead of assuming the content is empty, but it should only fail close on the request level not
-     * service level meaning the service could still start and running probably for the APIs which do not need the pre-configuration file
-     */
-    public String readFromBlobStorage() {
-
-        BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient(fileName).getBlockBlobClient();
-        if (blockBlobClient.exists())
-        {
-            try (ByteArrayOutputStream downloadStream = new ByteArrayOutputStream()) {
-                blockBlobClient.download(downloadStream);
-                String content = downloadStream.toString(StandardCharsets.UTF_8.name());
-                return content;
-            } catch (Exception e) {
-                String message = String.format("read %s failed", fileName);
-                throw new AppException(500, "Server error", message);
-            }
-        } else {
-            String message = String.format("%s does not exist on partition %s", fileName, tenantInfo.getDataPartitionId());
-            throw new AppException(500, "Server error", message);
-        }
-    }
 }
\ No newline at end of file
diff --git a/provider/legal-azure/src/main/resources/application.properties b/provider/legal-azure/src/main/resources/application.properties
index f5d8e74e2..24909bf5a 100644
--- a/provider/legal-azure/src/main/resources/application.properties
+++ b/provider/legal-azure/src/main/resources/application.properties
@@ -42,6 +42,7 @@ azure.cosmosdb.legal.collection=LegalTag
 # Azure Blob Storage configuration
 azure.storage.container-name=legal-service-azure-configuration
 azure.storage.enable-https=true
+azure.blobStore.required=true
 
 # Azure Service Bus configuration
 azure.servicebus.topic-name=${servicebus_topic_name}
-- 
GitLab