View Javadoc

1   /**
2    * Copyright 2005-2015 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.serviceconnectors;
17  
18  import org.apache.cxf.aegis.databinding.AegisDatabinding;
19  import org.apache.cxf.frontend.ClientProxyFactoryBean;
20  import org.apache.cxf.interceptor.LoggingInInterceptor;
21  import org.apache.cxf.interceptor.LoggingOutInterceptor;
22  import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
23  import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
24  import org.apache.log4j.Logger;
25  import org.apache.ws.security.WSConstants;
26  import org.apache.ws.security.handler.WSHandlerConstants;
27  import org.kuali.rice.core.api.exception.RiceRuntimeException;
28  import org.kuali.rice.ksb.api.bus.support.SoapServiceConfiguration;
29  import org.kuali.rice.ksb.impl.cxf.interceptors.ImmutableCollectionsInInterceptor;
30  import org.kuali.rice.ksb.messaging.servicehandlers.BasicAuthenticationPasswordHandler;
31  import org.kuali.rice.ksb.security.soap.CXFWSS4JInInterceptor;
32  import org.kuali.rice.ksb.security.soap.CXFWSS4JOutInterceptor;
33  import org.kuali.rice.ksb.security.soap.CredentialsOutHandler;
34  import org.kuali.rice.ksb.service.BasicAuthenticationCredentials;
35  import org.kuali.rice.ksb.service.KSBServiceLocator;
36  
37  import java.net.URL;
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  /**
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   * @since 0.9
45   */
46  public class SOAPConnector extends AbstractServiceConnector {
47  
48      private static final Logger LOG = Logger.getLogger(SOAPConnector.class);
49  
50  	public SOAPConnector(final SoapServiceConfiguration serviceConfiguration, final URL alternateEndpointUrl) {
51  		super(serviceConfiguration, alternateEndpointUrl);
52  	}
53  
54      @Override
55  	public SoapServiceConfiguration getServiceConfiguration() {
56  		return (SoapServiceConfiguration) super.getServiceConfiguration();
57  	}
58  	
59  	/**
60  	 * This overridden method returns a CXF client praoxy for web service.
61  	 * 
62  	 * @see org.kuali.rice.ksb.messaging.serviceconnectors.ServiceConnector#getService()
63  	 */
64  	public Object getService() {
65  		ClientProxyFactoryBean clientFactory;
66  		
67  		//Use the correct bean factory depending on pojo service or jaxws service
68  		if (getServiceConfiguration().isJaxWsService()){
69  			clientFactory = new JaxWsProxyFactoryBean();
70  		} else {
71  			clientFactory = new ClientProxyFactoryBean();
72  			clientFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
73  		}		
74  
75  		try {
76  			clientFactory.setServiceClass(Class.forName(getServiceConfiguration().getServiceInterface()));
77  		} catch (ClassNotFoundException e) {
78  			throw new RiceRuntimeException("Failed to connect to soap service " + getServiceConfiguration().getServiceName() + " because failed to load interface class: " + getServiceConfiguration().getServiceInterface(), e);
79  		}
80  		clientFactory.setBus(KSBServiceLocator.getCXFBus());
81  		clientFactory.setServiceName(getServiceConfiguration().getServiceName());
82  		clientFactory.setAddress(getActualEndpointUrl().toExternalForm());
83  		
84  		//Set logging, transformation, and security interceptors
85          clientFactory.getOutInterceptors().add(new LoggingOutInterceptor());
86          clientFactory.getOutFaultInterceptors().add(new LoggingOutInterceptor());
87          CXFWSS4JOutInterceptor outSecurityInterceptor = new CXFWSS4JOutInterceptor(getServiceConfiguration().getBusSecurity());
88          clientFactory.getOutInterceptors().add(outSecurityInterceptor);
89          clientFactory.getOutFaultInterceptors().add(outSecurityInterceptor);
90          if (getCredentialsSource() != null) {
91  			clientFactory.getOutInterceptors().add(new CredentialsOutHandler(getCredentialsSource(), getServiceConfiguration()));
92  		}
93  
94  		/**
95  		 * Check the basic authentication service for credentials when connecting to a service which requires basic
96  		 * authentication.  If credentials are found in the service then this populates the WSSecurity header with
97  		 * the correct information.
98  		 */
99  		SoapServiceConfiguration soapServiceConfiguration = this.getServiceConfiguration();
100 
101 		if (soapServiceConfiguration.isBasicAuthentication()) {
102 			BasicAuthenticationCredentials credentials =
103 					KSBServiceLocator.getBasicAuthenticationService().getConnectionCredentials(
104 							soapServiceConfiguration.getServiceName().getNamespaceURI(),
105 							soapServiceConfiguration.getServiceName().getLocalPart());
106 			if (credentials != null) {
107 				Map<String, Object> outProps = new HashMap<String, Object>();
108 				outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
109 				outProps.put(WSHandlerConstants.USER, credentials.getUsername());
110 				outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
111 				outProps.put(WSHandlerConstants.PW_CALLBACK_REF, new BasicAuthenticationPasswordHandler(
112 						credentials.getPassword()));
113 				WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
114 				clientFactory.getOutInterceptors().add(wssOut);
115 				clientFactory.getOutFaultInterceptors().add(wssOut);
116 			}
117 		}
118 
119 	    clientFactory.getInInterceptors().add(new LoggingInInterceptor());
120         clientFactory.getInFaultInterceptors().add(new LoggingInInterceptor());
121         CXFWSS4JInInterceptor inSecurityInterceptor = new CXFWSS4JInInterceptor(getServiceConfiguration().getBusSecurity());
122         clientFactory.getInInterceptors().add(inSecurityInterceptor);
123         clientFactory.getInFaultInterceptors().add(inSecurityInterceptor);
124         clientFactory.getInInterceptors().add(new ImmutableCollectionsInInterceptor());
125 
126 		
127 		Object service = clientFactory.create();		
128 		return getServiceProxyWithFailureMode(service, getServiceConfiguration());
129 	}	
130 }