Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/s3 client rm url override #59

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import de.muenchen.refarch.integration.s3.client.api.FolderApiApi;
import de.muenchen.refarch.integration.s3.client.domain.model.SupportedFileExtensions;
import de.muenchen.refarch.integration.s3.client.properties.S3IntegrationClientProperties;
import de.muenchen.refarch.integration.s3.client.service.ApiClientFactory;
import de.muenchen.refarch.integration.s3.client.service.FileService;
import de.muenchen.refarch.integration.s3.client.service.S3DomainProvider;
import de.muenchen.refarch.integration.s3.client.service.S3StorageUrlProvider;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,7 +16,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
Expand All @@ -30,21 +26,6 @@
@ComponentScan(
basePackages = {
"de.muenchen.refarch.integration.s3.client"
},
excludeFilters = {
@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
classes = {
/*
* Exclude to avoid multiple instantiation of multiple beans with same name.
* This class is instantiated in {@link S3IntegrationClientAutoConfiguration}
* to give the bean another name.
*/
ApiClient.class,
FileApiApi.class,
FolderApiApi.class
}
)
}
)
@RequiredArgsConstructor
Expand All @@ -62,20 +43,20 @@ public void init() {

@Bean
@ConditionalOnProperty(prefix = "refarch.s3.client", name = "enable-security", havingValue = "true")
public ApiClientFactory securedApiClientFactory(final ClientRegistrationRepository clientRegistrationRepository,
public ApiClient securedApiClient(final ClientRegistrationRepository clientRegistrationRepository,
final OAuth2AuthorizedClientService authorizedClientService) {
return new ApiClientFactory(
this.webClient(clientRegistrationRepository, authorizedClientService));
return new ApiClient(
this.authenticatedWebClient(clientRegistrationRepository, authorizedClientService));
}

@Bean
@ConditionalOnProperty(prefix = "refarch.s3.client", name = "enable-security", havingValue = "false", matchIfMissing = true)
public ApiClientFactory apiClientFactory() {
return new ApiClientFactory(
public ApiClient apiClient() {
return new ApiClient(
WebClient.builder().build());
}

private WebClient webClient(
private WebClient authenticatedWebClient(
final ClientRegistrationRepository clientRegistrationRepository,
final OAuth2AuthorizedClientService authorizedClientService) {
final var oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(
Expand All @@ -102,7 +83,7 @@ public FileService fileService(final SupportedFileExtensions supportedFileExtens

/**
* Instance of a {@link FileService} containing supported file extensions configured within in the
* 'de.muenchen.oss.digiwf.s3' scope.
* 'de.muenchen.refarch.s3' scope.
*
* @return {@link FileService} for managing file extensions.
*/
Expand All @@ -113,30 +94,15 @@ public FileService fileServiceFromS3IntegrationClientProperties() {
this.s3IntegrationClientProperties.getMaxBatchSize());
}

/**
* Instance of an {@link S3StorageUrlProvider} containing an externally created
* {@link S3DomainProvider} for retrieving the S3 storage URL.
*
* @param s3DomainProvider Provider of domain specific S3 storages configured in process
* configurations.
* @return Provider of the S3 storage URL.
*/
@Bean
@ConditionalOnBean(S3DomainProvider.class)
public S3StorageUrlProvider s3StorageUrlProvider(final S3DomainProvider s3DomainProvider) {
return new S3StorageUrlProvider(s3DomainProvider, this.s3IntegrationClientProperties.getDocumentStorageUrl());
@ConditionalOnMissingBean
public FileApiApi fileApiApi(final ApiClient apiClient) {
return new FileApiApi(apiClient);
}

/**
* Instance of an {@link S3StorageUrlProvider} containing a default {@link S3DomainProvider}. The
* instance will only return the default S3 URL.
*
* @return Provider of the S3 storage URL.
*/
@Bean
@ConditionalOnMissingBean(S3DomainProvider.class)
public S3StorageUrlProvider s3StorageUrlProviderWithoutDomainProvider() {
return new S3StorageUrlProvider(this.s3IntegrationClientProperties.getDocumentStorageUrl());
@ConditionalOnMissingBean
public FolderApiApi folderApiApi(final ApiClient apiClient) {
return new FolderApiApi(apiClient);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import de.muenchen.refarch.integration.s3.client.model.FileSizeDto;
import de.muenchen.refarch.integration.s3.client.repository.presignedurl.PresignedUrlRepository;
import de.muenchen.refarch.integration.s3.client.repository.transfer.S3FileTransferRepository;
import de.muenchen.refarch.integration.s3.client.service.ApiClientFactory;
import java.io.InputStream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,43 +25,40 @@ public class DocumentStorageFileRepository {

private final S3FileTransferRepository s3FileTransferRepository;

private final ApiClientFactory apiClientFactory;
private final FileApiApi fileApi;

/**
* Gets the file specified in the parameter from the document storage.
*
* @param pathToFile defines the path to the file.
* @param expireInMinutes the expiration time of the presignedURL in minutes.
* @param documentStorageUrl to define to which document storage the request goes.
* @return the file.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public byte[] getFile(final String pathToFile, final int expireInMinutes, final String documentStorageUrl)
public byte[] getFile(final String pathToFile, final int expireInMinutes)
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final Mono<String> presignedUrl = this.presignedUrlRepository.getPresignedUrlGetFile(pathToFile, expireInMinutes, documentStorageUrl);
final Mono<String> presignedUrl = this.presignedUrlRepository.getPresignedUrlGetFile(pathToFile, expireInMinutes);
return this.s3FileTransferRepository.getFile(presignedUrl.block());
}

/**
* Retrieves the file size of a file specified in the parameter from the document storage.
*
* @param pathToFile defines the path to the file.
* @param documentStorageUrl defines to which document storage the request goes.
* @return the file size.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public Mono<Long> getFileSize(final String pathToFile, final String documentStorageUrl)
public Mono<Long> getFileSize(final String pathToFile)
throws DocumentStorageClientErrorException, DocumentStorageServerErrorException, DocumentStorageException {
try {
final FileApiApi fileApi = this.apiClientFactory.getFileApiForDocumentStorageUrl(documentStorageUrl);
return fileApi.getFileSize(pathToFile).mapNotNull(FileSizeDto::getFileSize);
} catch (final HttpClientErrorException exception) {
final String message = String.format("The request to get file size failed %s.", exception.getStatusCode());
Expand All @@ -84,17 +80,16 @@ public Mono<Long> getFileSize(final String pathToFile, final String documentStor
*
* @param pathToFile defines the path to the file.
* @param expireInMinutes the expiration time of the presignedURL in minutes.
* @param documentStorageUrl to define to which document storage the request goes.
* @return the InputStream for the file.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public InputStream getFileInputStream(final String pathToFile, final int expireInMinutes, final String documentStorageUrl)
public InputStream getFileInputStream(final String pathToFile, final int expireInMinutes)
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final Mono<String> presignedUrl = this.presignedUrlRepository.getPresignedUrlGetFile(pathToFile, expireInMinutes, documentStorageUrl);
final Mono<String> presignedUrl = this.presignedUrlRepository.getPresignedUrlGetFile(pathToFile, expireInMinutes);
return this.s3FileTransferRepository.getFileInputStream(presignedUrl.block());
}

Expand All @@ -104,16 +99,15 @@ public InputStream getFileInputStream(final String pathToFile, final int expireI
* @param pathToFile defines the path to the file.
* @param file to save.
* @param expireInMinutes the expiration time of the presignedURL in minutes.
* @param documentStorageUrl to define to which document storage the request goes.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public void saveFile(final String pathToFile, final byte[] file, final int expireInMinutes,
final String documentStorageUrl) throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlSaveFile(pathToFile, expireInMinutes, documentStorageUrl);
public void saveFile(final String pathToFile, final byte[] file, final int expireInMinutes)
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlSaveFile(pathToFile, expireInMinutes);
this.s3FileTransferRepository.saveFile(presignedUrl, file);
}

Expand All @@ -123,16 +117,15 @@ public void saveFile(final String pathToFile, final byte[] file, final int expir
* @param pathToFile defines the path to the file.
* @param file to save.
* @param expireInMinutes the expiration time of the presignedURL in minutes.
* @param documentStorageUrl to define to which document storage the request goes.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public void saveFileInputStream(final String pathToFile, final InputStream file, final int expireInMinutes,
final String documentStorageUrl) throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlSaveFile(pathToFile, expireInMinutes, documentStorageUrl);
public void saveFileInputStream(final String pathToFile, final InputStream file, final int expireInMinutes)
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlSaveFile(pathToFile, expireInMinutes);
this.s3FileTransferRepository.saveFileInputStream(presignedUrl, file);
}

Expand All @@ -142,16 +135,15 @@ public void saveFileInputStream(final String pathToFile, final InputStream file,
* @param pathToFile defines the path to the file.
* @param file which overwrites the file in the document storage.
* @param expireInMinutes the expiration time of the presignedURL in minutes.
* @param documentStorageUrl to define to which document storage the request goes.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public void updateFile(final String pathToFile, final byte[] file, final int expireInMinutes,
final String documentStorageUrl) throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlUpdateFile(pathToFile, expireInMinutes, documentStorageUrl);
public void updateFile(final String pathToFile, final byte[] file, final int expireInMinutes)
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlUpdateFile(pathToFile, expireInMinutes);
this.s3FileTransferRepository.updateFile(presignedUrl, file);
}

Expand All @@ -161,16 +153,15 @@ public void updateFile(final String pathToFile, final byte[] file, final int exp
* @param pathToFile defines the path to the file.
* @param file which overwrites the file in the document storage.
* @param expireInMinutes the expiration time of the presignedURL in minutes.
* @param documentStorageUrl to define to which document storage the request goes.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public void updateFileInputStream(final String pathToFile, final InputStream file, final int expireInMinutes,
final String documentStorageUrl) throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlUpdateFile(pathToFile, expireInMinutes, documentStorageUrl);
public void updateFileInputStream(final String pathToFile, final InputStream file, final int expireInMinutes)
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlUpdateFile(pathToFile, expireInMinutes);
this.s3FileTransferRepository.updateFileInputStream(presignedUrl, file);
}

Expand All @@ -179,16 +170,15 @@ public void updateFileInputStream(final String pathToFile, final InputStream fil
*
* @param pathToFile defines the path to the file.
* @param expireInMinutes the expiration time of the presignedURL in minutes.
* @param documentStorageUrl to define to which document storage the request goes.
* @throws DocumentStorageClientErrorException if the problem is with the client.
* @throws DocumentStorageServerErrorException if the problem is with the S3 storage or document
* storage.
* @throws DocumentStorageException if the problem cannot be assigned to either the client or the S3
* storage or the document storage.
*/
public void deleteFile(final String pathToFile, final int expireInMinutes, final String documentStorageUrl)
public void deleteFile(final String pathToFile, final int expireInMinutes)
throws DocumentStorageException, DocumentStorageClientErrorException, DocumentStorageServerErrorException {
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlDeleteFile(pathToFile, expireInMinutes, documentStorageUrl);
final String presignedUrl = this.presignedUrlRepository.getPresignedUrlDeleteFile(pathToFile, expireInMinutes);
this.s3FileTransferRepository.deleteFile(presignedUrl);
}

Expand Down
Loading