| 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.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | |
| 23 | import java.util.Map.Entry; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Named; |
| 27 | import javax.ws.rs.core.HttpHeaders; |
| 28 | |
| 29 | import org.jclouds.azureblob.domain.ContainerProperties; |
| 30 | import org.jclouds.azureblob.domain.MutableContainerPropertiesWithMetadata; |
| 31 | import org.jclouds.azureblob.domain.internal.MutableContainerPropertiesWithMetadataImpl; |
| 32 | import org.jclouds.blobstore.reference.BlobStoreConstants; |
| 33 | import org.jclouds.date.DateService; |
| 34 | import org.jclouds.http.HttpException; |
| 35 | import org.jclouds.http.HttpRequest; |
| 36 | import org.jclouds.http.HttpResponse; |
| 37 | import org.jclouds.rest.InvocationContext; |
| 38 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 39 | |
| 40 | import com.google.common.annotations.VisibleForTesting; |
| 41 | import com.google.common.base.Function; |
| 42 | |
| 43 | /** |
| 44 | * This parses @{link {@link org.jclouds.azureblob.domain.ListableContainerProperties} from |
| 45 | * HTTP headers. |
| 46 | * |
| 47 | * |
| 48 | * @author Adrian Cole |
| 49 | */ |
| 50 | public class ParseContainerPropertiesFromHeaders implements Function<HttpResponse, ContainerProperties>, |
| 51 | InvocationContext<ParseContainerPropertiesFromHeaders> { |
| 52 | |
| 53 | private final DateService dateParser; |
| 54 | private final String metadataPrefix; |
| 55 | private GeneratedHttpRequest<?> request; |
| 56 | |
| 57 | @Inject |
| 58 | public ParseContainerPropertiesFromHeaders(DateService dateParser, |
| 59 | @Named(BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX) String metadataPrefix) { |
| 60 | this.dateParser = dateParser; |
| 61 | this.metadataPrefix = metadataPrefix; |
| 62 | } |
| 63 | |
| 64 | public ContainerProperties apply(HttpResponse from) { |
| 65 | MutableContainerPropertiesWithMetadata to = new MutableContainerPropertiesWithMetadataImpl(); |
| 66 | to.setName(request.getArgs().get(0).toString()); |
| 67 | addUserMetadataTo(from, to); |
| 68 | parseLastModifiedOrThrowException(from, to); |
| 69 | addETagTo(from, to); |
| 70 | to.setUrl(request.getEndpoint()); |
| 71 | return to; |
| 72 | } |
| 73 | |
| 74 | @VisibleForTesting |
| 75 | void addUserMetadataTo(HttpResponse from, MutableContainerPropertiesWithMetadata metadata) { |
| 76 | for (Entry<String, String> header : from.getHeaders().entries()) { |
| 77 | if (header.getKey() != null && header.getKey().startsWith(metadataPrefix)) |
| 78 | metadata.getMetadata().put((header.getKey().substring(metadataPrefix.length())).toLowerCase(), |
| 79 | header.getValue()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @VisibleForTesting |
| 84 | void parseLastModifiedOrThrowException(HttpResponse from, MutableContainerPropertiesWithMetadata metadata) |
| 85 | throws HttpException { |
| 86 | String lastModified = from.getFirstHeaderOrNull(HttpHeaders.LAST_MODIFIED); |
| 87 | if (lastModified == null) |
| 88 | throw new HttpException(HttpHeaders.LAST_MODIFIED + " header not present in response: " + from); |
| 89 | metadata.setLastModified(dateParser.rfc822DateParse(lastModified)); |
| 90 | if (metadata.getLastModified() == null) |
| 91 | throw new HttpException("could not parse: " + HttpHeaders.LAST_MODIFIED + ": " + lastModified); |
| 92 | } |
| 93 | |
| 94 | @VisibleForTesting |
| 95 | protected void addETagTo(HttpResponse from, MutableContainerPropertiesWithMetadata metadata) { |
| 96 | String eTag = from.getFirstHeaderOrNull(HttpHeaders.ETAG); |
| 97 | if (metadata.getETag() == null && eTag != null) { |
| 98 | metadata.setETag(eTag); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public ParseContainerPropertiesFromHeaders setContext(HttpRequest request) { |
| 104 | checkArgument(request instanceof GeneratedHttpRequest<?>, "note this handler requires a GeneratedHttpRequest"); |
| 105 | this.request = (GeneratedHttpRequest<?>) request; |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | } |