| 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.s3.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 22 | |
| 23 | import java.util.Date; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | |
| 28 | import org.jclouds.s3.domain.BucketMetadata; |
| 29 | import org.jclouds.s3.domain.CanonicalUser; |
| 30 | import org.jclouds.date.DateService; |
| 31 | import org.jclouds.http.functions.ParseSax; |
| 32 | |
| 33 | import com.google.common.collect.Sets; |
| 34 | |
| 35 | /** |
| 36 | * Parses the following XML document: |
| 37 | * <p/> |
| 38 | * SetAllMyBucketsResult xmlns="http://doc.s3.amazonaws.com/2006-03-01" |
| 39 | * |
| 40 | * @see <a |
| 41 | * href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTServiceGET.html" |
| 42 | * /> |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | public class ListAllMyBucketsHandler extends ParseSax.HandlerWithResult<Set<BucketMetadata>> { |
| 46 | |
| 47 | private Set<BucketMetadata> buckets = Sets.newLinkedHashSet(); |
| 48 | private CanonicalUser currentOwner; |
| 49 | private StringBuilder currentText = new StringBuilder(); |
| 50 | |
| 51 | private final DateService dateParser; |
| 52 | private String currentName; |
| 53 | private Date currentCreationDate; |
| 54 | |
| 55 | @Inject |
| 56 | public ListAllMyBucketsHandler(DateService dateParser) { |
| 57 | this.dateParser = dateParser; |
| 58 | } |
| 59 | |
| 60 | public Set<BucketMetadata> getResult() { |
| 61 | return buckets; |
| 62 | } |
| 63 | |
| 64 | public void endElement(String uri, String name, String qName) { |
| 65 | if (qName.equals("ID")) { // owner stuff |
| 66 | currentOwner = new CanonicalUser(currentOrNull(currentText)); |
| 67 | } else if (qName.equals("DisplayName")) { |
| 68 | currentOwner.setDisplayName(currentOrNull(currentText)); |
| 69 | } else if (qName.equals("Bucket")) { |
| 70 | buckets.add(new BucketMetadata(currentName, currentCreationDate, currentOwner)); |
| 71 | } else if (qName.equals("Name")) { |
| 72 | currentName = currentOrNull(currentText); |
| 73 | } else if (qName.equals("CreationDate")) { |
| 74 | currentCreationDate = dateParser.iso8601DateParse(currentOrNull(currentText)); |
| 75 | } |
| 76 | currentText = new StringBuilder(); |
| 77 | } |
| 78 | |
| 79 | public void characters(char ch[], int start, int length) { |
| 80 | currentText.append(ch, start, length); |
| 81 | } |
| 82 | } |