/
RestClientProxyFactoryBean.java
executable file
·186 lines (167 loc) · 5.32 KB
/
RestClientProxyFactoryBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package org.jboss.resteasy.client.spring;
import org.apache.commons.httpclient.HttpClient;
import org.jboss.resteasy.client.ClientExecutor;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import java.net.URI;
/**
* {@link org.springframework.beans.factory.FactoryBean} to generate a client
* proxy from a REST annotated interface.
* <p/>
* <p>
* Example: The following spring xml configuration snippet makes a bean with the
* id echoClient. The bean is a generated proxy of the a.b.c.Echo interface to
* access the remote service on http://server.far.far.away:8080/echo base URI.
* </p>
* <p/>
* <pre>
* <bean id="echoClient" class="org.jboss.resteasy.client.spring.RestClientProxyFactoryBean"
* p:serviceInterface="a.b.c.Echo" p:baseUri="http://server.far.far.away:8080/echo" />
* </pre>
*
* @author Attila Kiraly
* @param <T>
* The type representing the client interface.
*/
public class RestClientProxyFactoryBean<T> implements FactoryBean<T>,
InitializingBean
{
private Class<T> serviceInterface;
private URI baseUri;
private T client;
private HttpClient httpClient;
private ClientExecutor clientExecutor;
private ResteasyProviderFactory resteasyProviderFactory;
/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
public T getObject() throws Exception
{
return client;
}
/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
public Class<T> getObjectType()
{
return serviceInterface;
}
/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
public boolean isSingleton()
{
return true;
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception
{
if (resteasyProviderFactory == null)
resteasyProviderFactory = ResteasyProviderFactory.getInstance();
RegisterBuiltin.register(resteasyProviderFactory);
if (clientExecutor == null)
{
if (httpClient == null)
httpClient = new HttpClient();
clientExecutor = new ApacheHttpClientExecutor(httpClient);
client = ProxyFactory.create(serviceInterface, baseUri, clientExecutor,
resteasyProviderFactory);
}
else
client = ProxyFactory.create(serviceInterface, baseUri,
clientExecutor, resteasyProviderFactory);
}
public Class<T> getServiceInterface()
{
return serviceInterface;
}
/**
* This is a mandatory property that needs to be set.
*
* @param serviceInterface the interface for which a proxy is needed to be generated.
*/
public void setServiceInterface(Class<T> serviceInterface)
{
this.serviceInterface = serviceInterface;
}
public URI getBaseUri()
{
return baseUri;
}
/**
* This is a mandatory property that needs to be set.
*
* @param baseUri the remote service base address.
*/
public void setBaseUri(URI baseUri)
{
this.baseUri = baseUri;
}
public HttpClient getHttpClient()
{
return httpClient;
}
/**
* Optional property. If this property is set and {@link #clientExecutor} is
* null, this will be used by proxy generation. This could be usefull for
* example when you want to use a
* {@link org.apache.commons.httpclient.MultiThreadedHttpConnectionManager}
* instead of a
* {@link org.apache.commons.httpclient.SimpleHttpConnectionManager} which
* is the default in {@link org.apache.commons.httpclient.HttpClient}.
*
* @param httpClient the instance to be used by proxy generation
* @see ProxyFactory#create(Class, URI, HttpClient, ResteasyProviderFactory)
*/
public void setHttpClient(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public ClientExecutor getClientExecutor()
{
return clientExecutor;
}
/**
* Optional property for advanced usage. If this property is set it will be
* used by proxy generation. If this property is set the {@link #httpClient}
* property is ignored.
*
* @param clientExecutor the instance to be used by proxy generation
* @see ProxyFactory#create(Class, URI, ClientExecutor,
* ResteasyProviderFactory)
*/
public void setClientExecutor(ClientExecutor clientExecutor)
{
this.clientExecutor = clientExecutor;
}
public ResteasyProviderFactory getResteasyProviderFactory()
{
return resteasyProviderFactory;
}
/**
* Optional property for advanced usage. For the most cases this property is
* not needed to be set.
*
* @param resteasyProviderFactory the instance to be used by proxy generation.
*/
public void setResteasyProviderFactory(
ResteasyProviderFactory resteasyProviderFactory)
{
this.resteasyProviderFactory = resteasyProviderFactory;
}
}