Use builder to create S3Proxy

Also hoist sanity checks out of main.
pull/42/head
Andrew Gaul 2015-02-19 14:06:17 -08:00
rodzic 2e82478f0e
commit 613da2f3ec
3 zmienionych plików z 115 dodań i 34 usunięć

Wyświetl plik

@ -55,7 +55,7 @@ public final class S3Proxy {
System.setProperty("org.eclipse.jetty.http.HttpParser.STRICT", "true"); System.setProperty("org.eclipse.jetty.http.HttpParser.STRICT", "true");
} }
public S3Proxy(BlobStore blobStore, URI endpoint, String identity, S3Proxy(BlobStore blobStore, URI endpoint, String identity,
String credential, String keyStorePath, String keyStorePassword, String credential, String keyStorePath, String keyStorePassword,
boolean forceMultiPartUpload, Optional<String> virtualHost) { boolean forceMultiPartUpload, Optional<String> virtualHost) {
checkNotNull(blobStore); checkNotNull(blobStore);
@ -63,6 +63,15 @@ public final class S3Proxy {
// TODO: allow service paths? // TODO: allow service paths?
checkArgument(endpoint.getPath().isEmpty(), checkArgument(endpoint.getPath().isEmpty(),
"endpoint path must be empty, was: " + endpoint.getPath()); "endpoint path must be empty, was: " + endpoint.getPath());
checkArgument(Strings.isNullOrEmpty(identity) ^
!Strings.isNullOrEmpty(credential),
"Must provide both identity and credential");
if (endpoint.getScheme().equals("https:")) {
checkNotNull(keyStorePath,
"Must provide keyStorePath with HTTPS endpoint");
checkNotNull(keyStorePassword,
"Must provide keyStorePassword with HTTPS endpoint");
}
checkNotNull(virtualHost); checkNotNull(virtualHost);
server = new Server(); server = new Server();
@ -85,6 +94,62 @@ public final class S3Proxy {
forceMultiPartUpload, virtualHost)); forceMultiPartUpload, virtualHost));
} }
public static final class Builder {
private BlobStore blobStore;
private URI endpoint;
private String identity;
private String credential;
private String keyStorePath;
private String keyStorePassword;
private boolean forceMultiPartUpload;
private String virtualHost;
Builder() {
}
public S3Proxy build() {
return new S3Proxy(blobStore, endpoint, identity, credential,
keyStorePath, keyStorePassword, forceMultiPartUpload,
Optional.fromNullable(virtualHost));
}
public Builder blobStore(BlobStore blobStore) {
this.blobStore = checkNotNull(blobStore);
return this;
}
public Builder endpoint(URI endpoint) {
this.endpoint = checkNotNull(endpoint);
return this;
}
public Builder awsAuthentication(String identity, String credential) {
this.identity = checkNotNull(identity);
this.credential = checkNotNull(credential);
return this;
}
public Builder keyStore(String keyStorePath, String keyStorePassword) {
this.keyStorePath = checkNotNull(keyStorePath);
this.keyStorePassword = checkNotNull(keyStorePassword);
return this;
}
public Builder forceMultiPartUpload(boolean forceMultiPartUpload) {
this.forceMultiPartUpload = forceMultiPartUpload;
return this;
}
public Builder virtualHost(String virtualHost) {
this.virtualHost = checkNotNull(virtualHost);
return this;
}
}
public static Builder builder() {
return new Builder();
}
public void start() throws Exception { public void start() throws Exception {
server.start(); server.start();
} }
@ -144,13 +209,6 @@ public final class S3Proxy {
S3ProxyConstants.PROPERTY_IDENTITY); S3ProxyConstants.PROPERTY_IDENTITY);
localCredential = properties.getProperty( localCredential = properties.getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL); S3ProxyConstants.PROPERTY_CREDENTIAL);
if (localIdentity == null || localCredential == null) {
System.err.println(
"Both " + S3ProxyConstants.PROPERTY_IDENTITY +
" and " + S3ProxyConstants.PROPERTY_CREDENTIAL +
" must be set");
System.exit(1);
}
} else if (!s3ProxyAuthorization.equalsIgnoreCase("none")) { } else if (!s3ProxyAuthorization.equalsIgnoreCase("none")) {
System.err.println(S3ProxyConstants.PROPERTY_AUTHORIZATION + System.err.println(S3ProxyConstants.PROPERTY_AUTHORIZATION +
" must be aws-v2 or none, was: " + s3ProxyAuthorization); " must be aws-v2 or none, was: " + s3ProxyAuthorization);
@ -161,22 +219,10 @@ public final class S3Proxy {
S3ProxyConstants.PROPERTY_KEYSTORE_PATH); S3ProxyConstants.PROPERTY_KEYSTORE_PATH);
String keyStorePassword = properties.getProperty( String keyStorePassword = properties.getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD); S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
if (s3ProxyEndpointString.startsWith("https")) {
if (Strings.isNullOrEmpty(keyStorePath) ||
Strings.isNullOrEmpty(keyStorePassword)) {
System.err.println(
"Both " + S3ProxyConstants.PROPERTY_KEYSTORE_PATH +
" and " + S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD +
" must be set with an HTTPS endpoint");
System.exit(1);
}
}
String forceMultiPartUpload = properties.getProperty( String forceMultiPartUpload = properties.getProperty(
S3ProxyConstants.PROPERTY_FORCE_MULTI_PART_UPLOAD); S3ProxyConstants.PROPERTY_FORCE_MULTI_PART_UPLOAD);
Optional<String> virtualHost = Optional.fromNullable( String virtualHost = properties.getProperty(
properties.getProperty( S3ProxyConstants.PROPERTY_VIRTUAL_HOST);
S3ProxyConstants.PROPERTY_VIRTUAL_HOST));
ContextBuilder builder = ContextBuilder ContextBuilder builder = ContextBuilder
.newBuilder(provider) .newBuilder(provider)
@ -188,10 +234,30 @@ public final class S3Proxy {
} }
BlobStoreContext context = builder.build(BlobStoreContext.class); BlobStoreContext context = builder.build(BlobStoreContext.class);
URI s3ProxyEndpoint = new URI(s3ProxyEndpointString); URI s3ProxyEndpoint = new URI(s3ProxyEndpointString);
S3Proxy s3Proxy = new S3Proxy(context.getBlobStore(), s3ProxyEndpoint,
localIdentity, localCredential, keyStorePath, S3Proxy s3Proxy;
keyStorePassword, try {
"true".equalsIgnoreCase(forceMultiPartUpload), virtualHost); S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder()
.blobStore(context.getBlobStore())
.endpoint(s3ProxyEndpoint)
.forceMultiPartUpload("true".equalsIgnoreCase(
forceMultiPartUpload));
if (localIdentity != null || localCredential != null) {
s3ProxyBuilder.awsAuthentication(localIdentity,
localCredential);
}
if (keyStorePath != null || keyStorePassword != null) {
s3ProxyBuilder.keyStore(keyStorePath, keyStorePassword);
}
if (virtualHost != null) {
s3ProxyBuilder.virtualHost(virtualHost);
}
s3Proxy = s3ProxyBuilder.build();
} catch (IllegalArgumentException | IllegalStateException e) {
System.err.println(e.getMessage());
System.exit(1);
throw e;
}
s3Proxy.start(); s3Proxy.start();
} }
} }

Wyświetl plik

@ -34,10 +34,14 @@
<module name="FallThrough"/> <module name="FallThrough"/>
<module name="FinalClass"/> <module name="FinalClass"/>
<module name="GenericWhitespace"/> <module name="GenericWhitespace"/>
<!--
// TODO: Checkstyle dislikes builders without set prefix
https://github.com/checkstyle/checkstyle/issues/619
<module name="HiddenField"> <module name="HiddenField">
<property name="ignoreConstructorParameter" value="true"/> <property name="ignoreConstructorParameter" value="true"/>
<property name="ignoreSetter" value="true"/> <property name="ignoreSetter" value="true"/>
</module> </module>
-->
<module name="HideUtilityClassConstructor"/> <module name="HideUtilityClassConstructor"/>
<module name="IllegalImport"/> <module name="IllegalImport"/>
<module name="IllegalInstantiation"> <module name="IllegalInstantiation">

Wyświetl plik

@ -26,7 +26,6 @@ import java.util.Random;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.google.common.base.Optional;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -95,9 +94,8 @@ public final class S3ProxyTest {
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD); S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
String forceMultiPartUpload = s3ProxyProperties.getProperty( String forceMultiPartUpload = s3ProxyProperties.getProperty(
S3ProxyConstants.PROPERTY_FORCE_MULTI_PART_UPLOAD); S3ProxyConstants.PROPERTY_FORCE_MULTI_PART_UPLOAD);
Optional<String> virtualHost = Optional.fromNullable( String virtualHost = s3ProxyProperties.getProperty(
s3ProxyProperties.getProperty( S3ProxyConstants.PROPERTY_VIRTUAL_HOST);
S3ProxyConstants.PROPERTY_VIRTUAL_HOST));
Properties properties = new Properties(); Properties properties = new Properties();
ContextBuilder builder = ContextBuilder ContextBuilder builder = ContextBuilder
@ -113,10 +111,23 @@ public final class S3ProxyTest {
containerName = createRandomContainerName(); containerName = createRandomContainerName();
blobStore.createContainerInLocation(null, containerName); blobStore.createContainerInLocation(null, containerName);
s3Proxy = new S3Proxy(blobStore, s3Endpoint, s3Identity, s3Credential, S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder()
Resources.getResource(keyStorePath).toString(), .blobStore(blobStore)
keyStorePassword, .endpoint(s3Endpoint)
"true".equalsIgnoreCase(forceMultiPartUpload), virtualHost); .forceMultiPartUpload("true".equalsIgnoreCase(
forceMultiPartUpload));
if (s3Identity != null || s3Credential != null) {
s3ProxyBuilder.awsAuthentication(s3Identity, s3Credential);
}
if (keyStorePath != null || keyStorePassword != null) {
s3ProxyBuilder.keyStore(
Resources.getResource(keyStorePath).toString(),
keyStorePassword);
}
if (virtualHost != null) {
s3ProxyBuilder.virtualHost(virtualHost);
}
s3Proxy = s3ProxyBuilder.build();
s3Proxy.start(); s3Proxy.start();
while (!s3Proxy.getState().equals(AbstractLifeCycle.STARTED)) { while (!s3Proxy.getState().equals(AbstractLifeCycle.STARTED)) {
Thread.sleep(1); Thread.sleep(1);