1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
53 public AbstractClient(String endpointUrl) {
54 this.endpointUrl = endpointUrl;
55 providers = new ArrayList<Object>();
56
57 @SuppressWarnings("rawtypes")
58 JAXBElementProvider<?> p = new JAXBElementProvider();
59
60
61 providers.add(p);
62
63
64
65
66
67
68
69
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
81
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
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
120 if (ConfigManager.isEnabled(RestClientConstants.CXF_CLIENT_LOGGING_IN_INTERCEPTOR_PROP_KEY)) {
121 WebClient.getConfig(serviceProxy).getInInterceptors().add(new LoggingInInterceptor());
122 }
123
124
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
132
133
134
135
136
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 }