Skip to content

Commit

Permalink
CodeStyle: Renamed functions, organized imports.
Browse files Browse the repository at this point in the history
Fixes: MISC-16
  • Loading branch information
ReadMails-ReallyFast committed Apr 28, 2022
1 parent f5df116 commit 6d1c19d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
28 changes: 16 additions & 12 deletions src/main/java/ninja/AwsUpstream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand All @@ -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.
* <br><b>Important: If you do not request the content, the connection must use the method "HEAD"*</b>
* <br><b>Important: If you do not request the content, the connection must use the method "HEAD"!</b>
*
* @param bucket from which an object is fetched
* @param object which should be fetched
Expand All @@ -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) {
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/ninja/S3Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<BoundRequestBuilder> requestTuner = brb -> brb.setMethod(sendFile ? "GET" : "HEAD");
Consumer<BoundRequestBuilder> requestTuner = requestBuilder -> requestBuilder.setMethod(sendFile ? "GET" : "HEAD");
webContext.enableTiming(null).respondWith().tunnel(fetchURL.toString(), requestTuner, null, null);
return;
}
Expand Down

0 comments on commit 6d1c19d

Please sign in to comment.