1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.ksb.messaging.serviceconnectors; |
18 | |
|
19 | |
import java.net.MalformedURLException; |
20 | |
import java.net.URL; |
21 | |
import java.util.List; |
22 | |
import java.util.regex.Matcher; |
23 | |
import java.util.regex.Pattern; |
24 | |
|
25 | |
import org.apache.commons.lang.StringUtils; |
26 | |
import org.apache.log4j.Logger; |
27 | |
import org.kuali.rice.core.api.config.property.Config; |
28 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
29 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
30 | |
import org.kuali.rice.core.api.security.credentials.CredentialsSource; |
31 | |
import org.kuali.rice.core.api.security.credentials.CredentialsSourceFactory; |
32 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
33 | |
import org.kuali.rice.ksb.api.bus.ServiceDefinition; |
34 | |
import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration; |
35 | |
import org.kuali.rice.ksb.api.bus.support.RestServiceConfiguration; |
36 | |
import org.kuali.rice.ksb.api.bus.support.SoapServiceConfiguration; |
37 | |
import org.kuali.rice.ksb.messaging.AlternateEndpoint; |
38 | |
import org.kuali.rice.ksb.messaging.AlternateEndpointLocation; |
39 | |
import org.kuali.rice.ksb.util.KSBConstants; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | public class ServiceConnectorFactory { |
57 | |
|
58 | 0 | private static final Logger LOG = Logger.getLogger(ServiceConnectorFactory.class); |
59 | |
|
60 | |
public static ServiceConnector getServiceConnector( |
61 | |
final ServiceConfiguration serviceConfiguration) { |
62 | 0 | final CredentialsSourceFactory credentialsSourceFactory = (CredentialsSourceFactory) ConfigContext |
63 | |
.getCurrentContextConfig().getObjects() |
64 | |
.get(Config.CREDENTIALS_SOURCE_FACTORY); |
65 | 0 | final CredentialsSource credentialsSource = credentialsSourceFactory != null ? credentialsSourceFactory |
66 | |
.getCredentialsForType(serviceConfiguration.getCredentialsType()) : null; |
67 | 0 | ServiceConnector serviceConnector = null; |
68 | |
|
69 | 0 | if (serviceConfiguration.getCredentialsType() != null && credentialsSource == null) { |
70 | 0 | throw new RiceRuntimeException("Service requires credentials but no factory or CredentialsSource could be located."); |
71 | |
} |
72 | |
|
73 | 0 | String alternateEndpoint = determineAlternateEndpoint(serviceConfiguration); |
74 | 0 | URL alternateEndpointUrl = null; |
75 | 0 | if (!StringUtils.isBlank(alternateEndpoint)) { |
76 | |
try { |
77 | 0 | alternateEndpointUrl = new URL(alternateEndpoint); |
78 | 0 | } catch (MalformedURLException e) { |
79 | 0 | throw new IllegalStateException("Encountered invalid alternate endpoint url: " + alternateEndpoint, e); |
80 | 0 | } |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 0 | if (ConfigContext.getCurrentContextConfig().getDevMode()) { |
88 | 0 | serviceConnector = new BusLocalConnector(serviceConfiguration); |
89 | 0 | } else if (serviceConfiguration instanceof JavaServiceConfiguration) { |
90 | 0 | serviceConnector = new HttpInvokerConnector((JavaServiceConfiguration) serviceConfiguration, alternateEndpointUrl); |
91 | 0 | } else if (serviceConfiguration instanceof SoapServiceConfiguration) { |
92 | 0 | serviceConnector = new SOAPConnector((SoapServiceConfiguration) serviceConfiguration, alternateEndpointUrl); |
93 | 0 | } else if (serviceConfiguration instanceof RestServiceConfiguration) { |
94 | 0 | serviceConnector = new RESTConnector((RestServiceConfiguration) serviceConfiguration, alternateEndpointUrl); |
95 | |
} |
96 | |
|
97 | 0 | if (serviceConnector == null) { |
98 | 0 | throw new RiceRuntimeException("Don't support service type of " + serviceConfiguration); |
99 | |
} |
100 | 0 | serviceConnector.setCredentialsSource(credentialsSource); |
101 | |
|
102 | 0 | return serviceConnector; |
103 | |
} |
104 | |
|
105 | |
public static String determineAlternateEndpoint(ServiceConfiguration serviceConfiguration) { |
106 | 0 | String alternateEndpointUrl = null; |
107 | 0 | List<AlternateEndpointLocation> alternateEndpointLocations = (List<AlternateEndpointLocation>) ConfigContext.getCurrentContextConfig().getObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINT_LOCATIONS); |
108 | 0 | if (alternateEndpointLocations != null) { |
109 | 0 | for (AlternateEndpointLocation alternateEndpointLocation : alternateEndpointLocations) { |
110 | 0 | if (Pattern.matches(".*" + alternateEndpointLocation.getEndpointHostReplacementPattern() + ".*", serviceConfiguration.getEndpointUrl().toExternalForm())) { |
111 | 0 | Pattern myPattern = Pattern.compile(alternateEndpointLocation.getEndpointHostReplacementPattern()); |
112 | 0 | Matcher myMatcher = myPattern.matcher(serviceConfiguration.getEndpointUrl().toExternalForm()); |
113 | 0 | String alternateEndpoint = myMatcher.replaceFirst(alternateEndpointLocation.getEndpointHostReplacementValue()); |
114 | 0 | if ( LOG.isInfoEnabled() ) { |
115 | 0 | LOG.info("Found an alternate url host value (" |
116 | |
+ alternateEndpointLocation.getEndpointHostReplacementValue() + ") for endpoint: " |
117 | |
+ serviceConfiguration.getEndpointUrl() + " -> instead using: " + alternateEndpoint); |
118 | |
} |
119 | 0 | alternateEndpointUrl = alternateEndpoint; |
120 | 0 | break; |
121 | |
} |
122 | |
} |
123 | |
} |
124 | 0 | List<AlternateEndpoint> alternateEndpoints = (List<AlternateEndpoint>) ConfigContext.getCurrentContextConfig().getObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINTS); |
125 | 0 | if (alternateEndpoints != null) { |
126 | 0 | for (AlternateEndpoint alternateEndpoint : alternateEndpoints) { |
127 | 0 | if (Pattern.matches(alternateEndpoint.getEndpointUrlPattern(), serviceConfiguration.getEndpointUrl().toExternalForm())) { |
128 | 0 | if ( LOG.isInfoEnabled() ) { |
129 | 0 | LOG.info("Found an alternate url for endpoint: " + serviceConfiguration.getEndpointUrl() + " -> instead using: " + alternateEndpoint.getActualEndpoint()); |
130 | |
} |
131 | 0 | alternateEndpointUrl = alternateEndpoint.getActualEndpoint(); |
132 | 0 | break; |
133 | |
} |
134 | |
} |
135 | |
} |
136 | 0 | return alternateEndpointUrl; |
137 | |
} |
138 | |
|
139 | |
} |