From 03a78e15f992cdcc389500190170e2883439e058 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi Date: Sat, 23 Nov 2024 23:48:26 +0100 Subject: [PATCH] fix: fixed bugs --- ...sorContentApplicationFailureException.java | 21 ++++++ ...cessorContentDownloadFailureException.java | 21 ++++++ ...rocessorContentUploadFailureException.java | 21 ++++++ .../repository/facade/RepositoryFacade.java | 8 +-- .../service/processor/ProcessorService.java | 72 +++++++++++++++---- .../workspace/facade/WorkspaceFacade.java | 31 ++++---- 6 files changed, 142 insertions(+), 32 deletions(-) create mode 100644 api-server/src/main/java/com/objectstorage/exception/ProcessorContentApplicationFailureException.java create mode 100644 api-server/src/main/java/com/objectstorage/exception/ProcessorContentDownloadFailureException.java create mode 100644 api-server/src/main/java/com/objectstorage/exception/ProcessorContentUploadFailureException.java diff --git a/api-server/src/main/java/com/objectstorage/exception/ProcessorContentApplicationFailureException.java b/api-server/src/main/java/com/objectstorage/exception/ProcessorContentApplicationFailureException.java new file mode 100644 index 0000000..1ce36a9 --- /dev/null +++ b/api-server/src/main/java/com/objectstorage/exception/ProcessorContentApplicationFailureException.java @@ -0,0 +1,21 @@ +package com.objectstorage.exception; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Formatter; + +/** + * Represents exception used when processor content application operation fails. + */ +public class ProcessorContentApplicationFailureException extends IOException { + public ProcessorContentApplicationFailureException() { + this(""); + } + + public ProcessorContentApplicationFailureException(Object... message) { + super( + new Formatter() + .format("ObjectStorage processor content application failed: %s", Arrays.stream(message).toArray()) + .toString()); + } +} diff --git a/api-server/src/main/java/com/objectstorage/exception/ProcessorContentDownloadFailureException.java b/api-server/src/main/java/com/objectstorage/exception/ProcessorContentDownloadFailureException.java new file mode 100644 index 0000000..012a7cb --- /dev/null +++ b/api-server/src/main/java/com/objectstorage/exception/ProcessorContentDownloadFailureException.java @@ -0,0 +1,21 @@ +package com.objectstorage.exception; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Formatter; + +/** + * Represents exception used when processor content download operation fails. + */ +public class ProcessorContentDownloadFailureException extends IOException { + public ProcessorContentDownloadFailureException() { + this(""); + } + + public ProcessorContentDownloadFailureException(Object... message) { + super( + new Formatter() + .format("ObjectStorage processor content download failed: %s", Arrays.stream(message).toArray()) + .toString()); + } +} diff --git a/api-server/src/main/java/com/objectstorage/exception/ProcessorContentUploadFailureException.java b/api-server/src/main/java/com/objectstorage/exception/ProcessorContentUploadFailureException.java new file mode 100644 index 0000000..e9d503b --- /dev/null +++ b/api-server/src/main/java/com/objectstorage/exception/ProcessorContentUploadFailureException.java @@ -0,0 +1,21 @@ +package com.objectstorage.exception; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Formatter; + +/** + * Represents exception used when processor content upload operation fails. + */ +public class ProcessorContentUploadFailureException extends IOException { + public ProcessorContentUploadFailureException() { + this(""); + } + + public ProcessorContentUploadFailureException(Object... message) { + super( + new Formatter() + .format("ObjectStorage processor content upload failed: %s", Arrays.stream(message).toArray()) + .toString()); + } +} diff --git a/api-server/src/main/java/com/objectstorage/repository/facade/RepositoryFacade.java b/api-server/src/main/java/com/objectstorage/repository/facade/RepositoryFacade.java index 28ca913..f14f80c 100644 --- a/api-server/src/main/java/com/objectstorage/repository/facade/RepositoryFacade.java +++ b/api-server/src/main/java/com/objectstorage/repository/facade/RepositoryFacade.java @@ -267,16 +267,15 @@ public void destroy(ValidationSecretsApplication validationSecretsApplication) t } /** - * Applies given content application, updating previous state. + * Applies given temporate upload application. * * @param location given object location. * @param hash given object hash. - * @param validationSecretsApplication given validation secrets application. + * @param validationSecretsUnit given validation secrets unit. * @throws RepositoryContentApplicationFailureException if ObjectStorage repository content application failed. */ - public void upload(String location, String hash, ValidationSecretsApplication validationSecretsApplication) + public void upload(String location, String hash, ValidationSecretsUnit validationSecretsUnit) throws RepositoryContentApplicationFailureException { - for (ValidationSecretsUnit validationSecretsUnit : validationSecretsApplication.getSecrets()) { ProviderEntity provider; try { @@ -314,6 +313,5 @@ public void upload(String location, String hash, ValidationSecretsApplication va } catch (RepositoryOperationFailureException e) { throw new RepositoryContentApplicationFailureException(e.getMessage()); } - } } } diff --git a/api-server/src/main/java/com/objectstorage/service/processor/ProcessorService.java b/api-server/src/main/java/com/objectstorage/service/processor/ProcessorService.java index 1132214..e2edc58 100644 --- a/api-server/src/main/java/com/objectstorage/service/processor/ProcessorService.java +++ b/api-server/src/main/java/com/objectstorage/service/processor/ProcessorService.java @@ -1,11 +1,8 @@ package com.objectstorage.service.processor; import com.objectstorage.entity.common.PropertiesEntity; -import com.objectstorage.exception.FileCreationFailureException; -import com.objectstorage.model.ContentDownload; -import com.objectstorage.model.ContentRetrievalResult; -import com.objectstorage.model.ValidationSecretsApplication; -import com.objectstorage.model.ValidationSecretsUnit; +import com.objectstorage.exception.*; +import com.objectstorage.model.*; import com.objectstorage.repository.facade.RepositoryFacade; import com.objectstorage.service.config.ConfigService; import com.objectstorage.service.telemetry.TelemetryService; @@ -140,37 +137,86 @@ public ContentRetrievalResult retrieveContent(ValidationSecretsApplication valid } /** - * Uploads given content, adding provided input to ObjectStorage Temporate Storage, which will then be processed and + * Applies given content, adding provided input to ObjectStorage Temporate Storage, which will then be processed and * added to selected provider. * + * @param contentApplication given content application. + * @param validationSecretsApplication given content application. + * @throws ProcessorContentApplicationFailureException if content application operation fails. + */ + public void apply(ContentApplication contentApplication, ValidationSecretsApplication validationSecretsApplication) + throws ProcessorContentApplicationFailureException { + for (ValidationSecretsUnit validationSecretsUnit : validationSecretsApplication.getSecrets()) { + try { + if (vendorFacade.isBucketPresent( + validationSecretsUnit.getProvider(), + validationSecretsUnit.getCredentials().getExternal(), + contentApplication.getRoot())) { + continue; + } + } catch (SecretsConversionException e) { + throw new ProcessorContentApplicationFailureException(e.getMessage()); + } + + try { + vendorFacade.createBucket( + validationSecretsUnit.getProvider(), + validationSecretsUnit.getCredentials().getExternal(), + contentApplication.getRoot()); + } catch (SecretsConversionException e) { + throw new ProcessorContentApplicationFailureException(e.getMessage()); + } + } + + try { + repositoryFacade.apply(contentApplication, validationSecretsApplication); + } catch (RepositoryContentApplicationFailureException e) { + throw new ProcessorContentApplicationFailureException(e.getMessage()); + } + } + + /** + * Uploads given content, adding provided input to ObjectStorage Temporate Storage, which will then be processed and + * added to configured providers. + * * @param location given file location. * @param file given input file stream. * @param validationSecretsApplication given content application. + * @throws ProcessorContentUploadFailureException if content upload operation fails. */ - public void upload(String location, InputStream file, ValidationSecretsApplication validationSecretsApplication) { + public void upload(String location, InputStream file, ValidationSecretsApplication validationSecretsApplication) + throws ProcessorContentUploadFailureException { logger.info(String.format("Uploading content at '%s' location", location)); + String workspaceUnitKey = + workspaceFacade.createWorkspaceUnitKey(validationSecretsApplication); + try { workspaceFacade.addFile(workspaceUnitKey, location, file); } catch (FileCreationFailureException e) { - throw new RuntimeException(e); + throw new ProcessorContentUploadFailureException(e.getMessage()); } for (ValidationSecretsUnit validationSecretsUnit : validationSecretsApplication.getSecrets()) { - String workspaceUnitKey = - workspaceFacade.createWorkspaceUnitKey( - validationSecretsUnit.getProvider(), validationSecretsUnit.getCredentials()); + try { + repositoryFacade.upload(location, "hash", validationSecretsUnit); + } catch (RepositoryContentApplicationFailureException e) { + throw new ProcessorContentUploadFailureException(e.getMessage()); + } } } /** - * Downloads given content with the help of the given content download application. + * Downloads given content with the help of the given content download application from Temporate Storage or + * configured provider. * * @param contentDownload given content download application. * @param validationSecretsApplication given content application. * @return downloaded content. + * @throws ProcessorContentDownloadFailureException if content download operation fails. */ - public byte[] download(String location, ValidationSecretsUnit validationSecretsUnit) { + public byte[] download(String location, ValidationSecretsUnit validationSecretsUnit) + throws ProcessorContentDownloadFailureException { logger.info(String.format("Downloading content for '%s' location", location)); String workspaceUnitKey = diff --git a/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java b/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java index 1ec62b9..a0f3254 100644 --- a/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java +++ b/api-server/src/main/java/com/objectstorage/service/workspace/facade/WorkspaceFacade.java @@ -5,6 +5,7 @@ import com.objectstorage.exception.FileNotFoundException; import com.objectstorage.model.CredentialsFieldsFull; import com.objectstorage.model.Provider; +import com.objectstorage.model.ValidationSecretsApplication; import com.objectstorage.service.workspace.WorkspaceService; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -13,6 +14,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * Provides high-level access to workspace operations. @@ -28,22 +30,23 @@ public class WorkspaceFacade { /** * Creates workspace unit key with the help of the given provider and credentials. * - * @param provider given provider. - * @param credentialsFields given credentials. + * @param validationSecretsApplication given validation secrets application. * @return created workspace unit key. */ - public String createWorkspaceUnitKey(Provider provider, CredentialsFieldsFull credentialsFields) { - return switch (provider) { - case S3 -> workspaceService.createUnitKey( - provider.name(), - String.valueOf(credentialsFields.getInternal().getId()), - credentialsFields.getExternal().getFile(), - credentialsFields.getExternal().getRegion()); - case GCS -> workspaceService.createUnitKey( - provider.name(), - String.valueOf(credentialsFields.getInternal().getId()), - credentialsFields.getExternal().getFile()); - }; + public String createWorkspaceUnitKey(ValidationSecretsApplication validationSecretsApplication) { + return validationSecretsApplication.getSecrets().stream().map(element -> + switch (element.getProvider()) { + case S3 -> workspaceService.createUnitKey( + element.getProvider().name(), + String.valueOf(element.getCredentials().getInternal().getId()), + element.getCredentials().getExternal().getFile(), + element.getCredentials().getExternal().getRegion()); + case GCS -> workspaceService.createUnitKey( + element.getProvider().name(), + String.valueOf(element.getCredentials().getInternal().getId()), + element.getCredentials().getExternal().getFile()); + }) + .collect(Collectors.joining("")); } /**