Unverified Commit 9e6e616b authored by Reshmi V Nair's avatar Reshmi V Nair Committed by GitHub
Browse files

Merge pull request #500 from BharathwajShankar/merge-5.0.1

Merge 5.0.1
Showing with 68 additions and 871 deletions
+68 -871
...@@ -14,8 +14,6 @@ import org.sunbird.actor.base.BaseActor; ...@@ -14,8 +14,6 @@ import org.sunbird.actor.base.BaseActor;
import org.sunbird.common.exception.ProjectCommonException; import org.sunbird.common.exception.ProjectCommonException;
import org.sunbird.common.models.response.Response; import org.sunbird.common.models.response.Response;
import org.sunbird.common.models.util.JsonKey; 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.models.util.TelemetryEnvKey;
import org.sunbird.common.request.Request; import org.sunbird.common.request.Request;
import org.sunbird.common.request.RequestContext; import org.sunbird.common.request.RequestContext;
...@@ -26,6 +24,10 @@ import org.sunbird.learner.constants.CourseJsonKey; ...@@ -26,6 +24,10 @@ import org.sunbird.learner.constants.CourseJsonKey;
import org.sunbird.learner.util.CourseBatchUtil; import org.sunbird.learner.util.CourseBatchUtil;
import org.sunbird.learner.util.Util; 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 { public class CourseBatchCertificateActor extends BaseActor {
private CourseBatchDao courseBatchDao = new CourseBatchDaoImpl(); private CourseBatchDao courseBatchDao = new CourseBatchDaoImpl();
...@@ -100,7 +102,8 @@ public class CourseBatchCertificateActor extends BaseActor { ...@@ -100,7 +102,8 @@ public class CourseBatchCertificateActor extends BaseActor {
String certName = (String) templateData.getOrDefault(JsonKey.TITLE , (String)templateDetails.getOrDefault(JsonKey.NAME, "")); String certName = (String) templateData.getOrDefault(JsonKey.TITLE , (String)templateDetails.getOrDefault(JsonKey.NAME, ""));
template.put(JsonKey.NAME, certName); 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))); template.put(JsonKey.CRITERIA, mapper.writeValueAsString(template.get(JsonKey.CRITERIA)));
if (null != template.get(CourseJsonKey.ISSUER)) { if (null != template.get(CourseJsonKey.ISSUER)) {
template.put( template.put(
...@@ -118,9 +121,13 @@ public class CourseBatchCertificateActor extends BaseActor { ...@@ -118,9 +121,13 @@ public class CourseBatchCertificateActor extends BaseActor {
CourseJsonKey.ISSUER, mapper.writeValueAsString(templateDetails.get(CourseJsonKey.SIGNATORY_LIST))); CourseJsonKey.ISSUER, mapper.writeValueAsString(templateDetails.get(CourseJsonKey.SIGNATORY_LIST)));
} }
if (MapUtils.isNotEmpty((Map<String,Object>)template.get(CourseJsonKey.NOTIFY_TEMPLATE))) { 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( template.put(
CourseJsonKey.NOTIFY_TEMPLATE, CourseJsonKey.NOTIFY_TEMPLATE,
mapper.writeValueAsString(template.get(CourseJsonKey.NOTIFY_TEMPLATE))); mapper.writeValueAsString(notifyData));
} }
if (MapUtils.isNotEmpty((Map<String,Object>)template.get(CourseJsonKey.ADDITIONAL_PROPS))) { if (MapUtils.isNotEmpty((Map<String,Object>)template.get(CourseJsonKey.ADDITIONAL_PROPS))) {
template.put( template.put(
...@@ -134,6 +141,17 @@ public class CourseBatchCertificateActor extends BaseActor { ...@@ -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) { private Map<String, Object> mapESFieldsToObject(Map<String, Object> courseBatch) {
Map<String, Map<String, Object>> certificateTemplates = Map<String, Map<String, Object>> certificateTemplates =
(Map<String, Map<String, Object>>) (Map<String, Map<String, Object>>)
...@@ -174,11 +192,14 @@ public class CourseBatchCertificateActor extends BaseActor { ...@@ -174,11 +192,14 @@ public class CourseBatchCertificateActor extends BaseActor {
new TypeReference<HashMap<String, Object>>() { 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( template.put(
CourseJsonKey.NOTIFY_TEMPLATE, CourseJsonKey.NOTIFY_TEMPLATE,
mapper.readValue( mapper.readValue(notifyTemplateData,
(String) template.get(CourseJsonKey.NOTIFY_TEMPLATE),
new TypeReference<HashMap<String, Object>>() { new TypeReference<HashMap<String, Object>>() {
})); }));
} }
......
...@@ -19,8 +19,6 @@ import org.sunbird.cassandra.CassandraOperation; ...@@ -19,8 +19,6 @@ import org.sunbird.cassandra.CassandraOperation;
import org.sunbird.common.exception.ProjectCommonException; import org.sunbird.common.exception.ProjectCommonException;
import org.sunbird.common.models.response.Response; import org.sunbird.common.models.response.Response;
import org.sunbird.common.models.util.JsonKey; 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.ProjectUtil;
import org.sunbird.common.models.util.TelemetryEnvKey; import org.sunbird.common.models.util.TelemetryEnvKey;
import org.sunbird.common.request.Request; import org.sunbird.common.request.Request;
...@@ -234,7 +232,12 @@ public class QRCodeDownloadManagementActor extends BaseActor { ...@@ -234,7 +232,12 @@ public class QRCodeDownloadManagementActor extends BaseActor {
if (null != obj && obj instanceof List) { if (null != obj && obj instanceof List) {
List<Map<String, Object>> listOfMap = (List<Map<String, Object>>) obj; List<Map<String, Object>> listOfMap = (List<Map<String, Object>>) obj;
if (CollectionUtils.isNotEmpty(listOfMap)) { 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 { ...@@ -263,22 +266,15 @@ public class QRCodeDownloadManagementActor extends BaseActor {
objectKey += file.getName(); objectKey += file.getName();
//CSP related changes //CSP related changes
String cloudStorage = getConfigValue(CONTENT_CLOUD_STORAGE_TYPE); String cloudStorage = getConfigValue(CONTENT_CLOUD_STORAGE_TYPE);
CloudStorageUtil.CloudStorageType storageType; if (cloudStorage == null) {
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 {
ProjectCommonException.throwClientErrorException( ProjectCommonException.throwClientErrorException(
ResponseCode.errorUnsupportedCloudStorage, ResponseCode.errorUnsupportedCloudStorage,
ProjectUtil.formatMessage( ProjectUtil.formatMessage(
ResponseCode.errorUnsupportedCloudStorage.getErrorMessage(), cloudStorage)); ResponseCode.errorUnsupportedCloudStorage.getErrorMessage()));
return null; return null;
} }
String fileUrl = String fileUrl =
CloudStorageUtil.upload(storageType, CloudStorageUtil.upload(cloudStorage,
getConfigValue(CONTENT_CLOUD_STORAGE_CONTAINER), getConfigValue(CONTENT_CLOUD_STORAGE_CONTAINER),
objectKey, objectKey,
file.getAbsolutePath()); file.getAbsolutePath());
......
...@@ -8,7 +8,6 @@ import org.sunbird.common.models.response.Response; ...@@ -8,7 +8,6 @@ import org.sunbird.common.models.response.Response;
import org.sunbird.common.models.util.ActorOperations; import org.sunbird.common.models.util.ActorOperations;
import org.sunbird.common.models.util.BulkUploadJsonKey; import org.sunbird.common.models.util.BulkUploadJsonKey;
import org.sunbird.common.models.util.JsonKey; 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.ProjectUtil;
import org.sunbird.common.models.util.PropertiesCache; import org.sunbird.common.models.util.PropertiesCache;
import org.sunbird.common.models.util.TelemetryEnvKey; import org.sunbird.common.models.util.TelemetryEnvKey;
...@@ -16,7 +15,6 @@ import org.sunbird.common.request.Request; ...@@ -16,7 +15,6 @@ import org.sunbird.common.request.Request;
import org.sunbird.common.request.RequestContext; import org.sunbird.common.request.RequestContext;
import org.sunbird.common.responsecode.ResponseCode; import org.sunbird.common.responsecode.ResponseCode;
import org.sunbird.common.util.CloudStorageUtil; import org.sunbird.common.util.CloudStorageUtil;
import org.sunbird.common.util.CloudStorageUtil.CloudStorageType;
import org.sunbird.helper.ServiceFactory; import org.sunbird.helper.ServiceFactory;
import org.sunbird.learner.actors.bulkupload.dao.impl.BulkUploadProcessDaoImpl; import org.sunbird.learner.actors.bulkupload.dao.impl.BulkUploadProcessDaoImpl;
import org.sunbird.learner.actors.bulkupload.model.BulkUploadProcess; import org.sunbird.learner.actors.bulkupload.model.BulkUploadProcess;
...@@ -74,8 +72,8 @@ public class BulkUploadManagementActor extends BaseBulkUploadActor { ...@@ -74,8 +72,8 @@ public class BulkUploadManagementActor extends BaseBulkUploadActor {
private void getBulkUploadDownloadStatusLink(Request actorMessage) { private void getBulkUploadDownloadStatusLink(Request actorMessage) {
String processId = (String) actorMessage.getRequest().get(JsonKey.PROCESS_ID); String processId = (String) actorMessage.getRequest().get(JsonKey.PROCESS_ID);
BulkUploadProcessDaoImpl bulkuploadDao = new BulkUploadProcessDaoImpl(); BulkUploadProcessDaoImpl bulkUploadDao = new BulkUploadProcessDaoImpl();
BulkUploadProcess bulkUploadProcess = bulkuploadDao.read(actorMessage.getRequestContext(), processId); BulkUploadProcess bulkUploadProcess = bulkUploadDao.read(actorMessage.getRequestContext(), processId);
if (bulkUploadProcess != null) { if (bulkUploadProcess != null) {
try { try {
...@@ -86,7 +84,7 @@ public class BulkUploadManagementActor extends BaseBulkUploadActor { ...@@ -86,7 +84,7 @@ public class BulkUploadManagementActor extends BaseBulkUploadActor {
} }
String signedUrl = String signedUrl =
CloudStorageUtil.getSignedUrl( CloudStorageUtil.getSignedUrl(
CloudStorageType.getByName(cloudStorageData.getStorageType()), cloudStorageData.getStorageType(),
cloudStorageData.getContainer(), cloudStorageData.getContainer(),
cloudStorageData.getFileName()); cloudStorageData.getFileName());
Response response = new Response(); Response response = new Response();
......
...@@ -219,7 +219,7 @@ ...@@ -219,7 +219,7 @@
<dependency> <dependency>
<groupId>org.sunbird</groupId> <groupId>org.sunbird</groupId>
<artifactId>cloud-store-sdk</artifactId> <artifactId>cloud-store-sdk</artifactId>
<version>1.4.3</version> <version>1.4.4</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>com.sun.jersey</groupId> <groupId>com.sun.jersey</groupId>
......
package org.sunbird.common.models.util; package org.sunbird.common.models.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
...@@ -789,6 +788,8 @@ public final class JsonKey { ...@@ -789,6 +788,8 @@ public final class JsonKey {
public static final String GCLOUD_STR = "gcloud"; public static final String GCLOUD_STR = "gcloud";
public static final String CLOUD_FOLDER_CONTENT = "sunbird_cloud_content_folder"; 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 TO_URL = "toUrl";
public static final String TTL = "ttl"; public static final String TTL = "ttl";
public static final String TEXTBOOK_TOC_CSV_TTL = "sunbird_texbook_toc_csv_ttl"; public static final String TEXTBOOK_TOC_CSV_TTL = "sunbird_texbook_toc_csv_ttl";
...@@ -1070,7 +1071,8 @@ public final class JsonKey { ...@@ -1070,7 +1071,8 @@ public final class JsonKey {
public static final String OLD_CREATED_DATE = "oldCreatedDate"; public static final String OLD_CREATED_DATE = "oldCreatedDate";
public static final String X_LOGGING_HEADERS = "X_LOGGING_HEADERS"; public static final String X_LOGGING_HEADERS = "X_LOGGING_HEADERS";
public static final String LAST_CONTENT_ACCESS_TIME = "lastcontentaccesstime"; public static final String LAST_CONTENT_ACCESS_TIME = "lastcontentaccesstime";
public static final String GCP="gcloud";
public static final String TEMPLATE_URL = "templateUrl";
private JsonKey() {} private JsonKey() {}
} }
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);
}
}
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;
}
}
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;
}
}
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);
}
}
/** */
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;
}
}
/** */
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;
}
}
/** */
/** @author Manzarul */
package org.sunbird.common.models.util.azure;
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);
}
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);
}
}
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;
}
}
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;
}
}
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);
}
}
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;
}
}
...@@ -2,60 +2,24 @@ package org.sunbird.common.util; ...@@ -2,60 +2,24 @@ package org.sunbird.common.util;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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.StorageConfig;
import org.sunbird.cloud.storage.factory.StorageServiceFactory; 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.JsonKey;
import org.sunbird.common.models.util.ProjectUtil; import org.sunbird.common.models.util.ProjectUtil;
import org.sunbird.common.models.util.PropertiesCache; import org.sunbird.common.models.util.PropertiesCache;
import org.sunbird.common.responsecode.ResponseCode;
import scala.Option; import scala.Option;
import scala.Some; import scala.Some;
import static org.sunbird.common.models.util.JsonKey.*;
public class CloudStorageUtil { public class CloudStorageUtil {
private static final int STORAGE_SERVICE_API_RETRY_COUNT = 3; private static final int STORAGE_SERVICE_API_RETRY_COUNT = 3;
private static final Map<String, IStorageService> storageServiceMap = new HashMap<>(); private static final Map<String, BaseStorageService> 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;
}
}
}
public static String upload( 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( return storageService.upload(
container, container,
...@@ -68,49 +32,35 @@ public class CloudStorageUtil { ...@@ -68,49 +32,35 @@ public class CloudStorageUtil {
} }
public static String getSignedUrl( public static String getSignedUrl(
CloudStorageType storageType, String container, String objectKey) { String storageType, String container, String objectKey) {
IStorageService storageService = getStorageService(storageType); BaseStorageService storageService = getStorageService(storageType);
return getSignedUrl(storageService, storageType, container, objectKey); return getSignedUrl(storageService, container, objectKey,storageType);
}
public static String getAnalyticsSignedUrl(
CloudStorageType storageType, String container, String objectKey) {
IStorageService analyticsStorageService = getAnalyticsStorageService(storageType);
return getSignedUrl(analyticsStorageService, storageType, container, objectKey);
} }
public static String getSignedUrl( public static String getSignedUrl(
IStorageService storageService, BaseStorageService storageService,
CloudStorageType storageType, String container,
String container, String objectKey, String cloudType) {
String objectKey) { return storageService.getSignedURLV2(container, objectKey, Some.apply(getTimeoutInSeconds()),
int timeoutInSeconds = getTimeoutInSeconds(); Some.apply("r"), Some.apply("application/pdf"));
return storageService.getSignedURL(
container, objectKey, Some.apply(timeoutInSeconds), Some.apply("r"));
} }
private static IStorageService getStorageService(CloudStorageType storageType) { private static BaseStorageService getStorageService(String storageType) {
String storageKey = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_NAME); String storageKey = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_NAME);
String storageSecret = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_KEY); String storageSecret = PropertiesCache.getInstance().getProperty(JsonKey.ACCOUNT_KEY);
return getStorageService(storageType, storageKey, storageSecret); return getStorageService(storageType, storageKey, storageSecret);
} }
private static IStorageService getAnalyticsStorageService(CloudStorageType storageType) { private static BaseStorageService getStorageService(
String storageKey = PropertiesCache.getInstance().getProperty(JsonKey.ANALYTICS_ACCOUNT_NAME); String storageType, String storageKey, String storageSecret) {
String storageSecret = PropertiesCache.getInstance().getProperty(JsonKey.ANALYTICS_ACCOUNT_KEY); String compositeKey = storageType + "-" + storageKey;
return getStorageService(storageType, storageKey, storageSecret);
}
private static IStorageService getStorageService(
CloudStorageType storageType, String storageKey, String storageSecret) {
String compositeKey = storageType.getType() + "-" + storageKey;
if (storageServiceMap.containsKey(compositeKey)) { if (storageServiceMap.containsKey(compositeKey)) {
return storageServiceMap.get(compositeKey); return storageServiceMap.get(compositeKey);
} }
synchronized (CloudStorageUtil.class) { synchronized (CloudStorageUtil.class) {
StorageConfig storageConfig = StorageConfig storageConfig =
new StorageConfig(storageType.getType(), storageKey, storageSecret); new StorageConfig(storageType, storageKey, storageSecret);
IStorageService storageService = StorageServiceFactory.getStorageService(storageConfig); BaseStorageService storageService = StorageServiceFactory.getStorageService(storageConfig);
storageServiceMap.put(compositeKey, storageService); storageServiceMap.put(compositeKey, storageService);
} }
return storageServiceMap.get(compositeKey); return storageServiceMap.get(compositeKey);
...@@ -122,8 +72,8 @@ public class CloudStorageUtil { ...@@ -122,8 +72,8 @@ public class CloudStorageUtil {
} }
public static String getUri( public static String getUri(
CloudStorageType storageType, String container, String prefix, boolean isDirectory) { String storageType, String container, String prefix, boolean isDirectory) {
IStorageService storageService = getStorageService(storageType); BaseStorageService storageService = getStorageService(storageType);
return storageService.getUri(container, prefix, Option.apply(isDirectory)); return storageService.getUri(container, prefix, Option.apply(isDirectory));
} }
} }
...@@ -205,4 +205,6 @@ kafka_assessment_topic= ...@@ -205,4 +205,6 @@ kafka_assessment_topic=
sunbird_msg_sender= sunbird_msg_sender=
sunbird_msg_91_auth= sunbird_msg_91_auth=
sunbird_api_mgr_base_url=https://dev.sunbirded.org/api sunbird_api_mgr_base_url=https://dev.sunbirded.org/api
enrollment_list_size=1000 enrollment_list_size=1000
\ No newline at end of file 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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment