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.http.client.HttpClient;
19  import org.apache.http.impl.client.HttpClientBuilder;
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration;
23  import org.kuali.rice.ksb.messaging.KSBHttpInvokerProxyFactoryBean;
24  import org.kuali.rice.ksb.messaging.KSBHttpInvokerRequestExecutor;
25  import org.kuali.rice.ksb.security.httpinvoker.AuthenticationCommonsHttpInvokerRequestExecutor;
26  
27  import java.net.URL;
28  
29  /**
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   * @since 0.9
32   */
33  public class HttpInvokerConnector extends AbstractServiceConnector {
34  
35  	private static final Logger LOG = Logger.getLogger(HttpInvokerConnector.class);
36  
37      private static final String HTTP_CLIENT_CONFIG_BEAN = "rice.ksb.httpClientConfigurer";
38  
39      /**
40       * Constructs an HttpInvokerConnector.
41       *
42       * @param serviceConfiguration the JavaServiceConfiguration
43       * @param alternateEndpointUrl an alternate URL to use for the service endpoint
44       */
45      public HttpInvokerConnector(final JavaServiceConfiguration serviceConfiguration, final URL alternateEndpointUrl) {
46  		super(serviceConfiguration, alternateEndpointUrl);
47      }
48  
49      @Override
50  	public JavaServiceConfiguration getServiceConfiguration() {
51  		return (JavaServiceConfiguration) super.getServiceConfiguration();
52  	}
53  	
54  	public Object getService() {
55  	    LOG.debug("Getting connector for endpoint " + getActualEndpointUrl());
56  		KSBHttpInvokerProxyFactoryBean client = new KSBHttpInvokerProxyFactoryBean();
57  		client.setServiceUrl(getActualEndpointUrl().toExternalForm());
58  		client.setServiceConfiguration(getServiceConfiguration());
59  		
60  		KSBHttpInvokerRequestExecutor executor;
61  		
62  		if (getCredentialsSource() != null) {
63  		    executor = new AuthenticationCommonsHttpInvokerRequestExecutor(getHttpClient(), getCredentialsSource(), getServiceConfiguration());
64  		} else {
65  		    executor = new KSBHttpInvokerRequestExecutor(getHttpClient());
66  		}
67  		executor.setSecure(getServiceConfiguration().getBusSecurity());
68  		client.setHttpInvokerRequestExecutor(executor);	
69  		client.afterPropertiesSet();
70  		return getServiceProxyWithFailureMode(client.getObject(), getServiceConfiguration());
71  	}
72  
73  	/**
74  	 * Creates a httpcomponents HttpClient for service invocation.
75       *
76       * <p>The client is configured by the HttpClientConfigurer</p>
77  	 */
78  	public HttpClient getHttpClient() {
79          HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
80  
81          getHttpClientConfigurer().customizeHttpClient(httpClientBuilder);
82  
83          return httpClientBuilder.build();
84  	}
85  
86      /**
87       * Lazy initialization holder class idiom for static fields, see Effective Java item 71
88       */
89      private static class HttpClientConfigurerHolder {
90          static final HttpClientConfigurer httpClientConfigurer =
91                  GlobalResourceLoader.getService(HTTP_CLIENT_CONFIG_BEAN);
92      }
93  
94      /**
95       * Gets the HttpClientConfigurer that will be used to customize http clients.
96       *
97       * <p>On first call, the bean specified by {@link #HTTP_CLIENT_CONFIG_BEAN} will be lazily assigned and returned.</p>
98       *
99       * @return the HttpClientConfigurer
100      */
101     private static HttpClientConfigurer getHttpClientConfigurer() {
102         return HttpClientConfigurerHolder.httpClientConfigurer;
103     }
104 }