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

COVERAGE SUMMARY FOR SOURCE FILE [ParseObjectInfoListFromJsonResponse.java]

nameclass, %method, %block, %line, %
ParseObjectInfoListFromJsonResponse.java100% (4/4)100% (11/11)87%  (169/194)90%  (22.5/25)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseObjectInfoListFromJsonResponse100% (1/1)100% (7/7)84%  (127/152)89%  (20.5/23)
apply (InputStream): PageSet 100% (1/1)77%  (84/109)82%  (11.5/14)
ParseObjectInfoListFromJsonResponse (Json, Provider): void 100% (1/1)100% (10/10)100% (3/3)
access$000 (ParseObjectInfoListFromJsonResponse): GeneratedHttpRequest 100% (1/1)100% (3/3)100% (1/1)
access$100 (ParseObjectInfoListFromJsonResponse): Provider 100% (1/1)100% (3/3)100% (1/1)
access$200 (ParseObjectInfoListFromJsonResponse): String 100% (1/1)100% (3/3)100% (1/1)
setContainer (String): ParseObjectInfoListFromJsonResponse 100% (1/1)100% (5/5)100% (2/2)
setContext (HttpRequest): ParseObjectInfoListFromJsonResponse 100% (1/1)100% (19/19)100% (3/3)
     
class ParseObjectInfoListFromJsonResponse$1100% (1/1)100% (1/1)100% (3/3)100% (1/1)
ParseObjectInfoListFromJsonResponse$1 (): void 100% (1/1)100% (3/3)100% (1/1)
     
class ParseObjectInfoListFromJsonResponse$2100% (1/1)100% (1/1)100% (6/6)100% (1/1)
ParseObjectInfoListFromJsonResponse$2 (ParseObjectInfoListFromJsonResponse): ... 100% (1/1)100% (6/6)100% (1/1)
     
class ParseObjectInfoListFromJsonResponse$3100% (1/1)100% (2/2)100% (33/33)100% (2/2)
ParseObjectInfoListFromJsonResponse$3 (ParseObjectInfoListFromJsonResponse): ... 100% (1/1)100% (6/6)100% (1/1)
apply (ObjectInfoImpl): ObjectInfo 100% (1/1)100% (27/27)100% (1/1)

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.openstack.swift.functions;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkState;
23 
24import java.io.IOException;
25import java.io.InputStream;
26import java.lang.reflect.Type;
27import java.util.SortedSet;
28 
29import javax.inject.Inject;
30import javax.inject.Provider;
31import javax.ws.rs.core.UriBuilder;
32 
33import org.jclouds.blobstore.domain.PageSet;
34import org.jclouds.blobstore.domain.internal.PageSetImpl;
35import org.jclouds.http.HttpRequest;
36import org.jclouds.http.functions.ParseJson;
37import org.jclouds.json.Json;
38import org.jclouds.openstack.swift.domain.ObjectInfo;
39import org.jclouds.openstack.swift.domain.internal.ObjectInfoImpl;
40import org.jclouds.openstack.swift.options.ListContainerOptions;
41import org.jclouds.rest.InvocationContext;
42import org.jclouds.rest.internal.GeneratedHttpRequest;
43 
44import com.google.common.base.Function;
45import com.google.common.collect.Iterables;
46import com.google.common.collect.Sets;
47import com.google.gson.reflect.TypeToken;
48import com.google.inject.TypeLiteral;
49 
50/**
51 * This parses {@link ObjectInfo} from a gson string.
52 * 
53 * @author Adrian Cole
54 */
55public class ParseObjectInfoListFromJsonResponse extends ParseJson<PageSet<ObjectInfo>> implements
56         InvocationContext<ParseObjectInfoListFromJsonResponse> {
57   private final Provider<UriBuilder> uriBuilders;
58 
59   private GeneratedHttpRequest<?> request;
60   private String container;
61 
62   @Inject
63   public ParseObjectInfoListFromJsonResponse(Json json, Provider<UriBuilder> uriBuilders) {
64      super(json, new TypeLiteral<PageSet<ObjectInfo>>() {
65      });
66      this.uriBuilders = uriBuilders;
67   }
68 
69   public PageSet<ObjectInfo> apply(InputStream stream) {
70      checkState(request != null, "request should be initialized at this point");
71      checkState(request.getArgs() != null, "request.getArgs() should be initialized at this point");
72      checkArgument(request.getArgs().get(0) instanceof String, "arg[0] must be a container name");
73      checkArgument(request.getArgs().get(1) instanceof ListContainerOptions[],
74               "arg[1] must be an array of ListContainerOptions");
75      ListContainerOptions[] optionsList = (ListContainerOptions[]) request.getArgs().get(1);
76      ListContainerOptions options = optionsList.length > 0 ? optionsList[0] : ListContainerOptions.NONE;
77      Type listType = new TypeToken<SortedSet<ObjectInfoImpl>>() {
78      }.getType();
79 
80      try {
81         SortedSet<ObjectInfoImpl> list = apply(stream, listType);
82         SortedSet<ObjectInfo> returnVal = Sets.newTreeSet(Iterables.transform(list,
83                  new Function<ObjectInfoImpl, ObjectInfo>() {
84                     public ObjectInfo apply(ObjectInfoImpl from) {
85                        return from.toBuilder().container(container).uri(
86                                 uriBuilders.get().uri(request.getEndpoint()).path(from.getName()).replaceQuery("")
87                                          .build()).build();
88                     }
89                  }));
90         boolean truncated = options.getMaxResults() == returnVal.size();
91         String marker = truncated ? returnVal.last().getName() : null;
92         return new PageSetImpl<ObjectInfo>(returnVal, marker);
93      } catch (IOException e) {
94         throw new RuntimeException("problem reading response from request: " + request, e);
95      }
96   }
97 
98   @Override
99   public ParseObjectInfoListFromJsonResponse setContext(HttpRequest request) {
100      checkArgument(request instanceof GeneratedHttpRequest<?>, "note this handler requires a GeneratedHttpRequest");
101      this.request = (GeneratedHttpRequest<?>) request;
102      return setContainer(GeneratedHttpRequest.class.cast(request).getArgs().get(0).toString());
103   }
104 
105   private ParseObjectInfoListFromJsonResponse setContainer(String container) {
106      this.container = container;
107      return this;
108   }
109}

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