1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
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
61
62
63
64 public Object getService() {
65 ClientProxyFactoryBean clientFactory;
66
67
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
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
96
97
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 }