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
This commit is contained in:
Jean-Noël Rouvignac 2021-01-21 13:54:44 +01:00 committed by GitHub
parent 17fd80cd5a
commit 647af7e365
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 44 deletions

View File

@ -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;
}
}

View File

@ -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<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,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;
}
}

View File

@ -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 = "<?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);

View File

@ -499,7 +499,7 @@
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java17</artifactId>
<artifactId>java18</artifactId>
<version>1.0</version>
</signature>
</configuration>