001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.wsrp;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.Iterator;
029:
030: import javax.wsdl.Definition;
031: import javax.wsdl.Port;
032: import javax.wsdl.Service;
033: import javax.wsdl.extensions.ExtensibilityElement;
034: import javax.wsdl.extensions.soap.SOAPAddress;
035: import javax.wsdl.factory.WSDLFactory;
036: import javax.wsdl.xml.WSDLLocator;
037: import javax.xml.namespace.QName;
038:
039: import junit.framework.Test;
040:
041: import org.jboss.logging.Logger;
042: import org.jboss.test.webservice.WebserviceTestBase;
043: import org.xml.sax.InputSource;
044:
045: //$Id: WSRPWSDLToEndpointTestCase.java 62137 2007-04-05 16:07:01Z fnasser@redhat.com $
046:
047: /**
048: * Given a WSRP wsdl, derive the endpoint urls for the various
049: * wsrp services
050: * @author <a href="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
051: * @since May 5, 2006
052: * @version $Revision: 62137 $
053: */
054: public class WSRPWSDLToEndpointTestCase extends WebserviceTestBase {
055: private static Logger log = Logger
056: .getLogger(WSRPWSDLToEndpointTestCase.class);
057:
058: /**
059: * deploy the test archives
060: */
061: public static Test suite() throws Exception {
062: return getDeploySetup(WSRPWSDLToEndpointTestCase.class,
063: "wsrp.war");
064: }
065:
066: public WSRPWSDLToEndpointTestCase(String name) {
067: super (name);
068: }
069:
070: public void testEndpointGrab() throws Exception {
071: String wsdl_url = "http://" + this .getServerHost()
072: + ":8080/wsrp/MarkupService?wsdl";
073: Definition def = getWSDLDefinition(new URL(wsdl_url));
074: Service serve = def.getService(new QName(
075: "urn:oasis:names:tc:wsrp:v1:wsdl", "WSRPService"));
076: assertMarkupService(serve);
077: assertPortletManagementService(serve);
078: assertServiceDescriptionService(serve);
079: assertRegistrationService(serve);
080: }
081:
082: //Requires wsdl4j
083: private Definition getWSDLDefinition(URL url) throws Exception {
084: WSDLFactory wsdlFactory = WSDLFactory.newInstance();
085: javax.wsdl.xml.WSDLReader wsdlReader = wsdlFactory
086: .newWSDLReader();
087: return wsdlReader.readWSDL(new WSDLLocatorImpl(url));
088: }
089:
090: private void assertMarkupService(Service serve) {
091: Port markupPort = serve.getPort("WSRPMarkupService");
092: if (markupPort == null)
093: markupPort = serve.getPort("WSRPBaseService");
094: assertNotNull("MarkupService Port is not null", markupPort);
095: String markupEndpoint = getLocation(markupPort);
096: assertTrue("MarkupService endpoint is not null",
097: markupEndpoint != null
098: && markupEndpoint.indexOf("MarkupService") > 0);
099: }
100:
101: private void assertPortletManagementService(Service serve) {
102: Port pmPort = serve.getPort("WSRPPortletManagementService");
103: assertNotNull("WSRPPortletManagementService Port is not null",
104: pmPort);
105: String pmEndpoint = getLocation(pmPort);
106: assertTrue(
107: "WSRPPortletManagementService endpoint is not null",
108: pmEndpoint != null
109: && pmEndpoint
110: .indexOf("PortletManagementService") > 0);
111: }
112:
113: private void assertServiceDescriptionService(Service serve) {
114: Port sdPort = serve.getPort("WSRPServiceDescriptionService");
115: assertNotNull("WSRPServiceDescriptionService Port is not null",
116: sdPort);
117: String sdEndpoint = getLocation(sdPort);
118: assertTrue(
119: "WSRPServiceDescriptionService endpoint is not null",
120: sdEndpoint != null
121: && sdEndpoint
122: .indexOf("ServiceDescriptionService") > 0);
123: }
124:
125: private void assertRegistrationService(Service serve) {
126: Port rsPort = serve.getPort("WSRPRegistrationService");
127: assertNotNull("WSRPRegistrationService Port is not null",
128: rsPort);
129: String rsEndpoint = getLocation(rsPort);
130: assertTrue(
131: "WSRPRegistrationService endpoint is not null",
132: rsEndpoint != null
133: && rsEndpoint.indexOf("RegistrationService") > 0);
134: }
135:
136: /* A WSDLLocator that can handle wsdl imports
137: */
138: public static class WSDLLocatorImpl implements WSDLLocator {
139: private URL wsdlURL;
140: private String latestImportURI;
141:
142: public WSDLLocatorImpl(URL wsdlFile) {
143: if (wsdlFile == null)
144: throw new IllegalArgumentException(
145: "WSDL file argument cannot be null");
146:
147: this .wsdlURL = wsdlFile;
148: }
149:
150: public InputSource getBaseInputSource() {
151: log.trace("getBaseInputSource [wsdlUrl=" + wsdlURL + "]");
152: try {
153: InputStream is = wsdlURL.openStream();
154: if (is == null)
155: throw new IllegalArgumentException(
156: "Cannot obtain wsdl from [" + wsdlURL + "]");
157:
158: return new InputSource(is);
159: } catch (IOException e) {
160: throw new RuntimeException("Cannot access wsdl from ["
161: + wsdlURL + "], " + e.getMessage());
162: }
163: }
164:
165: public String getBaseURI() {
166: return wsdlURL.toExternalForm();
167: }
168:
169: public InputSource getImportInputSource(String parent,
170: String resource) {
171: log.trace("getImportInputSource [parent=" + parent
172: + ",resource=" + resource + "]");
173:
174: URL parentURL = null;
175: try {
176: parentURL = new URL(parent);
177: } catch (MalformedURLException e) {
178: log.error("Not a valid URL: " + parent);
179: return null;
180: }
181:
182: String wsdlImport = null;
183: String external = parentURL.toExternalForm();
184:
185: // An external URL
186: if (resource.startsWith("http://")
187: || resource.startsWith("https://")) {
188: wsdlImport = resource;
189: }
190:
191: // Absolute path
192: else if (resource.startsWith("/")) {
193: String beforePath = external.substring(0, external
194: .indexOf(parentURL.getPath()));
195: wsdlImport = beforePath + resource;
196: }
197:
198: // A relative path
199: else {
200: String parentDir = external.substring(0, external
201: .lastIndexOf("/"));
202:
203: // remove references to current dir
204: while (resource.startsWith("./"))
205: resource = resource.substring(2);
206:
207: // remove references to parentdir
208: while (resource.startsWith("../")) {
209: parentDir = parentDir.substring(0, parentDir
210: .lastIndexOf("/"));
211: resource = resource.substring(3);
212: }
213:
214: wsdlImport = parentDir + "/" + resource;
215: }
216:
217: try {
218: log.trace("Resolved to: " + wsdlImport);
219: InputStream is = new URL(wsdlImport).openStream();
220: if (is == null)
221: throw new IllegalArgumentException(
222: "Cannot import wsdl from [" + wsdlImport
223: + "]");
224:
225: latestImportURI = wsdlImport;
226: return new InputSource(is);
227: } catch (IOException e) {
228: throw new RuntimeException(
229: "Cannot access imported wsdl [" + wsdlImport
230: + "], " + e.getMessage());
231: }
232: }
233:
234: public String getLatestImportURI() {
235: return latestImportURI;
236: }
237:
238: public void close() {
239: }
240: }
241:
242: private String getLocation(Port port) {
243: String loc = "";
244: Iterator iter = port.getExtensibilityElements().iterator();
245: while (iter.hasNext()) {
246: ExtensibilityElement ext = (ExtensibilityElement) iter
247: .next();
248: if (ext instanceof SOAPAddress) {
249: SOAPAddress add = (SOAPAddress) ext;
250: loc = add.getLocationURI();
251: }
252: }
253: return loc;
254: }
255: }
|