diff --git a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/certificate/service/CourseBatchCertificateActor.java b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/certificate/service/CourseBatchCertificateActor.java index 768f00167be8862f7ba3318732bfcbeb4773e345..3139cf0226175a843c7e53dffd8eb5a1d79a4ef7 100644 --- a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/certificate/service/CourseBatchCertificateActor.java +++ b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/certificate/service/CourseBatchCertificateActor.java @@ -14,8 +14,6 @@ import org.sunbird.actor.base.BaseActor; import org.sunbird.common.exception.ProjectCommonException; import org.sunbird.common.models.response.Response; import org.sunbird.common.models.util.JsonKey; -import org.sunbird.common.models.util.LoggerUtil; -import org.sunbird.common.models.util.ProjectLogger; import org.sunbird.common.models.util.TelemetryEnvKey; import org.sunbird.common.request.Request; import org.sunbird.common.request.RequestContext; @@ -26,6 +24,10 @@ import org.sunbird.learner.constants.CourseJsonKey; import org.sunbird.learner.util.CourseBatchUtil; import org.sunbird.learner.util.Util; +import static org.sunbird.common.models.util.JsonKey.CLOUD_STORE_BASE_PATH; +import static org.sunbird.common.models.util.JsonKey.CLOUD_STORE_BASE_PATH_PLACEHOLDER; +import static org.sunbird.common.models.util.ProjectUtil.getConfigValue; + public class CourseBatchCertificateActor extends BaseActor { private CourseBatchDao courseBatchDao = new CourseBatchDaoImpl(); @@ -100,7 +102,8 @@ public class CourseBatchCertificateActor extends BaseActor { String certName = (String) templateData.getOrDefault(JsonKey.TITLE , (String)templateDetails.getOrDefault(JsonKey.NAME, "")); template.put(JsonKey.NAME, certName); - template.put(JsonKey.URL, templateDetails.getOrDefault("artifactUrl", "")); + String templateUrl = getPlaceholderUrl(templateDetails,"artifactUrl"); + template.put(JsonKey.URL, templateUrl); template.put(JsonKey.CRITERIA, mapper.writeValueAsString(template.get(JsonKey.CRITERIA))); if (null != template.get(CourseJsonKey.ISSUER)) { template.put( @@ -118,9 +121,13 @@ public class CourseBatchCertificateActor extends BaseActor { CourseJsonKey.ISSUER, mapper.writeValueAsString(templateDetails.get(CourseJsonKey.SIGNATORY_LIST))); } if (MapUtils.isNotEmpty((Map<String,Object>)template.get(CourseJsonKey.NOTIFY_TEMPLATE))) { + // We need to change stateImgUrl in notifyTemplate + Map<String, Object> notifyData = (Map<String, Object>) template.get(CourseJsonKey.NOTIFY_TEMPLATE); + String notifyTemplateUrl = getPlaceholderUrl(notifyData,JsonKey.stateImgUrl); + notifyData.replace(JsonKey.stateImgUrl,notifyTemplateUrl); template.put( CourseJsonKey.NOTIFY_TEMPLATE, - mapper.writeValueAsString(template.get(CourseJsonKey.NOTIFY_TEMPLATE))); + mapper.writeValueAsString(notifyData)); } if (MapUtils.isNotEmpty((Map<String,Object>)template.get(CourseJsonKey.ADDITIONAL_PROPS))) { template.put( @@ -134,6 +141,17 @@ public class CourseBatchCertificateActor extends BaseActor { } } + private String getPlaceholderUrl(Map<String, Object> templateDetails, String key) { + String templateUrl = ""; + if (MapUtils.isNotEmpty(templateDetails) && templateDetails.containsKey(key)) { + // replace the actual cloud url with the template value + templateUrl = (String) templateDetails.get(key); + if (templateUrl.contains(getConfigValue(CLOUD_STORE_BASE_PATH))) + templateUrl = templateUrl.replace(getConfigValue(CLOUD_STORE_BASE_PATH), getConfigValue(CLOUD_STORE_BASE_PATH_PLACEHOLDER)); + } + return templateUrl; + } + private Map<String, Object> mapESFieldsToObject(Map<String, Object> courseBatch) { Map<String, Map<String, Object>> certificateTemplates = (Map<String, Map<String, Object>>) @@ -174,11 +192,14 @@ public class CourseBatchCertificateActor extends BaseActor { new TypeReference<HashMap<String, Object>>() { })); } - if(StringUtils.isNotEmpty((String)template.get(CourseJsonKey.NOTIFY_TEMPLATE))) { + if (StringUtils.isNotEmpty((String) template.get(CourseJsonKey.NOTIFY_TEMPLATE))) { + String notifyTemplateData = (String) template.get(CourseJsonKey.NOTIFY_TEMPLATE); + //Modify the placeholder with the actual configured cloud base path as ES should have the actual cloud path + if (notifyTemplateData.contains(getConfigValue(CLOUD_STORE_BASE_PATH_PLACEHOLDER))) + notifyTemplateData = notifyTemplateData.replace(getConfigValue(CLOUD_STORE_BASE_PATH_PLACEHOLDER), getConfigValue(CLOUD_STORE_BASE_PATH)); template.put( CourseJsonKey.NOTIFY_TEMPLATE, - mapper.readValue( - (String) template.get(CourseJsonKey.NOTIFY_TEMPLATE), + mapper.readValue(notifyTemplateData, new TypeReference<HashMap<String, Object>>() { })); } diff --git a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/qrcodedownload/QRCodeDownloadManagementActor.java b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/qrcodedownload/QRCodeDownloadManagementActor.java index 703867cb435bbcf6a0df8efec23128c52bbde906..8d21258cff26bf2f09cca5d9b976092282a9fba0 100644 --- a/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/qrcodedownload/QRCodeDownloadManagementActor.java +++ b/course-mw/course-actors-common/src/main/java/org/sunbird/learner/actors/qrcodedownload/QRCodeDownloadManagementActor.java @@ -19,8 +19,6 @@ import org.sunbird.cassandra.CassandraOperation; import org.sunbird.common.exception.ProjectCommonException; import org.sunbird.common.models.response.Response; import org.sunbird.common.models.util.JsonKey; -import org.sunbird.common.models.util.LoggerUtil; -import org.sunbird.common.models.util.ProjectLogger; import org.sunbird.common.models.util.ProjectUtil; import org.sunbird.common.models.util.TelemetryEnvKey; import org.sunbird.common.request.Request; @@ -234,7 +232,12 @@ public class QRCodeDownloadManagementActor extends BaseActor { if (null != obj && obj instanceof List) { List<Map<String, Object>> listOfMap = (List<Map<String, Object>>) obj; if (CollectionUtils.isNotEmpty(listOfMap)) { - return (String) listOfMap.get(0).get("url"); + //TODO Resolve it and store. Assuming dial code db will have the template url with data migration script + String templateUrl = (String) listOfMap.get(0).get("url"); + //replace template url with the actual cloud url + if (templateUrl.contains(getConfigValue(CLOUD_STORE_BASE_PATH_PLACEHOLDER))) + templateUrl = templateUrl.replace(getConfigValue(CLOUD_STORE_BASE_PATH_PLACEHOLDER), getConfigValue(CLOUD_STORE_BASE_PATH)); + return templateUrl; } } } @@ -263,22 +266,15 @@ public class QRCodeDownloadManagementActor extends BaseActor { objectKey += file.getName(); //CSP related changes String cloudStorage = getConfigValue(CONTENT_CLOUD_STORAGE_TYPE); - CloudStorageUtil.CloudStorageType storageType; - if (cloudStorage.equalsIgnoreCase(AWS_STR)) { - storageType = CloudStorageUtil.CloudStorageType.AWS; - } else if (cloudStorage.equalsIgnoreCase(GCLOUD_STR)) { - storageType = CloudStorageUtil.CloudStorageType.GCLOUD; - } else if (cloudStorage.equalsIgnoreCase(AZURE_STR)) { - storageType = CloudStorageUtil.CloudStorageType.AZURE; - } else { + if (cloudStorage == null) { ProjectCommonException.throwClientErrorException( ResponseCode.errorUnsupportedCloudStorage, ProjectUtil.formatMessage( - ResponseCode.errorUnsupportedCloudStorage.getErrorMessage(), cloudStorage)); + ResponseCode.errorUnsupportedCloudStorage.getErrorMessage())); return null; } String fileUrl = - CloudStorageUtil.upload(storageType, + CloudStorageUtil.upload(cloudStorage, getConfigValue(CONTENT_CLOUD_STORAGE_CONTAINER), objectKey, file.getAbsolutePath()); diff --git a/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/bulkupload/BulkUploadManagementActor.java b/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/bulkupload/BulkUploadManagementActor.java index 4b387afc04527c44e397eb76d1c3009c80350563..8e011de92640970769241982ce2d52ae470d9f93 100644 --- a/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/bulkupload/BulkUploadManagementActor.java +++ b/course-mw/course-actors/src/main/java/org/sunbird/learner/actors/bulkupload/BulkUploadManagementActor.java @@ -8,7 +8,6 @@ import org.sunbird.common.models.response.Response; import org.sunbird.common.models.util.ActorOperations; import org.sunbird.common.models.util.BulkUploadJsonKey; import org.sunbird.common.models.util.JsonKey; -import org.sunbird.common.models.util.LoggerUtil; import org.sunbird.common.models.util.ProjectUtil; import org.sunbird.common.models.util.PropertiesCache; import org.sunbird.common.models.util.TelemetryEnvKey; @@ -16,7 +15,6 @@ import org.sunbird.common.request.Request; import org.sunbird.common.request.RequestContext; import org.sunbird.common.responsecode.ResponseCode; import org.sunbird.common.util.CloudStorageUtil; -import org.sunbird.common.util.CloudStorageUtil.CloudStorageType; import org.sunbird.helper.ServiceFactory; import org.sunbird.learner.actors.bulkupload.dao.impl.BulkUploadProcessDaoImpl; import org.sunbird.learner.actors.bulkupload.model.BulkUploadProcess; @@ -74,8 +72,8 @@ public class BulkUploadManagementActor extends BaseBulkUploadActor { private void getBulkUploadDownloadStatusLink(Request actorMessage) { String processId = (String) actorMessage.getRequest().get(JsonKey.PROCESS_ID); - BulkUploadProcessDaoImpl bulkuploadDao = new BulkUploadProcessDaoImpl(); - BulkUploadProcess bulkUploadProcess = bulkuploadDao.read(actorMessage.getRequestContext(), processId); + BulkUploadProcessDaoImpl bulkUploadDao = new BulkUploadProcessDaoImpl(); + BulkUploadProcess bulkUploadProcess = bulkUploadDao.read(actorMessage.getRequestContext(), processId); if (bulkUploadProcess != null) { try { @@ -86,7 +84,7 @@ public class BulkUploadManagementActor extends BaseBulkUploadActor { } String signedUrl = CloudStorageUtil.getSignedUrl( - CloudStorageType.getByName(cloudStorageData.getStorageType()), + cloudStorageData.getStorageType(), cloudStorageData.getContainer(), cloudStorageData.getFileName()); Response response = new Response(); diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/pom.xml b/course-mw/sunbird-util/sunbird-platform-core/common-util/pom.xml index d42c3775964b5afaea8bad61404d22c4d61e647e..d4a6ddc3f251a894c25aaa1df11a9479a44abdcd 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/pom.xml +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/pom.xml @@ -219,7 +219,7 @@ <dependency> <groupId>org.sunbird</groupId> <artifactId>cloud-store-sdk</artifactId> - <version>1.4.3</version> + <version>1.4.4</version> <exclusions> <exclusion> <groupId>com.sun.jersey</groupId> diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java index 3ccd9735d42492e29551e47ab34afbdbc1435f0d..61c8186bbfab486c07e741e8e5a55a7b3244e659 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/JsonKey.java @@ -1,7 +1,6 @@ package org.sunbird.common.models.util; import java.util.Arrays; -import java.util.ArrayList; import java.util.List; /** @@ -789,6 +788,8 @@ public final class JsonKey { public static final String GCLOUD_STR = "gcloud"; public static final String CLOUD_FOLDER_CONTENT = "sunbird_cloud_content_folder"; + public static final String CLOUD_STORE_BASE_PATH = "cloud_storage_base_url"; + public static final String CLOUD_STORE_BASE_PATH_PLACEHOLDER = "cloud_store_base_path_placeholder"; public static final String TO_URL = "toUrl"; public static final String TTL = "ttl"; public static final String TEXTBOOK_TOC_CSV_TTL = "sunbird_texbook_toc_csv_ttl"; @@ -1070,7 +1071,8 @@ public final class JsonKey { public static final String OLD_CREATED_DATE = "oldCreatedDate"; public static final String X_LOGGING_HEADERS = "X_LOGGING_HEADERS"; public static final String LAST_CONTENT_ACCESS_TIME = "lastcontentaccesstime"; - + public static final String GCP="gcloud"; + public static final String TEMPLATE_URL = "templateUrl"; private JsonKey() {} } diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsCloudService.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsCloudService.java deleted file mode 100644 index 5aae45c1868603415dc667a7de9b4b2f39383ecf..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsCloudService.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.sunbird.common.models.util.aws; - -import org.sunbird.common.models.util.cloud.CloudService; - -import java.io.File; -import java.util.List; - -public class AwsCloudService implements CloudService { - @Override - public String uploadFile(String containerName, String fileName, String fileLocation) { - return AwsFileUtility.uploadFile(containerName, fileName, fileLocation); - } - - @Override - public boolean downLoadFile(String containerName, String fileName, String downloadFolder) { - return AwsFileUtility.downLoadFile(containerName, fileName, downloadFolder); - } - - @Override - public String uploadFile(String containerName, File file) { - return AwsFileUtility.uploadFile(containerName, file); - } - - @Override - public boolean deleteFile(String containerName, String fileName) { - return AwsFileUtility.deleteFile(containerName, fileName); - } - - @Override - public List<String> listAllFiles(String containerName) { - return AwsFileUtility.listAllFiles(containerName); - } - - @Override - public boolean deleteContainer(String containerName) { - return AwsFileUtility.deleteContainer(containerName); - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsConnectionManager.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsConnectionManager.java deleted file mode 100644 index bc1b5f42aa3cf0bb9d970e373a20e8372ae4064a..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsConnectionManager.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.sunbird.common.models.util.aws; - -import org.apache.commons.lang3.StringUtils; -import org.sunbird.cloud.storage.BaseStorageService; -import org.sunbird.cloud.storage.factory.StorageConfig; -import org.sunbird.cloud.storage.factory.StorageServiceFactory; -import org.sunbird.common.models.util.JsonKey; -import org.sunbird.common.models.util.ProjectLogger; -import org.sunbird.common.models.util.PropertiesCache; -import static org.sunbird.common.models.util.JsonKey.AWS_STR; - -public class AwsConnectionManager { - private static String accountName = ""; - private static String accountKey = ""; - private static AwsConnectionManager connectionManager; - private static BaseStorageService baseStorageService; - - static { - String name = System.getenv(JsonKey.ACCOUNT_NAME); - String key = System.getenv(JsonKey.ACCOUNT_KEY); - if (StringUtils.isBlank(name) || StringUtils.isBlank(key)) { - ProjectLogger.log( - "Aws account name and key is not provided by environment variable." + name + " " + key); - accountName = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_NAME); - accountKey = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_KEY); - } else { - accountName = name; - accountKey = key; - ProjectLogger.log( - "Aws account name and key is provided by environment variable." + name + " " + key); - } - } - - private AwsConnectionManager() throws CloneNotSupportedException { - if (connectionManager != null) throw new CloneNotSupportedException(); - } - - public static BaseStorageService getStorageService(){ - if(null == baseStorageService){ - baseStorageService = StorageServiceFactory.getStorageService(new StorageConfig(AWS_STR, accountName, accountKey)); - ProjectLogger.log( - "Aws account storage service with account name and key as " + accountName + " " + accountKey); - } - return baseStorageService; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsFileUtility.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsFileUtility.java deleted file mode 100644 index f5b3dfdffa0634b6bb14e64b79f2ddc3e17da3f3..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/aws/AwsFileUtility.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.sunbird.common.models.util.aws; - -import scala.Option; - -import java.io.File; -import java.util.List; - -import static org.sunbird.common.models.util.cloud.CloudUtils.getObjectKey; - -public class AwsFileUtility { - - public static String uploadFile(String containerName, String fileName, String fileLocation) { - String objectKey = getObjectKey(containerName,fileName); - return AwsConnectionManager.getStorageService().upload(containerName, fileLocation + fileName, objectKey, Option.apply(false), Option.apply(1), Option.apply(3), Option.empty()); - } - - public static boolean downLoadFile(String containerName, String fileName, String downloadFolder) { - return false; - } - - public static String uploadFile(String containerName, File file) { - String objectKey = getObjectKey(containerName,file.getName()); - return AwsConnectionManager.getStorageService().upload(containerName, file.getAbsolutePath(), objectKey, Option.apply(false), Option.apply(1), Option.apply(3), Option.empty()); - } - - public static boolean deleteFile(String containerName, String fileName) { - return false; - } - - public static List<String> listAllFiles(String containerName) { - return null; - } - - public static boolean deleteContainer(String containerName) { - return false; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureCloudService.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureCloudService.java deleted file mode 100644 index fa9a2799e7527b8a23a78b3d29d517e755d415c0..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureCloudService.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.sunbird.common.models.util.azure; - -import org.sunbird.common.models.util.cloud.CloudService; -import java.io.File; -import java.util.List; - -/** Created by arvind on 24/8/17. */ -public class AzureCloudService implements CloudService { - - @Override - public String uploadFile(String containerName, String fileName, String fileLocation) { - return AzureFileUtility.uploadFile(containerName, fileName, fileLocation); - } - - @Override - public boolean downLoadFile(String containerName, String fileName, String downloadFolder) { - return AzureFileUtility.downloadFile(containerName, fileName, downloadFolder); - } - - @Override - public String uploadFile(String containerName, File file) { - return AzureFileUtility.uploadFile(containerName, file); - } - - @Override - public boolean deleteFile(String containerName, String fileName) { - return AzureFileUtility.deleteFile(containerName, fileName); - } - - @Override - public List<String> listAllFiles(String containerName) { - return AzureFileUtility.listAllBlobbs(containerName); - } - - @Override - public boolean deleteContainer(String containerName) { - return AzureFileUtility.deleteContainer(containerName); - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureConnectionManager.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureConnectionManager.java deleted file mode 100644 index f7ba726badf18929f5a0a6944c2a8670061a2678..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureConnectionManager.java +++ /dev/null @@ -1,130 +0,0 @@ -/** */ -package org.sunbird.common.models.util.azure; - -import com.microsoft.azure.storage.CloudStorageAccount; -import com.microsoft.azure.storage.StorageException; -import com.microsoft.azure.storage.blob.BlobContainerPermissions; -import com.microsoft.azure.storage.blob.BlobContainerPublicAccessType; -import com.microsoft.azure.storage.blob.CloudBlobClient; -import com.microsoft.azure.storage.blob.CloudBlobContainer; -import java.net.URISyntaxException; -import java.security.InvalidKeyException; -import java.util.Locale; -import org.apache.commons.lang3.StringUtils; -import org.sunbird.common.models.util.JsonKey; -import org.sunbird.common.models.util.ProjectLogger; -import org.sunbird.common.models.util.PropertiesCache; - -/** - * This class will manage azure connection. - * - * @author Manzarul - */ -public class AzureConnectionManager { - - private static String accountName = ""; - private static String accountKey = ""; - private static String storageAccountString; - private static AzureConnectionManager connectionManager; - - static { - String name = System.getenv(JsonKey.ACCOUNT_NAME); - String key = System.getenv(JsonKey.ACCOUNT_KEY); - if (StringUtils.isBlank(name) || StringUtils.isBlank(key)) { - ProjectLogger.log( - "Azure account name and key is not provided by environment variable." + name + " " + key); - accountName = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_NAME); - accountKey = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_KEY); - storageAccountString = - "DefaultEndpointsProtocol=https;AccountName=" - + accountName - + ";AccountKey=" - + accountKey - + ";EndpointSuffix=core.windows.net"; - } else { - accountName = name; - accountKey = key; - ProjectLogger.log( - "Azure account name and key is provided by environment variable." + name + " " + key); - storageAccountString = - "DefaultEndpointsProtocol=https;AccountName=" - + accountName - + ";AccountKey=" - + accountKey - + ";EndpointSuffix=core.windows.net"; - } - } - - private AzureConnectionManager() throws CloneNotSupportedException { - if (connectionManager != null) throw new CloneNotSupportedException(); - } - - /** - * This method will provide Azure CloudBlobContainer object or in case of error it will provide - * null; - * - * @param containerName String - * @return CloudBlobContainer or null - */ - public static CloudBlobContainer getContainer(String containerName, boolean isPublicAccess) { - - try { - CloudBlobClient cloudBlobClient = getBlobClient(); - // Get a reference to a container , The container name must be lower case - CloudBlobContainer container = - cloudBlobClient.getContainerReference(containerName.toLowerCase(Locale.ENGLISH)); - // Create the container if it does not exist. - boolean response = container.createIfNotExists(); - ProjectLogger.log("container creation done if not exist==" + response); - // Create a permissions object. - if (isPublicAccess) { - BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); - // Include public access in the permissions object. - containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER); - // Set the permissions on the container. - container.uploadPermissions(containerPermissions); - } - return container; - } catch (Exception e) { - ProjectLogger.log(e.getMessage(), e); - } - return null; - } - - public static CloudBlobContainer getContainerReference(String containerName) { - - CloudBlobContainer container = null; - try { - // Create the blob client. - CloudBlobClient blobClient = getBlobClient(); - // Retrieve reference to a previously created container. - container = blobClient.getContainerReference(containerName.toLowerCase(Locale.ENGLISH)); - if (container.exists()) { - return container; - } - } catch (URISyntaxException e) { - ProjectLogger.log(e.getMessage(), e); - } catch (StorageException e) { - ProjectLogger.log(e.getMessage(), e); - } - ProjectLogger.log("Container does not exist ==" + containerName); - return null; - } - - private static CloudBlobClient getBlobClient() { - - // Retrieve storage account from connection-string. - CloudStorageAccount storageAccount = null; - CloudBlobClient blobClient = null; - try { - storageAccount = CloudStorageAccount.parse(storageAccountString); - // Create the blob client. - blobClient = storageAccount.createCloudBlobClient(); - } catch (URISyntaxException e) { - ProjectLogger.log(e.getMessage(), e); - } catch (InvalidKeyException e) { - ProjectLogger.log(e.getMessage(), e); - } - return blobClient; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureFileUtility.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureFileUtility.java deleted file mode 100644 index f619776a21f53922fa054c31666086a6b24db1d5..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/AzureFileUtility.java +++ /dev/null @@ -1,234 +0,0 @@ -/** */ -package org.sunbird.common.models.util.azure; - -import com.microsoft.azure.storage.StorageException; -import com.microsoft.azure.storage.blob.CloudBlobContainer; -import com.microsoft.azure.storage.blob.CloudBlockBlob; -import com.microsoft.azure.storage.blob.ListBlobItem; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; -import org.apache.commons.lang3.StringUtils; -import org.apache.tika.Tika; -import org.sunbird.common.models.util.LoggerUtil; -import org.sunbird.common.models.util.ProjectLogger; -import org.sunbird.common.models.util.Slug; - -/** @author Manzarul */ -public class AzureFileUtility { - - private static final String DEFAULT_CONTAINER = "default"; - private static LoggerUtil logger = new LoggerUtil(Slug.class); - - /** - * This method will remove the file from Azure Storage. - * - * @param fileName - * @param containerName - * @return boolean - */ - public static boolean deleteFile(String containerName, String fileName) { - if (fileName == null) { - logger.info(null,"File name can not be null"); - return false; - } - if (StringUtils.isBlank(containerName)) { - logger.info(null,"Container name can't be null or empty"); - return false; - } - CloudBlobContainer container = AzureConnectionManager.getContainer(containerName, true); - if (container == null) { - logger.info(null,"Unable to get Azure contains object"); - return false; - } - try { - // Retrieve reference to a blob named "myimage.jpg". - CloudBlockBlob blob = container.getBlockBlobReference(fileName); - // Delete the blob. - boolean response = blob.deleteIfExists(); - if (!response) { - logger.info(null,"Provided file not found to delete."); - } - return true; - } catch (Exception e) { - logger.error(null, e.getMessage(), e); - } - return false; - } - - /** - * This method will remove the container from Azure Storage. - * - * @param containerName - * @return boolean - */ - public static boolean deleteContainer(String containerName) { - if (StringUtils.isBlank(containerName)) { - logger.info(null,"Container name can't be null or empty"); - return false; - } - CloudBlobContainer container = AzureConnectionManager.getContainer(containerName, true); - if (container == null) { - logger.info(null,"Unable to get Azure contains object"); - return false; - } - try { - boolean response = container.deleteIfExists(); - if (!response) { - logger.info(null,"Container not found.."); - } else { - logger.info(null,"Container is deleted==="); - } - return true; - } catch (Exception e) { - logger.error(null, e.getMessage(), e); - } - return false; - } - - public static String uploadFile(String containerName, String blobName, String fileName) { - - CloudBlobContainer container = AzureConnectionManager.getContainer(containerName, true); - // Create or overwrite the "myimage.jpg" blob with contents from a local file. - CloudBlockBlob blob = null; - String fileUrl = null; - FileInputStream fis = null; - Tika tika = new Tika(); - try { - blob = container.getBlockBlobReference(blobName); - File source = new File(fileName); - fis = new FileInputStream(source); - String mimeType = tika.detect(source); - logger.info(null,"File - " + source.getName() + " mimeType " + mimeType); - blob.getProperties().setContentType(mimeType); - blob.upload(fis, source.length()); - // fileUrl = blob.getStorageUri().getPrimaryUri().getPath(); - fileUrl = blob.getUri().toString(); - } catch (URISyntaxException | IOException e) { - logger.error(null,"Unable to upload file :" + fileName, e); - } catch (Exception e) { - logger.error(null, e.getMessage(), e); - } finally { - if (null != fis) { - try { - fis.close(); - } catch (IOException e) { - logger.error(null, e.getMessage(), e); - } - } - } - - return fileUrl; - } - - public static String uploadFile(String containerName, File source) { - - String containerPath = ""; - String filePath = ""; - Tika tika = new Tika(); - String contrName = containerName; - - if (StringUtils.isBlank(containerName)) { - contrName = DEFAULT_CONTAINER; - } else { - contrName = containerName.toLowerCase(); - } - if (containerName.startsWith("/")) { - contrName = containerName.substring(1); - } - if (contrName.contains("/")) { - String[] arr = contrName.split("/", 2); - containerPath = arr[0]; - if (arr[1].length() > 0 && arr[1].endsWith("/")) { - filePath = arr[1]; - } else if (arr[1].length() > 0) { - filePath = arr[1] + "/"; - } - } else { - containerPath = contrName; - } - - CloudBlobContainer container = AzureConnectionManager.getContainer(containerPath, true); - // Create or overwrite the "myimage.jpg" blob with contents from a local file. - CloudBlockBlob blob = null; - String fileUrl = null; - FileInputStream fis = null; - try { - blob = container.getBlockBlobReference(filePath + source.getName()); - // File source = new File(fileName); - fis = new FileInputStream(source); - String mimeType = tika.detect(source); - logger.info(null,"File - " + source.getName() + " mimeType " + mimeType); - blob.getProperties().setContentType(mimeType); - blob.upload(fis, source.length()); - // fileUrl = blob.getStorageUri().getPrimaryUri().getPath(); - fileUrl = blob.getUri().toString(); - } catch (URISyntaxException | IOException e) { - logger.error(null,"Unable to upload file :" + source.getName(), e); - } catch (Exception e) { - logger.error(null, e.getMessage(), e); - } finally { - if (null != fis) { - try { - fis.close(); - } catch (IOException e) { - logger.error(null, e.getMessage(), e); - } - } - } - return fileUrl; - } - - public static boolean downloadFile(String containerName, String blobName, String downloadFolder) { - - String dwnldFolder = ""; - boolean flag = false; - CloudBlobContainer container = AzureConnectionManager.getContainer(containerName, true); - // Create or overwrite blob with contents . - CloudBlockBlob blob = null; - FileOutputStream fos = null; - - try { - blob = container.getBlockBlobReference(blobName); - if (blob.exists()) { - if (!(downloadFolder.endsWith(("/")))) { - dwnldFolder = downloadFolder + "/"; - } - File file = new File(dwnldFolder + blobName); - fos = new FileOutputStream(file); - blob.download(fos); - } - } catch (URISyntaxException | StorageException | FileNotFoundException e) { - logger.error(null,"Unable to upload blobfile :" + blobName, e); - } catch (Exception e) { - logger.error(null, e.getMessage(), e); - } finally { - if (null != fos) { - try { - fos.close(); - } catch (IOException e) { - logger.error(null, e.getMessage(), e); - } - } - } - return flag; - } - - public static List<String> listAllBlobbs(String containerName) { - - List<String> blobsList = new ArrayList<>(); - CloudBlobContainer container = AzureConnectionManager.getContainer(containerName, true); - // Loop over blobs within the container and output the URI to each of them. - if (container != null) { - for (ListBlobItem blobItem : container.listBlobs()) { - blobsList.add(blobItem.getUri().toString()); - } - } - return blobsList; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/package-info.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/package-info.java deleted file mode 100644 index 37f0b9c0507330e2726f57dd96e9c98a15e38985..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/azure/package-info.java +++ /dev/null @@ -1,3 +0,0 @@ -/** */ -/** @author Manzarul */ -package org.sunbird.common.models.util.azure; diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudService.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudService.java deleted file mode 100644 index 992d0107374c1e9c7e594001379ff67c87b8f365..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudService.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.sunbird.common.models.util.cloud; - -import java.io.File; -import java.util.List; - -/** Created by arvind on 24/8/17. */ -public interface CloudService { - - String uploadFile(String containerName, String filName, String fileLocation); - - boolean downLoadFile(String containerName, String fileName, String downloadFolder); - - String uploadFile(String containerName, File file); - - boolean deleteFile(String containerName, String fileName); - - List<String> listAllFiles(String containerName); - - boolean deleteContainer(String containerName); -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudServiceFactory.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudServiceFactory.java deleted file mode 100644 index 797e2f3674e2a89958a282645edcb3bf199c6ed3..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudServiceFactory.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.sunbird.common.models.util.cloud; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.sunbird.common.models.util.ProjectUtil; -import org.sunbird.common.models.util.aws.AwsCloudService; -import org.sunbird.common.models.util.azure.AzureCloudService; -import org.sunbird.common.models.util.gcloud.GcpCloudService; - -import static org.sunbird.common.models.util.JsonKey.*; - -/** - * Factory class to store the various upload download services like Azure , Amazon S3 etc... Created - * by arvind on 24/8/17. - */ -public class CloudServiceFactory { - - private static Map<String, CloudService> factory = new HashMap<>(); - private static List<String> allowedServiceNames = Arrays.asList(AZURE_STR, AWS_STR, GCLOUD_STR); - - private CloudServiceFactory() {} - - /** - * @param serviceName - * @return - */ - public static Object get(String serviceName) { - - if (ProjectUtil.isNotNull(factory.get(serviceName))) { - return factory.get(serviceName); - } else { - // create the service with the given name - return createService(serviceName); - } - } - - /** - * @param serviceName - * @return - */ - private static CloudService createService(String serviceName) { - - if (!(allowedServiceNames.contains(serviceName))) { - return null; - } - - synchronized (CloudServiceFactory.class) { - if (ProjectUtil.isNull(factory.get(serviceName)) && AZURE_STR.equalsIgnoreCase(serviceName)) { - CloudService service = new AzureCloudService(); - factory.put(AZURE_STR, service); - } else if (ProjectUtil.isNull(factory.get(serviceName)) && AWS_STR.equalsIgnoreCase(serviceName)) { - CloudService service = new AwsCloudService(); - factory.put(AWS_STR, service); - } else if (ProjectUtil.isNull(factory.get(serviceName)) && GCLOUD_STR.equalsIgnoreCase(serviceName)) { - CloudService service = new GcpCloudService(); - factory.put(GCLOUD_STR, service); - } - } - return factory.get(serviceName); - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudUtils.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudUtils.java deleted file mode 100644 index 1e2b86684674be333325f971c68423ace3f3fdb4..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/cloud/CloudUtils.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.sunbird.common.models.util.cloud; - -import org.apache.commons.lang3.StringUtils; -import org.sunbird.common.models.util.ProjectLogger; - -public class CloudUtils { - - public static String getObjectKey(String containerName, String fileName) { - if (containerName == null || fileName == null) { - ProjectLogger.log("Container or fileName can not be null"); - return ""; - } - String containerPath = ""; - String filePath = ""; - String contrName = containerName; - String objectKey = ""; - - if (containerName.startsWith("/")) { - contrName = containerName.substring(1); - } - if (contrName.contains("/")) { - String[] arr = contrName.split("/", 2); - containerPath = arr[0]; - if (arr[1].length() > 0 && arr[1].endsWith("/")) { - filePath = arr[1]; - } else if (arr[1].length() > 0) { - filePath = arr[1] + "/"; - } - } else { - containerPath = contrName; - } - - if(StringUtils.isBlank(filePath)) - objectKey = containerPath.endsWith("/") ? containerPath + fileName : containerPath + "/" + fileName; - else - objectKey = filePath + fileName; - - return objectKey; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpCloudConnectionManager.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpCloudConnectionManager.java deleted file mode 100644 index d7166d03e5febcafcd38c2ad1b2f1d7d28d9e048..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpCloudConnectionManager.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.sunbird.common.models.util.gcloud; - -import org.apache.commons.lang3.StringUtils; -import org.sunbird.cloud.storage.BaseStorageService; -import org.sunbird.cloud.storage.factory.StorageConfig; -import org.sunbird.cloud.storage.factory.StorageServiceFactory; -import org.sunbird.common.models.util.JsonKey; -import org.sunbird.common.models.util.ProjectLogger; -import org.sunbird.common.models.util.PropertiesCache; -import static org.sunbird.common.models.util.JsonKey.GCLOUD_STR; - -public class GcpCloudConnectionManager { - private static String accountName = ""; - private static String accountKey = ""; - private static GcpCloudConnectionManager connectionManager; - private static BaseStorageService baseStorageService; - - static { - String name = System.getenv(JsonKey.ACCOUNT_NAME); - String key = System.getenv(JsonKey.ACCOUNT_KEY); - if (StringUtils.isBlank(name) || StringUtils.isBlank(key)) { - ProjectLogger.log( - "Gcloud account name and key is not provided by environment variable." + name + " " + key); - accountName = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_NAME); - accountKey = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_KEY); - } else { - accountName = name; - accountKey = key; - ProjectLogger.log( - "Gcloud account name and key is provided by environment variable." + name + " " + key); - } - } - - private GcpCloudConnectionManager() throws CloneNotSupportedException { - if (connectionManager != null) throw new CloneNotSupportedException(); - } - - public static BaseStorageService getStorageService(){ - if(null == baseStorageService){ - baseStorageService = StorageServiceFactory.getStorageService(new StorageConfig(GCLOUD_STR, accountName, accountKey)); - ProjectLogger.log( - "Gcloud account storage service with account name and key as " + accountName + " " + accountKey); - } - return baseStorageService; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpCloudService.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpCloudService.java deleted file mode 100644 index a4532c1203b005fc391e43eeac4055249c0a4a4e..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpCloudService.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.sunbird.common.models.util.gcloud; - -import org.sunbird.common.models.util.cloud.CloudService; - -import java.io.File; -import java.util.List; - -public class GcpCloudService implements CloudService { - @Override - public String uploadFile(String containerName, String fileName, String fileLocation) { - return GcpFileUtility.uploadFile(containerName, fileName, fileLocation); - } - - @Override - public boolean downLoadFile(String containerName, String fileName, String downloadFolder) { - return GcpFileUtility.downLoadFile(containerName, fileName, downloadFolder); - } - - @Override - public String uploadFile(String containerName, File file) { - return GcpFileUtility.uploadFile(containerName, file); - } - - @Override - public boolean deleteFile(String containerName, String fileName) { - return GcpFileUtility.deleteFile(containerName, fileName); - } - - @Override - public List<String> listAllFiles(String containerName) { - return GcpFileUtility.listAllFiles(containerName); - } - - @Override - public boolean deleteContainer(String containerName) { - return GcpFileUtility.deleteContainer(containerName); - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpFileUtility.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpFileUtility.java deleted file mode 100644 index 6086a4772b69e463571bdc5cd60e81653cc2a0be..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/models/util/gcloud/GcpFileUtility.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.sunbird.common.models.util.gcloud; - -import org.sunbird.common.models.util.aws.AwsConnectionManager; -import scala.Option; - -import java.io.File; -import java.util.List; - -import static org.sunbird.common.models.util.cloud.CloudUtils.getObjectKey; - -public class GcpFileUtility { - - public static String uploadFile(String containerName, String fileName, String fileLocation) { - String objectKey = getObjectKey(containerName, fileName); - return GcpCloudConnectionManager.getStorageService().upload(containerName, fileLocation + fileName, objectKey, Option.apply(false), Option.apply(1), Option.apply(3), Option.empty()); - } - - public static boolean downLoadFile(String containerName, String fileName, String downloadFolder) { - return false; - } - - public static String uploadFile(String containerName, File file) { - String objectKey = getObjectKey(containerName, file.getName()); - return GcpCloudConnectionManager.getStorageService().upload(containerName, file.getAbsolutePath(), objectKey, Option.apply(false), Option.apply(1), Option.apply(3), Option.empty()); - } - - public static boolean deleteFile(String containerName, String fileName) { - return false; - } - - public static List<String> listAllFiles(String containerName) { - return null; - } - - public static boolean deleteContainer(String containerName) { - return false; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/util/CloudStorageUtil.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/util/CloudStorageUtil.java index 80e189fc1184928eda1af4853db0e4796502ef8b..288930091450d01cc9464f0cabec122a356e39fa 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/util/CloudStorageUtil.java +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/java/org/sunbird/common/util/CloudStorageUtil.java @@ -2,60 +2,24 @@ package org.sunbird.common.util; import java.util.HashMap; import java.util.Map; -import org.sunbird.cloud.storage.IStorageService; +import org.sunbird.cloud.storage.BaseStorageService; import org.sunbird.cloud.storage.factory.StorageConfig; import org.sunbird.cloud.storage.factory.StorageServiceFactory; -import org.sunbird.common.exception.ProjectCommonException; import org.sunbird.common.models.util.JsonKey; import org.sunbird.common.models.util.ProjectUtil; import org.sunbird.common.models.util.PropertiesCache; -import org.sunbird.common.responsecode.ResponseCode; import scala.Option; import scala.Some; -import static org.sunbird.common.models.util.JsonKey.*; - public class CloudStorageUtil { private static final int STORAGE_SERVICE_API_RETRY_COUNT = 3; - private static final Map<String, IStorageService> storageServiceMap = new HashMap<>(); - - public enum CloudStorageType { - AZURE(AZURE_STR), - AWS(AWS_STR), - GCLOUD(GCLOUD_STR); - - private String type; - - private CloudStorageType(String type) { - this.type = type; - } - - public String getType() { - return this.type; - } - - public static CloudStorageType getByName(String type) { - if (AZURE.type.equalsIgnoreCase(type)) { - return CloudStorageType.AZURE; - } if (AWS.type.equalsIgnoreCase(type)) { - return CloudStorageType.AWS; - } if (GCLOUD.type.equalsIgnoreCase(type)) { - return CloudStorageType.GCLOUD; - } else { - ProjectCommonException.throwClientErrorException( - ResponseCode.errorUnsupportedCloudStorage, - ProjectUtil.formatMessage( - ResponseCode.errorUnsupportedCloudStorage.getErrorMessage(), type)); - return null; - } - } - } + private static final Map<String, BaseStorageService> storageServiceMap = new HashMap<>(); public static String upload( - CloudStorageType storageType, String container, String objectKey, String filePath) { + String storageType, String container, String objectKey, String filePath) { - IStorageService storageService = getStorageService(storageType); + BaseStorageService storageService = getStorageService(storageType); return storageService.upload( container, @@ -68,49 +32,35 @@ public class CloudStorageUtil { } public static String getSignedUrl( - CloudStorageType storageType, String container, String objectKey) { - IStorageService storageService = getStorageService(storageType); - return getSignedUrl(storageService, storageType, container, objectKey); - } - - public static String getAnalyticsSignedUrl( - CloudStorageType storageType, String container, String objectKey) { - IStorageService analyticsStorageService = getAnalyticsStorageService(storageType); - return getSignedUrl(analyticsStorageService, storageType, container, objectKey); + String storageType, String container, String objectKey) { + BaseStorageService storageService = getStorageService(storageType); + return getSignedUrl(storageService, container, objectKey,storageType); } public static String getSignedUrl( - IStorageService storageService, - CloudStorageType storageType, - String container, - String objectKey) { - int timeoutInSeconds = getTimeoutInSeconds(); - return storageService.getSignedURL( - container, objectKey, Some.apply(timeoutInSeconds), Some.apply("r")); + BaseStorageService storageService, + String container, + String objectKey, String cloudType) { + return storageService.getSignedURLV2(container, objectKey, Some.apply(getTimeoutInSeconds()), + Some.apply("r"), Some.apply("application/pdf")); } - private static IStorageService getStorageService(CloudStorageType storageType) { + private static BaseStorageService getStorageService(String storageType) { String storageKey = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_NAME); String storageSecret = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_KEY); return getStorageService(storageType, storageKey, storageSecret); } - private static IStorageService getAnalyticsStorageService(CloudStorageType storageType) { - String storageKey = PropertiesCache.getInstance().getProperty(JsonKey.ANALYTICS_ACCOUNT_NAME); - String storageSecret = PropertiesCache.getInstance().getProperty(JsonKey.ANALYTICS_ACCOUNT_KEY); - return getStorageService(storageType, storageKey, storageSecret); - } - - private static IStorageService getStorageService( - CloudStorageType storageType, String storageKey, String storageSecret) { - String compositeKey = storageType.getType() + "-" + storageKey; + private static BaseStorageService getStorageService( + String storageType, String storageKey, String storageSecret) { + String compositeKey = storageType + "-" + storageKey; if (storageServiceMap.containsKey(compositeKey)) { return storageServiceMap.get(compositeKey); } synchronized (CloudStorageUtil.class) { StorageConfig storageConfig = - new StorageConfig(storageType.getType(), storageKey, storageSecret); - IStorageService storageService = StorageServiceFactory.getStorageService(storageConfig); + new StorageConfig(storageType, storageKey, storageSecret); + BaseStorageService storageService = StorageServiceFactory.getStorageService(storageConfig); storageServiceMap.put(compositeKey, storageService); } return storageServiceMap.get(compositeKey); @@ -122,8 +72,8 @@ public class CloudStorageUtil { } public static String getUri( - CloudStorageType storageType, String container, String prefix, boolean isDirectory) { - IStorageService storageService = getStorageService(storageType); + String storageType, String container, String prefix, boolean isDirectory) { + BaseStorageService storageService = getStorageService(storageType); return storageService.getUri(container, prefix, Option.apply(isDirectory)); } } diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties index 29745e23bfac2fd8f656c4843f5df45f8590109e..08ac8c42afca182272a046bc5109dbabd48d0d7a 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/main/resources/externalresource.properties @@ -205,4 +205,6 @@ kafka_assessment_topic= sunbird_msg_sender= sunbird_msg_91_auth= sunbird_api_mgr_base_url=https://dev.sunbirded.org/api -enrollment_list_size=1000 \ No newline at end of file +enrollment_list_size=1000 +cloud_storage_base_url=https://sunbirddev.blob.core.windows.net +cloud_store_base_path_placeholder=$CLOUD_STORE_BASE_PATH \ No newline at end of file diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/aws/AwsServiceFactoryTest.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/aws/AwsServiceFactoryTest.java deleted file mode 100644 index 8dddb223eb7743b48556536ed705724dd6db7465..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/aws/AwsServiceFactoryTest.java +++ /dev/null @@ -1,156 +0,0 @@ -package org.sunbird.common.models.util.aws; - -import org.junit.*; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.sunbird.cloud.storage.BaseStorageService; -import org.sunbird.cloud.storage.factory.StorageServiceFactory; -import org.sunbird.common.models.util.PropertiesCache; -import org.sunbird.common.models.util.cloud.CloudService; -import org.sunbird.common.models.util.cloud.CloudServiceFactory; - -import java.io.File; -import java.util.List; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sunbird.common.models.util.JsonKey.AWS_STR; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({PropertiesCache.class, StorageServiceFactory.class}) -@PowerMockIgnore({ - "javax.management.*", - "javax.net.ssl.*", - "javax.security.*", - "com.microsoft.azure.storage.*", - "jdk.internal.reflect.*", - "javax.crypto.*", - "javax.script.*", - "javax.xml.*", - "com.sun.org.apache.xerces.*", - "org.xml.*" -}) -public class AwsServiceFactoryTest { - static Object obj = null; - String containerName = "testcontainer"; - - @BeforeClass - public static void getObject() { - PowerMockito.mockStatic(PropertiesCache.class); - PowerMockito.mockStatic(StorageServiceFactory.class); - PropertiesCache propertiesCache = mock(PropertiesCache.class); - StorageServiceFactory storageServiceFactory = mock(StorageServiceFactory.class); - BaseStorageService baseStorageService = mock(BaseStorageService.class); - when(PropertiesCache.getInstance()).thenReturn(propertiesCache); - when(propertiesCache.getProperty(Mockito.anyString())).thenReturn("anyString"); - when(storageServiceFactory.getStorageService(any())).thenReturn(baseStorageService); - - - obj = CloudServiceFactory.get(AWS_STR); - Assert.assertTrue(obj instanceof CloudService); - Assert.assertNotNull(obj); - } - - @Test - public void testGetFailureWithWrongType() { - Object obj = CloudServiceFactory.get("Aws12"); - Assert.assertNull(obj); - } - - @Test - public void testGetSuccess() { - Object obj1 = CloudServiceFactory.get(AWS_STR); - Assert.assertNotNull(obj1); - Assert.assertTrue(obj.equals(obj1)); - } - - @Test - @Ignore - public void testUploadFileSuccess() { - try { - CloudService service = (CloudService) obj; - service.uploadFile(containerName, new File("test.txt")); - } catch (Exception e) { - Assert.assertNotNull(e); - } - - } - - @Test - @Ignore - public void testUploadFileFailureWithoutContainerName() { - try { - CloudService service = (CloudService) obj; - service.uploadFile("", new File("test.txt")); - } catch (Exception e) { - Assert.assertNotNull(e); - } - } - - @Test - @Ignore - public void testUploadFileSuccessWithMultiplePath() { - try { - CloudService service = (CloudService) obj; - service.uploadFile("/tez/po/" + containerName, new File("test.txt")); - }catch (Exception e) { - Assert.assertNotNull(e); - } - } - - @Test - @Ignore - public void testUploadFileSuccessWithFileLocation() { - try{ - CloudService service = (CloudService) obj; - service.uploadFile(containerName, "test.txt", ""); - }catch (Exception e) { - Assert.assertNotNull(e); - } - } - - @Test - public void testListAllFilesSuccess() { - CloudService service = (CloudService) obj; - List<String> filesList = service.listAllFiles(containerName); - Assert.assertEquals(null, filesList); - } - - @Test - public void testDownloadFileSuccess() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.downLoadFile(containerName, "test1.txt", ""); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteFileSuccess() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.deleteFile(containerName, "test1.txt"); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteFileSuccessWithoutContainerName() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.deleteFile("", "test.abc"); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteContainerSuccess() { - CloudService service = (CloudService) obj; - boolean response = service.deleteContainer(containerName); - Assert.assertFalse(response); - } - - @AfterClass - public static void shutDown() { - obj = null; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/azure/AzureServiceFactoryTest.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/azure/AzureServiceFactoryTest.java deleted file mode 100644 index b8c269800c9b5f23ba23dce256ad34ed3869546b..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/azure/AzureServiceFactoryTest.java +++ /dev/null @@ -1,175 +0,0 @@ -/** */ -package org.sunbird.common.models.util.azure; - -import static org.powermock.api.mockito.PowerMockito.doReturn; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; -import static org.sunbird.common.models.util.JsonKey.*; - -import com.microsoft.azure.storage.CloudStorageAccount; -import com.microsoft.azure.storage.blob.CloudBlobClient; -import com.microsoft.azure.storage.blob.CloudBlobContainer; -import com.microsoft.azure.storage.blob.ListBlobItem; -import java.io.File; -import java.net.URI; -import java.util.ArrayList; -import java.util.List; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.sunbird.common.models.util.cloud.CloudService; -import org.sunbird.common.models.util.cloud.CloudServiceFactory; - -/** @author Manzarul */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - CloudStorageAccount.class, - CloudBlobClient.class, - CloudBlobContainer.class, - ListBlobItem.class -}) -@PowerMockIgnore({ "javax.management.*", "javax.net.ssl.*", "javax.security.*", "com.microsoft.azure.storage.*", - "jdk.internal.reflect.*", "sun.security.ssl.*", "javax.crypto.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"}) -public class AzureServiceFactoryTest { - - static Object obj = null; - static CloudBlobContainer container = null; - static CloudBlobContainer container1 = null; - String containerName = "testcontainerxyz"; - - @BeforeClass - public static void getObject() { - obj = CloudServiceFactory.get(AZURE_STR); - Assert.assertTrue(obj instanceof CloudService); - Assert.assertNotNull(obj); - } - - @Before - public void addMockRules() { - CloudStorageAccount cloudStorageAccount = mock(CloudStorageAccount.class); - CloudBlobClient cloudBlobClient = mock(CloudBlobClient.class); - CloudBlobContainer cloudBlobContainer = mock(CloudBlobContainer.class); - - ListBlobItem listBlobItem = mock(ListBlobItem.class); - List<ListBlobItem> lst = new ArrayList<>(); - lst.add(listBlobItem); - PowerMockito.mockStatic(CloudStorageAccount.class); - try { - doReturn(cloudStorageAccount).when(CloudStorageAccount.class, "parse", Mockito.anyString()); - doReturn(cloudBlobClient).when(cloudStorageAccount).createCloudBlobClient(); - doReturn(cloudBlobContainer).when(cloudBlobClient).getContainerReference(Mockito.anyString()); - doReturn(true).when(cloudBlobContainer).exists(); - when(cloudBlobContainer.listBlobs()).thenReturn(lst); - when(listBlobItem.getUri()).thenReturn(new URI("http://www.google.com")); - - } catch (Exception e) { - Assert.fail("Could not initalize mocks, underlying reason " + e.getLocalizedMessage()); - } - } - - @Test - public void testGetFailureWithWrongType() { - Object obj = CloudServiceFactory.get("Azure12"); - Assert.assertNull(obj); - } - - @Test - public void testGetSuccess() { - Object obj1 = CloudServiceFactory.get(AZURE_STR); - Assert.assertNotNull(obj1); - Assert.assertTrue(obj.equals(obj1)); - } - - @Test - public void testGetContainerSuccessWithAccessPublic() { - container = AzureConnectionManager.getContainer(containerName, true); - Assert.assertNotNull(container); - } - - @Test - public void testGetContainerReferenceSuccess() { - container1 = AzureConnectionManager.getContainerReference(containerName); - Assert.assertNotNull(container1); - } - - @Test - public void testUploadFileSuccess() { - CloudService service = (CloudService) obj; - String url = service.uploadFile(containerName, new File("test.txt")); - Assert.assertEquals(null, url); - } - - @Test - public void testUploadFileFailureWithoutContainerName() { - CloudService service = (CloudService) obj; - String url = service.uploadFile("", new File("test.txt")); - Assert.assertEquals(null, url); - } - - @Test - public void testUploadFileSuccessWithMultiplePath() { - CloudService service = (CloudService) obj; - String url = service.uploadFile("/tez/po/" + containerName, new File("test.txt")); - Assert.assertEquals(null, url); - } - - @Test - public void testUploadFileSuccessWithFileLocation() { - CloudService service = (CloudService) obj; - String url = service.uploadFile(containerName, "test.txt", ""); - Assert.assertEquals(null, url); - } - - @Test - public void testListAllFilesSuccess() { - CloudService service = (CloudService) obj; - List<String> filesList = service.listAllFiles(containerName); - Assert.assertEquals(1, filesList.size()); - } - - @Test - public void testDownloadFileSuccess() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.downLoadFile(containerName, "test1.txt", ""); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteFileSuccess() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.deleteFile(containerName, "test1.txt"); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteFileSuccessWithoutContainerName() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.deleteFile("", "test.abc"); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteContainerSuccess() { - CloudService service = (CloudService) obj; - boolean response = service.deleteContainer(containerName); - Assert.assertTrue(response); - } - - @AfterClass - public static void shutDown() { - container1 = null; - container = null; - obj = null; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/gcloud/GcpCloudServiceFactoryTest.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/gcloud/GcpCloudServiceFactoryTest.java deleted file mode 100644 index 2c0ab8c03311a373d07352030fceb06239df2129..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/models/util/gcloud/GcpCloudServiceFactoryTest.java +++ /dev/null @@ -1,156 +0,0 @@ -package org.sunbird.common.models.util.gcloud; - -import org.junit.*; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.sunbird.cloud.storage.BaseStorageService; -import org.sunbird.cloud.storage.factory.StorageServiceFactory; -import org.sunbird.common.models.util.PropertiesCache; -import org.sunbird.common.models.util.cloud.CloudService; -import org.sunbird.common.models.util.cloud.CloudServiceFactory; - -import java.io.File; -import java.util.List; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sunbird.common.models.util.JsonKey.GCLOUD_STR; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({PropertiesCache.class, StorageServiceFactory.class}) -@PowerMockIgnore({ - "javax.management.*", - "javax.net.ssl.*", - "javax.security.*", - "com.microsoft.azure.storage.*", - "jdk.internal.reflect.*", - "javax.crypto.*", - "javax.script.*", - "javax.xml.*", - "com.sun.org.apache.xerces.*", - "org.xml.*" -}) -public class GcpCloudServiceFactoryTest { - static Object obj = null; - String containerName = "testcontainer"; - - @BeforeClass - public static void getObject() { - PowerMockito.mockStatic(PropertiesCache.class); - PowerMockito.mockStatic(StorageServiceFactory.class); - PropertiesCache propertiesCache = mock(PropertiesCache.class); - StorageServiceFactory storageServiceFactory = mock(StorageServiceFactory.class); - BaseStorageService baseStorageService = mock(BaseStorageService.class); - when(PropertiesCache.getInstance()).thenReturn(propertiesCache); - when(propertiesCache.getProperty(Mockito.anyString())).thenReturn("anyString"); - when(storageServiceFactory.getStorageService(any())).thenReturn(baseStorageService); - - - obj = CloudServiceFactory.get(GCLOUD_STR); - Assert.assertTrue(obj instanceof CloudService); - Assert.assertNotNull(obj); - } - - @Test - public void testGetFailureWithWrongType() { - Object obj = CloudServiceFactory.get("gcloud12"); - Assert.assertNull(obj); - } - - @Test - public void testGetSuccess() { - Object obj1 = CloudServiceFactory.get(GCLOUD_STR); - Assert.assertNotNull(obj1); - Assert.assertTrue(obj.equals(obj1)); - } - - @Test - @Ignore - public void testUploadFileSuccess() { - try { - CloudService service = (CloudService) obj; - service.uploadFile(containerName, new File("test.txt")); - } catch (Exception e) { - Assert.assertNotNull(e); - } - - } - - @Test - @Ignore - public void testUploadFileFailureWithoutContainerName() { - try { - CloudService service = (CloudService) obj; - service.uploadFile("", new File("test.txt")); - } catch (Exception e) { - Assert.assertNotNull(e); - } - } - - @Test - @Ignore - public void testUploadFileSuccessWithMultiplePath() { - try { - CloudService service = (CloudService) obj; - service.uploadFile("/tez/po/" + containerName, new File("test.txt")); - }catch (Exception e) { - Assert.assertNotNull(e); - } - } - - @Test - @Ignore - public void testUploadFileSuccessWithFileLocation() { - try{ - CloudService service = (CloudService) obj; - service.uploadFile(containerName, "test.txt", ""); - }catch (Exception e) { - Assert.assertNotNull(e); - } - } - - @Test - public void testListAllFilesSuccess() { - CloudService service = (CloudService) obj; - List<String> filesList = service.listAllFiles(containerName); - Assert.assertEquals(null, filesList); - } - - @Test - public void testDownloadFileSuccess() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.downLoadFile(containerName, "test1.txt", ""); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteFileSuccess() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.deleteFile(containerName, "test1.txt"); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteFileSuccessWithoutContainerName() { - CloudService service = (CloudService) obj; - Boolean isFileDeleted = service.deleteFile("", "test.abc"); - Assert.assertFalse(isFileDeleted); - } - - @Test - public void testDeleteContainerSuccess() { - CloudService service = (CloudService) obj; - boolean response = service.deleteContainer(containerName); - Assert.assertFalse(response); - } - - @AfterClass - public static void shutDown() { - obj = null; - } -} diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/util/CloudStorageUtilTest.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/util/CloudStorageUtilTest.java index 708307e51145e2c233041b8ad764df940cfe5890..8cfc1f886c4c8ff76812d565567dc1ebf9d6291a 100644 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/util/CloudStorageUtilTest.java +++ b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/util/CloudStorageUtilTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertTrue; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; -import static org.sunbird.common.models.util.JsonKey.*; import org.junit.Assert; import org.junit.Before; @@ -17,8 +16,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.sunbird.cloud.storage.BaseStorageService; import org.sunbird.cloud.storage.factory.StorageServiceFactory; -import org.sunbird.common.exception.ProjectCommonException; -import org.sunbird.common.util.CloudStorageUtil.CloudStorageType; +import org.sunbird.common.models.util.JsonKey; import scala.Option; @RunWith(PowerMockRunner.class) @@ -29,6 +27,7 @@ public class CloudStorageUtilTest { String SIGNED_URL = "singedUrl"; String UPLOAD_URL = "uploadUrl"; + String PUT_SIGNED_URL = "gcpSignedUrl"; @Before public void initTest() { @@ -54,6 +53,27 @@ public class CloudStorageUtilTest { Mockito.any(Option.class), Mockito.any(Option.class))) .thenReturn(SIGNED_URL); + when(service.getPutSignedURL( + Mockito.anyString(), + Mockito.anyString(), + Mockito.any(Option.class), + Mockito.any(Option.class), + Mockito.any(Option.class))) + .thenReturn(PUT_SIGNED_URL); + when(service.getSignedURLV2( + Mockito.eq("azurecontainer"), + Mockito.anyString(), + Mockito.any(Option.class), + Mockito.any(Option.class), + Mockito.any(Option.class))) + .thenReturn(SIGNED_URL); + when(service.getSignedURLV2( + Mockito.eq("gcpcontainer"), + Mockito.anyString(), + Mockito.any(Option.class), + Mockito.any(Option.class), + Mockito.any(Option.class))) + .thenReturn(PUT_SIGNED_URL); } catch (Exception e) { Assert.fail(e.getMessage()); @@ -61,40 +81,21 @@ public class CloudStorageUtilTest { } @Test - public void testGetAzureStorageTypeSuccess() { - CloudStorageType storageType = CloudStorageType.getByName(AZURE_STR); - assertTrue(CloudStorageType.AZURE.equals(storageType)); - } - - @Test - public void testGetAwsStorageTypeSuccess() { - CloudStorageType storageType = CloudStorageType.getByName(AWS_STR); - assertTrue(CloudStorageType.AWS.equals(storageType)); - } - - @Test - public void testGetGcpStorageTypeSuccess() { - CloudStorageType storageType = CloudStorageType.getByName(GCLOUD_STR); - assertTrue(CloudStorageType.GCLOUD.equals(storageType)); - } - - @Test(expected = ProjectCommonException.class) - public void testGetStorageTypeFailureWithWrongType() { - CloudStorageType.getByName("wrongstorage"); - } - - @Test - @Ignore public void testUploadSuccess() { String result = - CloudStorageUtil.upload(CloudStorageType.AZURE, "container", "key", "/file/path"); + CloudStorageUtil.upload("azure", "container", "key", "/file/path"); assertTrue(UPLOAD_URL.equals(result)); } @Test - @Ignore public void testGetSignedUrlSuccess() { - String signedUrl = CloudStorageUtil.getSignedUrl(CloudStorageType.AZURE, "container", "key"); + String signedUrl = CloudStorageUtil.getSignedUrl("azure", "azurecontainer", "key"); assertTrue(SIGNED_URL.equals(signedUrl)); } + + @Test + public void testGetSignedUrlGCPSuccess() { + String signedUrl = CloudStorageUtil.getSignedUrl(JsonKey.GCP, "gcpcontainer", "key"); + assertTrue(PUT_SIGNED_URL.equals(signedUrl)); + } } diff --git a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/util/CloudUtilsTest.java b/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/util/CloudUtilsTest.java deleted file mode 100644 index 058d743e49101eb8c3680b05d1252a32d009c477..0000000000000000000000000000000000000000 --- a/course-mw/sunbird-util/sunbird-platform-core/common-util/src/test/java/org/sunbird/common/util/CloudUtilsTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.sunbird.common.util; - -import org.junit.Assert; -import org.junit.Test; -import org.sunbird.common.models.util.cloud.CloudUtils; - -public class CloudUtilsTest { - - @Test - public void testGetObjectKeyMultiplePath() { - String objKey = CloudUtils.getObjectKey("/tez/po/","test.txt"); - Assert.assertEquals("po/test.txt",objKey); - } - - @Test - public void testGetObjectKeyPath() { - String objKey = CloudUtils.getObjectKey("/po/","test.txt"); - Assert.assertEquals("po/test.txt",objKey); - } - - @Test - public void testGetObjectKeyContainerNamePath() { - String objKey = CloudUtils.getObjectKey("testcontainer","test.txt"); - Assert.assertEquals("testcontainer/test.txt",objKey); - } - - @Test - public void testGetObjectKeyContainerMultipleNamePath() { - String objKey = CloudUtils.getObjectKey("/tez/po/testcontainer","test.txt"); - Assert.assertEquals("po/testcontainer/test.txt",objKey); - } -} diff --git a/course-mw/textbook-actors/src/main/java/org/sunbird/content/util/ContentCloudStore.java b/course-mw/textbook-actors/src/main/java/org/sunbird/content/util/ContentCloudStore.java index dc7a71cb9a41e4aba5960c973d5faac2c813a28b..42ac3c6764048f8bdc0ed8492776d4e9606366fa 100644 --- a/course-mw/textbook-actors/src/main/java/org/sunbird/content/util/ContentCloudStore.java +++ b/course-mw/textbook-actors/src/main/java/org/sunbird/content/util/ContentCloudStore.java @@ -3,27 +3,15 @@ package org.sunbird.content.util; import static java.io.File.separator; import static org.sunbird.common.models.util.JsonKey.*; import static org.sunbird.common.models.util.ProjectUtil.getConfigValue; -import static org.sunbird.common.util.CloudStorageUtil.CloudStorageType.*; - import java.io.File; import org.sunbird.common.util.CloudStorageUtil; -import org.sunbird.common.util.CloudStorageUtil.CloudStorageType; public class ContentCloudStore { public static String FOLDER = getConfigValue(CLOUD_FOLDER_CONTENT); + public static String storageType = getConfigValue(CONTENT_CLOUD_STORAGE_TYPE); public static String getUri(String prefix, boolean isDirectory) { - prefix = FOLDER + prefix; - try { - CloudStorageType storageType = storageType(); - return CloudStorageUtil.getUri(storageType, getConfigValue(CONTENT_CLOUD_STORAGE_CONTAINER), prefix, isDirectory); - } catch (Exception e) { - return null; - } - } - - public static String getUri(CloudStorageType storageType, String prefix, boolean isDirectory) { prefix = FOLDER + prefix; try { return CloudStorageUtil.getUri(storageType, getConfigValue(CONTENT_CLOUD_STORAGE_CONTAINER), prefix, isDirectory); @@ -33,7 +21,6 @@ public class ContentCloudStore { } public static String upload(String objectKey, File file) { - CloudStorageType storageType = storageType(); objectKey = FOLDER + objectKey + separator; if (file.isFile()) { objectKey += file.getName(); @@ -44,7 +31,7 @@ public class ContentCloudStore { } } - public static String upload(CloudStorageType storageType, String objectKey, File file) { + public static String upload(String storageType, String objectKey, File file) { objectKey = FOLDER + objectKey + separator; if (file.isFile()) { objectKey += file.getName(); @@ -54,23 +41,4 @@ public class ContentCloudStore { return null; } } - - private static CloudStorageType storageType() { - CloudStorageType storageType = null; - switch (getConfigValue(CONTENT_CLOUD_STORAGE_TYPE)) { - case AZURE_STR: - storageType = AZURE; - break; - case AWS_STR: - storageType = AWS; - break; - case GCLOUD_STR: - storageType = GCLOUD; - break; - default: - break; - } - return storageType; - } - } diff --git a/course-mw/textbook-actors/src/main/java/org/sunbird/learner/actors/textbook/TextbookTocActor.java b/course-mw/textbook-actors/src/main/java/org/sunbird/learner/actors/textbook/TextbookTocActor.java index 3479ffcaa531e899b8cc19cef7246f63d805fcb1..900331e7d9618bbf6d64780911844f51f906f8b1 100644 --- a/course-mw/textbook-actors/src/main/java/org/sunbird/learner/actors/textbook/TextbookTocActor.java +++ b/course-mw/textbook-actors/src/main/java/org/sunbird/learner/actors/textbook/TextbookTocActor.java @@ -855,7 +855,7 @@ public class TextbookTocActor extends BaseActor { String textBookTocFileName = textbookId + "_" + textBookNameSlug + "_" + contentVersionKey; String prefix = TEXTBOOK_TOC_FOLDER + separator + textBookTocFileName + fileExtension.getDotExtension(); - + //No need to make any changes here as the uri is being generated based on the cloud service providers String cloudPath = getUri(prefix, false); logger.info(null, "Timed:TextbookTocActor:getTocUrl duration for get cloud path url: " diff --git a/service/app/filters/ResponseFilter.scala b/service/app/filters/ResponseFilter.scala new file mode 100644 index 0000000000000000000000000000000000000000..afcf461357f17c3763965c6ffbef994a440079f9 --- /dev/null +++ b/service/app/filters/ResponseFilter.scala @@ -0,0 +1,32 @@ +package filters + +import akka.stream.Materializer +import akka.util.ByteString +import org.sunbird.common.models.util.JsonKey +import play.api.http.HttpEntity.Strict +import play.api.mvc.{Filter, RequestHeader, Result} +import org.sunbird.common.models.util.ProjectUtil.getConfigValue +import play.api.Logger.logger + +import javax.inject.Inject +import scala.concurrent.{ExecutionContext, Future} + +class ResponseFilter @Inject()(implicit val mat: Materializer, ec: ExecutionContext) extends Filter { + + override def apply(nextFilter: (RequestHeader) => Future[Result])(rh: RequestHeader) = + nextFilter(rh) flatMap { result => + if (null != result.body && !result.body.isKnownEmpty){ + val contentType = result.body.contentType + val updatedBody = result.body.consumeData.map { x => + val y = x.utf8String.replaceAll(getConfigValue(JsonKey.CLOUD_STORE_BASE_PATH_PLACEHOLDER),getConfigValue(JsonKey.CLOUD_STORE_BASE_PATH)) + logger.info("updated body: " + y) + y + } + updatedBody map { x => + result.copy(body = Strict(ByteString(x), contentType)) + } + } else { + Future(result) + } + } +} \ No newline at end of file diff --git a/service/conf/application.conf b/service/conf/application.conf index 0307054be66b3bb36fa9329854f7f66478f7e8c1..8a9d2f577279793a3501084a41a86525be0405b9 100644 --- a/service/conf/application.conf +++ b/service/conf/application.conf @@ -332,5 +332,6 @@ play.filters { } enabled += filters.AccessLogFilter enabled += filters.CustomGzipFilter + enabled += filters.ResponseFilter disabled += play.filters.csrf.CSRFFilter }