Skip to content

Commit

Permalink
fix: fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikRevich committed Nov 23, 2024
1 parent 82fdb35 commit 03a78e1
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -314,6 +313,5 @@ public void upload(String location, String hash, ValidationSecretsApplication va
} catch (RepositoryOperationFailureException e) {
throw new RepositoryContentApplicationFailureException(e.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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(""));
}

/**
Expand Down

0 comments on commit 03a78e1

Please sign in to comment.