| 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 javax.inject.Inject; |
| 24 | import javax.inject.Provider; |
| 25 | import javax.ws.rs.core.UriBuilder; |
| 26 | |
| 27 | import org.jclouds.crypto.CryptoStreams; |
| 28 | import org.jclouds.date.DateService; |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.s3.domain.CanonicalUser; |
| 31 | import org.jclouds.s3.domain.ListBucketResponse; |
| 32 | import org.jclouds.s3.domain.ObjectMetadata; |
| 33 | import org.jclouds.s3.domain.ObjectMetadataBuilder; |
| 34 | import org.jclouds.s3.domain.internal.ListBucketResponseImpl; |
| 35 | import org.jclouds.util.Strings2; |
| 36 | import org.xml.sax.Attributes; |
| 37 | |
| 38 | import com.google.common.collect.ImmutableSet; |
| 39 | import com.google.common.collect.ImmutableSet.Builder; |
| 40 | |
| 41 | /** |
| 42 | * Parses the following XML document: |
| 43 | * <p/> |
| 44 | * ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01" |
| 45 | * |
| 46 | * @author Adrian Cole |
| 47 | * @see <a |
| 48 | * href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTBucketGET.html" |
| 49 | * /> |
| 50 | */ |
| 51 | public class ListBucketHandler extends ParseSax.HandlerWithResult<ListBucketResponse> { |
| 52 | private Builder<ObjectMetadata> contents = ImmutableSet.<ObjectMetadata> builder(); |
| 53 | private Builder<String> commonPrefixes = ImmutableSet.<String> builder(); |
| 54 | private CanonicalUser currentOwner; |
| 55 | private StringBuilder currentText = new StringBuilder(); |
| 56 | |
| 57 | private ObjectMetadataBuilder builder = new ObjectMetadataBuilder(); |
| 58 | |
| 59 | private final Provider<UriBuilder> uriBuilders; |
| 60 | private final DateService dateParser; |
| 61 | |
| 62 | private String bucketName; |
| 63 | private String prefix; |
| 64 | private String marker; |
| 65 | private int maxResults; |
| 66 | private String delimiter; |
| 67 | private boolean isTruncated; |
| 68 | |
| 69 | @Inject |
| 70 | public ListBucketHandler(DateService dateParser, Provider<UriBuilder> uriBuilders) { |
| 71 | this.dateParser = dateParser; |
| 72 | this.uriBuilders = uriBuilders; |
| 73 | } |
| 74 | |
| 75 | public ListBucketResponse getResult() { |
| 76 | return new ListBucketResponseImpl(bucketName, contents.build(), prefix, marker, |
| 77 | (isTruncated && nextMarker == null) ? currentKey : nextMarker, maxResults, delimiter, isTruncated, |
| 78 | commonPrefixes.build()); |
| 79 | } |
| 80 | |
| 81 | private boolean inCommonPrefixes; |
| 82 | private String currentKey; |
| 83 | private String nextMarker; |
| 84 | |
| 85 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 86 | if (qName.equals("CommonPrefixes")) { |
| 87 | inCommonPrefixes = true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public void endElement(String uri, String name, String qName) { |
| 92 | if (qName.equals("ID")) { |
| 93 | currentOwner = new CanonicalUser(currentOrNull(currentText)); |
| 94 | } else if (qName.equals("DisplayName")) { |
| 95 | currentOwner.setDisplayName(currentOrNull(currentText)); |
| 96 | } else if (qName.equals("Key")) { // content stuff |
| 97 | currentKey = currentOrNull(currentText); |
| 98 | builder.key(currentKey); |
| 99 | builder.uri(uriBuilders.get().uri(getRequest().getEndpoint()).path(currentKey).replaceQuery("").build()); |
| 100 | } else if (qName.equals("LastModified")) { |
| 101 | builder.lastModified(dateParser.iso8601DateParse(currentOrNull(currentText))); |
| 102 | } else if (qName.equals("ETag")) { |
| 103 | String currentETag = currentOrNull(currentText); |
| 104 | builder.eTag(currentETag); |
| 105 | builder.contentMD5(CryptoStreams.hex(Strings2.replaceAll(currentETag, '"', ""))); |
| 106 | } else if (qName.equals("Size")) { |
| 107 | builder.contentLength(new Long(currentOrNull(currentText))); |
| 108 | } else if (qName.equals("Owner")) { |
| 109 | builder.owner(currentOwner); |
| 110 | currentOwner = null; |
| 111 | } else if (qName.equals("StorageClass")) { |
| 112 | builder.storageClass(ObjectMetadata.StorageClass.valueOf(currentOrNull(currentText))); |
| 113 | } else if (qName.equals("Contents")) { |
| 114 | contents.add(builder.build()); |
| 115 | builder = new ObjectMetadataBuilder().bucket(bucketName); |
| 116 | } else if (qName.equals("Name")) { |
| 117 | this.bucketName = currentOrNull(currentText); |
| 118 | builder.bucket(bucketName); |
| 119 | } else if (qName.equals("Prefix")) { |
| 120 | String prefix = currentOrNull(currentText); |
| 121 | if (inCommonPrefixes) |
| 122 | commonPrefixes.add(prefix); |
| 123 | else |
| 124 | this.prefix = prefix; |
| 125 | } else if (qName.equals("Delimiter")) { |
| 126 | this.delimiter = currentOrNull(currentText); |
| 127 | } else if (qName.equals("Marker")) { |
| 128 | this.marker = currentOrNull(currentText); |
| 129 | } else if (qName.equals("NextMarker")) { |
| 130 | this.nextMarker = currentOrNull(currentText); |
| 131 | } else if (qName.equals("MaxKeys")) { |
| 132 | this.maxResults = Integer.parseInt(currentOrNull(currentText)); |
| 133 | } else if (qName.equals("IsTruncated")) { |
| 134 | this.isTruncated = Boolean.parseBoolean(currentOrNull(currentText)); |
| 135 | } |
| 136 | currentText = new StringBuilder(); |
| 137 | } |
| 138 | |
| 139 | public void characters(char ch[], int start, int length) { |
| 140 | currentText.append(ch, start, length); |
| 141 | } |
| 142 | } |