| 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.blobstore.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | /** |
| 25 | * Contains options supported in the list container operation. <h2> |
| 26 | * Usage</h2> The recommended way to instantiate a ListOptions object is to statically import |
| 27 | * ListContainerOptions.* and invoke a static creation method followed by an instance mutator (if |
| 28 | * needed): |
| 29 | * <p/> |
| 30 | * <code> |
| 31 | * import static org.jclouds.blobstore.options.ListContainerOptions.Builder.* |
| 32 | * <p/> |
| 33 | * BlobStore connection = // get connection |
| 34 | * Future<ListResponse<ResourceMetadata>> list = connection.list("container",inDirectory("home/users").maxResults(1000)); |
| 35 | * <code> |
| 36 | * |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | public class ListContainerOptions extends ListOptions implements Cloneable { |
| 40 | |
| 41 | public static final ImmutableListContainerOptions NONE = new ImmutableListContainerOptions( |
| 42 | new ListContainerOptions()); |
| 43 | |
| 44 | private String dir; |
| 45 | private boolean recursive; |
| 46 | private boolean detailed; |
| 47 | |
| 48 | public ListContainerOptions() { |
| 49 | } |
| 50 | |
| 51 | ListContainerOptions(Integer maxKeys, String marker, String dir, boolean recursive, |
| 52 | boolean detailed) { |
| 53 | super(maxKeys, marker); |
| 54 | this.dir = dir; |
| 55 | this.recursive = recursive; |
| 56 | this.detailed = detailed; |
| 57 | } |
| 58 | |
| 59 | public static class ImmutableListContainerOptions extends ListContainerOptions { |
| 60 | private final ListContainerOptions delegate; |
| 61 | |
| 62 | @Override |
| 63 | public ListContainerOptions afterMarker(String marker) { |
| 64 | throw new UnsupportedOperationException(); |
| 65 | } |
| 66 | |
| 67 | public ImmutableListContainerOptions(ListContainerOptions delegate) { |
| 68 | this.delegate = delegate; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public String getDir() { |
| 73 | return delegate.getDir(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public ListContainerOptions inDirectory(String dir) { |
| 78 | throw new UnsupportedOperationException(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean isDetailed() { |
| 83 | return delegate.isDetailed(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean isRecursive() { |
| 88 | return delegate.isRecursive(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public ListContainerOptions maxResults(int maxKeys) { |
| 93 | throw new UnsupportedOperationException(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public ListContainerOptions recursive() { |
| 98 | throw new UnsupportedOperationException(); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String getMarker() { |
| 104 | return delegate.getMarker(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Integer getMaxResults() { |
| 109 | return delegate.getMaxResults(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public ListContainerOptions clone() { |
| 114 | return delegate.clone(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String toString() { |
| 119 | return delegate.toString(); |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | public String getDir() { |
| 125 | return dir; |
| 126 | } |
| 127 | |
| 128 | public boolean isRecursive() { |
| 129 | return recursive; |
| 130 | } |
| 131 | |
| 132 | public boolean isDetailed() { |
| 133 | return detailed; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * This will list the contents of a virtual or real directory path. |
| 138 | * |
| 139 | */ |
| 140 | public ListContainerOptions inDirectory(String dir) { |
| 141 | this.dir = checkNotNull(dir, "dir"); |
| 142 | checkArgument(!dir.equals("/"), "dir must not be a slash"); |
| 143 | return this; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * {@inheritDoc} |
| 148 | */ |
| 149 | public ListContainerOptions afterMarker(String marker) { |
| 150 | return (ListContainerOptions) super.afterMarker(marker); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * {@inheritDoc} |
| 155 | */ |
| 156 | public ListContainerOptions maxResults(int maxKeys) { |
| 157 | return (ListContainerOptions) super.maxResults(maxKeys); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * return a listing of all objects inside the store, recursively. |
| 162 | */ |
| 163 | public ListContainerOptions recursive() { |
| 164 | // checkArgument(path == null, "path and recursive combination currently not supported"); |
| 165 | this.recursive = true; |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * populate each result with detailed such as metadata even if it incurs extra requests to the |
| 171 | * service. |
| 172 | */ |
| 173 | public ListContainerOptions withDetails() { |
| 174 | this.detailed = true; |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | public static class Builder { |
| 179 | |
| 180 | /** |
| 181 | * @see ListContainerOptions#inDirectory(String) |
| 182 | */ |
| 183 | public static ListContainerOptions inDirectory(String directory) { |
| 184 | ListContainerOptions options = new ListContainerOptions(); |
| 185 | return options.inDirectory(directory); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @see ListContainerOptions#afterMarker(String) |
| 190 | */ |
| 191 | public static ListContainerOptions afterMarker(String marker) { |
| 192 | ListContainerOptions options = new ListContainerOptions(); |
| 193 | return options.afterMarker(marker); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @see ListContainerOptions#maxResults(int) |
| 198 | */ |
| 199 | public static ListContainerOptions maxResults(int maxKeys) { |
| 200 | ListContainerOptions options = new ListContainerOptions(); |
| 201 | return options.maxResults(maxKeys); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @see ListContainerOptions#recursive() |
| 206 | */ |
| 207 | public static ListContainerOptions recursive() { |
| 208 | ListContainerOptions options = new ListContainerOptions(); |
| 209 | return options.recursive(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @see ListContainerOptions#withDetails() |
| 214 | */ |
| 215 | public static ListContainerOptions withDetails() { |
| 216 | ListContainerOptions options = new ListContainerOptions(); |
| 217 | return options.withDetails(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public ListContainerOptions clone() { |
| 223 | return new ListContainerOptions(getMaxResults(), getMarker(), dir, recursive, detailed); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public String toString() { |
| 228 | return "[dir=" + dir + ", recursive=" + recursive + ", detailed=" + detailed |
| 229 | + ", maxResults=" + getMaxResults() + "]"; |
| 230 | } |
| 231 | } |