|
37 | 37 | import java.lang.annotation.Annotation;
|
38 | 38 | import java.lang.reflect.Method;
|
39 | 39 |
|
| 40 | +import javax.ws.rs.CookieParam; |
| 41 | +import javax.ws.rs.FormParam; |
40 | 42 | import javax.ws.rs.HeaderParam;
|
| 43 | +import javax.ws.rs.MatrixParam; |
41 | 44 | import javax.ws.rs.Path;
|
| 45 | +import javax.ws.rs.PathParam; |
42 | 46 | import javax.ws.rs.QueryParam;
|
43 | 47 |
|
44 | 48 | import org.restlet.Request;
|
45 |
| -import org.restlet.engine.header.Header; |
| 49 | +import org.restlet.data.Parameter; |
46 | 50 | import org.restlet.engine.resource.ClientInvocationHandler;
|
47 | 51 | import org.restlet.ext.jaxrs.JaxRsClientResource;
|
| 52 | +import org.restlet.ext.jaxrs.internal.exceptions.IllegalMethodParamTypeException; |
48 | 53 | import org.restlet.ext.jaxrs.internal.util.Util;
|
49 | 54 | import org.restlet.representation.Representation;
|
50 | 55 | import org.restlet.resource.ClientResource;
|
51 |
| -import org.restlet.service.ConverterService; |
52 |
| -import org.restlet.util.Series; |
53 | 56 |
|
54 | 57 | /**
|
55 | 58 | * Reflection proxy invocation handler created for the
|
|
65 | 68 | */
|
66 | 69 | public class JaxRsClientInvocationHandler<T> extends ClientInvocationHandler<T> {
|
67 | 70 |
|
68 |
| - private ClientResource clientResource; |
| 71 | + private ClientResource clientResource; |
69 | 72 |
|
70 | 73 | /**
|
71 |
| - * Constructor. |
72 |
| - * |
73 |
| - * @param clientResource |
74 |
| - * The client resource. |
75 |
| - * @param resourceInterface |
76 |
| - * The annotated resource interface. |
77 |
| - */ |
78 |
| - public JaxRsClientInvocationHandler(ClientResource clientResource, |
79 |
| - Class<? extends T> resourceInterface) { |
80 |
| - super(clientResource, resourceInterface, JaxRsAnnotationUtils |
81 |
| - .getInstance()); |
82 |
| - |
83 |
| - this.clientResource = clientResource; |
84 |
| - } |
85 |
| - |
86 |
| - @Override |
87 |
| - protected Request getRequest(Method javaMethod, Object[] args) { |
88 |
| - Request request = super.getRequest(javaMethod, args); |
89 |
| - |
90 |
| - setRequestParams(javaMethod, args, request); |
91 |
| - |
92 |
| - setRequestPathToAnnotationPath(javaMethod, request); |
93 |
| - |
94 |
| - return request; |
95 |
| - } |
| 74 | + * Constructor. |
| 75 | + * |
| 76 | + * @param clientResource |
| 77 | + * The client resource. |
| 78 | + * @param resourceInterface |
| 79 | + * The annotated resource interface. |
| 80 | + */ |
| 81 | + public JaxRsClientInvocationHandler(ClientResource clientResource, |
| 82 | + Class<? extends T> resourceInterface) { |
| 83 | + super(clientResource, resourceInterface, JaxRsAnnotationUtils |
| 84 | + .getInstance()); |
| 85 | + |
| 86 | + this.clientResource = clientResource; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected Request getRequest(Method javaMethod, Object[] args) |
| 91 | + throws Throwable { |
| 92 | + Request request = super.getRequest(javaMethod, args); |
| 93 | + |
| 94 | + setRequestParams(javaMethod, args, request); |
| 95 | + |
| 96 | + setRequestPathToAnnotationPath(javaMethod, request); |
| 97 | + |
| 98 | + return request; |
| 99 | + } |
96 | 100 |
|
97 | 101 | private void setRequestParams(Method javaMethod, Object[] args,
|
98 |
| - Request request) { |
99 |
| - Annotation[][] parameterAnnotations = javaMethod.getParameterAnnotations(); |
100 |
| - for( Annotation[] annotations : parameterAnnotations ) { |
101 |
| - for( Annotation annotation : annotations ) { |
102 |
| - |
103 |
| - if(annotation instanceof HeaderParam) { |
104 |
| - HeaderParam headerParam = (HeaderParam)annotation; |
105 |
| - String value = headerParam.value(); |
106 |
| - Series< Header > headers = Util.getHttpHeaders( request ); |
107 |
| - |
108 |
| - ConverterService converterService = clientResource.getApplication().getConverterService(); |
109 |
| - |
110 |
| - //TODO - don't just use args[0], each param should be set |
111 |
| - Representation representation = converterService.toRepresentation( args[0] ); |
112 |
| - try { |
113 |
| - headers.add( value, representation.getText() ); |
114 |
| - } |
115 |
| - catch ( IOException exception ) { |
116 |
| - throw new RuntimeException(exception); |
117 |
| - } |
118 |
| - } else if(annotation instanceof QueryParam) { |
119 |
| - // TODO - encode & map QueryParam |
120 |
| - //resourceRef.addQueryParameter( new Parameter( "point", "<java.awt.Point> <x>22</x> <y>33</y> </java.awt.Point>" ) ); |
121 |
| - } |
122 |
| - // TODO - other param types |
123 |
| - } |
124 |
| - } |
| 102 | + Request request) throws IllegalMethodParamTypeException { |
| 103 | + |
| 104 | + int argIndex = 0; |
| 105 | + |
| 106 | + Annotation[][] parameterAnnotations = javaMethod |
| 107 | + .getParameterAnnotations(); |
| 108 | + for (Annotation[] annotations : parameterAnnotations) { |
| 109 | + |
| 110 | + String representationAsText = getRepresentationAsText(argIndex, |
| 111 | + args); |
| 112 | + |
| 113 | + for (Annotation annotation : annotations) { |
| 114 | + if (annotation instanceof HeaderParam |
| 115 | + && representationAsText != null) { |
| 116 | + Util.getHttpHeaders(request).add( |
| 117 | + ((HeaderParam) annotation).value(), |
| 118 | + representationAsText); |
| 119 | + argIndex++; |
| 120 | + } else if (annotation instanceof QueryParam) { |
| 121 | + request.getResourceRef().addQueryParameter( |
| 122 | + new Parameter(((QueryParam) annotation).value(), |
| 123 | + representationAsText)); |
| 124 | + argIndex++; |
| 125 | + } else if (annotation instanceof MatrixParam |
| 126 | + && representationAsText != null) { |
| 127 | + // TODO |
| 128 | + argIndex++; |
| 129 | + } else if (annotation instanceof CookieParam |
| 130 | + && representationAsText != null) { |
| 131 | + // TODO |
| 132 | + argIndex++; |
| 133 | + } else if (annotation instanceof FormParam |
| 134 | + && representationAsText != null) { |
| 135 | + // TODO |
| 136 | + argIndex++; |
| 137 | + } else if (annotation instanceof QueryParam |
| 138 | + && representationAsText != null) { |
| 139 | + // TODO |
| 140 | + argIndex++; |
| 141 | + } else if (annotation instanceof PathParam |
| 142 | + && representationAsText != null) { |
| 143 | + // TODO |
| 144 | + argIndex++; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // TODO - possibly throw an exception if the arg count != processed annotations? |
| 150 | + } |
| 151 | + |
| 152 | + private String getRepresentationAsText(int argIndex, Object[] args) { |
| 153 | + String representationAsText = null; |
| 154 | + Representation representation = clientResource.getApplication() |
| 155 | + .getConverterService().toRepresentation(args[argIndex]); |
| 156 | + try { |
| 157 | + representationAsText = representation.getText(); |
| 158 | + } catch (IOException exception) { |
| 159 | + throw new RuntimeException(exception); |
| 160 | + } |
| 161 | + return representationAsText; |
125 | 162 | }
|
126 | 163 |
|
127 | 164 | private void setRequestPathToAnnotationPath(Method javaMethod,
|
128 | 165 | Request request) {
|
129 | 166 | Path methodPathAnnotation = javaMethod.getAnnotation(Path.class);
|
130 |
| - if (methodPathAnnotation != null) { |
131 |
| - String methodPath = methodPathAnnotation.value(); |
132 |
| - if (methodPath != null && methodPath.length() > 0) { |
133 |
| - request.getResourceRef().setPath( |
134 |
| - request.getResourceRef().getPath() + "/" + methodPath); |
135 |
| - } |
136 |
| - } |
| 167 | + if (methodPathAnnotation != null) { |
| 168 | + String methodPath = methodPathAnnotation.value(); |
| 169 | + if (methodPath != null && methodPath.length() > 0) { |
| 170 | + request.getResourceRef().setPath( |
| 171 | + request.getResourceRef().getPath() + "/" + methodPath); |
| 172 | + } |
| 173 | + } |
137 | 174 | }
|
138 | 175 |
|
139 | 176 | }
|
0 commit comments