Coverage Report - org.kuali.rice.ksb.messaging.serviceconnectors.ServiceConnectorFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceConnectorFactory
0%
0/47
0%
0/34
11.5
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 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  
  * Constructs a ServiceConnector based on the provided
 43  
  * ServiceInfo/ServiceDefinition. Connects that ServiceConnector to the
 44  
  * appropriate CredentialsSource.
 45  
  * <p>
 46  
  * ServiceConnector will fail if a CredentialsSource for the Service Definition
 47  
  * cannot be located.
 48  
  * 
 49  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 50  
  * 
 51  
  * @since 0.9
 52  
  * @see ServiceConnector
 53  
  * @see ServiceDefinition
 54  
  * @see CredentialsSource
 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  
                 // TODO switch this to use serviceConfiguration.getType() at some point in the future and allow for
 84  
                 // this to be easily "pluggable" with new connector types
 85  
                 //
 86  
                 // if set in local mode then preempt any protocol connectors
 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  
 }