From 6d1c19d4f24984fbe1f007c4485a22feb6576ac5 Mon Sep 17 00:00:00 2001 From: mju Date: Thu, 28 Apr 2022 14:13:17 +0200 Subject: [PATCH] CodeStyle: Renamed functions, organized imports. Fixes: MISC-16 --- src/main/java/ninja/AwsUpstream.java | 28 ++++++++++++++----------- src/main/java/ninja/S3Dispatcher.java | 30 +++++++++++++++++++++------ 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main/java/ninja/AwsUpstream.java b/src/main/java/ninja/AwsUpstream.java index e20a845..705d2af 100644 --- a/src/main/java/ninja/AwsUpstream.java +++ b/src/main/java/ninja/AwsUpstream.java @@ -29,6 +29,8 @@ */ @Register(classes = AwsUpstream.class) public class AwsUpstream { + private static final String FALLBACK_REGION = "EU"; + private static final int SOCKET_TIMEOUT = 60 * 1000 * 5; /** * The secret key to connect to the upstream S3 instance. * When this value is not set, the proxy functionality is not enabled. @@ -82,7 +84,7 @@ public boolean isConfigured() { * @return client instance to upstream instance * @throws IllegalStateException if called when not configured */ - public AmazonS3 getClient() throws IllegalStateException { + public AmazonS3 fetchClient() throws IllegalStateException { if (client == null) { client = createAWSClient(); } @@ -93,23 +95,25 @@ private AmazonS3 createAWSClient() throws IllegalStateException { if (!isConfigured()) { throw new IllegalStateException("Use of not configured instance"); } - AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(s3AccessKey, s3SecretKey)); - AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder - .EndpointConfiguration(s3EndPoint, Optional.ofNullable(s3SigningRegion).orElse("EU")); - ClientConfiguration config = new ClientConfiguration().withSocketTimeout(60 * 1000 * 5); + AWSStaticCredentialsProvider credentialsProvider = + new AWSStaticCredentialsProvider(new BasicAWSCredentials(s3AccessKey, s3SecretKey)); + AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration( + s3EndPoint, + Optional.ofNullable(s3SigningRegion).orElse(FALLBACK_REGION)); + ClientConfiguration config = new ClientConfiguration().withSocketTimeout(SOCKET_TIMEOUT); Optional.ofNullable(s3SignerType).ifPresent(config::withSignerOverride); return AmazonS3ClientBuilder.standard() - .withClientConfiguration(config) - .withPathStyleAccessEnabled(true) - .withCredentials(credentialsProvider) - .withEndpointConfiguration(endpointConfiguration) - .build(); + .withClientConfiguration(config) + .withPathStyleAccessEnabled(true) + .withCredentials(credentialsProvider) + .withEndpointConfiguration(endpointConfiguration) + .build(); } /** * Creates the url used to tunnel request to upstream instance. - *
Important: If you do not request the content, the connection must use the method "HEAD"* + *
Important: If you do not request the content, the connection must use the method "HEAD"! * * @param bucket from which an object is fetched * @param object which should be fetched @@ -125,7 +129,7 @@ public URL generateGetObjectURL(Bucket bucket, StoredObject object, boolean requ request.setMethod(HttpMethod.HEAD); } - return getClient().generatePresignedUrl(request); + return fetchClient().generatePresignedUrl(request); } public void setS3SecretKey(String s3SecretKey) { diff --git a/src/main/java/ninja/S3Dispatcher.java b/src/main/java/ninja/S3Dispatcher.java index 0d662de..002b7af 100644 --- a/src/main/java/ninja/S3Dispatcher.java +++ b/src/main/java/ninja/S3Dispatcher.java @@ -21,7 +21,11 @@ import ninja.queries.S3QuerySynthesizer; import org.asynchttpclient.BoundRequestBuilder; import sirius.kernel.async.CallContext; -import sirius.kernel.commons.*; +import sirius.kernel.commons.Callback; +import sirius.kernel.commons.Hasher; +import sirius.kernel.commons.Strings; +import sirius.kernel.commons.Tuple; +import sirius.kernel.commons.Value; import sirius.kernel.di.GlobalContext; import sirius.kernel.di.std.ConfigValue; import sirius.kernel.di.std.Part; @@ -38,7 +42,11 @@ import sirius.web.http.WebContext; import sirius.web.http.WebDispatcher; -import java.io.*; +import java.io.File; +import java.io.FileFilter; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; import java.net.InetAddress; import java.net.URL; import java.nio.channels.FileChannel; @@ -47,7 +55,17 @@ import java.time.chrono.IsoChronology; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; -import java.util.*; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.Set; +import java.util.TreeSet; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.stream.Collectors; @@ -634,10 +652,10 @@ private void deleteObject(final WebContext webContext, final Bucket bucket, fina object.delete(); // If it exists online, we mark it locally as "deleted" - if (awsUpstream.isConfigured() && awsUpstream.getClient().doesObjectExist(bucket.getName(), id)) { + if (awsUpstream.isConfigured() && awsUpstream.fetchClient().doesObjectExist(bucket.getName(), id)) { try { object.markDeleted(); - } catch (IOException e) { + } catch (IOException ignored) { signalObjectError(webContext, bucket.getName(), id, @@ -775,7 +793,7 @@ private void getObject(WebContext webContext, Bucket bucket, String id, boolean StoredObject object = bucket.getObject(id); if (!object.exists() && !object.isMarkedDeleted() && awsUpstream.isConfigured()) { URL fetchURL = awsUpstream.generateGetObjectURL(bucket, object, sendFile); - Consumer requestTuner = brb -> brb.setMethod(sendFile ? "GET" : "HEAD"); + Consumer requestTuner = requestBuilder -> requestBuilder.setMethod(sendFile ? "GET" : "HEAD"); webContext.enableTiming(null).respondWith().tunnel(fetchURL.toString(), requestTuner, null, null); return; }