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 /**
20 *
21 * Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
22 *
23 * ====================================================================
24 * Licensed under the Apache License, Version 2.0 (the "License");
25 * you may not use this file except in compliance with the License.
26 * You may obtain a copy of the License at
27 *
28 * http://www.apache.org/licenses/LICENSE-2.0
29 *
30 * Unless required by applicable law or agreed to in writing, software
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
35 * ====================================================================
36 */
37
38 package org.jclouds.blobstore.functions;
39
40 import static com.google.common.base.Preconditions.checkNotNull;
41
42 import java.util.Date;
43
44 import javax.inject.Inject;
45 import javax.inject.Singleton;
46
47 import org.jclouds.blobstore.options.GetOptions;
48 import org.jclouds.date.DateService;
49 import org.jclouds.util.Strings2;
50
51 import com.google.common.base.Function;
52
53 /**
54 *
55 * @author Adrian Cole
56 */
57 @Singleton
58 public class HttpGetOptionsListToGetOptions implements
59 Function<org.jclouds.http.options.GetOptions[], org.jclouds.blobstore.options.GetOptions> {
60 private final DateService dateService;
61
62 @Inject
63 HttpGetOptionsListToGetOptions(DateService dateService) {
64 this.dateService = checkNotNull(dateService, "dateService");
65 }
66
67 public GetOptions apply(org.jclouds.http.options.GetOptions[] from) {
68 checkNotNull(from, "options");
69
70 org.jclouds.blobstore.options.GetOptions to = new org.jclouds.blobstore.options.GetOptions();
71 if (from.length != 0) {
72 if (from[0].getIfMatch() != null) {
73 to.ifETagMatches(Strings2.replaceAll(from[0].getIfMatch(), '"', ""));
74 }
75 if (from[0].getIfModifiedSince() != null) {
76 Date time = dateService.rfc822DateParse(from[0].getIfModifiedSince());
77 to.ifModifiedSince(time);
78 }
79 if (from[0].getIfNoneMatch() != null) {
80 to.ifETagDoesntMatch(Strings2.replaceAll(from[0].getIfNoneMatch(), '"', ""));
81 }
82 if (from[0].getIfUnmodifiedSince() != null) {
83 Date time = dateService.rfc822DateParse(from[0].getIfUnmodifiedSince());
84 to.ifUnmodifiedSince(time);
85 }
86 for (String range : from[0].getRanges()) {
87 String[] firstLast = range.split("\\-");
88 to.range(Long.parseLong(firstLast[0]), Long.parseLong(firstLast[1]));
89 }
90 }
91 return to;
92 }
93
94 }