| 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.binders; |
| 20 | |
| 21 | import java.util.Properties; |
| 22 | |
| 23 | import javax.inject.Singleton; |
| 24 | import javax.ws.rs.core.MediaType; |
| 25 | import javax.xml.parsers.FactoryConfigurationError; |
| 26 | import javax.xml.parsers.ParserConfigurationException; |
| 27 | |
| 28 | import org.jclouds.s3.domain.BucketLogging; |
| 29 | import org.jclouds.s3.domain.AccessControlList.CanonicalUserGrantee; |
| 30 | import org.jclouds.s3.domain.AccessControlList.EmailAddressGrantee; |
| 31 | import org.jclouds.s3.domain.AccessControlList.Grant; |
| 32 | import org.jclouds.s3.domain.AccessControlList.GroupGrantee; |
| 33 | import org.jclouds.s3.reference.S3Constants; |
| 34 | import org.jclouds.http.HttpRequest; |
| 35 | import org.jclouds.rest.Binder; |
| 36 | |
| 37 | import com.google.common.base.Throwables; |
| 38 | import com.jamesmurty.utils.XMLBuilder; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | @Singleton |
| 45 | public class BindBucketLoggingToXmlPayload implements Binder { |
| 46 | @Override |
| 47 | public <R extends HttpRequest> R bindToRequest(R request, Object payload) { |
| 48 | BucketLogging from = (BucketLogging) payload; |
| 49 | Properties outputProperties = new Properties(); |
| 50 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 51 | try { |
| 52 | String stringPayload = generateBuilder(from).asString(outputProperties); |
| 53 | request.setPayload(stringPayload); |
| 54 | request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_XML); |
| 55 | } catch (Exception e) { |
| 56 | Throwables.propagateIfPossible(e); |
| 57 | throw new RuntimeException("error transforming bucketLogging: " + from, e); |
| 58 | } |
| 59 | return request; |
| 60 | } |
| 61 | |
| 62 | protected XMLBuilder generateBuilder(BucketLogging bucketLogging) throws ParserConfigurationException, |
| 63 | FactoryConfigurationError { |
| 64 | XMLBuilder rootBuilder = XMLBuilder.create("BucketLoggingStatus") |
| 65 | .attr("xmlns", S3Constants.S3_REST_API_XML_NAMESPACE).e("LoggingEnabled"); |
| 66 | rootBuilder.e("TargetBucket").t(bucketLogging.getTargetBucket()); |
| 67 | rootBuilder.e("TargetPrefix").t(bucketLogging.getTargetPrefix()); |
| 68 | XMLBuilder grantsBuilder = rootBuilder.elem("TargetGrants"); |
| 69 | for (Grant grant : bucketLogging.getTargetGrants()) { |
| 70 | XMLBuilder grantBuilder = grantsBuilder.elem("Grant"); |
| 71 | XMLBuilder granteeBuilder = grantBuilder.elem("Grantee").attr("xmlns:xsi", |
| 72 | "http://www.w3.org/2001/XMLSchema-instance"); |
| 73 | |
| 74 | if (grant.getGrantee() instanceof GroupGrantee) { |
| 75 | granteeBuilder.attr("xsi:type", "Group").elem("URI").text(grant.getGrantee().getIdentifier()); |
| 76 | } else if (grant.getGrantee() instanceof CanonicalUserGrantee) { |
| 77 | CanonicalUserGrantee grantee = (CanonicalUserGrantee) grant.getGrantee(); |
| 78 | granteeBuilder.attr("xsi:type", "CanonicalUser").elem("ID").text(grantee.getIdentifier()).up(); |
| 79 | if (grantee.getDisplayName() != null) { |
| 80 | granteeBuilder.elem("DisplayName").text(grantee.getDisplayName()); |
| 81 | } |
| 82 | } else if (grant.getGrantee() instanceof EmailAddressGrantee) { |
| 83 | granteeBuilder.attr("xsi:type", "AmazonCustomerByEmail").elem("EmailAddress") |
| 84 | .text(grant.getGrantee().getIdentifier()); |
| 85 | } |
| 86 | grantBuilder.elem("Permission").text(grant.getPermission().toString()); |
| 87 | } |
| 88 | return grantsBuilder; |
| 89 | } |
| 90 | |
| 91 | } |