Skip to content

Commit 75e62d8

Browse files
committed
---
yaml --- r: 6391 b: refs/heads/tswast-patch-1 c: f0fe56a h: refs/heads/master i: 6389: 1bc5bdd 6387: 76e8be8 6383: 1f24d65
1 parent 77b731e commit 75e62d8

13 files changed

Lines changed: 147 additions & 765 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: d9c6b85f306c5493c3943fd30292e4ba1f4a4387
60+
refs/heads/tswast-patch-1: f0fe56a51fbe05f30d53d37fb2ee4bd4c5417bb9
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@
4040
import java.lang.reflect.Method;
4141
import java.net.HttpURLConnection;
4242
import java.net.URL;
43+
import java.util.Enumeration;
4344
import java.util.Locale;
4445
import java.util.Objects;
4546
import java.util.ServiceLoader;
4647
import java.util.Set;
48+
import java.util.jar.Attributes;
49+
import java.util.jar.JarFile;
50+
import java.util.jar.Manifest;
4751
import java.util.regex.Matcher;
4852
import java.util.regex.Pattern;
4953

@@ -56,6 +60,11 @@ public abstract class ServiceOptions<
5660
private static final String DEFAULT_HOST = "https://www.googleapis.com";
5761
private static final long serialVersionUID = 1203687993961393350L;
5862
private static final String PROJECT_ENV_NAME = "GCLOUD_PROJECT";
63+
private static final String MANIFEST_ARTIFACT_ID_KEY = "artifactId";
64+
private static final String MANIFEST_VERSION_KEY = "Implementation-Version";
65+
private static final String ARTIFACT_ID = "gcloud-java-core";
66+
private static final String APPLICATION_BASE_NAME = "gcloud-java";
67+
private static final String APPLICATION_NAME = getApplicationName();
5968

6069
private final String projectId;
6170
private final String host;
@@ -522,6 +531,13 @@ public Clock clock() {
522531
return clock;
523532
}
524533

534+
/**
535+
* Returns the application's name as a string in the format {@code gcloud-java/[version]}.
536+
*/
537+
public String applicationName() {
538+
return APPLICATION_NAME;
539+
}
540+
525541
protected int baseHashCode() {
526542
return Objects.hash(projectId, host, httpTransportFactoryClassName, authCredentialsState,
527543
retryParams, serviceFactoryClassName, serviceRpcFactoryClassName, connectTimeout,
@@ -568,4 +584,23 @@ private static <T> T newInstance(String className) throws IOException, ClassNotF
568584
private static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
569585
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
570586
}
587+
588+
private static String getApplicationName() {
589+
String version = null;
590+
try {
591+
Enumeration<URL> resources =
592+
ServiceOptions.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
593+
while (resources.hasMoreElements() && version == null) {
594+
Manifest manifest = new Manifest(resources.nextElement().openStream());
595+
Attributes manifestAttributes = manifest.getMainAttributes();
596+
String artifactId = manifestAttributes.getValue(MANIFEST_ARTIFACT_ID_KEY);
597+
if (artifactId != null && artifactId.equals(ARTIFACT_ID)) {
598+
version = manifestAttributes.getValue(MANIFEST_VERSION_KEY);
599+
}
600+
}
601+
} catch (IOException e) {
602+
// ignore
603+
}
604+
return version != null ? APPLICATION_BASE_NAME + "/" + version : APPLICATION_BASE_NAME;
605+
}
571606
}

branches/tswast-patch-1/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.gcloud.storage.BlobId;
2525
import com.google.gcloud.storage.BlobInfo;
2626
import com.google.gcloud.storage.BlobReadChannel;
27-
import com.google.gcloud.storage.CopyWriter;
2827
import com.google.gcloud.storage.BlobWriteChannel;
2928
import com.google.gcloud.storage.Bucket;
3029
import com.google.gcloud.storage.BucketInfo;
@@ -367,8 +366,8 @@ public String params() {
367366
private static class CopyAction extends StorageAction<CopyRequest> {
368367
@Override
369368
public void run(Storage storage, CopyRequest request) {
370-
CopyWriter copyWriter = storage.copy(request);
371-
System.out.println("Copied " + copyWriter.result());
369+
BlobInfo copiedBlobInfo = storage.copy(request);
370+
System.out.println("Copied " + copiedBlobInfo);
372371
}
373372

374373
@Override

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ public class DefaultStorageRpc implements StorageRpc {
7878

7979
// see: https://cloud.google.com/storage/docs/concepts-techniques#practices
8080
private static final Set<Integer> RETRYABLE_CODES = ImmutableSet.of(504, 503, 502, 500, 429, 408);
81-
private static final long MEGABYTE = 1024L * 1024L;
8281

8382
public DefaultStorageRpc(StorageOptions options) {
8483
HttpTransport transport = options.httpTransportFactory().create();
8584
HttpRequestInitializer initializer = options.httpRequestInitializer();
8685
this.options = options;
8786
storage = new Storage.Builder(transport, new JacksonFactory(), initializer)
8887
.setRootUrl(options.host())
89-
.setApplicationName("gcloud-java")
88+
.setApplicationName(options.applicationName())
9089
.build();
9190
}
9291

@@ -321,6 +320,30 @@ public StorageObject compose(Iterable<StorageObject> sources, StorageObject targ
321320
}
322321
}
323322

323+
@Override
324+
public StorageObject copy(StorageObject source, Map<Option, ?> sourceOptions,
325+
StorageObject target, Map<Option, ?> targetOptions) throws StorageException {
326+
try {
327+
return storage
328+
.objects()
329+
.copy(source.getBucket(), source.getName(), target.getBucket(), target.getName(),
330+
target.getContentType() != null ? target : null)
331+
.setProjection(DEFAULT_PROJECTION)
332+
.setIfSourceMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(sourceOptions))
333+
.setIfSourceMetagenerationNotMatch(
334+
IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(sourceOptions))
335+
.setIfSourceGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(sourceOptions))
336+
.setIfSourceGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(sourceOptions))
337+
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(targetOptions))
338+
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(targetOptions))
339+
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(targetOptions))
340+
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(targetOptions))
341+
.execute();
342+
} catch (IOException ex) {
343+
throw translate(ex);
344+
}
345+
}
346+
324347
@Override
325348
public byte[] load(StorageObject from, Map<Option, ?> options)
326349
throws StorageException {
@@ -498,46 +521,4 @@ public String open(StorageObject object, Map<Option, ?> options)
498521
throw translate(ex);
499522
}
500523
}
501-
502-
@Override
503-
public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException {
504-
return rewrite(rewriteRequest, null);
505-
}
506-
507-
@Override
508-
public RewriteResponse continueRewrite(RewriteResponse previousResponse) throws StorageException {
509-
return rewrite(previousResponse.rewriteRequest, previousResponse.rewriteToken);
510-
}
511-
512-
private RewriteResponse rewrite(RewriteRequest req, String token) throws StorageException {
513-
try {
514-
Long maxBytesRewrittenPerCall = req.megabytesRewrittenPerCall != null
515-
? req.megabytesRewrittenPerCall * MEGABYTE : null;
516-
com.google.api.services.storage.model.RewriteResponse rewriteReponse = storage.objects()
517-
.rewrite(req.source.getBucket(), req.source.getName(), req.target.getBucket(),
518-
req.target.getName(), req.target.getContentType() != null ? req.target : null)
519-
.setRewriteToken(token)
520-
.setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall)
521-
.setProjection(DEFAULT_PROJECTION)
522-
.setIfSourceMetagenerationMatch(IF_SOURCE_METAGENERATION_MATCH.getLong(req.sourceOptions))
523-
.setIfSourceMetagenerationNotMatch(
524-
IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(req.sourceOptions))
525-
.setIfSourceGenerationMatch(IF_SOURCE_GENERATION_MATCH.getLong(req.sourceOptions))
526-
.setIfSourceGenerationNotMatch(IF_SOURCE_GENERATION_NOT_MATCH.getLong(req.sourceOptions))
527-
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(req.targetOptions))
528-
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(req.targetOptions))
529-
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(req.targetOptions))
530-
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(req.targetOptions))
531-
.execute();
532-
return new RewriteResponse(
533-
req,
534-
rewriteReponse.getResource(),
535-
rewriteReponse.getObjectSize().longValue(),
536-
rewriteReponse.getDone(),
537-
rewriteReponse.getRewriteToken(),
538-
rewriteReponse.getTotalBytesRewritten().longValue());
539-
} catch (IOException ex) {
540-
throw translate(ex);
541-
}
542-
}
543524
}

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.InputStream;
2828
import java.util.List;
2929
import java.util.Map;
30-
import java.util.Objects;
3130

3231
public interface StorageRpc {
3332

@@ -133,89 +132,6 @@ public BatchResponse(Map<StorageObject, Tuple<Boolean, StorageException>> delete
133132
}
134133
}
135134

136-
class RewriteRequest {
137-
138-
public final StorageObject source;
139-
public final Map<StorageRpc.Option, ?> sourceOptions;
140-
public final StorageObject target;
141-
public final Map<StorageRpc.Option, ?> targetOptions;
142-
public final Long megabytesRewrittenPerCall;
143-
144-
public RewriteRequest(StorageObject source, Map<StorageRpc.Option, ?> sourceOptions,
145-
StorageObject target, Map<StorageRpc.Option, ?> targetOptions,
146-
Long megabytesRewrittenPerCall) {
147-
this.source = source;
148-
this.sourceOptions = sourceOptions;
149-
this.target = target;
150-
this.targetOptions = targetOptions;
151-
this.megabytesRewrittenPerCall = megabytesRewrittenPerCall;
152-
}
153-
154-
@Override
155-
public boolean equals(Object obj) {
156-
if (obj == null) {
157-
return false;
158-
}
159-
if (!(obj instanceof RewriteRequest)) {
160-
return false;
161-
}
162-
final RewriteRequest other = (RewriteRequest) obj;
163-
return Objects.equals(this.source, other.source)
164-
&& Objects.equals(this.sourceOptions, other.sourceOptions)
165-
&& Objects.equals(this.target, other.target)
166-
&& Objects.equals(this.targetOptions, other.targetOptions)
167-
&& Objects.equals(this.megabytesRewrittenPerCall, other.megabytesRewrittenPerCall);
168-
}
169-
170-
@Override
171-
public int hashCode() {
172-
return Objects.hash(source, sourceOptions, target, targetOptions, megabytesRewrittenPerCall);
173-
}
174-
}
175-
176-
class RewriteResponse {
177-
178-
public final RewriteRequest rewriteRequest;
179-
public final StorageObject result;
180-
public final long blobSize;
181-
public final boolean isDone;
182-
public final String rewriteToken;
183-
public final long totalBytesRewritten;
184-
185-
public RewriteResponse(RewriteRequest rewriteRequest, StorageObject result, long blobSize,
186-
boolean isDone, String rewriteToken, long totalBytesRewritten) {
187-
this.rewriteRequest = rewriteRequest;
188-
this.result = result;
189-
this.blobSize = blobSize;
190-
this.isDone = isDone;
191-
this.rewriteToken = rewriteToken;
192-
this.totalBytesRewritten = totalBytesRewritten;
193-
}
194-
195-
@Override
196-
public boolean equals(Object obj) {
197-
if (obj == null) {
198-
return false;
199-
}
200-
if (!(obj instanceof RewriteResponse)) {
201-
return false;
202-
}
203-
final RewriteResponse other = (RewriteResponse) obj;
204-
return Objects.equals(this.rewriteRequest, other.rewriteRequest)
205-
&& Objects.equals(this.result, other.result)
206-
&& Objects.equals(this.rewriteToken, other.rewriteToken)
207-
&& this.blobSize == other.blobSize
208-
&& Objects.equals(this.isDone, other.isDone)
209-
&& this.totalBytesRewritten == other.totalBytesRewritten;
210-
}
211-
212-
@Override
213-
public int hashCode() {
214-
return Objects.hash(rewriteRequest, result, blobSize, isDone, rewriteToken,
215-
totalBytesRewritten);
216-
}
217-
}
218-
219135
Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageException;
220136

221137
StorageObject create(StorageObject object, InputStream content, Map<Option, ?> options)
@@ -245,6 +161,9 @@ StorageObject patch(StorageObject storageObject, Map<Option, ?> options)
245161
StorageObject compose(Iterable<StorageObject> sources, StorageObject target,
246162
Map<Option, ?> targetOptions) throws StorageException;
247163

164+
StorageObject copy(StorageObject source, Map<Option, ?> sourceOptions,
165+
StorageObject target, Map<Option, ?> targetOptions) throws StorageException;
166+
248167
byte[] load(StorageObject storageObject, Map<Option, ?> options)
249168
throws StorageException;
250169

@@ -255,8 +174,4 @@ byte[] read(StorageObject from, Map<Option, ?> options, long position, int bytes
255174

256175
void write(String uploadId, byte[] toWrite, int toWriteOffset, StorageObject dest,
257176
long destOffset, int length, boolean last) throws StorageException;
258-
259-
RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException;
260-
261-
RewriteResponse continueRewrite(RewriteResponse previousResponse) throws StorageException;
262177
}

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,19 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
213213
}
214214

215215
/**
216-
* Sends a copy request for the current blob to the target blob. Possibly also some of the
217-
* metadata are copied (e.g. content-type).
216+
* Copies this blob to the specified target. Possibly copying also some of the metadata
217+
* (e.g. content-type).
218218
*
219219
* @param targetBlob target blob's id
220220
* @param options source blob options
221-
* @return a {@link CopyWriter} object that can be used to get information on the newly created
222-
* blob or to complete the copy if more than one RPC request is needed
221+
* @return the copied blob
223222
* @throws StorageException upon failure
224223
*/
225-
public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
226-
BlobInfo updatedInfo = BlobInfo.builder(targetBlob).build();
224+
public Blob copyTo(BlobId targetBlob, BlobSourceOption... options) {
225+
BlobInfo updatedInfo = info.toBuilder().blobId(targetBlob).build();
227226
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
228227
.sourceOptions(convert(info, options)).target(updatedInfo).build();
229-
return storage.copy(copyRequest);
228+
return new Blob(storage, storage.copy(copyRequest));
230229
}
231230

232231
/**
@@ -241,35 +240,33 @@ public boolean delete(BlobSourceOption... options) {
241240
}
242241

243242
/**
244-
* Sends a copy request for the current blob to the target bucket, preserving its name. Possibly
245-
* copying also some of the metadata (e.g. content-type).
243+
* Copies this blob to the target bucket, preserving its name. Possibly copying also some of the
244+
* metadata (e.g. content-type).
246245
*
247246
* @param targetBucket target bucket's name
248247
* @param options source blob options
249-
* @return a {@link CopyWriter} object that can be used to get information on the newly created
250-
* blob or to complete the copy if more than one RPC request is needed
248+
* @return the copied blob
251249
* @throws StorageException upon failure
252250
*/
253-
public CopyWriter copyTo(String targetBucket, BlobSourceOption... options) {
251+
public Blob copyTo(String targetBucket, BlobSourceOption... options) {
254252
return copyTo(targetBucket, info.name(), options);
255253
}
256254

257255
/**
258-
* Sends a copy request for the current blob to the target blob. Possibly also some of the
259-
* metadata are copied (e.g. content-type).
256+
* Copies this blob to the target bucket with a new name. Possibly copying also some of the
257+
* metadata (e.g. content-type).
260258
*
261259
* @param targetBucket target bucket's name
262260
* @param targetBlob target blob's name
263261
* @param options source blob options
264-
* @return a {@link CopyWriter} object that can be used to get information on the newly created
265-
* blob or to complete the copy if more than one RPC request is needed
262+
* @return the copied blob
266263
* @throws StorageException upon failure
267264
*/
268-
public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
269-
BlobInfo updatedInfo = BlobInfo.builder(targetBucket, targetBlob).build();
265+
public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
266+
BlobInfo updatedInfo = info.toBuilder().blobId(BlobId.of(targetBucket, targetBlob)).build();
270267
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
271268
.sourceOptions(convert(info, options)).target(updatedInfo).build();
272-
return storage.copy(copyRequest);
269+
return new Blob(storage, storage.copy(copyRequest));
273270
}
274271

275272
/**

0 commit comments

Comments
 (0)