1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.http.options;
20
21 import static com.google.common.base.Preconditions.checkArgument;
22 import static com.google.common.base.Preconditions.checkNotNull;
23
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.List;
28
29 import javax.ws.rs.core.HttpHeaders;
30
31 import org.jclouds.date.DateService;
32 import org.jclouds.date.internal.SimpleDateFormatDateService;
33
34 import com.google.common.base.Joiner;
35 import com.google.common.collect.Multimap;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class GetOptions extends BaseHttpRequestOptions {
55 private final static DateService dateService = new SimpleDateFormatDateService();
56 public static final GetOptions NONE = new GetOptions();
57 private final List<String> ranges = new ArrayList<String>();
58
59 @Override
60 public Multimap<String, String> buildRequestHeaders() {
61 Multimap<String, String> headers = super.buildRequestHeaders();
62 String range = getRange();
63 if (range != null)
64 headers.put("Range", this.getRange());
65 return headers;
66 }
67
68
69
70
71 public GetOptions range(long start, long end) {
72 checkArgument(start >= 0, "start must be >= 0");
73 checkArgument(end >= 0, "end must be >= 0");
74 ranges.add(String.format("%d-%d", start, end));
75 return this;
76 }
77
78
79
80
81 public GetOptions startAt(long start) {
82 checkArgument(start >= 0, "start must be >= 0");
83 ranges.add(String.format("%d-", start));
84 return this;
85 }
86
87
88
89
90 public GetOptions tail(long count) {
91 checkArgument(count > 0, "count must be > 0");
92 ranges.add(String.format("-%d", count));
93 return this;
94 }
95
96
97
98
99
100
101
102 public String getRange() {
103 return (ranges.size() > 0) ? String.format("bytes=%s", Joiner.on(",").join(ranges)) : null;
104 }
105
106
107
108
109
110
111 public GetOptions ifModifiedSince(Date ifModifiedSince) {
112 checkArgument(getIfMatch() == null, "ifETagMatches() is not compatible with ifModifiedSince()");
113 checkArgument(getIfUnmodifiedSince() == null, "ifUnmodifiedSince() is not compatible with ifModifiedSince()");
114 this.headers.put(HttpHeaders.IF_MODIFIED_SINCE,
115 dateService.rfc822DateFormat(checkNotNull(ifModifiedSince, "ifModifiedSince")));
116 return this;
117 }
118
119
120
121
122
123
124
125
126
127 public String getIfModifiedSince() {
128 return this.getFirstHeaderOrNull(HttpHeaders.IF_MODIFIED_SINCE);
129 }
130
131
132
133
134
135
136 public GetOptions ifUnmodifiedSince(Date ifUnmodifiedSince) {
137 checkArgument(getIfNoneMatch() == null, "ifETagDoesntMatch() is not compatible with ifUnmodifiedSince()");
138 checkArgument(getIfModifiedSince() == null, "ifModifiedSince() is not compatible with ifUnmodifiedSince()");
139 this.headers.put(HttpHeaders.IF_UNMODIFIED_SINCE,
140 dateService.rfc822DateFormat(checkNotNull(ifUnmodifiedSince, "ifUnmodifiedSince")));
141 return this;
142 }
143
144
145
146
147
148
149
150
151
152 public String getIfUnmodifiedSince() {
153 return this.getFirstHeaderOrNull(HttpHeaders.IF_UNMODIFIED_SINCE);
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167 public GetOptions ifETagMatches(String eTag) {
168 checkArgument(getIfNoneMatch() == null, "ifETagDoesntMatch() is not compatible with ifETagMatches()");
169 checkArgument(getIfModifiedSince() == null, "ifModifiedSince() is not compatible with ifETagMatches()");
170 this.headers.put(HttpHeaders.IF_MATCH, String.format("\"%1$s\"", checkNotNull(eTag, "eTag")));
171 return this;
172 }
173
174
175
176
177
178
179
180
181
182 public String getIfMatch() {
183 return this.getFirstHeaderOrNull(HttpHeaders.IF_MATCH);
184 }
185
186
187
188
189
190
191
192
193
194
195
196 public GetOptions ifETagDoesntMatch(String eTag) {
197 checkArgument(getIfMatch() == null, "ifETagMatches() is not compatible with ifETagDoesntMatch()");
198 checkArgument(getIfUnmodifiedSince() == null, "ifUnmodifiedSince() is not compatible with ifETagDoesntMatch()");
199 this.headers.put(HttpHeaders.IF_NONE_MATCH, String.format("\"%1$s\"", checkNotNull(eTag, "ifETagDoesntMatch")));
200 return this;
201 }
202
203
204
205
206
207
208
209
210
211 public String getIfNoneMatch() {
212 return this.getFirstHeaderOrNull(HttpHeaders.IF_NONE_MATCH);
213 }
214
215 public List<String> getRanges() {
216 return ranges;
217 }
218
219 public static class Builder {
220
221
222
223
224 public static GetOptions range(long start, long end) {
225 GetOptions options = new GetOptions();
226 return options.range(start, end);
227 }
228
229
230
231
232 public static GetOptions startAt(long start) {
233 GetOptions options = new GetOptions();
234 return options.startAt(start);
235 }
236
237
238
239
240 public static GetOptions tail(long count) {
241 GetOptions options = new GetOptions();
242 return options.tail(count);
243 }
244
245
246
247
248 public static GetOptions ifModifiedSince(Date ifModifiedSince) {
249 GetOptions options = new GetOptions();
250 return options.ifModifiedSince(ifModifiedSince);
251 }
252
253
254
255
256 public static GetOptions ifUnmodifiedSince(Date ifUnmodifiedSince) {
257 GetOptions options = new GetOptions();
258 return options.ifUnmodifiedSince(ifUnmodifiedSince);
259 }
260
261
262
263
264 public static GetOptions ifETagMatches(String eTag) {
265 GetOptions options = new GetOptions();
266 return options.ifETagMatches(eTag);
267 }
268
269
270
271
272 public static GetOptions ifETagDoesntMatch(String eTag) {
273 GetOptions options = new GetOptions();
274 return options.ifETagDoesntMatch(eTag);
275 }
276
277 }
278
279 @Override
280 public int hashCode() {
281 final int prime = 31;
282 int result = super.hashCode();
283 result = prime * result + ((ranges == null) ? 0 : ranges.hashCode());
284 return result;
285 }
286
287 @Override
288 public boolean equals(Object obj) {
289 if (this == obj)
290 return true;
291 if (!super.equals(obj))
292 return false;
293 if (getClass() != obj.getClass())
294 return false;
295 GetOptions other = (GetOptions) obj;
296 if (ranges == null) {
297 if (other.ranges != null)
298 return false;
299 } else if (!ranges.equals(other.ranges))
300 return false;
301 return true;
302 }
303
304 @Override
305 public String toString() {
306 return "[matrixParameters=" + matrixParameters + ", formParameters=" + formParameters + ", queryParameters="
307 + queryParameters + ", headers=" + headers + ", payload=" + payload + ", pathSuffix=" + pathSuffix
308 + ", ranges=" + ranges + "]";
309 }
310
311 }