Skip to content

Commit

Permalink
rename EX_HOST to SERVICE_ENDPOINT,rename withExtHostName to withHost…
Browse files Browse the repository at this point in the history
…Name and used google-java-formatter
  • Loading branch information
Praful Makani committed Nov 16, 2018
1 parent 5b38ded commit 1c2e70b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ class SignUrlOption implements Serializable {
private final Object value;

enum Option {
HTTP_METHOD, CONTENT_TYPE, MD5, EXT_HEADERS, SERVICE_ACCOUNT_CRED,EXT_HOST
HTTP_METHOD, CONTENT_TYPE, MD5, EXT_HEADERS, SERVICE_ACCOUNT_CRED,SERVICE_ENDPOINT
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@
import static com.google.common.base.Preconditions.checkState;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import com.google.api.gax.paging.Page;
import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
Expand Down Expand Up @@ -78,6 +62,21 @@
import com.google.common.io.BaseEncoding;
import com.google.common.net.UrlEscapers;
import com.google.common.primitives.Ints;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

final class StorageImpl extends BaseService<StorageOptions> implements Storage {

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

@Override
public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOption... options) {
EnumMap<SignUrlOption.Option, Object> optionMap = Maps.newEnumMap(SignUrlOption.Option.class);
for (SignUrlOption option : options) {
optionMap.put(option.getOption(), option.getValue());
}
ServiceAccountSigner credentials =
(ServiceAccountSigner) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
if (credentials == null) {
checkState(this.getOptions().getCredentials() instanceof ServiceAccountSigner,
"Signing key was not provided and could not be derived");
credentials = (ServiceAccountSigner) this.getOptions().getCredentials();
}
long expiration = TimeUnit.SECONDS.convert(
getOptions().getClock().millisTime() + unit.toMillis(duration), TimeUnit.MILLISECONDS);
StringBuilder stPath = new StringBuilder();
if (!blobInfo.getBucket().startsWith(PATH_DELIMITER)) {
stPath.append(PATH_DELIMITER);
}
stPath.append(blobInfo.getBucket());
if (!blobInfo.getBucket().endsWith(PATH_DELIMITER)) {
stPath.append(PATH_DELIMITER);
}
if (blobInfo.getName().startsWith(PATH_DELIMITER)) {
stPath.setLength(stPath.length() - 1);
}
String escapedName = UrlEscapers.urlFragmentEscaper().escape(blobInfo.getName());
stPath.append(escapedName.replace("?", "%3F"));
URI path = URI.create(stPath.toString());
try {
SignatureInfo signatureInfo = buildSignatureInfo(optionMap, blobInfo, expiration, path);
byte[] signatureBytes =
credentials.sign(signatureInfo.constructUnsignedPayload().getBytes(UTF_8));
StringBuilder stBuilder = new StringBuilder();
if(optionMap.get(SignUrlOption.Option.EXT_HOST) == null){
stBuilder.append(DEFAULT_STORAGE_HOST).append(path);
}
else{
stBuilder.append(optionMap.get(SignUrlOption.Option.EXT_HOST).toString()).append(path);
}
String signature =
URLEncoder.encode(BaseEncoding.base64().encode(signatureBytes), UTF_8.name());
stBuilder.append("?GoogleAccessId=").append(credentials.getAccount());
stBuilder.append("&Expires=").append(expiration);
stBuilder.append("&Signature=").append(signature);
return new URL(stBuilder.toString());
} catch (MalformedURLException | UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
EnumMap<SignUrlOption.Option, Object> optionMap = Maps.newEnumMap(SignUrlOption.Option.class);
for (SignUrlOption option : options) {
optionMap.put(option.getOption(), option.getValue());
}
ServiceAccountSigner credentials =
(ServiceAccountSigner) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
if (credentials == null) {
checkState(this.getOptions().getCredentials() instanceof ServiceAccountSigner,
"Signing key was not provided and could not be derived");
credentials = (ServiceAccountSigner) this.getOptions().getCredentials();
}

long expiration = TimeUnit.SECONDS.convert(
getOptions().getClock().millisTime() + unit.toMillis(duration), TimeUnit.MILLISECONDS);

StringBuilder stPath = new StringBuilder();
if (!blobInfo.getBucket().startsWith(PATH_DELIMITER)) {
stPath.append(PATH_DELIMITER);
}
stPath.append(blobInfo.getBucket());
if (!blobInfo.getBucket().endsWith(PATH_DELIMITER)) {
stPath.append(PATH_DELIMITER);
}
if (blobInfo.getName().startsWith(PATH_DELIMITER)) {
stPath.setLength(stPath.length() - 1);
}

String escapedName = UrlEscapers.urlFragmentEscaper().escape(blobInfo.getName());
stPath.append(escapedName.replace("?", "%3F"));
URI path = URI.create(stPath.toString());

try {
SignatureInfo signatureInfo = buildSignatureInfo(optionMap, blobInfo, expiration, path);
byte[] signatureBytes =
credentials.sign(signatureInfo.constructUnsignedPayload().getBytes(UTF_8));
StringBuilder stBuilder = new StringBuilder();
if (optionMap.get(SignUrlOption.Option.SERVICE_ENDPOINT) == null){
stBuilder.append(DEFAULT_STORAGE_HOST).append(path);
}
else {
stBuilder.append(optionMap.get(SignUrlOption.Option.SERVICE_ENDPOINT)).append(path);
}
String signature =
URLEncoder.encode(BaseEncoding.base64().encode(signatureBytes), UTF_8.name());
stBuilder.append("?GoogleAccessId=").append(credentials.getAccount());
stBuilder.append("&Expires=").append(expiration);
stBuilder.append("&Signature=").append(signature);

return new URL(stBuilder.toString());

} catch (MalformedURLException | UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1645,10 +1645,10 @@ public void testSignUrlWithCustomUrl()
ServiceAccountCredentials credentials =
new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null);
storage = options.toBuilder().setCredentials(credentials).build().getService();
URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
String stringUrl = url.toString();
String expectedUrl =
new StringBuilder("https://custom.host.com/")
new StringBuilder("https://example.com/")
.append(BUCKET_NAME1)
.append('/')
.append(BLOB_NAME1)
Expand Down Expand Up @@ -1730,11 +1730,11 @@ public void testSignUrlLeadingSlashWithCustomUrl()
new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null);
storage = options.toBuilder().setCredentials(credentials).build().getService();
URL url =
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName);
String stringUrl = url.toString();
String expectedUrl =
new StringBuilder("https://custom.host.com/")
new StringBuilder("https://example.com/")
.append(BUCKET_NAME1)
.append(escapedBlobName)
.append("?GoogleAccessId=")
Expand Down Expand Up @@ -1830,10 +1830,10 @@ public void testSignUrlWithOptionsAndCustomUrl()
Storage.SignUrlOption.httpMethod(HttpMethod.POST),
Storage.SignUrlOption.withContentType(),
Storage.SignUrlOption.withMd5(),
Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
Storage.SignUrlOption.withHostName("https://example.com"));
String stringUrl = url.toString();
String expectedUrl =
new StringBuilder("https://custom.host.com/")
new StringBuilder("https://example.com/")
.append(BUCKET_NAME1)
.append('/')
.append(BLOB_NAME1)
Expand Down Expand Up @@ -1937,12 +1937,12 @@ public void testSignUrlForBlobWithSpecialCharsAndCustomUrl()
for (char specialChar : specialChars) {
String blobName = "/a" + specialChar + "b";
URL url =
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
String escapedBlobName =
UrlEscapers.urlFragmentEscaper().escape(blobName).replace("?", "%3F");
String stringUrl = url.toString();
String expectedUrl =
new StringBuilder("https://custom.host.com/")
new StringBuilder("https://example.com/")
.append(BUCKET_NAME1)
.append(escapedBlobName)
.append("?GoogleAccessId=")
Expand Down Expand Up @@ -2047,10 +2047,10 @@ public void testSignUrlWithExtHeadersAndCustomUrl()
Storage.SignUrlOption.httpMethod(HttpMethod.PUT),
Storage.SignUrlOption.withContentType(),
Storage.SignUrlOption.withExtHeaders(extHeaders),
Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
Storage.SignUrlOption.withHostName("https://example.com"));
String stringUrl = url.toString();
String expectedUrl =
new StringBuilder("https://custom.host.com/")
new StringBuilder("https://example.com/")
.append(BUCKET_NAME1)
.append('/')
.append(BLOB_NAME1)
Expand Down Expand Up @@ -2140,11 +2140,11 @@ public void testSignUrlForBlobWithSlashesAndCustomUrl()

String blobName = "/foo/bar/baz #%20other cool stuff.txt";
URL url =
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withExtHostName("https://custom.host.com"));
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS, Storage.SignUrlOption.withHostName("https://example.com"));
String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName);
String stringUrl = url.toString();
String expectedUrl =
new StringBuilder("https://custom.host.com/")
new StringBuilder("https://example.com/")
.append(BUCKET_NAME1)
.append(escapedBlobName)
.append("?GoogleAccessId=")
Expand Down

0 comments on commit 1c2e70b

Please sign in to comment.