/
ResourceMethodRegistry.java
executable file
·457 lines (411 loc) · 15.9 KB
/
ResourceMethodRegistry.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package org.jboss.resteasy.core;
import org.jboss.resteasy.core.registry.RootClassNode;
import org.jboss.resteasy.core.registry.RootNode;
import org.jboss.resteasy.plugins.server.resourcefactory.JndiResourceFactory;
import org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory;
import org.jboss.resteasy.plugins.server.resourcefactory.SingletonResource;
import org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages;
import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages;
import org.jboss.resteasy.specimpl.ResteasyUriBuilder;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.InjectorFactory;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResourceFactory;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.metadata.ResourceBuilder;
import org.jboss.resteasy.spi.metadata.ResourceClass;
import org.jboss.resteasy.spi.metadata.ResourceLocator;
import org.jboss.resteasy.spi.metadata.ResourceMethod;
import org.jboss.resteasy.spi.statistics.StatisticsController;
import org.jboss.resteasy.util.GetRestful;
import org.jboss.resteasy.util.IsHttpMethod;
import javax.ws.rs.Path;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Registry of resources and methods/classes that can dispatch HTTP method requests.
*
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class ResourceMethodRegistry implements Registry
{
public static final String REGISTRY_MATCHING_EXCEPTION = "registry.matching.exception";
protected ResteasyProviderFactory providerFactory;
protected RootClassNode root = new RootClassNode();
protected boolean widerMatching;
protected RootNode rootNode = new RootNode();
protected ResourceBuilder resourceBuilder;
protected StatisticsController statisticsController;
public ResourceMethodRegistry(final ResteasyProviderFactory providerFactory)
{
this.providerFactory = providerFactory;
this.resourceBuilder = providerFactory.getResourceBuilder();
this.statisticsController = providerFactory.getStatisticsController();
}
public boolean isWiderMatching()
{
return widerMatching;
}
public void setWiderMatching(boolean widerMatching)
{
this.widerMatching = widerMatching;
}
public void addPerRequestResource(Class clazz, String basePath)
{
addResourceFactory(new POJOResourceFactory(resourceBuilder, clazz), basePath);
}
/**
* Register a vanilla JAX-RS resource class.
*
* @param clazz class
*/
public void addPerRequestResource(Class clazz)
{
addResourceFactory(new POJOResourceFactory(resourceBuilder, clazz));
}
@Override
public void addPerRequestResource(ResourceClass clazz)
{
POJOResourceFactory resourceFactory = new POJOResourceFactory(resourceBuilder, clazz);
register(resourceFactory, null, clazz);
resourceFactory.registered(providerFactory);
}
@Override
public void addPerRequestResource(ResourceClass clazz, String basePath)
{
POJOResourceFactory resourceFactory = new POJOResourceFactory(resourceBuilder, clazz);
register(resourceFactory, basePath, clazz);
resourceFactory.registered(providerFactory);
}
public void addSingletonResource(Object singleton)
{
ResourceClass resourceClass = resourceBuilder.getRootResourceFromAnnotations(singleton.getClass());
addResourceFactory(new SingletonResource(singleton, resourceClass));
}
public void addSingletonResource(Object singleton, String basePath)
{
ResourceClass resourceClass = resourceBuilder.getRootResourceFromAnnotations(singleton.getClass());
addResourceFactory(new SingletonResource(singleton, resourceClass), basePath);
}
@Override
public void addSingletonResource(Object singleton, ResourceClass resourceClass)
{
SingletonResource resourceFactory = new SingletonResource(singleton, resourceClass);
register(resourceFactory, null, resourceClass);
resourceFactory.registered(providerFactory);
}
@Override
public void addSingletonResource(Object singleton, ResourceClass resourceClass, String basePath)
{
SingletonResource resourceFactory = new SingletonResource(singleton, resourceClass);
register(resourceFactory, basePath, resourceClass);
resourceFactory.registered(providerFactory);
}
public void addJndiResource(String jndiName)
{
addResourceFactory(new JndiResourceFactory(jndiName));
}
public void addJndiResource(String jndiName, String basePath)
{
addResourceFactory(new JndiResourceFactory(jndiName), basePath);
}
@Override
public void addJndiResource(String jndiName, ResourceClass resourceClass)
{
JndiResourceFactory resourceFactory = new JndiResourceFactory(jndiName);
register(resourceFactory, null, resourceClass);
resourceFactory.registered(providerFactory);
}
@Override
public void addJndiResource(String jndiName, ResourceClass resourceClass, String basePath)
{
JndiResourceFactory resourceFactory = new JndiResourceFactory(jndiName);
register(resourceFactory, basePath, resourceClass);
resourceFactory.registered(providerFactory);
}
/**
* Bind an endpoint ResourceFactory. ResourceFactory.getScannableClass() defines what class should be scanned
* for JAX-RS annotations. The class and any implemented interfaces are scanned for annotations.
*
* @param ref resource factory
*/
public void addResourceFactory(ResourceFactory ref)
{
addResourceFactory(ref, null);
}
/**
* ResourceFactory.getScannableClass() defines what class should be scanned
* for JAX-RS annotations. The class and any implemented interfaces are scanned for annotations.
*
* @param ref resource factory
* @param base base URI path for any resources provided by the factory, in addition to rootPath
*/
public void addResourceFactory(ResourceFactory ref, String base)
{
Class<?> clazz = ref.getScannableClass();
Class restful = GetRestful.getRootResourceClass(clazz);
if (restful == null)
{
String msg = Messages.MESSAGES.classIsNotRootResource(clazz.getName());
for (Class intf : clazz.getInterfaces())
{
msg += " " + intf.getName();
}
throw new RuntimeException(msg);
}
addResourceFactory(ref, base, restful);
}
/**
* ResourceFactory.getScannableClass() is not used, only the clazz parameter and not any implemented interfaces
* of the clazz parameter.
*
* @param ref resource factory
* @param base base URI path for any resources provided by the factory, in addition to rootPath
* @param clazz specific class
*/
public void addResourceFactory(ResourceFactory ref, String base, Class<?> clazz)
{
Class<?>[] classes = {clazz};
addResourceFactory(ref, base, classes);
if (ref != null) ref.registered(providerFactory);
}
/**
* ResourceFactory.getScannableClass() is not used, only the clazz parameter and not any implemented interfaces
* of the clazz parameter.
*
* @param ref resource factory
* @param base base URI path for any resources provided by the factory, in addition to rootPath
* @param classes specific class
*/
public void addResourceFactory(ResourceFactory ref, String base, Class<?>[] classes)
{
if (ref != null) ref.registered(providerFactory);
for (Class<?> clazz : classes)
{
if (Proxy.isProxyClass(clazz))
{
for (Class<?> intf : clazz.getInterfaces())
{
ResourceClass resourceClass = resourceBuilder.getRootResourceFromAnnotations(intf);
register(ref, base, resourceClass);
}
}
else
{
ResourceClass resourceClass = resourceBuilder.getRootResourceFromAnnotations(clazz);
register(ref, base, resourceClass);
}
}
// https://issues.jboss.org/browse/JBPAPP-7871
for (Class<?> clazz : classes)
{
for (Method method : getDeclaredMethods(clazz))
{
Method _method = resourceBuilder.getAnnotatedMethod(clazz, method);
if (_method != null && !java.lang.reflect.Modifier.isPublic(_method.getModifiers()))
{
LogMessages.LOGGER.JAXRSAnnotationsFoundAtNonPublicMethod(method.getDeclaringClass().getName(), method.getName());
}
}
}
}
private Method[] getDeclaredMethods(final Class<?> clazz) {
Method[] methodList = new Method[0];
try {
if (System.getSecurityManager() == null) {
methodList = clazz.getDeclaredMethods();
} else {
methodList = AccessController.doPrivileged(new PrivilegedExceptionAction<Method[]>() {
@Override
public Method[] run() throws Exception {
return clazz.getDeclaredMethods();
}
});
}
} catch (PrivilegedActionException pae) {
}
return methodList;
}
@Override
public void addResourceFactory(ResourceFactory rf, String base, ResourceClass resourceClass)
{
if (rf != null) rf.registered(providerFactory);
register(rf, base, resourceClass);
}
protected void register(ResourceFactory rf, String base, ResourceClass resourceClass)
{
for (ResourceMethod method : resourceClass.getResourceMethods())
{
processMethod(rf, base, method);
}
for (ResourceLocator method : resourceClass.getResourceLocators())
{
processMethod(rf, base, method);
}
if (rf instanceof SingletonResource) {
providerFactory.registerSingletonResource((SingletonResource) rf);
}
}
/**
* Resteasy 2.x does not properly handle sub-resource and sub-resource locator
* endpoints with the same uri. Resteasy 3.x does handle this properly. In
* assisting customers identify this issue during an upgrade from Resteasy 2 to 3
* provides a waring when the situation is found.
*/
public void checkAmbiguousUri()
{
for (Map.Entry<String, List<ResourceInvoker>> entry : this.root.getBounded().entrySet())
{
List<ResourceInvoker> values = entry.getValue();
if (values.size() > 1) {
int locatorCnt = 0;
int methodCnt = 0;
for(ResourceInvoker rInvoker : values)
{
if (rInvoker instanceof ResourceLocatorInvoker)
{
locatorCnt++;
} else if (rInvoker instanceof ResourceMethodInvoker)
{
methodCnt++;
}
}
if (methodCnt > 0 && locatorCnt > 0)
{
StringBuilder sb = new StringBuilder();
int cnt = values.size();
for (int i=0; i < cnt; i++) {
ResourceInvoker exp = values.get(i);
sb.append(exp.getMethod().getDeclaringClass().getName())
.append(".")
.append(exp.getMethod().getName());
if (i < cnt-1)
{
sb.append(", ");
}
}
LogMessages.LOGGER.uriAmbiguity(entry.getKey(), sb.toString());
}
}
}
}
protected void processMethod(ResourceFactory rf, String base, ResourceLocator method)
{
ResteasyUriBuilder builder = new ResteasyUriBuilder();
if (base != null)
builder.path(base);
builder.path(method.getFullpath());
String fullpath = builder.getPath();
if (fullpath == null)
fullpath = "";
builder = new ResteasyUriBuilder();
if (base != null)
builder.path(base);
builder.path(method.getResourceClass().getPath());
String classExpression = builder.getPath();
if (classExpression == null)
classExpression = "";
InjectorFactory injectorFactory = providerFactory.getInjectorFactory();
if (method instanceof ResourceMethod)
{
ResourceMethodInvoker invoker = new ResourceMethodInvoker((ResourceMethod) method, injectorFactory, rf, providerFactory);
if (widerMatching)
rootNode.addInvoker(fullpath, invoker);
else root.addInvoker(classExpression, fullpath, invoker);
statisticsController.register(invoker);
}
else
{
ResourceLocatorInvoker locator = new ResourceLocatorInvoker(rf, injectorFactory, providerFactory, method);
if (widerMatching)
rootNode.addInvoker(fullpath, locator);
else root.addInvoker(classExpression, fullpath, locator);
statisticsController.register(locator);
}
}
/**
* Find all endpoints reachable by clazz and unregister them.
*
* @param clazz class
*/
public void removeRegistrations(Class clazz)
{
removeRegistrations(clazz, null);
}
public void removeRegistrations(Class clazz, String base)
{
Class restful = GetRestful.getRootResourceClass(clazz);
removeRegistration(base, restful);
}
@Override
public void removeRegistrations(ResourceClass resourceClass)
{
for (ResourceMethod method : resourceClass.getResourceMethods())
{
if (widerMatching) rootNode.removeBinding(method.getFullpath(), method.getMethod());
else root.removeBinding(resourceClass.getPath(), method.getFullpath(), method.getMethod());
}
for (ResourceLocator method : resourceClass.getResourceLocators())
{
if (widerMatching) rootNode.removeBinding(method.getFullpath(), method.getMethod());
else root.removeBinding(resourceClass.getPath(), method.getFullpath(), method.getMethod());
}
}
private void removeRegistration(String base, Class<?> clazz)
{
for (Method method : clazz.getMethods())
{
Path path = method.getAnnotation(Path.class);
Set<String> httpMethods = IsHttpMethod.getHttpMethods(method);
if (path == null && httpMethods == null) continue;
ResteasyUriBuilder builder = new ResteasyUriBuilder();
if (base != null) builder.path(base);
if (clazz.isAnnotationPresent(Path.class)) builder.path(clazz);
String classExpression = builder.getPath();
if (path != null) builder.path(method);
String fullpath = builder.getPath();
if (fullpath == null) fullpath = "";
if (widerMatching) rootNode.removeBinding(fullpath, method);
else root.removeBinding(classExpression, fullpath, method);
}
}
public Map<String, List<ResourceInvoker>> getBounded()
{
if (widerMatching) return rootNode.getBounded();
else return root.getBounded();
}
/**
* Number of endpoints registered.
*
* @return number of endpoints registered
*/
public int getSize()
{
if (widerMatching) return rootNode.getSize();
else return root.getSize();
}
/**
* Find a resource to invoke on.
*
* @return resource invoker
*/
public ResourceInvoker getResourceInvoker(HttpRequest request)
{
try
{
if (widerMatching) return rootNode.match(request, 0);
else return root.match(request, 0);
}
catch (RuntimeException e)
{
throw e;
}
}
}