mirror of https://github.com/apache/jclouds.git
Issue 695: Converted XML generation to use XMLBuilder
This commit is contained in:
parent
7f71cb3896
commit
7525fc7a15
|
@ -60,6 +60,11 @@
|
|||
<artifactId>jclouds-compute</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jamesmurty.utils</groupId>
|
||||
<artifactId>java-xmlbuilder</artifactId>
|
||||
<version>0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jclouds</groupId>
|
||||
<artifactId>jclouds-core</artifactId>
|
||||
|
@ -86,6 +91,11 @@
|
|||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jamesmurty.utils</groupId>
|
||||
<artifactId>java-xmlbuilder</artifactId>
|
||||
<version>0.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<profiles>
|
||||
<profile>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
<SshKey name="{name}">
|
||||
<Default>{isDefault}</Default>
|
||||
<FingerPrint>{fingerPrint}</FingerPrint>
|
||||
</SshKey>
|
|
@ -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("<SshKey name=\"%s\"><Default>%b</Default><FingerPrint>%s</FingerPrint></SshKey>",
|
||||
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);
|
||||
|
|
|
@ -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 = "<SshKey name=\"newName\">\n" +
|
||||
" <Default>false</Default>\n" +
|
||||
" <FingerPrint>123</FingerPrint>\n" +
|
||||
"</SshKey>";
|
||||
String xml = "<SshKey name=\"newName\"><Default>false</Default><FingerPrint>123</FingerPrint></SshKey>";
|
||||
assertPayloadEquals(httpRequest, xml, "application/xml", false);
|
||||
|
||||
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
|
||||
|
|
Loading…
Reference in New Issue