001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import javax.wsdl.Definition;
023: import javax.wsdl.Import;
024: import javax.wsdl.WSDLException;
025: import javax.wsdl.factory.WSDLFactory;
026: import javax.wsdl.xml.WSDLWriter;
027: import javax.xml.namespace.QName;
028:
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Node;
031:
032: import org.apache.cxf.Bus;
033: import org.apache.cxf.BusException;
034: import org.apache.cxf.BusFactory;
035: import org.apache.cxf.aegis.databinding.AegisDatabinding;
036: import org.apache.cxf.aegis.databinding.AegisServiceConfiguration;
037: import org.apache.cxf.aegis.util.XmlConstants;
038: import org.apache.cxf.binding.BindingFactoryManager;
039: import org.apache.cxf.binding.soap.SoapBindingFactory;
040: import org.apache.cxf.binding.soap.SoapTransportFactory;
041: import org.apache.cxf.bus.extension.ExtensionManagerBus;
042: import org.apache.cxf.endpoint.Server;
043: import org.apache.cxf.endpoint.ServerRegistry;
044: import org.apache.cxf.frontend.AbstractEndpointFactory;
045: import org.apache.cxf.frontend.ServerFactoryBean;
046: import org.apache.cxf.service.Service;
047: import org.apache.cxf.test.AbstractCXFTest;
048: import org.apache.cxf.transport.ConduitInitiatorManager;
049: import org.apache.cxf.transport.DestinationFactoryManager;
050: import org.apache.cxf.transport.local.LocalTransportFactory;
051: import org.apache.cxf.wsdl.WSDLManager;
052: import org.apache.cxf.wsdl11.ServiceWSDLBuilder;
053: import org.apache.cxf.wsdl11.WSDLDefinitionBuilder;
054: import org.apache.cxf.wsdl11.WSDLManagerImpl;
055: import org.junit.Before;
056:
057: public abstract class AbstractAegisTest extends AbstractCXFTest {
058: protected LocalTransportFactory localTransport;
059:
060: @Before
061: public void setUp() throws Exception {
062: super .setUpBus();
063:
064: SoapBindingFactory bindingFactory = new SoapBindingFactory();
065:
066: bus.getExtension(BindingFactoryManager.class)
067: .registerBindingFactory(
068: "http://schemas.xmlsoap.org/wsdl/soap/",
069: bindingFactory);
070:
071: DestinationFactoryManager dfm = bus
072: .getExtension(DestinationFactoryManager.class);
073:
074: SoapTransportFactory soapDF = new SoapTransportFactory();
075: soapDF.setBus(bus);
076: dfm.registerDestinationFactory(
077: "http://schemas.xmlsoap.org/wsdl/soap/", soapDF);
078: dfm.registerDestinationFactory(
079: "http://schemas.xmlsoap.org/soap/", soapDF);
080: dfm.registerDestinationFactory(
081: "http://cxf.apache.org/transports/local", soapDF);
082:
083: localTransport = new LocalTransportFactory();
084: dfm.registerDestinationFactory(
085: "http://schemas.xmlsoap.org/soap/http", localTransport);
086: dfm.registerDestinationFactory(
087: "http://schemas.xmlsoap.org/wsdl/soap/http",
088: localTransport);
089: dfm.registerDestinationFactory(
090: "http://cxf.apache.org/bindings/xformat",
091: localTransport);
092: dfm.registerDestinationFactory(
093: "http://cxf.apache.org/transports/local",
094: localTransport);
095:
096: ConduitInitiatorManager extension = bus
097: .getExtension(ConduitInitiatorManager.class);
098: extension.registerConduitInitiator(
099: LocalTransportFactory.TRANSPORT_ID, localTransport);
100: extension
101: .registerConduitInitiator(
102: "http://schemas.xmlsoap.org/wsdl/soap/",
103: localTransport);
104: extension.registerConduitInitiator(
105: "http://schemas.xmlsoap.org/soap/http", localTransport);
106: extension.registerConduitInitiator(
107: "http://schemas.xmlsoap.org/soap/", localTransport);
108:
109: bus.setExtension(new WSDLManagerImpl(), WSDLManager.class);
110:
111: addNamespace("wsdl", XmlConstants.WSDL11_NS);
112: addNamespace("wsdlsoap", XmlConstants.WSDL11_SOAP_NS);
113: addNamespace("xsd", XmlConstants.XSD);
114:
115: }
116:
117: @Override
118: protected Bus createBus() throws BusException {
119: ExtensionManagerBus bus = new ExtensionManagerBus();
120: BusFactory.setDefaultBus(bus);
121: return bus;
122: }
123:
124: protected Node invoke(String service, String message)
125: throws Exception {
126: return invoke("local://" + service,
127: LocalTransportFactory.TRANSPORT_ID, message);
128: }
129:
130: public Server createService(Class serviceClass, QName name) {
131: return createService(serviceClass,
132: serviceClass.getSimpleName(), name);
133: }
134:
135: public Server createService(Class serviceClass, String address,
136: QName name) {
137: ServerFactoryBean sf = createServiceFactory(serviceClass,
138: address, name);
139: return sf.create();
140: }
141:
142: protected ServerFactoryBean createServiceFactory(
143: Class serviceClass, String address, QName name) {
144: ServerFactoryBean sf = new ServerFactoryBean();
145: sf.setServiceClass(serviceClass);
146: sf.getServiceFactory().setServiceName(name);
147: sf.setAddress("local://" + address);
148: setupAegis(sf);
149: return sf;
150: }
151:
152: protected void setupAegis(AbstractEndpointFactory sf) {
153: sf.getServiceFactory().getServiceConfigurations().add(0,
154: new AegisServiceConfiguration());
155: sf.getServiceFactory().setDataBinding(new AegisDatabinding());
156: }
157:
158: protected Collection<Document> getWSDLDocuments(String string)
159: throws WSDLException {
160: WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
161:
162: Collection<Document> docs = new ArrayList<Document>();
163: Definition definition = getWSDLDefinition(string);
164: if (definition == null) {
165: return null;
166: }
167: docs.add(writer.getDocument(definition));
168:
169: for (Import wsdlImport : WSDLDefinitionBuilder
170: .getImports(definition)) {
171: docs.add(writer.getDocument(wsdlImport.getDefinition()));
172: }
173: return docs;
174: }
175:
176: protected Definition getWSDLDefinition(String string)
177: throws WSDLException {
178: ServerRegistry svrMan = getBus().getExtension(
179: ServerRegistry.class);
180: for (Server s : svrMan.getServers()) {
181: Service svc = s.getEndpoint().getService();
182: if (svc.getName().getLocalPart().equals(string)) {
183: ServiceWSDLBuilder builder = new ServiceWSDLBuilder(
184: bus, svc.getServiceInfos());
185: return builder.build();
186: }
187: }
188: return null;
189:
190: }
191:
192: protected Document getWSDLDocument(String string)
193: throws WSDLException {
194: Definition definition = getWSDLDefinition(string);
195: if (definition == null) {
196: return null;
197: }
198: WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
199: return writer.getDocument(definition);
200: }
201: }
|