View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging.serviceexporters;
17  
18  import org.apache.cxf.Bus;
19  import org.apache.cxf.aegis.databinding.AegisDatabinding;
20  import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
21  import org.apache.cxf.frontend.ServerFactoryBean;
22  import org.apache.cxf.interceptor.LoggingInInterceptor;
23  import org.apache.cxf.interceptor.LoggingOutInterceptor;
24  import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
25  import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
26  import org.apache.log4j.Logger;
27  import org.apache.ws.security.WSConstants;
28  import org.apache.ws.security.handler.WSHandlerConstants;
29  import org.kuali.rice.core.api.exception.RiceRuntimeException;
30  import org.kuali.rice.ksb.api.bus.ServiceDefinition;
31  import org.kuali.rice.ksb.api.bus.support.SoapServiceDefinition;
32  import org.kuali.rice.ksb.impl.cxf.interceptors.ImmutableCollectionsInInterceptor;
33  import org.kuali.rice.ksb.messaging.servicehandlers.BasicAuthenticationHandler;
34  import org.kuali.rice.ksb.security.soap.CXFWSS4JInInterceptor;
35  import org.kuali.rice.ksb.security.soap.CXFWSS4JOutInterceptor;
36  
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  /**
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class SOAPServiceExporter extends AbstractWebServiceExporter implements ServiceExporter {
45  
46  	static final Logger LOG = Logger.getLogger(SOAPServiceExporter.class);
47  		
48  	public SOAPServiceExporter(SoapServiceDefinition serviceDefinition, Bus cxfBus) {
49  	    super(serviceDefinition, cxfBus);
50  	}
51  
52  	/**
53  	 * This publishes the cxf service onto the cxf bus.
54  	 * 
55  	 * @param serviceImpl
56  	 * @throws Exception
57  	 */
58  	@Override
59      public void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) {
60  		ServerFactoryBean svrFactory;
61  		
62  		SoapServiceDefinition soapServiceDefinition = (SoapServiceDefinition)serviceDefinition;
63  		
64  		//Use the correct bean factory depending on pojo service or jaxws service
65  		if (soapServiceDefinition.isJaxWsService()){
66  			LOG.info("Creating JaxWsService " + soapServiceDefinition.getServiceName());
67  			svrFactory = new JaxWsServerFactoryBean();
68  		} else {
69  			svrFactory = new ServerFactoryBean();
70  			
71  			//JAXB Binding not supported for pojo service (CXF-897)
72  			svrFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
73  		}
74  	
75  		svrFactory.setBus(getCXFBus());
76  		svrFactory.setServiceName(soapServiceDefinition.getServiceName());
77  		svrFactory.setAddress(address);
78  		svrFactory.setPublishedEndpointUrl(soapServiceDefinition.getEndpointUrl().toExternalForm());
79  		svrFactory.setServiceBean(serviceImpl);
80  		
81  		try {
82  			svrFactory.setServiceClass(Class.forName(soapServiceDefinition.getServiceInterface()));
83  		} catch (ClassNotFoundException e) {
84  			throw new RiceRuntimeException("Failed to publish service " + soapServiceDefinition.getServiceName() + " because service interface could not be loaded: " + soapServiceDefinition.getServiceInterface(), e);
85  		}
86  		
87  		//Set logging and security interceptors
88  		if (soapServiceDefinition.isBasicAuthentication()) {
89  			Map<String, Object> properties = new HashMap<String, Object>();
90  			properties.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
91  			properties.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
92  			BasicAuthenticationHandler authenticationHandler = new BasicAuthenticationHandler(
93  				soapServiceDefinition.getServiceNameSpaceURI(), serviceDefinition.getServiceName());
94  			properties.put(WSHandlerConstants.PW_CALLBACK_REF, authenticationHandler);
95  			svrFactory.getInInterceptors().add(new WSS4JInInterceptor(properties));
96  			svrFactory.getInInterceptors().add(new SAAJInInterceptor());
97  		} else {
98  			svrFactory.getInInterceptors().add(new CXFWSS4JInInterceptor(soapServiceDefinition.getBusSecurity()));
99  			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 }