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