com.distelli.aws.AmazonWebServiceClients Maven / Gradle / Ivy
package com.distelli.aws;
import com.amazonaws.AmazonWebServiceClient;
import com.amazonaws.regions.Regions;
import java.net.URI;
import java.net.URISyntaxException;
import javax.inject.Singleton;
import javax.inject.Inject;
@Singleton
public class AmazonWebServiceClients {
@Inject
protected AmazonWebServiceClients() {}
public T withEndpoint(T client, URI endpoint) {
if ( null == endpoint ) return client;
if ( null != endpoint.getHost() ) {
try {
client.withRegion(Regions.fromName(endpoint.getHost()));
return client;
} catch ( IllegalArgumentException ex ) {
}
}
if ( null != endpoint.getScheme() && ! (
"https".equals(endpoint.getScheme().toLowerCase()) ||
"http".equals(endpoint.getScheme().toLowerCase()) ) )
{
// Sometimes scheme is used to indicate the service, so fix the scheme:
try {
endpoint = new URI(
"https",
endpoint.getUserInfo(),
endpoint.getHost(),
endpoint.getPort(),
endpoint.getPath(),
endpoint.getQuery(),
endpoint.getFragment());
} catch ( URISyntaxException ex ) {
throw new RuntimeException(ex);
}
}
client.withEndpoint(endpoint.toString());
return client;
}
}