1 package com.atlassian.plugins.rest.module.util;
2
3 import com.sun.jersey.core.reflection.AnnotatedMethod;
4 import net.sf.cglib.proxy.InvocationHandler;
5
6 import javax.ws.rs.Path;
7 import javax.ws.rs.PathParam;
8 import javax.ws.rs.core.UriBuilder;
9 import java.lang.annotation.Annotation;
10 import java.lang.reflect.Method;
11 import java.net.URI;
12 import java.util.HashMap;
13 import java.util.Map;
14
15
16
17
18 public abstract class ResourceInvokable implements InvocationHandler
19 {
20 protected Class<?> resourceClass;
21 private final URI baseUri;
22
23 public ResourceInvokable(final Class<?> resourceClass, final URI baseUri)
24 {
25 this.resourceClass = resourceClass;
26 this.baseUri = baseUri;
27 }
28
29 protected Map<String, Object> buildParamMap(final AnnotatedMethod called, final Object[] args)
30 {
31 Map<String, Object> rv = new HashMap<String, Object>();
32
33 Annotation[][] allParameterAnnotations = called.getParameterAnnotations();
34
35 for (int i = 0; i < allParameterAnnotations.length; i++)
36 {
37 Annotation[] parameterAnnotations = allParameterAnnotations[i];
38
39 for (Annotation annotation : parameterAnnotations)
40 {
41 if (annotation instanceof PathParam && args[i] != null)
42 {
43 rv.put(((PathParam) annotation).value(), args[i]);
44 }
45 }
46 }
47
48 return rv;
49 }
50
51 protected URI getURI(final Method method, final Object[] args)
52 {
53 final UriBuilder builder = UriBuilder.fromUri(baseUri).path(resourceClass);
54 if(new AnnotatedMethod(method).getAnnotation(Path.class) != null)
55 {
56 builder.path(method);
57 }
58 return builder.buildFromMap(buildParamMap(new AnnotatedMethod(method), args));
59 }
60
61 }