Skip to content

Commit c405eb7

Browse files
author
Praful Makani
committed
rename EX_HOST to SERVICE_ENDPOINT,rename withExtHostName to withHostName and used google-java-formatter
1 parent ad66d4c commit c405eb7

File tree

3 files changed

+85
-86
lines changed

3 files changed

+85
-86
lines changed

google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ class SignUrlOption implements Serializable {
892892
private final Object value;
893893

894894
enum Option {
895-
HTTP_METHOD, CONTENT_TYPE, MD5, EXT_HEADERS, SERVICE_ACCOUNT_CRED,EXT_HOST
895+
HTTP_METHOD, CONTENT_TYPE, MD5, EXT_HEADERS, SERVICE_ACCOUNT_CRED,SERVICE_ENDPOINT
896896
}
897897

898898
private SignUrlOption(Option option, Object value) {
@@ -957,8 +957,8 @@ public static SignUrlOption signWith(ServiceAccountSigner signer) {
957957
/**
958958
* Provides a host name to sign the URL. If not provided than host name will be default
959959
*/
960-
public static SignUrlOption withExtHostName(String extHostName){
961-
return new SignUrlOption(Option.EXT_HOST, extHostName);
960+
public static SignUrlOption withHostName(String hostName){
961+
return new SignUrlOption(Option.SERVICE_ENDPOINT, hostName);
962962
}
963963
}
964964

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

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@
3333
import static com.google.common.base.Preconditions.checkState;
3434
import static java.nio.charset.StandardCharsets.UTF_8;
3535

36-
import java.io.ByteArrayInputStream;
37-
import java.io.InputStream;
38-
import java.io.UnsupportedEncodingException;
39-
import java.net.MalformedURLException;
40-
import java.net.URI;
41-
import java.net.URL;
42-
import java.net.URLEncoder;
43-
import java.util.Arrays;
44-
import java.util.Collections;
45-
import java.util.EnumMap;
46-
import java.util.List;
47-
import java.util.Map;
48-
import java.util.Set;
49-
import java.util.concurrent.Callable;
50-
import java.util.concurrent.TimeUnit;
51-
5236
import com.google.api.gax.paging.Page;
5337
import com.google.api.services.storage.model.BucketAccessControl;
5438
import com.google.api.services.storage.model.ObjectAccessControl;
@@ -78,6 +62,21 @@
7862
import com.google.common.io.BaseEncoding;
7963
import com.google.common.net.UrlEscapers;
8064
import com.google.common.primitives.Ints;
65+
import java.io.ByteArrayInputStream;
66+
import java.io.InputStream;
67+
import java.io.UnsupportedEncodingException;
68+
import java.net.MalformedURLException;
69+
import java.net.URI;
70+
import java.net.URL;
71+
import java.net.URLEncoder;
72+
import java.util.Arrays;
73+
import java.util.Collections;
74+
import java.util.EnumMap;
75+
import java.util.List;
76+
import java.util.Map;
77+
import java.util.Set;
78+
import java.util.concurrent.Callable;
79+
import java.util.concurrent.TimeUnit;
8180

8281
final class StorageImpl extends BaseService<StorageOptions> implements Storage {
8382

@@ -501,61 +500,61 @@ private BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options)
501500

502501
@Override
503502
public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOption... options) {
504-
EnumMap<SignUrlOption.Option, Object> optionMap = Maps.newEnumMap(SignUrlOption.Option.class);
505-
for (SignUrlOption option : options) {
506-
optionMap.put(option.getOption(), option.getValue());
507-
}
508-
ServiceAccountSigner credentials =
509-
(ServiceAccountSigner) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
510-
if (credentials == null) {
511-
checkState(this.getOptions().getCredentials() instanceof ServiceAccountSigner,
512-
"Signing key was not provided and could not be derived");
513-
credentials = (ServiceAccountSigner) this.getOptions().getCredentials();
514-
}
515-
516-
long expiration = TimeUnit.SECONDS.convert(
517-
getOptions().getClock().millisTime() + unit.toMillis(duration), TimeUnit.MILLISECONDS);
518-
519-
StringBuilder stPath = new StringBuilder();
520-
if (!blobInfo.getBucket().startsWith(PATH_DELIMITER)) {
521-
stPath.append(PATH_DELIMITER);
522-
}
523-
stPath.append(blobInfo.getBucket());
524-
if (!blobInfo.getBucket().endsWith(PATH_DELIMITER)) {
525-
stPath.append(PATH_DELIMITER);
526-
}
527-
if (blobInfo.getName().startsWith(PATH_DELIMITER)) {
528-
stPath.setLength(stPath.length() - 1);
529-
}
530-
531-
String escapedName = UrlEscapers.urlFragmentEscaper().escape(blobInfo.getName());
532-
stPath.append(escapedName.replace("?", "%3F"));
533-
534-
URI path = URI.create(stPath.toString());
535-
536-
try {
537-
SignatureInfo signatureInfo = buildSignatureInfo(optionMap, blobInfo, expiration, path);
538-
byte[] signatureBytes =
539-
credentials.sign(signatureInfo.constructUnsignedPayload().getBytes(UTF_8));
540-
StringBuilder stBuilder = new StringBuilder();
541-
if(optionMap.get(SignUrlOption.Option.EXT_HOST) == null){
542-
stBuilder.append(DEFAULT_STORAGE_HOST).append(path);
543-
}
544-
else{
545-
stBuilder.append(optionMap.get(SignUrlOption.Option.EXT_HOST).toString()).append(path);
546-
}
547-
548-
String signature =
549-
URLEncoder.encode(BaseEncoding.base64().encode(signatureBytes), UTF_8.name());
550-
stBuilder.append("?GoogleAccessId=").append(credentials.getAccount());
551-
stBuilder.append("&Expires=").append(expiration);
552-
stBuilder.append("&Signature=").append(signature);
553-
554-
return new URL(stBuilder.toString());
555-
556-
} catch (MalformedURLException | UnsupportedEncodingException ex) {
557-
throw new IllegalStateException(ex);
558-
}
503+
EnumMap<SignUrlOption.Option, Object> optionMap = Maps.newEnumMap(SignUrlOption.Option.class);
504+
for (SignUrlOption option : options) {
505+
optionMap.put(option.getOption(), option.getValue());
506+
}
507+
ServiceAccountSigner credentials =
508+
(ServiceAccountSigner) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
509+
if (credentials == null) {
510+
checkState(this.getOptions().getCredentials() instanceof ServiceAccountSigner,
511+
"Signing key was not provided and could not be derived");
512+
credentials = (ServiceAccountSigner) this.getOptions().getCredentials();
513+
}
514+
515+
long expiration = TimeUnit.SECONDS.convert(
516+
getOptions().getClock().millisTime() + unit.toMillis(duration), TimeUnit.MILLISECONDS);
517+
518+
StringBuilder stPath = new StringBuilder();
519+
if (!blobInfo.getBucket().startsWith(PATH_DELIMITER)) {
520+
stPath.append(PATH_DELIMITER);
521+
}
522+
stPath.append(blobInfo.getBucket());
523+
if (!blobInfo.getBucket().endsWith(PATH_DELIMITER)) {
524+
stPath.append(PATH_DELIMITER);
525+
}
526+
if (blobInfo.getName().startsWith(PATH_DELIMITER)) {
527+
stPath.setLength(stPath.length() - 1);
528+
}
529+
530+
String escapedName = UrlEscapers.urlFragmentEscaper().escape(blobInfo.getName());
531+
stPath.append(escapedName.replace("?", "%3F"));
532+
533+
URI path = URI.create(stPath.toString());
534+
535+
try {
536+
SignatureInfo signatureInfo = buildSignatureInfo(optionMap, blobInfo, expiration, path);
537+
byte[] signatureBytes =
538+
credentials.sign(signatureInfo.constructUnsignedPayload().getBytes(UTF_8));
539+
StringBuilder stBuilder = new StringBuilder();
540+
if (optionMap.get(SignUrlOption.Option.SERVICE_ENDPOINT) == null){
541+
stBuilder.append(DEFAULT_STORAGE_HOST).append(path);
542+
}
543+
else {
544+
stBuilder.append(optionMap.get(SignUrlOption.Option.SERVICE_ENDPOINT)).append(path);
545+
}
546+
547+
String signature =
548+
URLEncoder.encode(BaseEncoding.base64().encode(signatureBytes), UTF_8.name());
549+
stBuilder.append("?GoogleAccessId=").append(credentials.getAccount());
550+
stBuilder.append("&Expires=").append(expiration);
551+
stBuilder.append("&Signature=").append(signature);
552+
553+
return new URL(stBuilder.toString());
554+
555+
} catch (MalformedURLException | UnsupportedEncodingException ex) {
556+
throw new IllegalStateException(ex);
557+
}
559558
}
560559

561560
/**

google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,10 +1645,10 @@ public void testSignUrlWithCustomUrl()
16451645
ServiceAccountCredentials credentials =
16461646
new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null);
16471647
storage = options.toBuilder().setCredentials(credentials).build().getService();
1648-
URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
1648+
URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
16491649
String stringUrl = url.toString();
16501650
String expectedUrl =
1651-
new StringBuilder("https://custom.host.com/")
1651+
new StringBuilder("https://example.com/")
16521652
.append(BUCKET_NAME1)
16531653
.append('/')
16541654
.append(BLOB_NAME1)
@@ -1730,11 +1730,11 @@ public void testSignUrlLeadingSlashWithCustomUrl()
17301730
new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null);
17311731
storage = options.toBuilder().setCredentials(credentials).build().getService();
17321732
URL url =
1733-
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
1733+
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
17341734
String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName);
17351735
String stringUrl = url.toString();
17361736
String expectedUrl =
1737-
new StringBuilder("https://custom.host.com/")
1737+
new StringBuilder("https://example.com/")
17381738
.append(BUCKET_NAME1)
17391739
.append(escapedBlobName)
17401740
.append("?GoogleAccessId=")
@@ -1830,10 +1830,10 @@ public void testSignUrlWithOptionsAndCustomUrl()
18301830
Storage.SignUrlOption.httpMethod(HttpMethod.POST),
18311831
Storage.SignUrlOption.withContentType(),
18321832
Storage.SignUrlOption.withMd5(),
1833-
Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
1833+
Storage.SignUrlOption.withHostName("https://example.com"));
18341834
String stringUrl = url.toString();
18351835
String expectedUrl =
1836-
new StringBuilder("https://custom.host.com/")
1836+
new StringBuilder("https://example.com/")
18371837
.append(BUCKET_NAME1)
18381838
.append('/')
18391839
.append(BLOB_NAME1)
@@ -1937,12 +1937,12 @@ public void testSignUrlForBlobWithSpecialCharsAndCustomUrl()
19371937
for (char specialChar : specialChars) {
19381938
String blobName = "/a" + specialChar + "b";
19391939
URL url =
1940-
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
1940+
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
19411941
String escapedBlobName =
19421942
UrlEscapers.urlFragmentEscaper().escape(blobName).replace("?", "%3F");
19431943
String stringUrl = url.toString();
19441944
String expectedUrl =
1945-
new StringBuilder("https://custom.host.com/")
1945+
new StringBuilder("https://example.com/")
19461946
.append(BUCKET_NAME1)
19471947
.append(escapedBlobName)
19481948
.append("?GoogleAccessId=")
@@ -2047,10 +2047,10 @@ public void testSignUrlWithExtHeadersAndCustomUrl()
20472047
Storage.SignUrlOption.httpMethod(HttpMethod.PUT),
20482048
Storage.SignUrlOption.withContentType(),
20492049
Storage.SignUrlOption.withExtHeaders(extHeaders),
2050-
Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
2050+
Storage.SignUrlOption.withHostName("https://example.com"));
20512051
String stringUrl = url.toString();
20522052
String expectedUrl =
2053-
new StringBuilder("https://custom.host.com/")
2053+
new StringBuilder("https://example.com/")
20542054
.append(BUCKET_NAME1)
20552055
.append('/')
20562056
.append(BLOB_NAME1)
@@ -2140,11 +2140,11 @@ public void testSignUrlForBlobWithSlashesAndCustomUrl()
21402140

21412141
String blobName = "/foo/bar/baz #%20other cool stuff.txt";
21422142
URL url =
2143-
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
2143+
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
21442144
String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName);
21452145
String stringUrl = url.toString();
21462146
String expectedUrl =
2147-
new StringBuilder("https://custom.host.com/")
2147+
new StringBuilder("https://example.com/")
21482148
.append(BUCKET_NAME1)
21492149
.append(escapedBlobName)
21502150
.append("?GoogleAccessId=")

0 commit comments

Comments
 (0)