Skip to content

Commit 0246cc7

Browse files
committed
Merge pull request #43 from aozarov/master
adding storage api
2 parents d4d9427 + 01d873e commit 0246cc7

35 files changed

+3484
-327
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
<dependency>
116116
<groupId>com.google.apis</groupId>
117117
<artifactId>google-api-services-storage</artifactId>
118-
<version>v1-rev23-1.19.0</version>
118+
<version>v1-rev33-1.20.0</version>
119119
<scope>compile</scope>
120120
</dependency>
121121
<dependency>

src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,46 @@
3030
import com.google.auth.oauth2.GoogleCredentials;
3131

3232
import java.io.IOException;
33+
import java.io.ObjectInputStream;
34+
import java.io.ObjectStreamException;
35+
import java.io.Serializable;
3336
import java.security.GeneralSecurityException;
3437
import java.security.PrivateKey;
38+
import java.util.Objects;
3539
import java.util.Set;
3640

3741
/**
3842
* Credentials for accessing Google Cloud services.
3943
*/
40-
public abstract class AuthCredentials {
44+
public abstract class AuthCredentials implements Serializable {
45+
46+
private static final long serialVersionUID = 236297804453464604L;
4147

4248
private static class AppEngineAuthCredentials extends AuthCredentials {
4349

50+
private static final long serialVersionUID = 7931300552744202954L;
51+
52+
private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials();
53+
4454
@Override
45-
protected HttpRequestInitializer httpRequestInitializer(
46-
HttpTransport transport, Set<String> scopes) {
55+
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
56+
Set<String> scopes) {
4757
return new AppIdentityCredential(scopes);
4858
}
59+
60+
private Object readResolve() throws ObjectStreamException {
61+
return INSTANCE;
62+
}
4963
}
5064

5165
private static class ServiceAccountAuthCredentials extends AuthCredentials {
5266

67+
private static final long serialVersionUID = 8007708734318445901L;
5368
private final String account;
5469
private final PrivateKey privateKey;
5570

71+
private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials();
72+
5673
ServiceAccountAuthCredentials(String account, PrivateKey privateKey) {
5774
this.account = checkNotNull(account);
5875
this.privateKey = checkNotNull(privateKey);
@@ -76,55 +93,106 @@ protected HttpRequestInitializer httpRequestInitializer(
7693
}
7794
return builder.build();
7895
}
96+
97+
@Override
98+
public int hashCode() {
99+
return Objects.hash(account, privateKey);
100+
}
101+
102+
@Override
103+
public boolean equals(Object obj) {
104+
if (!(obj instanceof ServiceAccountAuthCredentials)) {
105+
return false;
106+
}
107+
ServiceAccountAuthCredentials other = (ServiceAccountAuthCredentials) obj;
108+
return Objects.equals(account, other.account)
109+
&& Objects.equals(privateKey, other.privateKey);
110+
}
111+
}
112+
113+
private static class ComputeEngineAuthCredentials extends AuthCredentials {
114+
115+
private static final long serialVersionUID = -5217355402127260144L;
116+
117+
private transient ComputeCredential computeCredential;
118+
119+
ComputeEngineAuthCredentials() throws IOException, GeneralSecurityException {
120+
computeCredential = getComputeCredential();
121+
}
122+
123+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
124+
in.defaultReadObject();
125+
try {
126+
computeCredential = getComputeCredential();
127+
} catch (GeneralSecurityException e) {
128+
throw new IOException(e);
129+
}
130+
}
131+
132+
@Override
133+
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
134+
Set<String> scopes) {
135+
return computeCredential;
136+
}
137+
}
138+
139+
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
140+
141+
private static final long serialVersionUID = -8306873864136099893L;
142+
143+
private transient GoogleCredentials googleCredentials;
144+
145+
ApplicationDefaultAuthCredentials() throws IOException {
146+
googleCredentials = GoogleCredentials.getApplicationDefault();
147+
}
148+
149+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
150+
in.defaultReadObject();
151+
googleCredentials = GoogleCredentials.getApplicationDefault();
152+
}
153+
154+
@Override
155+
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
156+
Set<String> scopes) {
157+
return new HttpCredentialsAdapter(googleCredentials);
158+
}
79159
}
80160

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

84164
public static AuthCredentials createForAppEngine() {
85-
return new AppEngineAuthCredentials();
165+
return AppEngineAuthCredentials.INSTANCE;
86166
}
87167

88168
public static AuthCredentials createForComputeEngine()
89169
throws IOException, GeneralSecurityException {
90-
final ComputeCredential cred = getComputeCredential();
91-
return new AuthCredentials() {
92-
@Override
93-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
94-
Set<String> scopes) {
95-
return cred;
96-
}
97-
};
170+
return new ComputeEngineAuthCredentials();
98171
}
99172

100173
/**
101174
* Returns the Application Default Credentials.
102175
*
103-
* <p>Returns the Application Default Credentials which are credentials that identify and
104-
* authorize the whole application. This is the built-in service account if running on Google
105-
* Compute Engine or the credentials file from the path in the environment variable
106-
* GOOGLE_APPLICATION_CREDENTIALS.</p>
176+
* <p>
177+
* Returns the Application Default Credentials which are credentials that identify and authorize
178+
* the whole application. This is the built-in service account if running on Google Compute Engine
179+
* or the credentials file from the path in the environment variable
180+
* GOOGLE_APPLICATION_CREDENTIALS.
181+
* </p>
107182
*
108183
* @return the credentials instance.
109184
* @throws IOException if the credentials cannot be created in the current environment.
110185
*/
111186
public static AuthCredentials createApplicationDefaults() throws IOException {
112-
final GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
113-
return new AuthCredentials() {
114-
@Override
115-
protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
116-
Set<String> scopes) {
117-
return new HttpCredentialsAdapter(credentials);
118-
}
119-
};
187+
return new ApplicationDefaultAuthCredentials();
120188
}
121189

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

126194
public static AuthCredentials noCredentials() {
127-
return new ServiceAccountAuthCredentials();
195+
return ServiceAccountAuthCredentials.NO_CREDENTIALS;
128196
}
129197

130198
static ComputeCredential getComputeCredential() throws IOException, GeneralSecurityException {

src/main/java/com/google/gcloud/ExceptionHandler.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public interface Interceptor extends Serializable {
4949

5050
enum RetryResult {
5151

52-
RETRY(true),
53-
ABORT(false);
52+
RETRY(true), ABORT(false);
5453

5554
private final boolean booleanValue;
5655

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

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

99-
private Builder() {
100-
}
98+
private Builder() {}
10199

102100

103101
/**
104-
* Adds the exception handler interceptors.
105-
* Call order will be maintained.
106-
102+
* Adds the exception handler interceptors. Call order will be maintained.
103+
*
107104
* @param interceptors the interceptors for this exception handler
108105
* @return the Builder for chaining
109106
*/
@@ -214,7 +211,7 @@ private static RetryInfo findMostSpecificRetryInfo(Set<RetryInfo> retryInfo,
214211
Class<? extends Exception> exception) {
215212
for (RetryInfo current : retryInfo) {
216213
if (current.exception.isAssignableFrom(exception)) {
217-
RetryInfo match = findMostSpecificRetryInfo(current.children, exception);
214+
RetryInfo match = findMostSpecificRetryInfo(current.children, exception);
218215
return match == null ? current : match;
219216
}
220217
}
@@ -239,8 +236,8 @@ void verifyCaller(Callable<?> callable) {
239236
for (Class<?> exceptionOrError : callMethod.getExceptionTypes()) {
240237
Preconditions.checkArgument(Exception.class.isAssignableFrom(exceptionOrError),
241238
"Callable method exceptions must be derived from Exception");
242-
@SuppressWarnings("unchecked") Class<? extends Exception> exception =
243-
(Class<? extends Exception>) exceptionOrError;
239+
@SuppressWarnings("unchecked")
240+
Class<? extends Exception> exception = (Class<? extends Exception>) exceptionOrError;
244241
Preconditions.checkArgument(findMostSpecificRetryInfo(retryInfo, exception) != null,
245242
"Declared exception '" + exception + "' is not covered by exception handler");
246243
}

src/main/java/com/google/gcloud/RetryHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
import java.util.logging.Logger;
3636

3737
/**
38-
* Utility class for retrying operations.
39-
* For more details about the parameters, see {@link RetryParams}.
40-
* If the request is never successful, a {@link RetriesExhaustedException} will be thrown.
38+
* Utility class for retrying operations. For more details about the parameters, see
39+
* {@link RetryParams}. If the request is never successful, a {@link RetriesExhaustedException} will
40+
* be thrown.
4141
*
4242
* @param <V> return value of the closure that is being run with retries
4343
*/
@@ -201,8 +201,8 @@ private V doRetry() throws RetryHelperException {
201201
}
202202
long sleepDurationMillis = getSleepDuration(params, attemptNumber);
203203
if (log.isLoggable(Level.FINE)) {
204-
log.fine(this + ": Attempt #" + attemptNumber + " failed [" + exception + "], sleeping for "
205-
+ sleepDurationMillis + " ms");
204+
log.fine(this + ": Attempt #" + attemptNumber + " failed [" + exception
205+
+ "], sleeping for " + sleepDurationMillis + " ms");
206206
}
207207
try {
208208
Thread.sleep(sleepDurationMillis);

src/main/java/com/google/gcloud/RetryParams.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import java.util.Objects;
2626

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

0 commit comments

Comments
 (0)