| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.azureblob.binders; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Singleton; |
| 26 | import javax.ws.rs.core.HttpHeaders; |
| 27 | |
| 28 | import org.jclouds.azureblob.blobstore.functions.AzureBlobToBlob; |
| 29 | import org.jclouds.azureblob.domain.AzureBlob; |
| 30 | import org.jclouds.blobstore.binders.BindUserMetadataToHeadersWithPrefix; |
| 31 | import org.jclouds.http.HttpRequest; |
| 32 | import org.jclouds.http.utils.ModifyRequest; |
| 33 | import org.jclouds.rest.Binder; |
| 34 | |
| 35 | import com.google.common.collect.ImmutableMap; |
| 36 | import com.google.common.collect.ImmutableMap.Builder; |
| 37 | import com.google.common.collect.Multimaps; |
| 38 | |
| 39 | @Singleton |
| 40 | public class BindAzureBlobMetadataToRequest implements Binder { |
| 41 | |
| 42 | private final AzureBlobToBlob azureBlob2Blob; |
| 43 | private final BindUserMetadataToHeadersWithPrefix blobBinder; |
| 44 | |
| 45 | @Inject |
| 46 | public BindAzureBlobMetadataToRequest(AzureBlobToBlob azureBlob2Blob, BindUserMetadataToHeadersWithPrefix blobBinder) { |
| 47 | this.azureBlob2Blob = checkNotNull(azureBlob2Blob, "azureBlob2Blob"); |
| 48 | this.blobBinder = checkNotNull(blobBinder, "blobBinder"); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 53 | checkArgument(checkNotNull(input, "input") instanceof AzureBlob, "this binder is only valid for AzureBlobs!"); |
| 54 | checkNotNull(request, "request"); |
| 55 | AzureBlob blob = AzureBlob.class.cast(input); |
| 56 | |
| 57 | checkArgument(blob.getPayload().getContentMetadata().getContentLength() != null |
| 58 | && blob.getPayload().getContentMetadata().getContentLength() >= 0, "size must be set"); |
| 59 | |
| 60 | Builder<String, String> headers = ImmutableMap.<String, String> builder(); |
| 61 | |
| 62 | headers.put("x-ms-blob-type", blob.getProperties().getType().toString()); |
| 63 | |
| 64 | switch (blob.getProperties().getType()) { |
| 65 | case PAGE_BLOB: |
| 66 | headers.put(HttpHeaders.CONTENT_LENGTH, "0"); |
| 67 | headers.put("x-ms-blob-content-length", blob.getPayload().getContentMetadata().getContentLength().toString()); |
| 68 | break; |
| 69 | case BLOCK_BLOB: |
| 70 | checkArgument( |
| 71 | checkNotNull(blob.getPayload().getContentMetadata().getContentLength(), "blob.getContentLength()") <= 64l * 1024 * 1024, |
| 72 | "maximum size for put Blob is 64MB"); |
| 73 | break; |
| 74 | } |
| 75 | request = ModifyRequest.putHeaders(request, Multimaps.forMap(headers.build())); |
| 76 | |
| 77 | return blobBinder.bindToRequest(request, azureBlob2Blob.apply(blob)); |
| 78 | } |
| 79 | } |