3333import static com .google .common .base .Preconditions .checkState ;
3434import 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+
3652import com .google .api .gax .paging .Page ;
3753import com .google .api .services .storage .model .BucketAccessControl ;
3854import com .google .api .services .storage .model .ObjectAccessControl ;
6278import com .google .common .io .BaseEncoding ;
6379import com .google .common .net .UrlEscapers ;
6480import 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 ;
8081
8182final class StorageImpl extends BaseService <StorageOptions > implements Storage {
8283
@@ -500,52 +501,50 @@ private BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options)
500501
501502 @ Override
502503 public URL signUrl (BlobInfo blobInfo , long duration , TimeUnit unit , SignUrlOption ... options ) {
503- return signUrlOptions (DEFAULT_STORAGE_HOST , blobInfo , duration , unit , options );
504- }
505-
506- @ Override
507- public URL signUrl (String url , BlobInfo blobInfo , long duration , TimeUnit unit , SignUrlOption ... options ) {
508- return signUrlOptions (url , blobInfo , duration , unit , options );
509- }
510-
511- private URL signUrlOptions (String url , BlobInfo blobInfo , long duration , TimeUnit unit , SignUrlOption ... options ){
512504 EnumMap <SignUrlOption .Option , Object > optionMap = Maps .newEnumMap (SignUrlOption .Option .class );
513- for (SignUrlOption option : options ) {
514- optionMap .put (option .getOption (), option .getValue ());
515- }
516- ServiceAccountSigner credentials =
517- (ServiceAccountSigner ) optionMap .get (SignUrlOption .Option .SERVICE_ACCOUNT_CRED );
518- if (credentials == null ) {
519- checkState (this .getOptions ().getCredentials () instanceof ServiceAccountSigner ,
520- "Signing key was not provided and could not be derived" );
521- credentials = (ServiceAccountSigner ) this .getOptions ().getCredentials ();
522- }
523-
524- long expiration = TimeUnit .SECONDS .convert (
525- getOptions ().getClock ().millisTime () + unit .toMillis (duration ), TimeUnit .MILLISECONDS );
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+ }
526515
527- StringBuilder stPath = new StringBuilder ();
528- if (!blobInfo .getBucket ().startsWith (PATH_DELIMITER )) {
529- stPath .append (PATH_DELIMITER );
530- }
531- stPath .append (blobInfo .getBucket ());
532- if (!blobInfo .getBucket ().endsWith (PATH_DELIMITER )) {
533- stPath .append (PATH_DELIMITER );
534- }
535- if (blobInfo .getName ().startsWith (PATH_DELIMITER )) {
536- stPath .setLength (stPath .length () - 1 );
537- }
516+ long expiration = TimeUnit .SECONDS .convert (
517+ getOptions ().getClock ().millisTime () + unit .toMillis (duration ), TimeUnit .MILLISECONDS );
538518
539- String escapedName = UrlEscapers .urlFragmentEscaper ().escape (blobInfo .getName ());
540- stPath .append (escapedName .replace ("?" , "%3F" ));
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+ }
541530
542- URI path = URI .create (stPath .toString ());
531+ String escapedName = UrlEscapers .urlFragmentEscaper ().escape (blobInfo .getName ());
532+ stPath .append (escapedName .replace ("?" , "%3F" ));
533+
534+ URI path = URI .create (stPath .toString ());
543535
544- try {
536+ try {
545537 SignatureInfo signatureInfo = buildSignatureInfo (optionMap , blobInfo , expiration , path );
546538 byte [] signatureBytes =
547539 credentials .sign (signatureInfo .constructUnsignedPayload ().getBytes (UTF_8 ));
548- StringBuilder stBuilder = new StringBuilder (url ).append (path );
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+
549548 String signature =
550549 URLEncoder .encode (BaseEncoding .base64 ().encode (signatureBytes ), UTF_8 .name ());
551550 stBuilder .append ("?GoogleAccessId=" ).append (credentials .getAccount ());
@@ -556,7 +555,7 @@ private URL signUrlOptions(String url, BlobInfo blobInfo, long duration, TimeUni
556555
557556 } catch (MalformedURLException | UnsupportedEncodingException ex ) {
558557 throw new IllegalStateException (ex );
559- }
558+ }
560559 }
561560
562561 /**
0 commit comments