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 protected Class<?> resourceClass;
20 private final URI baseUri;
21
22 public ResourceInvokable(final Class<?> resourceClass, final URI baseUri) {
23 this.resourceClass = resourceClass;
24 this.baseUri = baseUri;
25 }
26
27 protected Map<String, Object> buildParamMap(final AnnotatedMethod called, final Object[] args) {
28 Map<String, Object> rv = new HashMap<String, Object>();
29
30 Annotation[][] allParameterAnnotations = called.getParameterAnnotations();
31
32 for (int i = 0; i < allParameterAnnotations.length; i++) {
33 Annotation[] parameterAnnotations = allParameterAnnotations[i];
34
35 for (Annotation annotation : parameterAnnotations) {
36 if (annotation instanceof PathParam && args[i] != null) {
37 rv.put(((PathParam) annotation).value(), args[i]);
38 }
39 }
40 }
41
42 return rv;
43 }
44
45 protected URI getURI(final Method method, final Object[] args) {
46 final UriBuilder builder = UriBuilder.fromUri(baseUri).path(resourceClass);
47 if (new AnnotatedMethod(method).getAnnotation(Path.class) != null) {
48 builder.path(method);
49 }
50 return builder.buildFromMap(buildParamMap(new AnnotatedMethod(method), args));
51 }
52
53 }