EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.azureblob.functions]

COVERAGE SUMMARY FOR SOURCE FILE [ParseContainerPropertiesFromHeaders.java]

nameclass, %method, %block, %line, %
ParseContainerPropertiesFromHeaders.java100% (1/1)33%  (2/6)13%  (19/141)24%  (7/29)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseContainerPropertiesFromHeaders100% (1/1)33%  (2/6)13%  (19/141)24%  (7/29)
addETagTo (HttpResponse, MutableContainerPropertiesWithMetadata): void 0%   (0/1)0%   (0/13)0%   (0/4)
addUserMetadataTo (HttpResponse, MutableContainerPropertiesWithMetadata): void 0%   (0/1)0%   (0/38)0%   (0/4)
apply (HttpResponse): ContainerProperties 0%   (0/1)0%   (0/31)0%   (0/7)
parseLastModifiedOrThrowException (HttpResponse, MutableContainerPropertiesWi... 0%   (0/1)0%   (0/40)0%   (0/7)
ParseContainerPropertiesFromHeaders (DateService, String): void 100% (1/1)100% (9/9)100% (4/4)
setContext (HttpRequest): ParseContainerPropertiesFromHeaders 100% (1/1)100% (10/10)100% (3/3)

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 */
19package org.jclouds.azureblob.functions;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22 
23import java.util.Map.Entry;
24 
25import javax.inject.Inject;
26import javax.inject.Named;
27import javax.ws.rs.core.HttpHeaders;
28 
29import org.jclouds.azureblob.domain.ContainerProperties;
30import org.jclouds.azureblob.domain.MutableContainerPropertiesWithMetadata;
31import org.jclouds.azureblob.domain.internal.MutableContainerPropertiesWithMetadataImpl;
32import org.jclouds.blobstore.reference.BlobStoreConstants;
33import org.jclouds.date.DateService;
34import org.jclouds.http.HttpException;
35import org.jclouds.http.HttpRequest;
36import org.jclouds.http.HttpResponse;
37import org.jclouds.rest.InvocationContext;
38import org.jclouds.rest.internal.GeneratedHttpRequest;
39 
40import com.google.common.annotations.VisibleForTesting;
41import 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 */
50public 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}

[all classes][org.jclouds.azureblob.functions]
EMMA 2.0.5312 (C) Vladimir Roubtsov