From 7525fc7a156f57b8a69bec35090e33c1cb083724 Mon Sep 17 00:00:00 2001 From: Jason King Date: Wed, 14 Dec 2011 16:41:55 +0000 Subject: [PATCH] Issue 695: Converted XML generation to use XMLBuilder --- .../tmrk-enterprisecloud/pom.xml | 10 ++++++ .../binders/BindSSHKeyToXmlPayload.java | 31 +++++++++++++------ ...remarkEnterpriseCloudRestClientModule.java | 7 ----- .../src/main/resources/editSSHKey.xml | 4 --- .../binders/BindSSHKeyToXmlPayloadTest.java | 23 ++++++-------- .../features/SSHKeyAsyncClientTest.java | 5 +-- 6 files changed, 41 insertions(+), 39 deletions(-) delete mode 100644 sandbox-providers/tmrk-enterprisecloud/src/main/resources/editSSHKey.xml diff --git a/sandbox-providers/tmrk-enterprisecloud/pom.xml b/sandbox-providers/tmrk-enterprisecloud/pom.xml index 398db07912..bc481936f1 100644 --- a/sandbox-providers/tmrk-enterprisecloud/pom.xml +++ b/sandbox-providers/tmrk-enterprisecloud/pom.xml @@ -60,6 +60,11 @@ jclouds-compute ${project.version} + + com.jamesmurty.utils + java-xmlbuilder + 0.4 + org.jclouds jclouds-core @@ -86,6 +91,11 @@ ${project.version} test + + com.jamesmurty.utils + java-xmlbuilder + 0.3 + diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayload.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayload.java index 3cbc5f6dc5..1720c1cad1 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayload.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayload.java @@ -18,16 +18,17 @@ */ package org.jclouds.tmrk.enterprisecloud.binders; -import com.google.common.collect.ImmutableMap; +import com.jamesmurty.utils.XMLBuilder; import org.jclouds.http.HttpRequest; import org.jclouds.rest.Binder; import org.jclouds.rest.binders.BindToStringPayload; import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey; -import org.jclouds.util.Strings2; import javax.inject.Inject; -import javax.inject.Named; import javax.inject.Singleton; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import java.util.Properties; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -39,13 +40,10 @@ import static com.google.common.base.Preconditions.checkNotNull; @Singleton public class BindSSHKeyToXmlPayload implements Binder { - private final String xmlTemplate; private final BindToStringPayload stringBinder; @Inject - BindSSHKeyToXmlPayload(@Named("EditSSHKey") String xmlTemplate, - BindToStringPayload stringBinder) { - this.xmlTemplate = xmlTemplate; + BindSSHKeyToXmlPayload(BindToStringPayload stringBinder) { this.stringBinder = stringBinder; } @@ -59,9 +57,22 @@ public class BindSSHKeyToXmlPayload implements Binder { String isDefault = Boolean.toString(sshKey.isDefaultKey()); String fingerPrint = sshKey.getFingerPrint(); - String payload = Strings2.replaceTokens(xmlTemplate, - ImmutableMap.of("name", name, "isDefault", isDefault, "fingerPrint", fingerPrint)); - + String payload = createXMLPayload(name,isDefault,fingerPrint); return stringBinder.bindToRequest(request, payload); } + + private String createXMLPayload(String name, String isDefault, String fingerPrint) { + try { + Properties outputProperties = new Properties(); + outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); + return XMLBuilder.create("SshKey").a("name",name) + .e("Default").t(isDefault).up() + .e("FingerPrint").t(fingerPrint) + .asString(outputProperties); + } catch (ParserConfigurationException e) { + throw new RuntimeException(e); + } catch (TransformerException t) { + throw new RuntimeException(t); + } + } } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java index 5cff6ef7aa..9e93968f74 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java @@ -82,11 +82,4 @@ public class TerremarkEnterpriseCloudRestClientModule extends protected void bindRetryHandlers() { bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(BackoffLimitedRetryHandler.class); } - - @Singleton - @Provides - @Named("EditSSHKey") - String provideEditSSHKey() throws IOException { - return Strings2.toStringAndClose(getClass().getResourceAsStream("/EditSSHKey.xml")); - } } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/resources/editSSHKey.xml b/sandbox-providers/tmrk-enterprisecloud/src/main/resources/editSSHKey.xml deleted file mode 100644 index bf23606594..0000000000 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/resources/editSSHKey.xml +++ /dev/null @@ -1,4 +0,0 @@ - - {isDefault} - {fingerPrint} - \ No newline at end of file diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayloadTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayloadTest.java index f13e59fd36..86ea476ffc 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayloadTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayloadTest.java @@ -46,28 +46,23 @@ public class BindSSHKeyToXmlPayloadTest { @Override protected void configure() { } - - @SuppressWarnings("unused") - @Singleton - @Provides - @Named("EditSSHKey") - String provideInstantiateVAppTemplateParams() throws IOException { - InputStream is = getClass().getResourceAsStream("/EditSSHKey.xml"); - return Strings2.toStringAndClose(is); - } }); - public void testApplyInputStream() throws IOException { - String expected = Strings2.toStringAndClose(getClass().getResourceAsStream( - "/EditSSHKey-test.xml")); + public void testPayloadXmlContent() throws IOException { + final String name = "newName"; + final boolean isDefault = false; + final String fingerPrint = "123"; + final String expected = String.format("%b%s", + name,isDefault,fingerPrint); + HttpRequest request = new HttpRequest("GET", URI.create("http://test")); BindSSHKeyToXmlPayload binder = injector .getInstance(BindSSHKeyToXmlPayload.class); SSHKey key = SSHKey.builder().type("application/vnd.tmrk.cloud.admin.sshKey") .href(URI.create("/cloudapi/ecloud/admin/sshkeys/77")) - .name("newName") - .defaultKey(false).fingerPrint("123").build(); + .name(name) + .defaultKey(isDefault).fingerPrint(fingerPrint).build(); binder.bindToRequest(request, key); assertEquals(request.getPayload().getRawContent(), expected); diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java index 25a600b390..30b37079ef 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java @@ -99,10 +99,7 @@ public class SSHKeyAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClie assertRequestLineEquals(httpRequest, "PUT https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/admin/sshkeys/77 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.admin.sshKey\nx-tmrk-version: 2011-07-01\n"); - String xml = "\n" + - " false\n" + - " 123\n" + - ""; + String xml = "false123"; assertPayloadEquals(httpRequest, xml, "application/xml", false); assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);