|
33 | 33 | import static com.google.common.base.Preconditions.checkState; |
34 | 34 | import static java.nio.charset.StandardCharsets.UTF_8; |
35 | 35 |
|
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 | | - |
52 | 36 | import com.google.api.gax.paging.Page; |
53 | 37 | import com.google.api.services.storage.model.BucketAccessControl; |
54 | 38 | import com.google.api.services.storage.model.ObjectAccessControl; |
|
78 | 62 | import com.google.common.io.BaseEncoding; |
79 | 63 | import com.google.common.net.UrlEscapers; |
80 | 64 | 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; |
81 | 80 |
|
82 | 81 | final class StorageImpl extends BaseService<StorageOptions> implements Storage { |
83 | 82 |
|
@@ -501,61 +500,61 @@ private BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) |
501 | 500 |
|
502 | 501 | @Override |
503 | 502 | 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 | + } |
559 | 558 | } |
560 | 559 |
|
561 | 560 | /** |
|
0 commit comments