From e3f51bd0305bc4d5f3ecc97517ec2ea74818698c Mon Sep 17 00:00:00 2001 From: adriancole Date: Sat, 23 Mar 2013 17:16:27 -0700 Subject: [PATCH] refactored out common id parsing logic in ultradns --- .../ws/features/ResourceRecordAsyncApi.java | 4 +- .../ws/features/RoundRobinPoolAsyncApi.java | 11 ++-- .../ultradns/ws/features/TaskAsyncApi.java | 4 +- .../xml/{GuidHandler.java => IDHandler.java} | 42 +++++++++---- .../ultradns/ws/xml/RRPoolIDHandler.java | 61 ------------------- .../ws/parse/RunTestResponseTest.java | 4 +- 6 files changed, 42 insertions(+), 84 deletions(-) rename providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/{GuidHandler.java => IDHandler.java} (65%) delete mode 100644 providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java index b2bcd9536c..2fee1d8524 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/ResourceRecordAsyncApi.java @@ -35,7 +35,7 @@ import org.jclouds.ultradns.ws.binders.ZoneAndResourceRecordToXML; import org.jclouds.ultradns.ws.domain.ResourceRecord; import org.jclouds.ultradns.ws.domain.ResourceRecordMetadata; import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth; -import org.jclouds.ultradns.ws.xml.GuidHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.jclouds.ultradns.ws.xml.ResourceRecordListHandler; import com.google.common.collect.FluentIterable; @@ -56,7 +56,7 @@ public interface ResourceRecordAsyncApi { */ @Named("createResourceRecord") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @MapBinder(ZoneAndResourceRecordToXML.class) ListenableFuture create(@PayloadParam("resourceRecord") ResourceRecord toCreate) throws ResourceAlreadyExistsException; diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java index 9e8c9d39c7..e7087f525d 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/RoundRobinPoolAsyncApi.java @@ -33,8 +33,7 @@ import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsExcepti import org.jclouds.ultradns.ws.domain.ResourceRecord; import org.jclouds.ultradns.ws.domain.RoundRobinPool; import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth; -import org.jclouds.ultradns.ws.xml.GuidHandler; -import org.jclouds.ultradns.ws.xml.RRPoolIDHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.jclouds.ultradns.ws.xml.ResourceRecordListHandler; import org.jclouds.ultradns.ws.xml.RoundRobinPoolListHandler; @@ -75,7 +74,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRRLBPool") @POST - @XMLResponseParser(RRPoolIDHandler.class) + @XMLResponseParser(IDHandler.RRPool.class) @Payload("{zoneName}{hostName}{description}1") ListenableFuture createAPoolForHostname(@PayloadParam("description") String name, @PayloadParam("hostName") String hostname) throws ResourceAlreadyExistsException; @@ -85,7 +84,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRecordToRRPool") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @Payload("") ListenableFuture addARecordWithAddressAndTTL(@PayloadParam("lbPoolID") String lbPoolID, @PayloadParam("address") String ipv4Address, @PayloadParam("ttl") int ttl) @@ -115,7 +114,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRRLBPool") @POST - @XMLResponseParser(RRPoolIDHandler.class) + @XMLResponseParser(IDHandler.RRPool.class) @Payload("{zoneName}{hostName}{description}28") ListenableFuture createAAAAPoolForHostname(@PayloadParam("description") String name, @PayloadParam("hostName") String hostname) throws ResourceAlreadyExistsException; @@ -125,7 +124,7 @@ public interface RoundRobinPoolAsyncApi { */ @Named("addRecordToRRPool") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @Payload("") ListenableFuture addAAAARecordWithAddressAndTTL(@PayloadParam("lbPoolID") String lbPoolID, @PayloadParam("address") String ipv6Address, @PayloadParam("ttl") int ttl) diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java index f1a748ee52..654c9ef1be 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/features/TaskAsyncApi.java @@ -31,7 +31,7 @@ import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.XMLResponseParser; import org.jclouds.ultradns.ws.domain.Task; import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth; -import org.jclouds.ultradns.ws.xml.GuidHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.jclouds.ultradns.ws.xml.TaskHandler; import org.jclouds.ultradns.ws.xml.TaskListHandler; @@ -52,7 +52,7 @@ public interface TaskAsyncApi { */ @Named("runTest") @POST - @XMLResponseParser(GuidHandler.class) + @XMLResponseParser(IDHandler.Guid.class) @Payload("{value}") ListenableFuture runTest(@PayloadParam("value") String value); diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/GuidHandler.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java similarity index 65% rename from providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/GuidHandler.java rename to providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java index 7de3d7e953..2c1cd6ebf2 100644 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/GuidHandler.java +++ b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/IDHandler.java @@ -23,33 +23,53 @@ import static org.jclouds.util.SaxUtils.currentOrNull; import static org.jclouds.util.SaxUtils.equalsOrSuffix; import org.jclouds.http.functions.ParseSax; -import org.xml.sax.Attributes; /** * * @author Adrian Cole */ -public class GuidHandler extends ParseSax.HandlerForGeneratedRequestWithResult { +public abstract class IDHandler extends ParseSax.HandlerForGeneratedRequestWithResult { + + public static class Guid extends IDHandler { + public Guid() { + super("guid"); + } + } + + public static class RRPool extends IDHandler { + public RRPool() { + super("RRPoolID"); + } + } + + public static class TCPool extends IDHandler { + public TCPool() { + super("TCPoolID"); + } + } + + private String idElement; + private StringBuilder currentText = new StringBuilder(); - private String guid = null; + private String id = null; + + private IDHandler(String idElement) { + this.idElement = checkNotNull(idElement, "idElement"); + } @Override public String getResult() { try { - return checkNotNull(guid, "guid not present in the response"); + return checkNotNull(id, "%s not present in the response", idElement); } finally { - guid = null; + id = null; } } - @Override - public void startElement(String url, String name, String qName, Attributes attributes) { - } - @Override public void endElement(String uri, String name, String qName) { - if (equalsOrSuffix(qName, "guid")) { - guid = currentOrNull(currentText); + if (equalsOrSuffix(qName, idElement)) { + id = currentOrNull(currentText); } currentText = new StringBuilder(); } diff --git a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java b/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java deleted file mode 100644 index b57f1632c1..0000000000 --- a/providers/ultradns-ws/src/main/java/org/jclouds/ultradns/ws/xml/RRPoolIDHandler.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you 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.ultradns.ws.xml; - -import static com.google.common.base.Preconditions.checkNotNull; -import static org.jclouds.util.SaxUtils.currentOrNull; -import static org.jclouds.util.SaxUtils.equalsOrSuffix; - -import org.jclouds.http.functions.ParseSax; -import org.xml.sax.Attributes; - -/** - * - * @author Adrian Cole - */ -public class RRPoolIDHandler extends ParseSax.HandlerForGeneratedRequestWithResult { - private StringBuilder currentText = new StringBuilder(); - private String rrPoolID = null; - - @Override - public String getResult() { - try { - return checkNotNull(rrPoolID, "RRPoolId not present in the response"); - } finally { - rrPoolID = null; - } - } - - @Override - public void startElement(String url, String name, String qName, Attributes attributes) { - } - - @Override - public void endElement(String uri, String name, String qName) { - if (equalsOrSuffix(qName, "RRPoolID")) { - rrPoolID = currentOrNull(currentText); - } - currentText = new StringBuilder(); - } - - @Override - public void characters(char ch[], int start, int length) { - currentText.append(ch, start, length); - } -} diff --git a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java index 4b33527bf6..f818089f62 100644 --- a/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java +++ b/providers/ultradns-ws/src/test/java/org/jclouds/ultradns/ws/parse/RunTestResponseTest.java @@ -23,7 +23,7 @@ import static org.testng.Assert.assertEquals; import java.io.InputStream; import org.jclouds.http.functions.BaseHandlerTest; -import org.jclouds.ultradns.ws.xml.GuidHandler; +import org.jclouds.ultradns.ws.xml.IDHandler; import org.testng.annotations.Test; /** @@ -35,7 +35,7 @@ public class RunTestResponseTest extends BaseHandlerTest { public void test() { InputStream is = getClass().getResourceAsStream("/taskid.xml"); - GuidHandler handler = injector.getInstance(GuidHandler.class); + IDHandler.Guid handler = injector.getInstance(IDHandler.Guid.class); assertEquals(factory.create(handler).parse(is), "8d7a1725-4f4a-4b70-affa-f01dcce1526e"); } }