Skip to content

Commit bd5f566

Browse files
committed
googleapis#2421 Used URI instead Path for storage resources
1 parent b25f54e commit bd5f566

3 files changed

Lines changed: 85 additions & 50 deletions

File tree

google-cloud-storage/src/main/java/com/google/cloud/storage/SignatureInfo.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.google.cloud.storage;
218

319
import static com.google.common.base.Preconditions.checkArgument;
4-
import java.nio.file.Path;
20+
21+
import java.net.URI;
522
import java.util.ArrayList;
623
import java.util.Collections;
724
import java.util.List;
@@ -22,7 +39,7 @@ public class SignatureInfo {
2239
private final String contentType;
2340
private final long expiration;
2441
private final Map<String, String> canonicalizedExtensionHeaders;
25-
private final Path canonicalizedResource;
42+
private final URI canonicalizedResource;
2643

2744
private SignatureInfo(Builder builder) {
2845
this.httpVerb = builder.httpVerb;
@@ -36,7 +53,7 @@ private SignatureInfo(Builder builder) {
3653
/**
3754
* Constructs payload to be signed.
3855
*
39-
* @return
56+
* @return paylod to sign
4057
* @see https://cloud.google.com/storage/docs/access-control#Signed-URLs
4158
*/
4259
public String constructUnsignedPayload() {
@@ -90,7 +107,7 @@ public Map<String, String> getCanonicalizedExtensionHeaders() {
90107
return canonicalizedExtensionHeaders;
91108
}
92109

93-
public Path getCanonicalizedResource() {
110+
public URI getCanonicalizedResource() {
94111
return canonicalizedResource;
95112
}
96113

@@ -101,17 +118,17 @@ public final static class Builder {
101118
private String contentType;
102119
private final long expiration;
103120
private Map<String, String> canonicalizedExtensionHeaders;
104-
private final Path canonicalizedResource;
121+
private final URI canonicalizedResource;
105122

106123
/**
107124
* Constructs builder.
108125
*
109126
* @param httpVerb the HTTP method
110127
* @param expiration the EPOX expiration date
111-
* @param canonicalizedResource the resource path
128+
* @param canonicalizedResource the resource URI
112129
* @throws IllegalArgumentException if required field is not provided.
113130
*/
114-
public Builder(HttpMethod httpVerb, long expiration, Path canonicalizedResource) {
131+
public Builder(HttpMethod httpVerb, long expiration, URI canonicalizedResource) {
115132
checkArgument(httpVerb != null, "Required HTTP method");
116133
checkArgument(canonicalizedResource != null, "Required canonicalized resource");
117134

google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@
6666
import java.io.InputStream;
6767
import java.io.UnsupportedEncodingException;
6868
import java.net.MalformedURLException;
69+
import java.net.URI;
6970
import java.net.URL;
7071
import java.net.URLEncoder;
71-
import java.nio.file.Path;
72-
import java.nio.file.Paths;
7372
import java.util.Arrays;
7473
import java.util.Collections;
7574
import java.util.EnumMap;
@@ -529,61 +528,63 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
529528

530529
String escapedName = UrlEscapers.urlFragmentEscaper().escape(blobInfo.getName());
531530
stPath.append(escapedName.replace("?", "%3F"));
532-
533-
Path path = Paths.get(stPath.toString());
534-
531+
532+
URI resource = URI.create(stPath.toString());
533+
535534
try {
536-
SignatureInfo signatureInfo = buildSignarueInfo(optionMap, blobInfo, expiration, path);
535+
SignatureInfo signatureInfo = buildSignarueInfo(optionMap, blobInfo, expiration, resource);
537536
byte[] signatureBytes =
538537
credentials.sign(signatureInfo.constructUnsignedPayload().getBytes(UTF_8));
539-
StringBuilder stBuilder = new StringBuilder("https://storage.googleapis.com").append(path);
538+
StringBuilder stBuilder =
539+
new StringBuilder("https://storage.googleapis.com").append(resource);
540540
String signature =
541541
URLEncoder.encode(BaseEncoding.base64().encode(signatureBytes), UTF_8.name());
542542
stBuilder.append("?GoogleAccessId=").append(credentials.getAccount());
543543
stBuilder.append("&Expires=").append(expiration);
544544
stBuilder.append("&Signature=").append(signature);
545-
545+
546546
return new URL(stBuilder.toString());
547-
547+
548548
} catch (MalformedURLException | UnsupportedEncodingException ex) {
549549
throw new IllegalStateException(ex);
550550
}
551551
}
552552

553553
/**
554554
* Builds signature info.
555-
* @param optionMap
556-
* @param blobInfo
557-
* @param expiration
558-
* @param path
559-
* @return
555+
* @param optionMap the option map
556+
* @param blobInfo the blob info
557+
* @param expiration the expiration in seconds
558+
* @param resource the resource URI
559+
* @return signature info
560560
*/
561561
private SignatureInfo buildSignarueInfo(Map<SignUrlOption.Option, Object> optionMap,
562-
BlobInfo blobInfo, long expiration, Path path) {
563-
564-
HttpMethod httpVerb = optionMap.containsKey(SignUrlOption.Option.HTTP_METHOD) ?
565-
(HttpMethod) optionMap.get(SignUrlOption.Option.HTTP_METHOD) : HttpMethod.GET;
566-
562+
BlobInfo blobInfo, long expiration, URI resource) {
563+
564+
HttpMethod httpVerb = optionMap.containsKey(SignUrlOption.Option.HTTP_METHOD)
565+
? (HttpMethod) optionMap.get(SignUrlOption.Option.HTTP_METHOD)
566+
: HttpMethod.GET;
567+
567568
SignatureInfo.Builder signatureInfoBuilder =
568-
new SignatureInfo.Builder(httpVerb, expiration, path);
569-
570-
if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.MD5), false)) {
571-
checkArgument(blobInfo.getMd5() != null, "Blob is missing a value for md5");
572-
signatureInfoBuilder.setContentMd5(blobInfo.getMd5());
573-
}
574-
575-
if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.CONTENT_TYPE), false)) {
576-
checkArgument(blobInfo.getContentType() != null, "Blob is missing a value for content-type");
577-
signatureInfoBuilder.setContentType(blobInfo.getContentType());
578-
}
579-
580-
@SuppressWarnings("unchecked")
581-
Map<String, String> extHeaders =
582-
(Map<String, String>) (optionMap.containsKey(SignUrlOption.Option.EXT_HEADERS) ?
583-
(Map<String, String>) optionMap.get(SignUrlOption.Option.EXT_HEADERS) :
584-
Collections.emptyMap());
585-
586-
return signatureInfoBuilder.setCanonicalizedExtensionHeaders(extHeaders).build();
569+
new SignatureInfo.Builder(httpVerb, expiration, resource);
570+
571+
if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.MD5), false)) {
572+
checkArgument(blobInfo.getMd5() != null, "Blob is missing a value for md5");
573+
signatureInfoBuilder.setContentMd5(blobInfo.getMd5());
574+
}
575+
576+
if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.CONTENT_TYPE), false)) {
577+
checkArgument(blobInfo.getContentType() != null, "Blob is missing a value for content-type");
578+
signatureInfoBuilder.setContentType(blobInfo.getContentType());
579+
}
580+
581+
@SuppressWarnings("unchecked")
582+
Map<String, String> extHeaders =
583+
(Map<String, String>) (optionMap.containsKey(SignUrlOption.Option.EXT_HEADERS)
584+
? (Map<String, String>) optionMap.get(SignUrlOption.Option.EXT_HEADERS)
585+
: Collections.emptyMap());
586+
587+
return signatureInfoBuilder.setCanonicalizedExtensionHeaders(extHeaders).build();
587588
}
588589

589590
@Override

google-cloud-storage/src/test/java/com/google/cloud/storage/SignatureInfoTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.google.cloud.storage;
218

319
import static org.junit.Assert.assertEquals;
4-
import java.nio.file.Paths;
20+
21+
import com.google.cloud.storage.SignatureInfo.Builder;
22+
import java.net.URI;
523
import java.util.HashMap;
624
import java.util.Map;
725
import org.junit.Test;
8-
import com.google.cloud.storage.SignatureInfo.Builder;
926

1027
public class SignatureInfoTest {
1128

@@ -14,7 +31,7 @@ public class SignatureInfoTest {
1431
@Test(expected = IllegalArgumentException.class)
1532
public void requireHttpVerb() {
1633

17-
new SignatureInfo.Builder(null, 0L, Paths.get(RESOURCE)).build();
34+
new SignatureInfo.Builder(null, 0L, URI.create(RESOURCE)).build();
1835
}
1936

2037
@Test(expected = IllegalArgumentException.class)
@@ -26,7 +43,7 @@ public void requireResource() {
2643
@Test
2744
public void constructUnsignedPayload() {
2845

29-
Builder builder = new SignatureInfo.Builder(HttpMethod.PUT, 0L, Paths.get(RESOURCE));
46+
Builder builder = new SignatureInfo.Builder(HttpMethod.PUT, 0L, URI.create(RESOURCE));
3047

3148
String unsignedPayload = builder.build().constructUnsignedPayload();
3249

@@ -36,7 +53,7 @@ public void constructUnsignedPayload() {
3653
@Test
3754
public void constructUnsignedPayloadWithExtensionHeaders() {
3855

39-
Builder builder = new SignatureInfo.Builder(HttpMethod.PUT, 0L, Paths.get(RESOURCE));
56+
Builder builder = new SignatureInfo.Builder(HttpMethod.PUT, 0L, URI.create(RESOURCE));
4057

4158
Map<String, String> extensionHeaders = new HashMap<>();
4259
extensionHeaders.put("x-goog-acl", "public-read");

0 commit comments

Comments
 (0)