| 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.functions; |
| 20 | |
| 21 | import static org.jclouds.http.HttpUtils.attemptToParseSizeAndRangeFromHeaders; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 26 | import org.jclouds.blobstore.functions.ParseSystemAndUserMetadataFromHeaders; |
| 27 | import org.jclouds.crypto.CryptoStreams; |
| 28 | import org.jclouds.http.HttpRequest; |
| 29 | import org.jclouds.http.HttpResponse; |
| 30 | import org.jclouds.openstack.swift.blobstore.functions.ResourceToObjectInfo; |
| 31 | import org.jclouds.openstack.swift.domain.MutableObjectInfoWithMetadata; |
| 32 | import org.jclouds.rest.InvocationContext; |
| 33 | |
| 34 | import com.google.common.base.Function; |
| 35 | |
| 36 | /** |
| 37 | * This parses @{link {@link MutableObjectInfoWithMetadata} from HTTP headers. |
| 38 | * |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | public class ParseObjectInfoFromHeaders implements Function<HttpResponse, MutableObjectInfoWithMetadata>, |
| 42 | InvocationContext<ParseObjectInfoFromHeaders> { |
| 43 | private final ParseSystemAndUserMetadataFromHeaders blobMetadataParser; |
| 44 | private final ResourceToObjectInfo blobToObjectInfo; |
| 45 | |
| 46 | @Inject |
| 47 | public ParseObjectInfoFromHeaders(ParseSystemAndUserMetadataFromHeaders blobMetadataParser, |
| 48 | ResourceToObjectInfo blobToObjectInfo) { |
| 49 | this.blobMetadataParser = blobMetadataParser; |
| 50 | this.blobToObjectInfo = blobToObjectInfo; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * parses the http response headers to create a new {@link MutableObjectInfoWithMetadata} object. |
| 55 | */ |
| 56 | public MutableObjectInfoWithMetadata apply(HttpResponse from) { |
| 57 | BlobMetadata base = blobMetadataParser.apply(from); |
| 58 | MutableObjectInfoWithMetadata to = blobToObjectInfo.apply(base); |
| 59 | to.setBytes(attemptToParseSizeAndRangeFromHeaders(from)); |
| 60 | String eTagHeader = from.getFirstHeaderOrNull("Etag"); |
| 61 | if (eTagHeader != null) { |
| 62 | to.setHash(CryptoStreams.hex(eTagHeader)); |
| 63 | } |
| 64 | return to; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public ParseObjectInfoFromHeaders setContext(HttpRequest request) { |
| 69 | blobMetadataParser.setContext(request); |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | } |