| 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.blobstore; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.blobstore.domain.Blob; |
| 29 | import org.jclouds.blobstore.functions.BlobToHttpGetOptions; |
| 30 | import org.jclouds.blobstore.options.GetOptions; |
| 31 | import org.jclouds.http.HttpRequest; |
| 32 | import org.jclouds.http.HttpUtils; |
| 33 | import org.jclouds.http.filters.BasicAuthentication; |
| 34 | import org.jclouds.location.Provider; |
| 35 | |
| 36 | /** |
| 37 | * |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | @Singleton |
| 41 | public class TransientBlobRequestSigner implements BlobRequestSigner { |
| 42 | |
| 43 | private final BasicAuthentication basicAuth; |
| 44 | private final BlobToHttpGetOptions blob2HttpGetOptions; |
| 45 | private final String endpoint; |
| 46 | |
| 47 | @Inject |
| 48 | public TransientBlobRequestSigner(BasicAuthentication basicAuth, BlobToHttpGetOptions blob2HttpGetOptions, @Provider URI endpoint) { |
| 49 | this.basicAuth = checkNotNull(basicAuth, "basicAuth"); |
| 50 | this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions"); |
| 51 | this.endpoint = endpoint.toString(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public HttpRequest signGetBlob(String container, String name) { |
| 56 | HttpRequest request = new HttpRequest("GET", URI.create(String.format("%s/%s/%s", endpoint, container, name))); |
| 57 | return basicAuth.filter(request); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public HttpRequest signPutBlob(String container, Blob blob) { |
| 62 | HttpRequest request = HttpRequest.builder().method("PUT").endpoint( |
| 63 | URI.create(String.format("%s/%s/%s", endpoint, container, blob.getMetadata().getName()))).payload( |
| 64 | blob.getPayload()).headers( |
| 65 | HttpUtils.getContentHeadersFromMetadata(blob.getMetadata().getContentMetadata())).build(); |
| 66 | return basicAuth.filter(request); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public HttpRequest signRemoveBlob(String container, String name) { |
| 71 | HttpRequest request = new HttpRequest("DELETE", URI.create(String.format("%s/%s/%s", endpoint, container, |
| 72 | name))); |
| 73 | return basicAuth.filter(request); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public HttpRequest signGetBlob(String container, String name, GetOptions options) { |
| 78 | HttpRequest request = HttpRequest.builder().method("GET").endpoint( |
| 79 | URI.create(String.format("%s/%s/%s", endpoint, container, name))).headers( |
| 80 | blob2HttpGetOptions.apply(options).buildRequestHeaders()).build(); |
| 81 | return basicAuth.filter(request); |
| 82 | } |
| 83 | |
| 84 | } |