Skip to content

Commit

Permalink
Merge pull request #43 from aozarov/master
Browse files Browse the repository at this point in the history
adding storage api
  • Loading branch information
aozarov committed May 7, 2015
2 parents cbe93b9 + d2c6344 commit 251ac25
Show file tree
Hide file tree
Showing 35 changed files with 3,484 additions and 327 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev23-1.19.0</version>
<version>v1-rev33-1.20.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
118 changes: 93 additions & 25 deletions src/main/java/com/google/gcloud/AuthCredentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,46 @@
import com.google.auth.oauth2.GoogleCredentials;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.util.Objects;
import java.util.Set;

/**
* Credentials for accessing Google Cloud services.
*/
public abstract class AuthCredentials {
public abstract class AuthCredentials implements Serializable {

private static final long serialVersionUID = 236297804453464604L;

private static class AppEngineAuthCredentials extends AuthCredentials {

private static final long serialVersionUID = 7931300552744202954L;

private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials();

@Override
protected HttpRequestInitializer httpRequestInitializer(
HttpTransport transport, Set<String> scopes) {
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
Set<String> scopes) {
return new AppIdentityCredential(scopes);
}

private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
}

private static class ServiceAccountAuthCredentials extends AuthCredentials {

private static final long serialVersionUID = 8007708734318445901L;
private final String account;
private final PrivateKey privateKey;

private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials();

ServiceAccountAuthCredentials(String account, PrivateKey privateKey) {
this.account = checkNotNull(account);
this.privateKey = checkNotNull(privateKey);
Expand All @@ -76,55 +93,106 @@ protected HttpRequestInitializer httpRequestInitializer(
}
return builder.build();
}

@Override
public int hashCode() {
return Objects.hash(account, privateKey);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof ServiceAccountAuthCredentials)) {
return false;
}
ServiceAccountAuthCredentials other = (ServiceAccountAuthCredentials) obj;
return Objects.equals(account, other.account)
&& Objects.equals(privateKey, other.privateKey);
}
}

private static class ComputeEngineAuthCredentials extends AuthCredentials {

private static final long serialVersionUID = -5217355402127260144L;

private transient ComputeCredential computeCredential;

ComputeEngineAuthCredentials() throws IOException, GeneralSecurityException {
computeCredential = getComputeCredential();
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
try {
computeCredential = getComputeCredential();
} catch (GeneralSecurityException e) {
throw new IOException(e);
}
}

@Override
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
Set<String> scopes) {
return computeCredential;
}
}

private static class ApplicationDefaultAuthCredentials extends AuthCredentials {

private static final long serialVersionUID = -8306873864136099893L;

private transient GoogleCredentials googleCredentials;

ApplicationDefaultAuthCredentials() throws IOException {
googleCredentials = GoogleCredentials.getApplicationDefault();
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
googleCredentials = GoogleCredentials.getApplicationDefault();
}

@Override
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
Set<String> scopes) {
return new HttpCredentialsAdapter(googleCredentials);
}
}

protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
Set<String> scopes);

public static AuthCredentials createForAppEngine() {
return new AppEngineAuthCredentials();
return AppEngineAuthCredentials.INSTANCE;
}

public static AuthCredentials createForComputeEngine()
throws IOException, GeneralSecurityException {
final ComputeCredential cred = getComputeCredential();
return new AuthCredentials() {
@Override
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
Set<String> scopes) {
return cred;
}
};
return new ComputeEngineAuthCredentials();
}

/**
* Returns the Application Default Credentials.
*
* <p>Returns the Application Default Credentials which are credentials that identify and
* authorize the whole application. This is the built-in service account if running on Google
* Compute Engine or the credentials file from the path in the environment variable
* GOOGLE_APPLICATION_CREDENTIALS.</p>
* <p>
* Returns the Application Default Credentials which are credentials that identify and authorize
* the whole application. This is the built-in service account if running on Google Compute Engine
* or the credentials file from the path in the environment variable
* GOOGLE_APPLICATION_CREDENTIALS.
* </p>
*
* @return the credentials instance.
* @throws IOException if the credentials cannot be created in the current environment.
*/
public static AuthCredentials createApplicationDefaults() throws IOException {
final GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
return new AuthCredentials() {
@Override
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
Set<String> scopes) {
return new HttpCredentialsAdapter(credentials);
}
};
return new ApplicationDefaultAuthCredentials();
}

public static AuthCredentials createFor(String account, PrivateKey privateKey) {
return new ServiceAccountAuthCredentials(account, privateKey);
}

public static AuthCredentials noCredentials() {
return new ServiceAccountAuthCredentials();
return ServiceAccountAuthCredentials.NO_CREDENTIALS;
}

static ComputeCredential getComputeCredential() throws IOException, GeneralSecurityException {
Expand Down
29 changes: 13 additions & 16 deletions src/main/java/com/google/gcloud/ExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public interface Interceptor extends Serializable {

enum RetryResult {

RETRY(true),
ABORT(false);
RETRY(true), ABORT(false);

private final boolean booleanValue;

Expand All @@ -67,9 +66,9 @@ boolean booleanValue() {
* This method is called before exception evaluation and could short-circuit the process.
*
* @param exception the exception that is being evaluated
* @return {@link RetryResult} to indicate if the exception should be ignored
* ({@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}),
* or evaluation should proceed ({@code null}).
* @return {@link RetryResult} to indicate if the exception should be ignored (
* {@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}), or evaluation
* should proceed ({@code null}).
*/
RetryResult beforeEval(Exception exception);

Expand All @@ -78,9 +77,9 @@ boolean booleanValue() {
*
* @param exception the exception that is being evaluated
* @param retryResult the result of the evaluation so far.
* @return {@link RetryResult} to indicate if the exception should be ignored
* ({@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}),
* or evaluation should proceed ({@code null}).
* @return {@link RetryResult} to indicate if the exception should be ignored (
* {@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}), or evaluation
* should proceed ({@code null}).
*/
RetryResult afterEval(Exception exception, RetryResult retryResult);
}
Expand All @@ -96,14 +95,12 @@ public static class Builder {
private final ImmutableSet.Builder<Class<? extends Exception>> nonRetriableExceptions =
ImmutableSet.builder();

private Builder() {
}
private Builder() {}


/**
* Adds the exception handler interceptors.
* Call order will be maintained.
* Adds the exception handler interceptors. Call order will be maintained.
*
* @param interceptors the interceptors for this exception handler
* @return the Builder for chaining
*/
Expand Down Expand Up @@ -214,7 +211,7 @@ private static RetryInfo findMostSpecificRetryInfo(Set<RetryInfo> retryInfo,
Class<? extends Exception> exception) {
for (RetryInfo current : retryInfo) {
if (current.exception.isAssignableFrom(exception)) {
RetryInfo match = findMostSpecificRetryInfo(current.children, exception);
RetryInfo match = findMostSpecificRetryInfo(current.children, exception);
return match == null ? current : match;
}
}
Expand All @@ -239,8 +236,8 @@ void verifyCaller(Callable<?> callable) {
for (Class<?> exceptionOrError : callMethod.getExceptionTypes()) {
Preconditions.checkArgument(Exception.class.isAssignableFrom(exceptionOrError),
"Callable method exceptions must be derived from Exception");
@SuppressWarnings("unchecked") Class<? extends Exception> exception =
(Class<? extends Exception>) exceptionOrError;
@SuppressWarnings("unchecked")
Class<? extends Exception> exception = (Class<? extends Exception>) exceptionOrError;
Preconditions.checkArgument(findMostSpecificRetryInfo(retryInfo, exception) != null,
"Declared exception '" + exception + "' is not covered by exception handler");
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/google/gcloud/RetryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import java.util.logging.Logger;

/**
* Utility class for retrying operations.
* For more details about the parameters, see {@link RetryParams}.
* If the request is never successful, a {@link RetriesExhaustedException} will be thrown.
* Utility class for retrying operations. For more details about the parameters, see
* {@link RetryParams}. If the request is never successful, a {@link RetriesExhaustedException} will
* be thrown.
*
* @param <V> return value of the closure that is being run with retries
*/
Expand Down Expand Up @@ -201,8 +201,8 @@ private V doRetry() throws RetryHelperException {
}
long sleepDurationMillis = getSleepDuration(params, attemptNumber);
if (log.isLoggable(Level.FINE)) {
log.fine(this + ": Attempt #" + attemptNumber + " failed [" + exception + "], sleeping for "
+ sleepDurationMillis + " ms");
log.fine(this + ": Attempt #" + attemptNumber + " failed [" + exception
+ "], sleeping for " + sleepDurationMillis + " ms");
}
try {
Thread.sleep(sleepDurationMillis);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/google/gcloud/RetryParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.util.Objects;

/**
* Parameters for configuring retries with an exponential backoff.
* Initial request is executed immediately. If the request fails but passes the
* {@link ExceptionHandler} criteria the calling thread sleeps for {@code initialRetryDelayMillis}.
* Each subsequent failure the sleep interval is calculated as:
* Parameters for configuring retries with an exponential backoff. Initial request is executed
* immediately. If the request fails but passes the {@link ExceptionHandler} criteria the calling
* thread sleeps for {@code initialRetryDelayMillis}. Each subsequent failure the sleep interval is
* calculated as:
* <p>
* {@code retryDelayBackoffFactor ^ attempts * initialRetryDelayMillis} but would be upper-bounded
* to {@code maxRetryDelayMillis}
Expand Down
Loading

0 comments on commit 251ac25

Please sign in to comment.