| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.openstack.swift.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.MediaType; |
| 27 | |
| 28 | import org.jclouds.blobstore.binders.BindUserMetadataToHeadersWithPrefix; |
| 29 | import org.jclouds.http.HttpRequest; |
| 30 | import org.jclouds.http.utils.ModifyRequest; |
| 31 | import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlob; |
| 32 | import org.jclouds.openstack.swift.domain.SwiftObject; |
| 33 | import org.jclouds.rest.Binder; |
| 34 | |
| 35 | @Singleton |
| 36 | public class BindSwiftObjectMetadataToRequest implements Binder { |
| 37 | |
| 38 | private final BindUserMetadataToHeadersWithPrefix mdBinder; |
| 39 | private final ObjectToBlob object2Blob; |
| 40 | |
| 41 | @Inject |
| 42 | public BindSwiftObjectMetadataToRequest(ObjectToBlob object2Blob, BindUserMetadataToHeadersWithPrefix mdBinder) { |
| 43 | this.mdBinder = mdBinder; |
| 44 | this.object2Blob = object2Blob; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 49 | checkArgument(checkNotNull(input, "input") instanceof SwiftObject, "this binder is only valid for SwiftObject!"); |
| 50 | checkNotNull(request, "request"); |
| 51 | |
| 52 | SwiftObject object = (SwiftObject) input; |
| 53 | if (object.getPayload().getContentMetadata().getContentType() == null) |
| 54 | object.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM); |
| 55 | |
| 56 | if (object.getPayload().getContentMetadata().getContentLength() != null |
| 57 | && object.getPayload().getContentMetadata().getContentLength() >= 0) { |
| 58 | checkArgument(object.getPayload().getContentMetadata().getContentLength() <= 5l * 1024 * 1024 * 1024, |
| 59 | "maximum size for put object is 5GB"); |
| 60 | } else { |
| 61 | // Enable "chunked"/"streamed" data, where the size needn't be known in advance. |
| 62 | request = ModifyRequest.replaceHeader(request, "Transfer-Encoding", "chunked"); |
| 63 | } |
| 64 | request = mdBinder.bindToRequest(request, object2Blob.apply(object)); |
| 65 | return request; |
| 66 | } |
| 67 | } |