| 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.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 | import javax.inject.Provider; |
| 31 | import javax.ws.rs.core.UriBuilder; |
| 32 | |
| 33 | import org.jclouds.blobstore.domain.PageSet; |
| 34 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 35 | import org.jclouds.http.HttpRequest; |
| 36 | import org.jclouds.http.functions.ParseJson; |
| 37 | import org.jclouds.json.Json; |
| 38 | import org.jclouds.openstack.swift.domain.ObjectInfo; |
| 39 | import org.jclouds.openstack.swift.domain.internal.ObjectInfoImpl; |
| 40 | import org.jclouds.openstack.swift.options.ListContainerOptions; |
| 41 | import org.jclouds.rest.InvocationContext; |
| 42 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 43 | |
| 44 | import com.google.common.base.Function; |
| 45 | import com.google.common.collect.Iterables; |
| 46 | import com.google.common.collect.Sets; |
| 47 | import com.google.gson.reflect.TypeToken; |
| 48 | import com.google.inject.TypeLiteral; |
| 49 | |
| 50 | /** |
| 51 | * This parses {@link ObjectInfo} from a gson string. |
| 52 | * |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | public 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 | } |