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.handler.xstream;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.wsif.WSIFException;
023    import org.apache.wsif.base.WSIFServiceImpl;
024    import org.apache.wsif.providers.WSIFDynamicTypeMap;
025    import org.apache.wsif.wsdl.extensions.java.JavaAddress;
026    import org.codehaus.activesoap.RestService;
027    import org.codehaus.activesoap.SoapClient;
028    import org.codehaus.activesoap.SoapService;
029    import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
030    import org.codehaus.activesoap.wsif.ASProviderSupport;
031    import org.w3c.dom.Element;
032    
033    import javax.wsdl.Definition;
034    import javax.wsdl.Port;
035    import javax.wsdl.Service;
036    import javax.wsdl.extensions.UnknownExtensibilityElement;
037    import java.util.Iterator;
038    import java.util.List;
039    
040    /**
041     * A <a href="http://ws.apache.org/wsif/">WSIF</a> provider for <a href="http://activesoap.codehaus.org/">ActiveSOAP</a>
042     * using <a href="http://xstream.codehaus.org/">XStream</a> as the mershalling layer
043     *
044     * @version $Revision: 1.2 $
045     */
046    public class XStreamWSIFProvider extends ASProviderSupport {
047        private static final transient Log log = LogFactory.getLog(XStreamWSIFProvider.class);
048    
049        public String[] getAddressNamespaceURIs() {
050            return new String[]{"http://activesoap.codehaus.org/wsif/xstream/1.0/"};
051        }
052    
053        public String[] getBindingNamespaceURIs() {
054            return new String[]{"http://activesoap.codehaus.org/wsif/xstream/1.0/"};
055        }
056    
057        public XStreamWSIFProvider() {
058            WSIFServiceImpl.addExtensionRegistry(new org.apache.wsif.wsdl.extensions.java.JavaExtensionRegistry());
059        }
060    
061        protected void applyClientPolicies(SoapClient client) {
062            XStreamHelper.useXStream(client);
063        }
064    
065        protected RestService createService(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) throws WSIFException {
066    
067            // lets make an echo service
068            XStreamRegistry registry = new XStreamRegistry();
069    
070            String className = getServiceClassName(definition, service, port, wsifDynamicTypeMap);
071            registerService(className, registry);
072    
073            //registry.setBodyHandler(new EchoHandler());
074            SoapService soapService = new SoapService(registry);
075            //soapService.setRepairingNamespace(true);
076            return soapService;
077        }
078    
079        protected void registerService(String className, XStreamRegistry registry) throws WSIFException {
080            log.debug("RegisterService with className: " + className);
081            if (className != null && className.length() > 0) {
082                try {
083                    Class serviceType = loadClass(className);
084                    registry.registerService(serviceType);
085                }
086                catch (Exception e) {
087                    throw new WSIFException("Failed to find class: " + className + ". Reason: " + e, e);
088                }
089            }
090        }
091    
092        protected AnyElementMarshaler createMarshaler(Definition definition, Service service, Port port, WSIFDynamicTypeMap wsifDynamicTypeMap) {
093            return new XStreamAnyElementMarshaler();
094        }
095    }