From 647af7e365ae307b3606607bfd8d2a88d0732c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Rouvignac?= Date: Thu, 21 Jan 2021 13:54:44 +0100 Subject: [PATCH] Simplify S3 code that uses java-xml-builder (#93) * animal sniffer should be on java18, just like `` * Only use XMLBuilder's elem() and text() methods to have similar looking code * Remove unnecessary call to XMLBuilder's up() because the returned value is never used * Simplify code * Deduplicate code * Make the code more explicit by returning the rootBuilder --- .../s3/binders/BindACLToXMLPayload.java | 36 ++++--------------- .../BindBucketLoggingToXmlPayload.java | 22 +++++++----- .../BindIterableAsPayloadToDeleteRequest.java | 7 ++-- project/pom.xml | 2 +- 4 files changed, 23 insertions(+), 44 deletions(-) diff --git a/apis/s3/src/main/java/org/jclouds/s3/binders/BindACLToXMLPayload.java b/apis/s3/src/main/java/org/jclouds/s3/binders/BindACLToXMLPayload.java index 6a7d9bae79..34e1110f7d 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/binders/BindACLToXMLPayload.java +++ b/apis/s3/src/main/java/org/jclouds/s3/binders/BindACLToXMLPayload.java @@ -26,15 +26,13 @@ import javax.xml.parsers.ParserConfigurationException; import org.jclouds.http.HttpRequest; import org.jclouds.rest.Binder; import org.jclouds.s3.domain.AccessControlList; -import org.jclouds.s3.domain.AccessControlList.CanonicalUserGrantee; -import org.jclouds.s3.domain.AccessControlList.EmailAddressGrantee; -import org.jclouds.s3.domain.AccessControlList.Grant; -import org.jclouds.s3.domain.AccessControlList.GroupGrantee; import org.jclouds.s3.reference.S3Constants; import com.google.common.base.Throwables; import com.jamesmurty.utils.XMLBuilder; +import static org.jclouds.s3.binders.BindBucketLoggingToXmlPayload.addGrants; + @Singleton public class BindACLToXMLPayload implements Binder { @Override @@ -46,11 +44,11 @@ public class BindACLToXMLPayload implements Binder { String stringPayload = generateBuilder(from).asString(outputProperties); request.setPayload(stringPayload); request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_XML); + return request; } catch (Exception e) { Throwables.propagateIfPossible(e); throw new RuntimeException("error transforming acl: " + from, e); } - return request; } protected XMLBuilder generateBuilder(AccessControlList acl) throws ParserConfigurationException, @@ -59,32 +57,12 @@ public class BindACLToXMLPayload implements Binder { S3Constants.S3_REST_API_XML_NAMESPACE); if (acl.getOwner() != null) { XMLBuilder ownerBuilder = rootBuilder.elem("Owner"); - ownerBuilder.elem("ID").text(acl.getOwner().getId()).up(); + ownerBuilder.elem("ID").text(acl.getOwner().getId()); if (acl.getOwner().getDisplayName() != null) { - ownerBuilder.elem("DisplayName").text(acl.getOwner().getDisplayName()).up(); + ownerBuilder.elem("DisplayName").text(acl.getOwner().getDisplayName()); } } - XMLBuilder grantsBuilder = rootBuilder.elem("AccessControlList"); - for (Grant grant : acl.getGrants()) { - XMLBuilder grantBuilder = grantsBuilder.elem("Grant"); - XMLBuilder granteeBuilder = grantBuilder.elem("Grantee").attr("xmlns:xsi", - "http://www.w3.org/2001/XMLSchema-instance"); - - if (grant.getGrantee() instanceof GroupGrantee) { - granteeBuilder.attr("xsi:type", "Group").elem("URI").text(grant.getGrantee().getIdentifier()); - } else if (grant.getGrantee() instanceof CanonicalUserGrantee) { - CanonicalUserGrantee grantee = (CanonicalUserGrantee) grant.getGrantee(); - granteeBuilder.attr("xsi:type", "CanonicalUser").elem("ID").text(grantee.getIdentifier()).up(); - if (grantee.getDisplayName() != null) { - granteeBuilder.elem("DisplayName").text(grantee.getDisplayName()); - } - } else if (grant.getGrantee() instanceof EmailAddressGrantee) { - granteeBuilder.attr("xsi:type", "AmazonCustomerByEmail").elem("EmailAddress") - .text(grant.getGrantee().getIdentifier()); - } - grantBuilder.elem("Permission").text(grant.getPermission().toString()); - } - return grantsBuilder; + addGrants(rootBuilder.elem("AccessControlList"), acl.getGrants()); + return rootBuilder; } - } diff --git a/apis/s3/src/main/java/org/jclouds/s3/binders/BindBucketLoggingToXmlPayload.java b/apis/s3/src/main/java/org/jclouds/s3/binders/BindBucketLoggingToXmlPayload.java index 732c6a364b..c887082291 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/binders/BindBucketLoggingToXmlPayload.java +++ b/apis/s3/src/main/java/org/jclouds/s3/binders/BindBucketLoggingToXmlPayload.java @@ -16,6 +16,7 @@ */ package org.jclouds.s3.binders; +import java.util.Collection; import java.util.Properties; import javax.inject.Singleton; @@ -46,21 +47,25 @@ public class BindBucketLoggingToXmlPayload implements Binder { String stringPayload = generateBuilder(from).asString(outputProperties); request.setPayload(stringPayload); request.getPayload().getContentMetadata().setContentType(MediaType.TEXT_XML); + return request; } catch (Exception e) { Throwables.propagateIfPossible(e); throw new RuntimeException("error transforming bucketLogging: " + from, e); } - return request; } protected XMLBuilder generateBuilder(BucketLogging bucketLogging) throws ParserConfigurationException, FactoryConfigurationError { XMLBuilder rootBuilder = XMLBuilder.create("BucketLoggingStatus") - .attr("xmlns", S3Constants.S3_REST_API_XML_NAMESPACE).e("LoggingEnabled"); - rootBuilder.e("TargetBucket").t(bucketLogging.getTargetBucket()); - rootBuilder.e("TargetPrefix").t(bucketLogging.getTargetPrefix()); - XMLBuilder grantsBuilder = rootBuilder.elem("TargetGrants"); - for (Grant grant : bucketLogging.getTargetGrants()) { + .attr("xmlns", S3Constants.S3_REST_API_XML_NAMESPACE).elem("LoggingEnabled"); + rootBuilder.elem("TargetBucket").text(bucketLogging.getTargetBucket()); + rootBuilder.elem("TargetPrefix").text(bucketLogging.getTargetPrefix()); + addGrants(rootBuilder.elem("TargetGrants"), bucketLogging.getTargetGrants()); + return rootBuilder; + } + + static void addGrants(XMLBuilder grantsBuilder, Collection grants) { + for (Grant grant : grants) { XMLBuilder grantBuilder = grantsBuilder.elem("Grant"); XMLBuilder granteeBuilder = grantBuilder.elem("Grantee").attr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); @@ -69,7 +74,7 @@ public class BindBucketLoggingToXmlPayload implements Binder { granteeBuilder.attr("xsi:type", "Group").elem("URI").text(grant.getGrantee().getIdentifier()); } else if (grant.getGrantee() instanceof CanonicalUserGrantee) { CanonicalUserGrantee grantee = (CanonicalUserGrantee) grant.getGrantee(); - granteeBuilder.attr("xsi:type", "CanonicalUser").elem("ID").text(grantee.getIdentifier()).up(); + granteeBuilder.attr("xsi:type", "CanonicalUser").elem("ID").text(grantee.getIdentifier()); if (grantee.getDisplayName() != null) { granteeBuilder.elem("DisplayName").text(grantee.getDisplayName()); } @@ -77,9 +82,8 @@ public class BindBucketLoggingToXmlPayload implements Binder { granteeBuilder.attr("xsi:type", "AmazonCustomerByEmail").elem("EmailAddress") .text(grant.getGrantee().getIdentifier()); } - grantBuilder.elem("Permission").text(grant.getPermission().toString()); + grantBuilder.elem("Permission").text(grant.getPermission()); } - return grantsBuilder; } } diff --git a/apis/s3/src/main/java/org/jclouds/s3/binders/BindIterableAsPayloadToDeleteRequest.java b/apis/s3/src/main/java/org/jclouds/s3/binders/BindIterableAsPayloadToDeleteRequest.java index 5a66d2b8bf..1a60ae6538 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/binders/BindIterableAsPayloadToDeleteRequest.java +++ b/apis/s3/src/main/java/org/jclouds/s3/binders/BindIterableAsPayloadToDeleteRequest.java @@ -53,18 +53,15 @@ public class BindIterableAsPayloadToDeleteRequest implements Binder { try { XMLBuilder rootBuilder = XMLBuilder.create("Delete"); for (String key : keys) { - XMLBuilder ownerBuilder = rootBuilder.elem("Object"); - XMLBuilder keyBuilder = ownerBuilder.elem("Key").text(key); + rootBuilder.elem("Object").elem("Key").text(key); } Properties outputProperties = new Properties(); outputProperties.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); content = "\n" + rootBuilder.asString(outputProperties); - } catch (ParserConfigurationException pce) { + } catch (ParserConfigurationException | TransformerException pce) { throw Throwables.propagate(pce); - } catch (TransformerException te) { - throw Throwables.propagate(te); } Payload payload = Payloads.newStringPayload(content); diff --git a/project/pom.xml b/project/pom.xml index 44f1a3fb79..f17fe861e2 100644 --- a/project/pom.xml +++ b/project/pom.xml @@ -499,7 +499,7 @@ org.codehaus.mojo.signature - java17 + java18 1.0