|
| 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 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import java.net.URI; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +/** |
| 24 | + * Signature Info holds payload components of the string that requires signing. |
| 25 | + * |
| 26 | + * @see <a href= |
| 27 | + * "https://cloud.google.com/storage/docs/access-control/signed-urls#string-components"> |
| 28 | + * Components</a> |
| 29 | + */ |
| 30 | +public class SignatureInfo { |
| 31 | + |
| 32 | + public static final char COMPONENT_SEPARATOR = '\n'; |
| 33 | + |
| 34 | + private final HttpMethod httpVerb; |
| 35 | + private final String contentMd5; |
| 36 | + private final String contentType; |
| 37 | + private final long expiration; |
| 38 | + private final Map<String, String> canonicalizedExtensionHeaders; |
| 39 | + private final URI canonicalizedResource; |
| 40 | + |
| 41 | + private SignatureInfo(Builder builder) { |
| 42 | + this.httpVerb = builder.httpVerb; |
| 43 | + this.contentMd5 = builder.contentMd5; |
| 44 | + this.contentType = builder.contentType; |
| 45 | + this.expiration = builder.expiration; |
| 46 | + this.canonicalizedExtensionHeaders = builder.canonicalizedExtensionHeaders; |
| 47 | + this.canonicalizedResource = builder.canonicalizedResource; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructs payload to be signed. |
| 52 | + * |
| 53 | + * @return paylod to sign |
| 54 | + * @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed URLs</a> |
| 55 | + */ |
| 56 | + public String constructUnsignedPayload() { |
| 57 | + StringBuilder payload = new StringBuilder(); |
| 58 | + |
| 59 | + payload.append(httpVerb.name()).append(COMPONENT_SEPARATOR); |
| 60 | + if (contentMd5 != null) { |
| 61 | + payload.append(contentMd5); |
| 62 | + } |
| 63 | + payload.append(COMPONENT_SEPARATOR); |
| 64 | + |
| 65 | + if (contentType != null) { |
| 66 | + payload.append(contentType); |
| 67 | + } |
| 68 | + payload.append(COMPONENT_SEPARATOR); |
| 69 | + |
| 70 | + payload.append(expiration).append(COMPONENT_SEPARATOR); |
| 71 | + |
| 72 | + if (canonicalizedExtensionHeaders != null) { |
| 73 | + payload.append(new CanonicalExtensionHeadersSerializer() |
| 74 | + .serialize(canonicalizedExtensionHeaders)); |
| 75 | + } |
| 76 | + |
| 77 | + payload.append(canonicalizedResource); |
| 78 | + |
| 79 | + return payload.toString(); |
| 80 | + } |
| 81 | + |
| 82 | + public HttpMethod getHttpVerb() { |
| 83 | + return httpVerb; |
| 84 | + } |
| 85 | + |
| 86 | + public String getContentMd5() { |
| 87 | + return contentMd5; |
| 88 | + } |
| 89 | + |
| 90 | + public String getContentType() { |
| 91 | + return contentType; |
| 92 | + } |
| 93 | + |
| 94 | + public long getExpiration() { |
| 95 | + return expiration; |
| 96 | + } |
| 97 | + |
| 98 | + public Map<String, String> getCanonicalizedExtensionHeaders() { |
| 99 | + return canonicalizedExtensionHeaders; |
| 100 | + } |
| 101 | + |
| 102 | + public URI getCanonicalizedResource() { |
| 103 | + return canonicalizedResource; |
| 104 | + } |
| 105 | + |
| 106 | + public static final class Builder { |
| 107 | + |
| 108 | + private final HttpMethod httpVerb; |
| 109 | + private String contentMd5; |
| 110 | + private String contentType; |
| 111 | + private final long expiration; |
| 112 | + private Map<String, String> canonicalizedExtensionHeaders; |
| 113 | + private final URI canonicalizedResource; |
| 114 | + |
| 115 | + /** |
| 116 | + * Constructs builder. |
| 117 | + * |
| 118 | + * @param httpVerb the HTTP method |
| 119 | + * @param expiration the EPOX expiration date |
| 120 | + * @param canonicalizedResource the resource URI |
| 121 | + * @throws IllegalArgumentException if required field is not provided. |
| 122 | + */ |
| 123 | + public Builder(HttpMethod httpVerb, long expiration, URI canonicalizedResource) { |
| 124 | + this.httpVerb = httpVerb; |
| 125 | + this.expiration = expiration; |
| 126 | + this.canonicalizedResource = canonicalizedResource; |
| 127 | + } |
| 128 | + |
| 129 | + public Builder(SignatureInfo signatureInfo) { |
| 130 | + this.httpVerb = signatureInfo.httpVerb; |
| 131 | + this.contentMd5 = signatureInfo.contentMd5; |
| 132 | + this.contentType = signatureInfo.contentType; |
| 133 | + this.expiration = signatureInfo.expiration; |
| 134 | + this.canonicalizedExtensionHeaders = signatureInfo.canonicalizedExtensionHeaders; |
| 135 | + this.canonicalizedResource = signatureInfo.canonicalizedResource; |
| 136 | + } |
| 137 | + |
| 138 | + public Builder setContentMd5(String contentMd5) { |
| 139 | + this.contentMd5 = contentMd5; |
| 140 | + |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public Builder setContentType(String contentType) { |
| 145 | + this.contentType = contentType; |
| 146 | + |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public Builder setCanonicalizedExtensionHeaders( |
| 151 | + Map<String, String> canonicalizedExtensionHeaders) { |
| 152 | + this.canonicalizedExtensionHeaders = canonicalizedExtensionHeaders; |
| 153 | + |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Creates an {@code SignatureInfo} object from this builder. |
| 159 | + */ |
| 160 | + public SignatureInfo build() { |
| 161 | + checkArgument(httpVerb != null, "Required HTTP method"); |
| 162 | + checkArgument(canonicalizedResource != null, "Required canonicalized resource"); |
| 163 | + checkArgument(expiration >= 0, "Expiration must be greater than or equal to zero"); |
| 164 | + |
| 165 | + return new SignatureInfo(this); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments