001    /** 
002     * 
003     * Copyright 2005 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.codehaus.activesoap.wsif;
019    
020    import org.apache.wsif.WSIFException;
021    import org.apache.wsif.WSIFPort;
022    import org.apache.wsif.wsdl.extensions.java.JavaAddress;
023    import org.apache.wsif.providers.WSIFDynamicTypeMap;
024    import org.apache.wsif.spi.WSIFProvider;
025    import org.codehaus.activesoap.RestClient;
026    import org.codehaus.activesoap.RestService;
027    import org.codehaus.activesoap.SoapClient;
028    import org.codehaus.activesoap.SoapService;
029    import org.codehaus.activesoap.handler.EchoHandler;
030    import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
031    import org.codehaus.activesoap.handler.xmlbeans.XMLBeansAnyElementMarshaler;
032    import org.codehaus.activesoap.handler.xmlbeans.XMLBeansHelper;
033    import org.codehaus.activesoap.handler.xmlbeans.XMLBeansRegistry;
034    import org.codehaus.activesoap.transport.LocalTransportClient;
035    import org.codehaus.activesoap.transport.TransportClient;
036    import org.codehaus.activesoap.transport.http.HttpTransportClient;
037    import org.w3c.dom.Element;
038    
039    import javax.wsdl.Definition;
040    import javax.wsdl.Port;
041    import javax.wsdl.Service;
042    import javax.wsdl.extensions.UnknownExtensibilityElement;
043    import java.util.List;
044    import java.util.Iterator;
045    import java.net.MalformedURLException;
046    
047    /**
048     * A <a href="http://ws.apache.org/wsif/">WSIF</a> provider for <a href="http://activesoap.codehaus.org/">ActiveSOAP</a>
049     *
050     * @version $Revision: 1.3 $
051     */
052    public abstract class ASProviderSupport implements WSIFProvider {
053        public WSIFPort createDynamicWSIFPort(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException {
054            RestClient client = createClient(definition, service, port, wsifDynamicTypeMap);
055            AnyElementMarshaler marshaler = createMarshaler(definition, service, port, wsifDynamicTypeMap);
056            return new ASPort(definition, service, port, wsifDynamicTypeMap, client, marshaler);
057        }
058    
059        // Implementation methods
060        //-------------------------------------------------------------------------
061        protected RestClient createClient(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException {
062            RestService restService = createService(definition, service, port, wsifDynamicTypeMap);
063            TransportClient transport = createTransport(definition, service, port, wsifDynamicTypeMap, restService);
064            SoapClient client = new SoapClient(transport, (SoapService) restService);
065            if (client.getClientHandler() == null) {
066                applyClientPolicies(client);
067            }
068            return client;
069        }
070    
071        protected abstract void applyClientPolicies(SoapClient client);
072    
073        protected abstract RestService createService(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException;
074    
075        protected abstract AnyElementMarshaler createMarshaler(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap);
076    
077        protected TransportClient createTransport(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap, RestService restService) throws WSIFException {
078            List list = port.getExtensibilityElements();
079            for (Iterator iter = list.iterator(); iter.hasNext();) {
080                Object object = iter.next();
081                if (object instanceof UnknownExtensibilityElement) {
082                    UnknownExtensibilityElement uElement = (UnknownExtensibilityElement) object;
083                    Element element = uElement.getElement();
084                    String http = element.getAttribute("location");
085                    if (http != null && http.length() > 0) {
086                        try {
087                            return new HttpTransportClient(http);
088                        }
089                        catch (MalformedURLException e) {
090                            throw new WSIFException("Failed to create HTTP transport for URL: " + http + ". Reason: " + e, e);
091                        }
092                    }
093                }
094            }
095            return new LocalTransportClient(restService);
096        }
097    
098        protected String getServiceClassName(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) {
099            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    }