| 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.atmos.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.http.HttpUtils.attemptToParseSizeAndRangeFromHeaders; |
| 23 | |
| 24 | import java.net.URI; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | |
| 28 | import org.jclouds.atmos.domain.AtmosObject; |
| 29 | import org.jclouds.blobstore.functions.ParseSystemAndUserMetadataFromHeaders; |
| 30 | import org.jclouds.http.HttpRequest; |
| 31 | import org.jclouds.http.HttpResponse; |
| 32 | import org.jclouds.rest.InvocationContext; |
| 33 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | |
| 37 | /** |
| 38 | * Parses response headers and creates a new AtmosObject from them and the HTTP content. |
| 39 | * |
| 40 | * @see ParseMetadataFromHeaders |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | public class ParseObjectFromHeadersAndHttpContent implements Function<HttpResponse, AtmosObject>, |
| 44 | InvocationContext<ParseObjectFromHeadersAndHttpContent> { |
| 45 | |
| 46 | private final ParseSystemMetadataFromHeaders systemMetadataParser; |
| 47 | private final ParseUserMetadataFromHeaders userMetadataParser; |
| 48 | private final AtmosObject.Factory objectProvider; |
| 49 | private URI uri; |
| 50 | private String path; |
| 51 | |
| 52 | @Inject |
| 53 | public ParseObjectFromHeadersAndHttpContent(ParseSystemMetadataFromHeaders systemMetadataParser, |
| 54 | ParseUserMetadataFromHeaders userMetadataParser, AtmosObject.Factory objectProvider) { |
| 55 | this.systemMetadataParser = checkNotNull(systemMetadataParser, "systemMetadataParser"); |
| 56 | this.userMetadataParser = checkNotNull(userMetadataParser, "userMetadataParser"); |
| 57 | this.objectProvider = checkNotNull(objectProvider, "objectProvider"); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * First, calls {@link ParseSystemAndUserMetadataFromHeaders}. |
| 62 | * |
| 63 | * Then, sets the object size based on the Content-Length header and adds the content to the |
| 64 | * {@link AtmosObject} result. |
| 65 | * |
| 66 | * @throws org.jclouds.http.HttpException |
| 67 | */ |
| 68 | public AtmosObject apply(HttpResponse from) { |
| 69 | checkNotNull(from, "http response"); |
| 70 | AtmosObject object = objectProvider.create(systemMetadataParser.apply(from), userMetadataParser.apply(from)); |
| 71 | object.getContentMetadata().setName(object.getSystemMetadata().getObjectName()); |
| 72 | object.getContentMetadata().setPath(path); |
| 73 | object.getContentMetadata().setUri(uri); |
| 74 | object.getAllHeaders().putAll(from.getHeaders()); |
| 75 | object.setPayload(from.getPayload()); |
| 76 | object.getContentMetadata().setContentLength(attemptToParseSizeAndRangeFromHeaders(from)); |
| 77 | return object; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public ParseObjectFromHeadersAndHttpContent setContext(HttpRequest request) { |
| 82 | this.uri = request.getEndpoint(); |
| 83 | return setPath(GeneratedHttpRequest.class.cast(request).getArgs().get(0).toString()); |
| 84 | } |
| 85 | |
| 86 | private ParseObjectFromHeadersAndHttpContent setPath(String path) { |
| 87 | this.path = path; |
| 88 | return this; |
| 89 | } |
| 90 | } |