Issue 435: revised parsers to support savvis org and orglist xml

This commit is contained in:
Adrian Cole 2011-01-19 11:02:40 -08:00
parent ec6d03ca37
commit 3e408568e4
14 changed files with 78 additions and 134 deletions

View File

@ -167,7 +167,9 @@ public class OrgImpl extends ReferenceTypeImpl implements Org {
@Override
public String toString() {
return "[id=" + getHref() + ", name=" + getName() + ", type=" + getType() + "]";
return "[href=" + getHref() + ", name=" + getName() + ", type=" + getType() + ", fullName=" + fullName
+ ", description=" + description + ", catalogs=" + catalogs + ", networks=" + networks + ", tasksList="
+ tasksList + ", vdcs=" + vdcs + ", tasks=" + tasks + "]";
}
}

View File

@ -40,7 +40,9 @@ public class Utils {
public static ReferenceType newReferenceType(Map<String, String> attributes, String defaultType) {
String uri = attributes.get("href");
String type = attributes.get("type");
return new ReferenceTypeImpl(attributes.get("name"), type != null ? type : defaultType, URI.create(uri));
// savvis org has null href
URI href = (uri != null) ? URI.create(uri) : null;
return new ReferenceTypeImpl(attributes.get("name"), type != null ? type : defaultType, href);
}
public static Map<String, String> cleanseAttributes(Attributes in) {

View File

@ -71,9 +71,9 @@ public class OrgHandler extends ParseSax.HandlerWithResult<Org> {
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
Map<String, String> attributes = cleanseAttributes(attrs);
if (qName.equals("Org")) {
if (qName.endsWith("Org")) {
org = newReferenceType(attributes);
} else if (qName.equals("Link")) {
} else if (qName.endsWith("Link")) {
String type = attributes.get("type");
if (type != null) {
if (type.indexOf("vdc+xml") != -1) {
@ -94,11 +94,11 @@ public class OrgHandler extends ParseSax.HandlerWithResult<Org> {
public void endElement(String uri, String name, String qName) {
taskHandler.endElement(uri, name, qName);
if (qName.equals("Task")) {
if (qName.endsWith("Task")) {
this.tasks.add(taskHandler.getResult());
} else if (qName.equals("Description")) {
} else if (qName.endsWith("Description")) {
description = currentOrNull();
} else if (qName.equals("FullName")) {
} else if (qName.endsWith("FullName")) {
fullName = currentOrNull();
}
currentText = new StringBuilder();

View File

@ -45,7 +45,7 @@ public class OrgListHandler extends ParseSax.HandlerWithResult<Map<String, Refer
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
Map<String, String> attributes = cleanseAttributes(attrs);
if (qName.equals("Org")) {
if (qName.endsWith("Org")) {
String type = attributes.get("type");
if (type != null) {
if (type.indexOf("org+xml") != -1) {

View File

@ -31,10 +31,14 @@ import org.jclouds.http.functions.ParseSax.Factory;
import org.jclouds.http.functions.config.SaxParserModule;
import org.jclouds.vcloud.VCloudMediaType;
import org.jclouds.vcloud.domain.Org;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.domain.Task;
import org.jclouds.vcloud.domain.internal.OrgImpl;
import org.jclouds.vcloud.domain.internal.ReferenceTypeImpl;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
@ -107,4 +111,17 @@ public class OrgHandlerTest {
assertEquals(result.getTasksList(), new ReferenceTypeImpl("188849 Task List", TASKSLIST_XML, URI
.create("https://vcloud.safesecureweb.com/api/v0.8/tasksList/188849")));
}
public void testSavvis() {
InputStream is = getClass().getResourceAsStream("/org-savvis.xml");
Injector injector = Guice.createInjector(new SaxParserModule());
Factory factory = injector.getInstance(ParseSax.Factory.class);
Org result = (Org) factory.create(injector.getInstance(OrgHandler.class)).parse(is);
assertEquals(result, new OrgImpl("607968.0", null, null, "607968.0", "Gravitant Inc", ImmutableMap
.<String, ReferenceType> of(), ImmutableMap.<String, ReferenceType> of("GravDataCenter1(Saved)",
new ReferenceTypeImpl("GravDataCenter1(Saved)", "application/vnd.vmware.vcloud.vdc+xml", URI
.create("https://api.symphonyVPDC.savvis.net/rest/api/v0.8/org/607968.0/vdc/2826"))),
ImmutableMap.<String, ReferenceType> of(), null, ImmutableSet.<Task> of()));
}
}

View File

@ -48,4 +48,13 @@ public class OrgListHandlerTest extends BaseHandlerTest {
assertEquals(result, ImmutableMap.of("adrian@jclouds.org", new ReferenceTypeImpl("adrian@jclouds.org",
"application/vnd.vmware.vcloud.org+xml", URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/org/48"))));
}
public void testSavvis() {
InputStream is = getClass().getResourceAsStream("/orglist-savvis.xml");
Map<String, ReferenceType> result = factory.create(injector.getInstance(OrgListHandler.class)).parse(is);
assertEquals(result, ImmutableMap.of("SAVVISStation Integration Testing", new ReferenceTypeImpl("SAVVISStation Integration Testing",
"application/vnd.vmware.vcloud.org+xml", URI.create("https://api.sandbox.symphonyvpdc.savvis.net/rest/api/v0.8/org/100000.0"))));
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<vApp:Org xmlns:vApp="http://www.vmware.com/vcloud/v0.8"
xmlns:common="http://schemas.dmtf.org/wbem/wscim/1/common"
xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData"
name="607968.0">
<vApp:Link
href="https://api.symphonyVPDC.savvis.net/rest/api/v0.8/org/607968.0/vdc/2826"
name="GravDataCenter1(Saved)" rel="down"
type="application/vnd.vmware.vcloud.vdc+xml" />
<vApp:Description>Gravitant Inc</vApp:Description>
</vApp:Org>

View File

@ -19,6 +19,8 @@
package org.jclouds.savvis;
import java.net.URI;
import javax.annotation.Nullable;
import javax.ws.rs.GET;
@ -27,7 +29,6 @@ import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.XMLResponseParser;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.vcloud.CommonVCloudClient;
import org.jclouds.vcloud.VCloudExpressAsyncClient;
import org.jclouds.vcloud.domain.Org;
import org.jclouds.vcloud.filters.SetVCloudTokenCookie;
@ -45,12 +46,25 @@ import com.google.common.util.concurrent.ListenableFuture;
*/
@RequestFilters(SetVCloudTokenCookie.class)
public interface SymphonyVPDCAsyncClient extends VCloudExpressAsyncClient {
/**
* @see CommonVCloudClient#getOrgNamed
* {@inheritDoc}
*/
// savvis doesn't work with accept header
@Override
@GET
@XMLResponseParser(OrgHandler.class)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<? extends Org> findOrgNamed(
@Nullable @EndpointParam(parser = OrgNameToEndpoint.class) String orgName);
/**
* {@inheritDoc}
*/
// savvis doesn't work with accept header
@Override
@GET
@XMLResponseParser(OrgHandler.class)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<? extends Org> getOrg(@EndpointParam URI orgId);
}

View File

@ -28,11 +28,9 @@ import org.jclouds.http.annotation.ServerError;
import org.jclouds.savvis.SymphonyVPDCAsyncClient;
import org.jclouds.savvis.SymphonyVPDCClient;
import org.jclouds.savvis.handlers.SymphonyVPDCErrorHandler;
import org.jclouds.savvis.xml.SymphonyVPDCOrgListHandler;
import org.jclouds.vcloud.VCloudExpressAsyncClient;
import org.jclouds.vcloud.VCloudExpressClient;
import org.jclouds.vcloud.config.BaseVCloudExpressRestClientModule;
import org.jclouds.vcloud.xml.OrgListHandler;
import com.google.inject.Provides;
@ -60,7 +58,8 @@ public class SymphonyVPDCRestClientModule extends
@Override
protected void configure() {
super.configure();
bind(OrgListHandler.class).to(SymphonyVPDCOrgListHandler.class);
// no longer needed.. just here to show an example of how to override an xml handler
// bind(OrgListHandler.class).to(SymphonyVPDCOrgListHandler.class);
}
@Override

View File

@ -1,58 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.savvis.xml;
import static org.jclouds.vcloud.util.Utils.cleanseAttributes;
import static org.jclouds.vcloud.util.Utils.putReferenceType;
import java.util.Map;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.xml.OrgListHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.google.common.collect.Maps;
/**
* @author Adrian Cole
*/
public class SymphonyVPDCOrgListHandler extends OrgListHandler {
private Map<String, ReferenceType> org = Maps.newHashMap();
public Map<String, ReferenceType> getResult() {
return org;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
Map<String, String> attributes = cleanseAttributes(attrs);
if (qName.contains("vApp:Org")) {
String type = attributes.get("type");
if (type != null) {
if (type.indexOf("org+xml") != -1) {
putReferenceType(org, attributes);
}
}
}
}
}

View File

@ -219,7 +219,7 @@ public class SymphonyVPDCAsyncClientTest extends RestClientTest<SymphonyVPDCAsyn
URI.create("https://api.sandbox.symphonyVPDC.savvis.net/rest/api/v0.8/org/1"));
assertRequestLineEquals(request, "GET https://api.sandbox.symphonyVPDC.savvis.net/rest/api/v0.8/org/1 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: application/vnd.vmware.vcloud.org+xml\n");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, ParseSax.class);
@ -234,7 +234,7 @@ public class SymphonyVPDCAsyncClientTest extends RestClientTest<SymphonyVPDCAsyn
HttpRequest request = processor.createRequest(method, "org");
assertRequestLineEquals(request, "GET https://api.sandbox.symphonyVPDC.savvis.net/rest/api/v0.8/org/1 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: application/vnd.vmware.vcloud.org+xml\n");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, null, null, false);
assertResponseParserClassEquals(method, request, ParseSax.class);

View File

@ -21,10 +21,8 @@ package org.jclouds.savvis;
import java.util.Properties;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.jclouds.vcloud.VCloudExpressClientLiveTest;
import org.testng.annotations.BeforeGroups;

View File

@ -1,52 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.savvis.xml;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import org.jclouds.http.functions.BaseHandlerTest;
import org.jclouds.vcloud.VCloudExpressMediaType;
import org.jclouds.vcloud.domain.ReferenceType;
import org.jclouds.vcloud.domain.internal.ReferenceTypeImpl;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
/**
* Tests behavior of {@code OrgListHandler}
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "OrgListHandlerTest")
public class SymphonyVPDCOrgListHandlerTest extends BaseHandlerTest {
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/orglist.xml");
Map<String, ReferenceType> result = factory.create(injector.getInstance(SymphonyVPDCOrgListHandler.class)).parse(is);
assertEquals(result, ImmutableMap.of("SAVVISStation Integration Testing", new ReferenceTypeImpl("SAVVISStation Integration Testing",
VCloudExpressMediaType.ORG_XML, URI.create("https://api.sandbox.symphonyvpdc.savvis.net/rest/api/v0.8/org/100000.0"))));
}
}