001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.soap;
018
019 import java.net.URI;
020 import java.util.Collections;
021 import java.util.HashMap;
022 import java.util.Iterator;
023 import java.util.List;
024 import java.util.Map;
025
026 import javax.jbi.component.ComponentContext;
027 import javax.jbi.messaging.MessageExchange.Role;
028 import javax.jbi.messaging.MessageExchange;
029 import javax.jbi.servicedesc.ServiceEndpoint;
030 import javax.wsdl.Definition;
031 import javax.wsdl.Import;
032 import javax.wsdl.Types;
033 import javax.wsdl.WSDLException;
034 import javax.wsdl.extensions.ExtensibilityElement;
035 import javax.wsdl.extensions.ExtensionRegistry;
036 import javax.wsdl.extensions.schema.Schema;
037 import javax.wsdl.extensions.schema.SchemaImport;
038 import javax.wsdl.factory.WSDLFactory;
039 import javax.wsdl.xml.WSDLReader;
040 import javax.wsdl.xml.WSDLWriter;
041 import javax.xml.namespace.QName;
042
043 import org.apache.servicemix.common.JbiConstants;
044 import org.apache.servicemix.common.endpoints.AbstractEndpoint;
045 import org.apache.servicemix.common.security.KeystoreManager;
046 import org.apache.servicemix.common.security.AuthenticationService;
047 import org.apache.servicemix.common.wsdl1.JbiExtension;
048 import org.apache.servicemix.soap.handlers.addressing.AddressingHandler;
049 import org.springframework.core.io.Resource;
050 import org.w3c.dom.Document;
051
052 import com.ibm.wsdl.Constants;
053
054 public abstract class SoapEndpoint extends AbstractEndpoint {
055
056 protected ServiceEndpoint activated;
057 protected SoapExchangeProcessor processor;
058 protected Role role;
059 protected URI defaultMep = JbiConstants.IN_OUT;
060 protected boolean soap;
061 protected String soapVersion;
062 protected Resource wsdlResource;
063 protected QName defaultOperation;
064 protected QName targetInterfaceName;
065 protected QName targetService;
066 protected String targetEndpoint;
067 protected List policies;
068 protected Map wsdls = new HashMap();
069 protected boolean dynamic;
070
071 public SoapEndpoint() {
072 policies = Collections.singletonList(new AddressingHandler());
073 }
074
075 public SoapEndpoint(boolean dynamic) {
076 this();
077 this.dynamic = dynamic;
078 }
079
080 public AuthenticationService getAuthenticationService() {
081 return null;
082 }
083
084 public KeystoreManager getKeystoreManager() {
085 return null;
086 }
087
088 /**
089 * @return the policies
090 */
091 public List getPolicies() {
092 return policies;
093 }
094 /**
095 * @param policies the policies to set
096 */
097 public void setPolicies(List policies) {
098 this.policies = policies;
099 }
100 /**
101 * @return Returns the defaultMep.
102 */
103 public URI getDefaultMep() {
104 return defaultMep;
105 }
106 /**
107 * @param defaultMep The defaultMep to set.
108 */
109 public void setDefaultMep(URI defaultMep) {
110 this.defaultMep = defaultMep;
111 }
112 /**
113 * @return Returns the defaultOperation.
114 */
115 public QName getDefaultOperation() {
116 return defaultOperation;
117 }
118 /**
119 * @param defaultOperation The defaultOperation to set.
120 */
121 public void setDefaultOperation(QName defaultOperation) {
122 this.defaultOperation = defaultOperation;
123 }
124 /**
125 * @return Returns the role.
126 */
127 public Role getRole() {
128 return role;
129 }
130 /**
131 * @param role The role to set.
132 */
133 public void setRole(Role role) {
134 this.role = role;
135 }
136 /**
137 * @return Returns the soap.
138 */
139 public boolean isSoap() {
140 return soap;
141 }
142 /**
143 * @param soap The soap to set.
144 */
145 public void setSoap(boolean soap) {
146 this.soap = soap;
147 }
148 /**
149 * @return Returns the soapVersion.
150 */
151 public String getSoapVersion() {
152 return soapVersion;
153 }
154 /**
155 * @param soapVersion The soapVersion to set.
156 */
157 public void setSoapVersion(String soapVersion) {
158 this.soapVersion = soapVersion;
159 }
160 /**
161 * @return Returns the targetEndpoint.
162 */
163 public String getTargetEndpoint() {
164 return targetEndpoint;
165 }
166 /**
167 * @param targetEndpoint The targetEndpoint to set.
168 */
169 public void setTargetEndpoint(String targetEndpoint) {
170 this.targetEndpoint = targetEndpoint;
171 }
172 /**
173 * @return Returns the targetInterfaceName.
174 */
175 public QName getTargetInterfaceName() {
176 return targetInterfaceName;
177 }
178 /**
179 * @param targetInterfaceName The targetInterfaceName to set.
180 */
181 public void setTargetInterfaceName(QName targetInterfaceName) {
182 this.targetInterfaceName = targetInterfaceName;
183 }
184 /**
185 * @return Returns the targetServiceName.
186 */
187 public QName getTargetService() {
188 return targetService;
189 }
190 /**
191 * @param targetServiceName The targetServiceName to set.
192 */
193 public void setTargetService(QName targetServiceName) {
194 this.targetService = targetServiceName;
195 }
196 /**
197 * @return Returns the wsdlResource.
198 */
199 public Resource getWsdlResource() {
200 return wsdlResource;
201 }
202 /**
203 * @param wsdlResource The wsdlResource to set.
204 */
205 public void setWsdlResource(Resource wsdlResource) {
206 this.wsdlResource = wsdlResource;
207 }
208 /**
209 * @org.apache.xbean.Property alias="role"
210 * @param role
211 */
212 public void setRoleAsString(String role) {
213 if (role == null) {
214 throw new IllegalArgumentException("Role must be specified");
215 } else if (JbiExtension.ROLE_CONSUMER.equals(role)) {
216 setRole(Role.CONSUMER);
217 } else if (JbiExtension.ROLE_PROVIDER.equals(role)) {
218 setRole(Role.PROVIDER);
219 } else {
220 throw new IllegalArgumentException("Unrecognized role: " + role);
221 }
222 }
223
224 /**
225 * Load the wsdl for this endpoint.
226 */
227 protected void loadWsdl() {
228 // Load WSDL from the resource
229 if (description == null && wsdlResource != null) {
230 ClassLoader cl = Thread.currentThread().getContextClassLoader();
231 try {
232 Thread.currentThread().setContextClassLoader(serviceUnit.getConfigurationClassLoader());
233 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
234 reader.setFeature(Constants.FEATURE_VERBOSE, false);
235 Definition def = reader.readWSDL(wsdlResource.getURL().toString());
236 overrideDefinition(def);
237 } catch (Exception e) {
238 logger.warn("Could not load description from resource", e);
239 } finally {
240 Thread.currentThread().setContextClassLoader(cl);
241 }
242 }
243 // If the endpoint is a consumer, try to find
244 // the proxied endpoint description
245 if (description == null && definition == null && getRole() == Role.CONSUMER) {
246 retrieveProxiedEndpointDefinition();
247 }
248 // If the wsdl definition is provided,
249 // convert it to a DOM document
250 if (description == null && definition != null) {
251 try {
252 description = WSDLFactory.newInstance().newWSDLWriter().getDocument(definition);
253 } catch (Exception e) {
254 logger.warn("Could not create document from wsdl description", e);
255 }
256 }
257 // If the dom description is provided
258 // convert it to a WSDL definition
259 if (definition == null && description != null) {
260 try {
261 definition = WSDLFactory.newInstance().newWSDLReader().readWSDL(null, description);
262 } catch (Exception e) {
263 logger.warn("Could not create wsdl definition from dom document", e);
264 }
265 }
266 if (definition != null) {
267 try {
268 mapDefinition(definition);
269 } catch (Exception e) {
270 logger.warn("Could not map wsdl definition to documents", e);
271 }
272 }
273 }
274
275 /**
276 * Create a wsdl definition for a consumer endpoint.
277 * Loads the target endpoint definition and add http binding
278 * informations to it.
279 */
280 protected void retrieveProxiedEndpointDefinition() {
281 if (logger.isDebugEnabled()) {
282 logger.debug("Retrieving proxied endpoint definition");
283 }
284 try {
285 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
286 ServiceEndpoint ep = null;
287 if (targetService != null && targetEndpoint != null) {
288 ep = ctx.getEndpoint(targetService, targetEndpoint);
289 if (ep == null && logger.isDebugEnabled()) {
290 logger.debug("Could not retrieve endpoint targetService/targetEndpoint");
291 }
292 }
293 if (ep == null && targetService != null) {
294 ServiceEndpoint[] eps = ctx.getEndpointsForService(targetService);
295 if (eps != null && eps.length > 0) {
296 ep = eps[0];
297 }
298 if (ep == null && logger.isDebugEnabled()) {
299 logger.debug("Could not retrieve endpoint for targetService");
300 }
301 }
302 if (ep == null && targetInterfaceName != null) {
303 ServiceEndpoint[] eps = ctx.getEndpoints(targetInterfaceName);
304 if (eps != null && eps.length > 0) {
305 ep = eps[0];
306 }
307 if (ep == null && logger.isDebugEnabled()) {
308 logger.debug("Could not retrieve endpoint for targetInterfaceName");
309 }
310 }
311 if (ep == null && service != null && endpoint != null) {
312 ep = ctx.getEndpoint(service, endpoint);
313 if (ep == null && logger.isDebugEnabled()) {
314 logger.debug("Could not retrieve endpoint for service/endpoint");
315 }
316 }
317 if (ep != null) {
318 Document doc = ctx.getEndpointDescriptor(ep);
319 if (doc != null) {
320 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
321 reader.setFeature(Constants.FEATURE_VERBOSE, false);
322 Definition def = reader.readWSDL(null, doc);
323 if (def != null) {
324 overrideDefinition(def);
325 }
326 }
327 }
328 } catch (Exception e) {
329 logger.debug("Unable to retrieve target endpoint descriptor", e);
330 }
331 }
332
333 /* (non-Javadoc)
334 * @see org.servicemix.common.Endpoint#activate()
335 */
336 public void activate() throws Exception {
337 if (dynamic) {
338 if (getRole() == Role.PROVIDER) {
339 processor = createProviderProcessor();
340 } else {
341 processor = createConsumerProcessor();
342 }
343 } else {
344 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
345 loadWsdl();
346 if (getRole() == Role.PROVIDER) {
347 activated = ctx.activateEndpoint(service, endpoint);
348 processor = createProviderProcessor();
349 } else {
350 activated = createExternalEndpoint();
351 ctx.registerExternalEndpoint(activated);
352 processor = createConsumerProcessor();
353 }
354 }
355 processor.init();
356 }
357
358 public void start() throws Exception {
359 processor.start();
360 }
361
362 public void stop() throws Exception {
363 processor.stop();
364 }
365
366 public void process(MessageExchange exchange) throws Exception {
367 processor.process(exchange);
368 }
369
370 /* (non-Javadoc)
371 * @see org.servicemix.common.Endpoint#deactivate()
372 */
373 public void deactivate() throws Exception {
374 processor.shutdown();
375 if (activated != null) {
376 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
377 if (getRole() == Role.PROVIDER) {
378 ServiceEndpoint ep = activated;
379 activated = null;
380 ctx.deactivateEndpoint(ep);
381 } else {
382 ServiceEndpoint ep = activated;
383 activated = null;
384 ctx.deregisterExternalEndpoint(ep);
385 }
386 }
387 }
388
389 protected abstract void overrideDefinition(Definition def) throws Exception;
390
391 protected abstract SoapExchangeProcessor createProviderProcessor();
392
393 protected abstract SoapExchangeProcessor createConsumerProcessor();
394
395 protected abstract ServiceEndpoint createExternalEndpoint();
396
397 protected WSDLReader createWsdlReader() throws WSDLException {
398 WSDLFactory factory = WSDLFactory.newInstance();
399 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();
400 registerExtensions(registry);
401 WSDLReader reader = factory.newWSDLReader();
402 reader.setFeature(Constants.FEATURE_VERBOSE, false);
403 reader.setExtensionRegistry(registry);
404 return reader;
405 }
406
407 protected WSDLWriter createWsdlWriter() throws WSDLException {
408 WSDLFactory factory = WSDLFactory.newInstance();
409 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry();
410 registerExtensions(registry);
411 WSDLWriter writer = factory.newWSDLWriter();
412 //writer.setExtensionRegistry(registry);
413 return writer;
414 }
415
416 protected void registerExtensions(ExtensionRegistry registry) {
417 JbiExtension.register(registry);
418 }
419
420
421 protected void mapDefinition(Definition def) throws WSDLException {
422 wsdls.put("main.wsdl", createWsdlWriter().getDocument(def));
423 mapImports(def);
424 }
425
426 protected void mapImports(Definition def) throws WSDLException {
427 // Add other imports to mapping
428 Map imports = def.getImports();
429 for (Iterator iter = imports.values().iterator(); iter.hasNext();) {
430 List imps = (List) iter.next();
431 for (Iterator iterator = imps.iterator(); iterator.hasNext();) {
432 Import imp = (Import) iterator.next();
433 Definition impDef = imp.getDefinition();
434 String impLoc = imp.getLocationURI();
435 if (impDef != null && impLoc != null && !URI.create(impLoc).isAbsolute()) {
436 wsdls.put(impLoc, createWsdlWriter().getDocument(impDef));
437 mapImports(impDef);
438 }
439 }
440 }
441 // Add schemas to mapping
442 Types types = def.getTypes();
443 if (types != null) {
444 for (Iterator it = types.getExtensibilityElements().iterator(); it.hasNext();) {
445 ExtensibilityElement ee = (ExtensibilityElement) it.next();
446 if (ee instanceof Schema) {
447 Schema schema = (Schema) ee;
448 mapSchemaImport(schema);
449 }
450 }
451 }
452 }
453
454 private void mapSchemaImport(Schema schema) {
455 Map schemaImports = schema.getImports();
456 for (Iterator iter = schemaImports.values().iterator(); iter.hasNext();) {
457 List imps = (List) iter.next();
458 for (Iterator iterator = imps.iterator(); iterator.hasNext();) {
459 SchemaImport schemaImport = (SchemaImport) iterator.next();
460 Schema schemaImp = schemaImport.getReferencedSchema();
461 String schemaLoc = schemaImport.getSchemaLocationURI();
462 if (schemaLoc != null && schemaImp != null && schemaImp.getElement() != null && !URI.create(schemaLoc).isAbsolute()) {
463 wsdls.put(schemaLoc, schemaImp.getElement());
464 mapSchemaImport(schemaImp);
465 }
466 }
467 }
468 }
469
470 /**
471 * @return Returns the wsdls.
472 */
473 public Map getWsdls() {
474 return wsdls;
475 }
476
477 }