View Javadoc
1   /******************************************************************************
2    * AbstractClient.java - Web client base class
3    * $Id: AbstractClient.java,v 1.3 2015/05/14 02:58:47 dick Exp $
4    * 
5    * BuckoVidLib - The BuckoSoft Video Library
6    * Copyright(c) 2014 - Dick Balaska
7    * 
8    * $Log: AbstractClient.java,v $
9    * Revision 1.3  2015/05/14 02:58:47  dick
10   * Bad form to make RestClientConstants an interface. Instead use public static.
11   *
12   * Revision 1.2  2014/10/07 02:49:14  dick
13   * ConfigManager moves to util, a more public place than the web client.
14   *
15   * Revision 1.1  2014/10/04 16:17:40  dick
16   * Define a web client that can talk to the plex server.
17   *
18   */
19  package com.buckosoft.BuckoVidLib.web.plex.client;
20  
21  
22  import java.lang.reflect.ParameterizedType;
23  import java.lang.reflect.Type;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.cxf.Bus;
28  import org.apache.cxf.bus.CXFBusFactory;
29  import org.apache.cxf.interceptor.LoggingInInterceptor;
30  import org.apache.cxf.interceptor.LoggingOutInterceptor;
31  import org.apache.cxf.jaxrs.client.ClientConfiguration;
32  import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
33  import org.apache.cxf.jaxrs.client.WebClient;
34  import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
35  import org.apache.cxf.transport.http.HTTPConduit;
36  import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
37  
38  import com.buckosoft.BuckoVidLib.util.ConfigManager;
39  
40  /**
41   *
42   * @author Janitor Par
43   */
44  abstract class AbstractClient<T>  {
45  
46  	protected List<Object> providers;
47  	protected String endpointUrl;
48  	protected T serviceProxy;
49  
50  	private static Bus bus = new CXFBusFactory().createBus();
51  
52  //	@SuppressWarnings("OverridableMethodCallInConstructor")
53  	public AbstractClient(String endpointUrl) {
54  		this.endpointUrl = endpointUrl;
55  		providers = new ArrayList<Object>();
56  //		JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
57  		@SuppressWarnings("rawtypes")
58  		JAXBElementProvider<?> p = new JAXBElementProvider();
59  //		jacksonProvider.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
60  //		providers.add(jacksonProvider);
61  		providers.add(p);
62  //		JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
63  //        sf.setResourceClasses(MediaContainer.class);
64  //        sf.setResourceProvider(MediaContainer.class,
65  //                               new SingletonResourceProvider(new MediaContainer()));
66  //        sf.setProviders(providers);
67  //        sf.create();
68  		
69  //		setupAdditionalProviders();
70  		createServiceProxy();
71  	}
72  
73  	public AbstractClient(String endpointUrl, List<Object> providers) {
74  		this.endpointUrl = endpointUrl;
75  		this.providers = providers;
76  		createServiceProxy();
77  	}
78  
79  	/**
80  	 * Override this if you would like to add additional providers (like exception mappers)
81  	 * jackson is added by default already.
82  	 */
83  	protected void setupAdditionalProviders() {
84  	}
85  
86  	public T getWebClient() {
87  		return serviceProxy;
88  	}
89  
90  	public void setReceiveTimeout(int timeout) {
91  		getHTTPClientPolicy().setReceiveTimeout(timeout);
92  	}
93  
94  	public void setConnectionTimeout(int timeout) {
95  		getHTTPClientPolicy().setConnectionTimeout(timeout);
96  	}
97  
98  	protected HTTPClientPolicy getHTTPClientPolicy() {
99  		ClientConfiguration config = WebClient.getConfig(serviceProxy);
100 		HTTPConduit conduit = (HTTPConduit) config.getConduit();
101 		HTTPClientPolicy policy = conduit.getClient();
102 		return policy;
103 	}
104 
105 	@SuppressWarnings("unchecked")
106 	private Class<T> getTypeParameterClass() {
107 		Type type = getClass().getGenericSuperclass();
108 		ParameterizedType paramType = (ParameterizedType) type;
109 		return (Class<T>) paramType.getActualTypeArguments()[0];
110 	}
111 
112 	private void createServiceProxy() {
113 		serviceProxy = create(endpointUrl, getTypeParameterClass(), providers, null);
114 		
115 		// read property and setup connection and receive timeouts
116 		setConnectionTimeout(ConfigManager.getInt(RestClientConstants.CXF_CLIENT_CONNECTION_TIMEOUT_PROP_KEY, RestClientConstants.DEFAULT_CXF_CLIENT_CONNECTION_TIMEOUT));
117 		setReceiveTimeout(ConfigManager.getInt(RestClientConstants.CXF_CLIENT_RECEIVE_TIMEOUT_PROP_KEY, RestClientConstants.DEFAULT_CXF_CLIENT_RECEIVE_TIMEOUT));
118 		
119 		// read property and add logging interceptors if enabled
120 		if (ConfigManager.isEnabled(RestClientConstants.CXF_CLIENT_LOGGING_IN_INTERCEPTOR_PROP_KEY)) {
121 			WebClient.getConfig(serviceProxy).getInInterceptors().add(new LoggingInInterceptor());
122 		}
123 		
124 //        WebClient.getConfig(serviceProxy).getInInterceptors().add(new RestSingleObjectToArrayInterceptor());
125 		if (ConfigManager.isEnabled(RestClientConstants.CXF_CLIENT_LOGGING_OUT_INTERCEPTOR_PROP_KEY)) {
126 			WebClient.getConfig(serviceProxy).getOutInterceptors().add(new LoggingOutInterceptor());
127 		}
128 	}
129 	
130    /**
131      * Creates a proxy
132      * @param baseAddress baseAddress
133      * @param cls proxy class, if not interface then a CGLIB proxy will be created
134      * @param providers list of providers
135      * @param configLocation classpath location of the configuration resource
136      * @return typed proxy
137      */
138     public static <T> T create(String baseAddress, Class<T> cls, List<?> providers, String configLocation) {
139         JAXRSClientFactoryBean bean = getBean(baseAddress, cls, configLocation);
140         bean.setProviders(providers);
141         return bean.create(cls);
142     }
143     
144     private static JAXRSClientFactoryBean getBean(String baseAddress, Class<?> cls, String configLocation) {
145         JAXRSClientFactoryBean bean = getBean(baseAddress, configLocation);
146         bean.setServiceClass(cls);
147         return bean;
148     }   
149     
150     private static JAXRSClientFactoryBean getBean(String baseAddress, String configLocation) {
151         JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
152         bean.setBus(bus);
153         bean.setAddress(baseAddress);
154         return bean;
155     }	
156 }