From 3e408568e468f9beed869dc0a908628e67b5d147 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 19 Jan 2011 11:02:40 -0800 Subject: [PATCH] Issue 435: revised parsers to support savvis org and orglist xml --- .../vcloud/domain/internal/OrgImpl.java | 4 +- .../java/org/jclouds/vcloud/util/Utils.java | 4 +- .../org/jclouds/vcloud/xml/OrgHandler.java | 10 ++-- .../jclouds/vcloud/xml/OrgListHandler.java | 2 +- .../jclouds/vcloud/xml/OrgHandlerTest.java | 17 ++++++ .../vcloud/xml/OrgListHandlerTest.java | 9 +++ .../vcloud/src/test/resources/org-savvis.xml | 13 +++++ .../src/test/resources/orglist-savvis.xml | 0 .../savvis/SymphonyVPDCAsyncClient.java | 32 +++++++--- .../config/SymphonyVPDCRestClientModule.java | 5 +- .../xml/SymphonyVPDCOrgListHandler.java | 58 ------------------- .../savvis/SymphonyVPDCAsyncClientTest.java | 4 +- .../savvis/SymphonyVPDCClientLiveTest.java | 2 - .../xml/SymphonyVPDCOrgListHandlerTest.java | 52 ----------------- 14 files changed, 78 insertions(+), 134 deletions(-) create mode 100644 common/vcloud/src/test/resources/org-savvis.xml rename sandbox-providers/savvis/src/test/resources/orglist.xml => common/vcloud/src/test/resources/orglist-savvis.xml (100%) delete mode 100644 sandbox-providers/savvis/src/main/java/org/jclouds/savvis/xml/SymphonyVPDCOrgListHandler.java delete mode 100644 sandbox-providers/savvis/src/test/java/org/jclouds/savvis/xml/SymphonyVPDCOrgListHandlerTest.java diff --git a/common/vcloud/src/main/java/org/jclouds/vcloud/domain/internal/OrgImpl.java b/common/vcloud/src/main/java/org/jclouds/vcloud/domain/internal/OrgImpl.java index d69cbec469..3b05044b2d 100644 --- a/common/vcloud/src/main/java/org/jclouds/vcloud/domain/internal/OrgImpl.java +++ b/common/vcloud/src/main/java/org/jclouds/vcloud/domain/internal/OrgImpl.java @@ -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 + "]"; } } \ No newline at end of file diff --git a/common/vcloud/src/main/java/org/jclouds/vcloud/util/Utils.java b/common/vcloud/src/main/java/org/jclouds/vcloud/util/Utils.java index a029e05d04..ef34edbd73 100644 --- a/common/vcloud/src/main/java/org/jclouds/vcloud/util/Utils.java +++ b/common/vcloud/src/main/java/org/jclouds/vcloud/util/Utils.java @@ -40,7 +40,9 @@ public class Utils { public static ReferenceType newReferenceType(Map 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 cleanseAttributes(Attributes in) { diff --git a/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java b/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java index e81916f2f1..fd05d24156 100644 --- a/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java +++ b/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgHandler.java @@ -71,9 +71,9 @@ public class OrgHandler extends ParseSax.HandlerWithResult { @Override public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { Map 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 { 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(); diff --git a/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java b/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java index 47b28735f6..855785f9ef 100755 --- a/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java +++ b/common/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgListHandler.java @@ -45,7 +45,7 @@ public class OrgListHandler extends ParseSax.HandlerWithResult 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) { diff --git a/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgHandlerTest.java b/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgHandlerTest.java index fdf2fdafe7..a22264ad7e 100644 --- a/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgHandlerTest.java +++ b/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgHandlerTest.java @@ -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 + . of(), ImmutableMap. 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. of(), null, ImmutableSet. of())); + + } } diff --git a/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgListHandlerTest.java b/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgListHandlerTest.java index 7cdf3fb7f0..f295c29413 100644 --- a/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgListHandlerTest.java +++ b/common/vcloud/src/test/java/org/jclouds/vcloud/xml/OrgListHandlerTest.java @@ -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 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")))); + } } diff --git a/common/vcloud/src/test/resources/org-savvis.xml b/common/vcloud/src/test/resources/org-savvis.xml new file mode 100644 index 0000000000..c2517c7046 --- /dev/null +++ b/common/vcloud/src/test/resources/org-savvis.xml @@ -0,0 +1,13 @@ + + + + Gravitant Inc + \ No newline at end of file diff --git a/sandbox-providers/savvis/src/test/resources/orglist.xml b/common/vcloud/src/test/resources/orglist-savvis.xml similarity index 100% rename from sandbox-providers/savvis/src/test/resources/orglist.xml rename to common/vcloud/src/test/resources/orglist-savvis.xml diff --git a/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/SymphonyVPDCAsyncClient.java b/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/SymphonyVPDCAsyncClient.java index cf995cdf4c..9273d42ec5 100644 --- a/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/SymphonyVPDCAsyncClient.java +++ b/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/SymphonyVPDCAsyncClient.java @@ -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 - */ - @GET - @XMLResponseParser(OrgHandler.class) - @ExceptionParser(ReturnNullOnNotFoundOr404.class) - ListenableFuture 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 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 getOrg(@EndpointParam URI orgId); } diff --git a/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/config/SymphonyVPDCRestClientModule.java b/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/config/SymphonyVPDCRestClientModule.java index f0bea68bd4..d65fac14b2 100644 --- a/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/config/SymphonyVPDCRestClientModule.java +++ b/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/config/SymphonyVPDCRestClientModule.java @@ -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 diff --git a/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/xml/SymphonyVPDCOrgListHandler.java b/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/xml/SymphonyVPDCOrgListHandler.java deleted file mode 100644 index 814f874454..0000000000 --- a/sandbox-providers/savvis/src/main/java/org/jclouds/savvis/xml/SymphonyVPDCOrgListHandler.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * - * Copyright (C) 2010 Cloud Conscious, LLC. - * - * ==================================================================== - * 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 org = Maps.newHashMap(); - - public Map getResult() { - return org; - } - - @Override - public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { - Map 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); - } - } - } - } - -} diff --git a/sandbox-providers/savvis/src/test/java/org/jclouds/savvis/SymphonyVPDCAsyncClientTest.java b/sandbox-providers/savvis/src/test/java/org/jclouds/savvis/SymphonyVPDCAsyncClientTest.java index f1a61235c8..3855497930 100644 --- a/sandbox-providers/savvis/src/test/java/org/jclouds/savvis/SymphonyVPDCAsyncClientTest.java +++ b/sandbox-providers/savvis/src/test/java/org/jclouds/savvis/SymphonyVPDCAsyncClientTest.java @@ -219,7 +219,7 @@ public class SymphonyVPDCAsyncClientTest extends RestClientTest - * - * ==================================================================== - * 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 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")))); - } -}