Factor out properties parsing into Builder

pull/246/head
Andrew Gaul 2017-11-08 15:39:22 -08:00
rodzic 732a24d990
commit 11f82ec997
3 zmienionych plików z 158 dodań i 162 usunięć

Wyświetl plik

@ -22,8 +22,6 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
@ -115,7 +113,8 @@ public final class Main {
localCredential, blobStore));
}
S3Proxy.Builder s3ProxyBuilder2 = parseS3ProxyProperties(properties)
S3Proxy.Builder s3ProxyBuilder2 = S3Proxy.Builder
.fromProperties(properties)
.blobStore(blobStore);
if (s3ProxyBuilder != null &&
@ -165,110 +164,6 @@ public final class Main {
}
}
private static S3Proxy.Builder parseS3ProxyProperties(
Properties properties) throws URISyntaxException {
String s3ProxyEndpointString = properties.getProperty(
S3ProxyConstants.PROPERTY_ENDPOINT);
String secureEndpoint = properties.getProperty(
S3ProxyConstants.PROPERTY_SECURE_ENDPOINT);
String s3ProxyAuthorizationString = properties.getProperty(
S3ProxyConstants.PROPERTY_AUTHORIZATION);
if ((s3ProxyEndpointString == null && secureEndpoint == null) ||
s3ProxyAuthorizationString == null) {
throw new IllegalArgumentException(
"Properties file must contain:\n" +
S3ProxyConstants.PROPERTY_AUTHORIZATION + "\n" +
"and one of\n" +
S3ProxyConstants.PROPERTY_ENDPOINT + "\n" +
S3ProxyConstants.PROPERTY_SECURE_ENDPOINT);
}
String s3ProxyServicePath = properties.getProperty(
S3ProxyConstants.PROPERTY_SERVICE_PATH);
AuthenticationType s3ProxyAuthorization =
AuthenticationType.fromString(s3ProxyAuthorizationString);
String localIdentity = null;
String localCredential = null;
switch (s3ProxyAuthorization) {
case AWS_V2:
case AWS_V4:
case AWS_V2_OR_V4:
localIdentity = properties.getProperty(
S3ProxyConstants.PROPERTY_IDENTITY);
localCredential = properties.getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL);
if (localIdentity == null || localCredential == null) {
throw new IllegalArgumentException("Must specify both " +
S3ProxyConstants.PROPERTY_IDENTITY + " and " +
S3ProxyConstants.PROPERTY_CREDENTIAL +
" when using authentication");
}
break;
case NONE:
break;
default:
throw new IllegalArgumentException(
S3ProxyConstants.PROPERTY_AUTHORIZATION +
" invalid value, was: " + s3ProxyAuthorization);
}
String keyStorePath = properties.getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PATH);
String keyStorePassword = properties.getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
String virtualHost = properties.getProperty(
S3ProxyConstants.PROPERTY_VIRTUAL_HOST);
String v4MaxNonChunkedRequestSize = properties.getProperty(
S3ProxyConstants.PROPERTY_V4_MAX_NON_CHUNKED_REQUEST_SIZE);
String ignoreUnknownHeaders = properties.getProperty(
S3ProxyConstants.PROPERTY_IGNORE_UNKNOWN_HEADERS);
String corsAllowAll = properties.getProperty(
S3ProxyConstants.PROPERTY_CORS_ALLOW_ALL);
S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder();
if (s3ProxyEndpointString != null) {
s3ProxyBuilder.endpoint(new URI(s3ProxyEndpointString));
}
if (secureEndpoint != null) {
s3ProxyBuilder.secureEndpoint(new URI(secureEndpoint));
}
if (localIdentity != null || localCredential != null) {
s3ProxyBuilder.awsAuthentication(
s3ProxyAuthorization, localIdentity,
localCredential);
}
if (keyStorePath != null || keyStorePassword != null) {
s3ProxyBuilder.keyStore(keyStorePath, keyStorePassword);
}
if (!Strings.isNullOrEmpty(virtualHost)) {
s3ProxyBuilder.virtualHost(virtualHost);
}
if (v4MaxNonChunkedRequestSize != null) {
s3ProxyBuilder.v4MaxNonChunkedRequestSize(Long.parseLong(
v4MaxNonChunkedRequestSize));
}
if (!Strings.isNullOrEmpty(ignoreUnknownHeaders)) {
s3ProxyBuilder.ignoreUnknownHeaders(Boolean.parseBoolean(
ignoreUnknownHeaders));
}
if (corsAllowAll != null) {
s3ProxyBuilder.corsAllowAll(Boolean.parseBoolean(
corsAllowAll));
}
if (s3ProxyServicePath != null) {
s3ProxyBuilder.servicePath(s3ProxyServicePath);
}
String jettyMaxThreads = properties.getProperty(
S3ProxyConstants.PROPERTY_JETTY_MAX_THREADS);
if (jettyMaxThreads != null) {
s3ProxyBuilder.jettyMaxThreads(Integer.parseInt(jettyMaxThreads));
}
return s3ProxyBuilder;
}
private static BlobStore parseMiddlewareProperties(BlobStore blobStore,
Properties properties) throws IOException {
Properties altProperties = new Properties();

Wyświetl plik

@ -21,7 +21,9 @@ import static java.util.Objects.requireNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.Properties;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
@ -122,6 +124,7 @@ public final class S3Proxy {
private BlobStore blobStore;
private URI endpoint;
private URI secureEndpoint;
private String servicePath;
private AuthenticationType authenticationType =
AuthenticationType.NONE;
private String identity;
@ -129,7 +132,6 @@ public final class S3Proxy {
private String keyStorePath;
private String keyStorePassword;
private String virtualHost;
private String servicePath;
private long v4MaxNonChunkedRequestSize = 32 * 1024 * 1024;
private boolean ignoreUnknownHeaders;
private boolean corsAllowAll;
@ -142,6 +144,117 @@ public final class S3Proxy {
return new S3Proxy(this);
}
public static Builder fromProperties(Properties properties)
throws URISyntaxException {
Builder builder = new Builder();
String endpoint = properties.getProperty(
S3ProxyConstants.PROPERTY_ENDPOINT);
String secureEndpoint = properties.getProperty(
S3ProxyConstants.PROPERTY_SECURE_ENDPOINT);
if (endpoint == null && secureEndpoint == null) {
throw new IllegalArgumentException(
"Properties file must contain: " +
S3ProxyConstants.PROPERTY_ENDPOINT + " or " +
S3ProxyConstants.PROPERTY_SECURE_ENDPOINT);
}
if (endpoint != null) {
builder.endpoint(new URI(endpoint));
}
if (secureEndpoint != null) {
builder.secureEndpoint(new URI(secureEndpoint));
}
String authorizationString = properties.getProperty(
S3ProxyConstants.PROPERTY_AUTHORIZATION);
if (authorizationString == null) {
throw new IllegalArgumentException(
"Properties file must contain: " +
S3ProxyConstants.PROPERTY_AUTHORIZATION);
}
AuthenticationType authorization =
AuthenticationType.fromString(authorizationString);
String localIdentity = null;
String localCredential = null;
switch (authorization) {
case AWS_V2:
case AWS_V4:
case AWS_V2_OR_V4:
localIdentity = properties.getProperty(
S3ProxyConstants.PROPERTY_IDENTITY);
localCredential = properties.getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL);
if (localIdentity == null || localCredential == null) {
throw new IllegalArgumentException("Must specify both " +
S3ProxyConstants.PROPERTY_IDENTITY + " and " +
S3ProxyConstants.PROPERTY_CREDENTIAL +
" when using authentication");
}
break;
case NONE:
break;
default:
throw new IllegalArgumentException(
S3ProxyConstants.PROPERTY_AUTHORIZATION +
" invalid value, was: " + authorization);
}
if (localIdentity != null || localCredential != null) {
builder.awsAuthentication(authorization, localIdentity,
localCredential);
}
String servicePath = Strings.nullToEmpty(properties.getProperty(
S3ProxyConstants.PROPERTY_SERVICE_PATH));
if (servicePath != null) {
builder.servicePath(servicePath);
}
String keyStorePath = properties.getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PATH);
String keyStorePassword = properties.getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
if (keyStorePath != null || keyStorePassword != null) {
builder.keyStore(keyStorePath, keyStorePassword);
}
String virtualHost = properties.getProperty(
S3ProxyConstants.PROPERTY_VIRTUAL_HOST);
if (!Strings.isNullOrEmpty(virtualHost)) {
builder.virtualHost(virtualHost);
}
String v4MaxNonChunkedRequestSize = properties.getProperty(
S3ProxyConstants.PROPERTY_V4_MAX_NON_CHUNKED_REQUEST_SIZE);
if (v4MaxNonChunkedRequestSize != null) {
builder.v4MaxNonChunkedRequestSize(Long.parseLong(
v4MaxNonChunkedRequestSize));
}
String ignoreUnknownHeaders = properties.getProperty(
S3ProxyConstants.PROPERTY_IGNORE_UNKNOWN_HEADERS);
if (!Strings.isNullOrEmpty(ignoreUnknownHeaders)) {
builder.ignoreUnknownHeaders(Boolean.parseBoolean(
ignoreUnknownHeaders));
}
String corsAllowAll = properties.getProperty(
S3ProxyConstants.PROPERTY_CORS_ALLOW_ALL);
if (corsAllowAll != null) {
builder.corsAllowAll(Boolean.parseBoolean(
corsAllowAll));
}
String jettyMaxThreads = properties.getProperty(
S3ProxyConstants.PROPERTY_JETTY_MAX_THREADS);
if (jettyMaxThreads != null) {
builder.jettyMaxThreads(Integer.parseInt(jettyMaxThreads));
}
return builder;
}
public Builder blobStore(BlobStore blobStore) {
this.blobStore = requireNonNull(blobStore);
return this;
@ -202,7 +315,7 @@ public final class S3Proxy {
return this;
}
public void servicePath(String s3ProxyServicePath) {
public Builder servicePath(String s3ProxyServicePath) {
String path = Strings.nullToEmpty(s3ProxyServicePath);
if (!path.isEmpty()) {
@ -212,6 +325,28 @@ public final class S3Proxy {
}
this.servicePath = path;
return this;
}
public URI getEndpoint() {
return endpoint;
}
public URI getSecureEndpoint() {
return secureEndpoint;
}
public String getServicePath() {
return servicePath;
}
public String getIdentity() {
return identity;
}
public String getCredential() {
return credential;
}
@Override

Wyświetl plik

@ -176,39 +176,6 @@ final class TestUtils {
}
String endpoint = info.getProperties().getProperty(
Constants.PROPERTY_ENDPOINT);
String s3ProxyAuthorizationString = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_AUTHORIZATION);
AuthenticationType s3ProxyAuthorization =
AuthenticationType.fromString(s3ProxyAuthorizationString);
info.s3Identity = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_IDENTITY);
info.s3Credential = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL);
info.endpoint = new URI(info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_ENDPOINT));
String secureEndpoint = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_SECURE_ENDPOINT);
String keyStorePath = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PATH);
String keyStorePassword = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
String virtualHost = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_VIRTUAL_HOST);
String jettyMaxThreads = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_JETTY_MAX_THREADS);
String servicePath = Strings.nullToEmpty(info.getProperties()
.getProperty(S3ProxyConstants.PROPERTY_SERVICE_PATH));
if (!servicePath.isEmpty()) {
if (!servicePath.startsWith("/")) {
servicePath = "/" + servicePath;
}
}
info.servicePath = servicePath;
info.getProperties().setProperty(Constants.PROPERTY_USER_AGENT,
String.format("s3proxy/%s jclouds/%s java/%s",
TestUtils.class.getPackage().getImplementationVersion(),
JcloudsVersion.get(),
System.getProperty("java.version")));
ContextBuilder builder = ContextBuilder
.newBuilder(provider)
@ -221,31 +188,30 @@ final class TestUtils {
BlobStoreContext context = builder.build(BlobStoreContext.class);
info.blobStore = context.getBlobStore();
S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder()
.blobStore(info.getBlobStore())
.endpoint(info.getEndpoint());
if (secureEndpoint != null) {
s3ProxyBuilder.secureEndpoint(new URI(secureEndpoint));
info.secureEndpoint = new URI(secureEndpoint);
}
if (info.getS3Identity() != null || info.getS3Credential() != null) {
s3ProxyBuilder.awsAuthentication(s3ProxyAuthorization,
info.getS3Identity(), info.getS3Credential());
}
S3Proxy.Builder s3ProxyBuilder = S3Proxy.Builder.fromProperties(
info.getProperties());
s3ProxyBuilder.blobStore(info.blobStore);
info.endpoint = s3ProxyBuilder.getEndpoint();
info.secureEndpoint = s3ProxyBuilder.getSecureEndpoint();
info.s3Identity = s3ProxyBuilder.getIdentity();
info.s3Credential = s3ProxyBuilder.getCredential();
info.servicePath = s3ProxyBuilder.getServicePath();
info.getProperties().setProperty(Constants.PROPERTY_USER_AGENT,
String.format("s3proxy/%s jclouds/%s java/%s",
TestUtils.class.getPackage().getImplementationVersion(),
JcloudsVersion.get(),
System.getProperty("java.version")));
// resolve relative path for tests
String keyStorePath = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PATH);
String keyStorePassword = info.getProperties().getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
if (keyStorePath != null || keyStorePassword != null) {
s3ProxyBuilder.keyStore(
Resources.getResource(keyStorePath).toString(),
keyStorePassword);
}
if (virtualHost != null) {
s3ProxyBuilder.virtualHost(virtualHost);
}
if (servicePath != null) {
s3ProxyBuilder.servicePath(servicePath);
}
if (jettyMaxThreads != null) {
s3ProxyBuilder.jettyMaxThreads(Integer.parseInt(jettyMaxThreads));
}
info.s3Proxy = s3ProxyBuilder.build();
info.s3Proxy.start();
while (!info.s3Proxy.getState().equals(AbstractLifeCycle.STARTED)) {