| 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.net.URI; |
| 24 | |
| 25 | import org.jclouds.http.functions.ParseSax; |
| 26 | import org.jclouds.s3.domain.AccessControlList; |
| 27 | import org.jclouds.s3.domain.CanonicalUser; |
| 28 | import org.jclouds.s3.domain.AccessControlList.CanonicalUserGrantee; |
| 29 | import org.jclouds.s3.domain.AccessControlList.EmailAddressGrantee; |
| 30 | import org.jclouds.s3.domain.AccessControlList.Grantee; |
| 31 | import org.jclouds.s3.domain.AccessControlList.GroupGrantee; |
| 32 | import org.xml.sax.Attributes; |
| 33 | |
| 34 | /** |
| 35 | * Parses the following XML document: |
| 36 | * <p/> |
| 37 | * AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/" |
| 38 | * |
| 39 | * @author James Murty |
| 40 | * @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html"/> |
| 41 | */ |
| 42 | public class AccessControlListHandler extends ParseSax.HandlerWithResult<AccessControlList> { |
| 43 | private AccessControlList acl = new AccessControlList(); |
| 44 | private StringBuilder currentText = new StringBuilder(); |
| 45 | |
| 46 | public AccessControlListHandler() { |
| 47 | } |
| 48 | |
| 49 | public AccessControlList getResult() { |
| 50 | return acl; |
| 51 | } |
| 52 | |
| 53 | private String currentId; |
| 54 | private String currentDisplayName; |
| 55 | private String currentGranteeType; |
| 56 | private String currentPermission; |
| 57 | private Grantee currentGrantee; |
| 58 | |
| 59 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 60 | if (qName.equals("Grantee")) { |
| 61 | currentGranteeType = attrs.getValue("xsi:type"); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public void endElement(String uri, String name, String qName) { |
| 66 | if (qName.equals("Owner")) { |
| 67 | CanonicalUser owner = new CanonicalUser(currentId); |
| 68 | owner.setDisplayName(currentDisplayName); |
| 69 | acl.setOwner(owner); |
| 70 | } else if (qName.equals("Grantee")) { |
| 71 | if ("AmazonCustomerByEmail".equals(currentGranteeType)) { |
| 72 | currentGrantee = new EmailAddressGrantee(currentId); |
| 73 | } else if ("CanonicalUser".equals(currentGranteeType)) { |
| 74 | currentGrantee = new CanonicalUserGrantee(currentId, currentDisplayName); |
| 75 | } else if ("Group".equals(currentGranteeType)) { |
| 76 | currentGrantee = new GroupGrantee(URI.create(currentId)); |
| 77 | } |
| 78 | } else if (qName.equals("Grant")) { |
| 79 | acl.addPermission(currentGrantee, currentPermission); |
| 80 | } |
| 81 | |
| 82 | else if (qName.equals("ID") || qName.equals("EmailAddress") || qName.equals("URI")) { |
| 83 | currentId = currentOrNull(currentText); |
| 84 | } else if (qName.equals("DisplayName")) { |
| 85 | currentDisplayName = currentOrNull(currentText); |
| 86 | } else if (qName.equals("Permission")) { |
| 87 | currentPermission = currentOrNull(currentText); |
| 88 | } |
| 89 | currentText = new StringBuilder(); |
| 90 | } |
| 91 | |
| 92 | public void characters(char ch[], int start, int length) { |
| 93 | currentText.append(ch, start, length); |
| 94 | } |
| 95 | } |