Debian Codegraph @

resteasy-jaxrs/src/main/java/org/jboss/resteasy/spi/touri/AbstractURITemplateAnnotationResolver.java

raw symbols SCIP
1 package org.jboss.resteasy.spi.touri;
2
3 import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages;
4 import org.jboss.resteasy.specimpl.ResteasyUriBuilder;
5 import org.jboss.resteasy.util.AnnotationResolver;
6
7 import java.beans.BeanInfo;
8 import java.beans.IntrospectionException;
9 import java.beans.Introspector;
10 import java.beans.PropertyDescriptor;
11 import java.lang.annotation.Annotation;
12 import java.lang.reflect.Method;
13 import java.lang.reflect.Modifier;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 public abstract class AbstractURITemplateAnnotationResolver implements
20 URIResolver
21 {
22
23 @SuppressWarnings("unchecked")
24 public boolean handles(Class type)
25 {
26 return AnnotationResolver.getClassWithAnnotation(type, getAnnotationType()) != null;
27 }
28
29 @SuppressWarnings("unchecked")
30 public String resolveURI(Object object)
31 {
32 Class<? extends Object> clazz = AnnotationResolver
33 .getClassWithAnnotation(object.getClass(), getAnnotationType());
34 ResteasyUriBuilder uriBuilderImpl = getUriBuilder(clazz);
35 Map<String, PropertyDescriptor> descriptors = getPropertyDescriptors(clazz);
36 List<Object> values = getValues(object, descriptors, uriBuilderImpl
37 .getPathParamNamesInDeclarationOrder());
38 return uriBuilderImpl.build(values.toArray()).toString();
39 }
40
41 protected abstract Class<? extends Annotation> getAnnotationType();
42
43 protected abstract ResteasyUriBuilder getUriBuilder(Class<? extends Object> clazz);
44
45 private List<Object> getValues(Object object,
46 Map<String, PropertyDescriptor> descriptors, List<String> params)
47 {
48 List<Object> values = new ArrayList<Object>();
49 for (String param : params)
50 {
51 PropertyDescriptor propertyDescriptor = descriptors.get(param);
52 if (propertyDescriptor == null)
53 {
54 throw new RuntimeException(Messages.MESSAGES.couldNotFindGetterForParam(param));
55 }
56
57 Method readMethod = propertyDescriptor.getReadMethod();
58 if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()))
59 {
60 readMethod.setAccessible(true);
61 }
62
63 try
64 {
65 values.add(readMethod.invoke(object, new Object[0]));
66 }
67 catch (Exception e)
68 {
69 throw new RuntimeException(Messages.MESSAGES.couldNotGetAValue(param), e);
70 }
71 }
72 return values;
73 }
74
75 private Map<String, PropertyDescriptor> getPropertyDescriptors(
76 Class<? extends Object> clazz)
77 {
78 try
79 {
80 BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
81 HashMap<String, PropertyDescriptor> results = new HashMap<String, PropertyDescriptor>();
82 PropertyDescriptor[] propertyDescriptors = beanInfo
83 .getPropertyDescriptors();
84 for (PropertyDescriptor propertyDescriptor : propertyDescriptors)
85 {
86 results.put(propertyDescriptor.getName(), propertyDescriptor);
87 }
88 return results;
89 }
90 catch (IntrospectionException e)
91 {
92 throw new RuntimeException(Messages.MESSAGES.couldNotIntrospectClass(clazz.getName()), e);
93 }
94 }
95 }