Skip to content
Permalink
Browse files
Simplify S3 code that uses java-xml-builder (#93)
* animal sniffer should be on java18, just like `<jdk.version>`

* 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
  • Loading branch information
JnRouvignac committed Jan 21, 2021
1 parent 17fd80c commit 647af7e365ae307b3606607bfd8d2a88d0732c88
Showing 4 changed files with 23 additions and 44 deletions.
@@ -26,15 +26,13 @@
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 <R extends HttpRequest> R bindToRequest(R request, Object payload) {
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 @@ protected XMLBuilder generateBuilder(AccessControlList acl) throws ParserConfigu
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;
}

}
@@ -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 <R extends HttpRequest> R bindToRequest(R request, Object payload) {
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<Grant> 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,17 +74,16 @@ protected XMLBuilder generateBuilder(BucketLogging bucketLogging) throws ParserC
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());
}
} else if (grant.getGrantee() instanceof EmailAddressGrantee) {
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;
}

}
@@ -53,18 +53,15 @@ public <R extends HttpRequest> R bindToRequest(R request, Object input) {
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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);
@@ -499,7 +499,7 @@
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java17</artifactId>
<artifactId>java18</artifactId>
<version>1.0</version>
</signature>
</configuration>

0 comments on commit 647af7e

Please sign in to comment.