1 /***
2 *
3 * Copyright 2005 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activesoap.wsif;
19
20 import org.apache.wsif.WSIFException;
21 import org.apache.wsif.WSIFPort;
22 import org.apache.wsif.wsdl.extensions.java.JavaAddress;
23 import org.apache.wsif.providers.WSIFDynamicTypeMap;
24 import org.apache.wsif.spi.WSIFProvider;
25 import org.codehaus.activesoap.RestClient;
26 import org.codehaus.activesoap.RestService;
27 import org.codehaus.activesoap.SoapClient;
28 import org.codehaus.activesoap.SoapService;
29 import org.codehaus.activesoap.handler.EchoHandler;
30 import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
31 import org.codehaus.activesoap.handler.xmlbeans.XMLBeansAnyElementMarshaler;
32 import org.codehaus.activesoap.handler.xmlbeans.XMLBeansHelper;
33 import org.codehaus.activesoap.handler.xmlbeans.XMLBeansRegistry;
34 import org.codehaus.activesoap.transport.LocalTransportClient;
35 import org.codehaus.activesoap.transport.TransportClient;
36 import org.codehaus.activesoap.transport.http.HttpTransportClient;
37 import org.w3c.dom.Element;
38
39 import javax.wsdl.Definition;
40 import javax.wsdl.Port;
41 import javax.wsdl.Service;
42 import javax.wsdl.extensions.UnknownExtensibilityElement;
43 import java.util.List;
44 import java.util.Iterator;
45 import java.net.MalformedURLException;
46
47 /***
48 * A <a href="http://ws.apache.org/wsif/">WSIF</a> provider for <a href="http://activesoap.codehaus.org/">ActiveSOAP</a>
49 *
50 * @version $Revision: 1.3 $
51 */
52 public abstract class ASProviderSupport implements WSIFProvider {
53 public WSIFPort createDynamicWSIFPort(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException {
54 RestClient client = createClient(definition, service, port, wsifDynamicTypeMap);
55 AnyElementMarshaler marshaler = createMarshaler(definition, service, port, wsifDynamicTypeMap);
56 return new ASPort(definition, service, port, wsifDynamicTypeMap, client, marshaler);
57 }
58
59
60
61 protected RestClient createClient(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException {
62 RestService restService = createService(definition, service, port, wsifDynamicTypeMap);
63 TransportClient transport = createTransport(definition, service, port, wsifDynamicTypeMap, restService);
64 SoapClient client = new SoapClient(transport, (SoapService) restService);
65 if (client.getClientHandler() == null) {
66 applyClientPolicies(client);
67 }
68 return client;
69 }
70
71 protected abstract void applyClientPolicies(SoapClient client);
72
73 protected abstract RestService createService(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException;
74
75 protected abstract AnyElementMarshaler createMarshaler(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap);
76
77 protected TransportClient createTransport(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap, RestService restService) throws WSIFException {
78 List list = port.getExtensibilityElements();
79 for (Iterator iter = list.iterator(); iter.hasNext();) {
80 Object object = iter.next();
81 if (object instanceof UnknownExtensibilityElement) {
82 UnknownExtensibilityElement uElement = (UnknownExtensibilityElement) object;
83 Element element = uElement.getElement();
84 String http = element.getAttribute("location");
85 if (http != null && http.length() > 0) {
86 try {
87 return new HttpTransportClient(http);
88 }
89 catch (MalformedURLException e) {
90 throw new WSIFException("Failed to create HTTP transport for URL: " + http + ". Reason: " + e, e);
91 }
92 }
93 }
94 }
95 return new LocalTransportClient(restService);
96 }
97
98 protected String getServiceClassName(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) {
99 String className = null;
100 List list = port.getExtensibilityElements();
101 for (Iterator iter = list.iterator(); iter.hasNext();) {
102 Object object = iter.next();
103 if (object instanceof JavaAddress) {
104 JavaAddress address = (JavaAddress) object;
105 className = address.getClassName();
106 }
107 else if (object instanceof UnknownExtensibilityElement) {
108 UnknownExtensibilityElement uElement = (UnknownExtensibilityElement) object;
109 Element element = uElement.getElement();
110 className = element.getAttribute("className");
111 }
112 if (className != null && className.length() > 0) {
113 break;
114 }
115 }
116 return className;
117 }
118
119 protected Class loadClass(String className) throws ClassNotFoundException {
120 return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
121 }
122 }