001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.messaging.serviceexporters;
017
018import org.apache.cxf.Bus;
019import org.apache.cxf.aegis.databinding.AegisDatabinding;
020import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
021import org.apache.cxf.frontend.ServerFactoryBean;
022import org.apache.cxf.interceptor.LoggingInInterceptor;
023import org.apache.cxf.interceptor.LoggingOutInterceptor;
024import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
025import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
026import org.apache.log4j.Logger;
027import org.apache.wss4j.dom.WSConstants;
028import org.apache.wss4j.dom.handler.WSHandlerConstants;
029import org.kuali.rice.core.api.exception.RiceRuntimeException;
030import org.kuali.rice.ksb.api.bus.ServiceDefinition;
031import org.kuali.rice.ksb.api.bus.support.SoapServiceDefinition;
032import org.kuali.rice.ksb.impl.cxf.interceptors.ImmutableCollectionsInInterceptor;
033import org.kuali.rice.ksb.messaging.servicehandlers.BasicAuthenticationHandler;
034import org.kuali.rice.ksb.security.soap.CXFWSS4JInInterceptor;
035import org.kuali.rice.ksb.security.soap.CXFWSS4JOutInterceptor;
036
037import java.util.HashMap;
038import java.util.Map;
039
040/**
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044public class SOAPServiceExporter extends AbstractWebServiceExporter implements ServiceExporter {
045
046        static final Logger LOG = Logger.getLogger(SOAPServiceExporter.class);
047                
048        public SOAPServiceExporter(SoapServiceDefinition serviceDefinition, Bus cxfBus) {
049            super(serviceDefinition, cxfBus);
050        }
051
052        /**
053         * This publishes the cxf service onto the cxf bus.
054         * 
055         * @param serviceImpl
056         * @throws Exception
057         */
058        @Override
059    public void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) {
060                ServerFactoryBean svrFactory;
061                
062                SoapServiceDefinition soapServiceDefinition = (SoapServiceDefinition)serviceDefinition;
063                
064                //Use the correct bean factory depending on pojo service or jaxws service
065                if (soapServiceDefinition.isJaxWsService()){
066                        LOG.info("Creating JaxWsService " + soapServiceDefinition.getServiceName());
067                        svrFactory = new JaxWsServerFactoryBean();
068                } else {
069                        svrFactory = new ServerFactoryBean();
070                        
071                        //JAXB Binding not supported for pojo service (CXF-897)
072                        svrFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
073                }
074        
075                svrFactory.setBus(getCXFBus());
076                svrFactory.setServiceName(soapServiceDefinition.getServiceName());
077                svrFactory.setAddress(address);
078                svrFactory.setPublishedEndpointUrl(soapServiceDefinition.getEndpointUrl().toExternalForm());
079                svrFactory.setServiceBean(serviceImpl);
080                
081                try {
082                        svrFactory.setServiceClass(Class.forName(soapServiceDefinition.getServiceInterface()));
083                } catch (ClassNotFoundException e) {
084                        throw new RiceRuntimeException("Failed to publish service " + soapServiceDefinition.getServiceName() + " because service interface could not be loaded: " + soapServiceDefinition.getServiceInterface(), e);
085                }
086                
087                //Set logging and security interceptors
088                if (soapServiceDefinition.isBasicAuthentication()) {
089                        Map<String, Object> properties = new HashMap<String, Object>();
090                        properties.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
091                        properties.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
092                        BasicAuthenticationHandler authenticationHandler = new BasicAuthenticationHandler(
093                                soapServiceDefinition.getServiceNameSpaceURI(), serviceDefinition.getServiceName());
094                        properties.put(WSHandlerConstants.PW_CALLBACK_REF, authenticationHandler);
095                        svrFactory.getInInterceptors().add(new WSS4JInInterceptor(properties));
096                        svrFactory.getInInterceptors().add(new SAAJInInterceptor());
097                } else {
098                        svrFactory.getInInterceptors().add(new CXFWSS4JInInterceptor(soapServiceDefinition.getBusSecurity()));
099                        svrFactory.getOutInterceptors().add(new CXFWSS4JOutInterceptor(soapServiceDefinition.getBusSecurity()));
100                        svrFactory.getInFaultInterceptors().add(new CXFWSS4JInInterceptor(soapServiceDefinition.getBusSecurity()));
101                        svrFactory.getOutFaultInterceptors().add(new CXFWSS4JOutInterceptor(soapServiceDefinition.getBusSecurity()));
102                }
103
104                svrFactory.getInInterceptors().add(new LoggingInInterceptor());
105                svrFactory.getInInterceptors().add(new ImmutableCollectionsInInterceptor());
106
107                svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
108                
109                svrFactory.create();
110        }
111
112}