Skip to content

adding storage api #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 49 commits into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0613f53
storage work in progress
aozarov Mar 2, 2015
125d6e9
wip
aozarov Mar 3, 2015
40e9739
wip
aozarov Mar 4, 2015
79215c4
wip
aozarov Mar 18, 2015
d15a682
work in progress
aozarov Mar 20, 2015
1188ac6
work in progress
aozarov Apr 1, 2015
c9611f2
work in progress
aozarov Apr 1, 2015
43d0761
work in progress
aozarov Apr 2, 2015
7dcee4b
wip
aozarov Apr 3, 2015
59f0d85
work in progress
aozarov Apr 9, 2015
a7b661a
Merge remote-tracking branch 'upstream/master'
aozarov Apr 9, 2015
126b043
work in progress
aozarov Apr 9, 2015
4f65b2d
work in progress
aozarov Apr 10, 2015
a3cf907
work in progress
aozarov Apr 10, 2015
2cdc4e3
work in progress
aozarov Apr 11, 2015
24f8a0b
work in progress
aozarov Apr 14, 2015
4836863
work in progress
aozarov Apr 14, 2015
fcb3dcd
work in progress
aozarov Apr 14, 2015
d3f0356
work in progress
aozarov Apr 14, 2015
3171d8c
work in progress
aozarov Apr 16, 2015
f88107c
work in progress
aozarov Apr 18, 2015
8ef30c8
work in progress
aozarov Apr 21, 2015
89a5024
work in progress
aozarov Apr 21, 2015
2841929
work in progress
aozarov Apr 22, 2015
803527d
work in progress
aozarov Apr 22, 2015
f327dad
work in progress
aozarov Apr 23, 2015
c92f2ea
make AuthCredentials serializable
aozarov Apr 24, 2015
d504256
work in progress
aozarov Apr 24, 2015
c148817
work in progress
aozarov Apr 24, 2015
09a890f
work in progress
aozarov Apr 24, 2015
a4076b7
work in progress
aozarov Apr 25, 2015
9cb77fa
work in progress
aozarov Apr 29, 2015
acff3c1
add default content type to compose
aozarov Apr 29, 2015
ad396b7
work in progress
aozarov Apr 30, 2015
6df1164
adding batch
aozarov May 1, 2015
a4ee293
work in progress
aozarov May 2, 2015
2ed5ce1
work in progress
aozarov May 2, 2015
4c2f40e
complete batch
aozarov May 4, 2015
59410ab
add multi info to example
aozarov May 4, 2015
f0920d0
work in progress
aozarov May 4, 2015
64da673
find project-id from env
aozarov May 4, 2015
e3222e5
update ini hanlding
aozarov May 4, 2015
9b751ae
implement Serializable reader
aozarov May 4, 2015
2d279f4
work in progress
aozarov May 5, 2015
4260e8e
work in progress
aozarov May 5, 2015
e830ad3
work in progress
aozarov May 6, 2015
a964351
work in progress
aozarov May 6, 2015
6d4ac35
work in progress
aozarov May 6, 2015
d2c6344
complete output writer
aozarov May 7, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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