Issue 76: added support for default header parameters on an interface

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1872 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-09-03 03:49:13 +00:00
parent 7dd111435b
commit 94b13626f1
3 changed files with 49 additions and 17 deletions

View File

@ -24,11 +24,15 @@
package org.jclouds.rest;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.HttpHeaders;
/**
* Designates that a header will be added to the request. This header will contain the specified
* {@code value}, expanding any variables annotated with {@code PathParam}.
@ -36,7 +40,7 @@ import java.lang.annotation.Target;
* @see PathParam
* @author Adrian Cole
*/
@Target(METHOD)
@Target( { TYPE, METHOD })
@Retention(RUNTIME)
public @interface Header {

View File

@ -216,18 +216,12 @@ public class JaxrsAnnotationProcessor {
if (declaring.isAnnotationPresent(Query.class)) {
Query query = declaring.getAnnotation(Query.class);
if (query.value().equals(Query.NULL))
builder.replaceQuery(query.key());
else
builder.queryParam(query.key(), query.value());
addQuery(builder, query);
}
if (method.isAnnotationPresent(Query.class)) {
Query query = method.getAnnotation(Query.class);
if (query.value().equals(Query.NULL))
builder.replaceQuery(query.key());
else
builder.queryParam(query.key(), query.value());
addQuery(builder, query);
}
Multimap<String, String> headers = buildHeaders(method, args);
@ -267,6 +261,13 @@ public class JaxrsAnnotationProcessor {
return request;
}
private void addQuery(UriBuilder builder, Query query) {
if (query.value().equals(Query.NULL))
builder.replaceQuery(query.key());
else
builder.queryParam(query.key(), query.value());
}
private void addFiltersIfAnnotated(Method method, HttpRequest request) {
if (declaring.isAnnotationPresent(RequestFilters.class)) {
for (Class<? extends HttpRequestFilter> clazz : declaring.getAnnotation(
@ -514,17 +515,25 @@ public class JaxrsAnnotationProcessor {
public void addHeaderIfAnnotationPresentOnMethod(Multimap<String, String> headers,
Method method, Object[] args, char... skipEncode) throws UnsupportedEncodingException {
if (declaring.isAnnotationPresent(Header.class)) {
Header header = declaring.getAnnotation(Header.class);
addHeader(headers, method, args, header);
}
if (method.isAnnotationPresent(Header.class)) {
Header header = method.getAnnotation(Header.class);
addHeader(headers, method, args, header);
}
}
private void addHeader(Multimap<String, String> headers, Method method, Object[] args,
Header header) throws UnsupportedEncodingException {
String value = header.value();
for (Entry<String, Object> tokenValue : getEncodedPathParamKeyValues(method, args)
.entrySet()) {
for (Entry<String, Object> tokenValue : getEncodedPathParamKeyValues(method, args).entrySet()) {
value = value.replaceAll("\\{" + tokenValue.getKey() + "\\}", tokenValue.getValue()
.toString());
}
headers.put(header.key(), value);
}
}
private Map<String, Object> getEncodedPathParamKeyValues(Method method, Object[] args,
char... skipEncode) throws UnsupportedEncodingException {

View File

@ -392,6 +392,25 @@ public class JaxrsAnnotationProcessorTest {
}
}
@Header(key = "x-amz-copy-source", value = "/{bucket}")
public class TestClassHeader {
@GET
public void oneHeader(@PathParam("bucket") String path) {
}
}
@Test
public void testBuildOneClassHeader() throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException {
Method oneHeader = TestClassHeader.class.getMethod("oneHeader", String.class);
Multimap<String, String> headers = HashMultimap.create();
factory.create(TestClassHeader.class).addHeaderIfAnnotationPresentOnMethod(headers,
oneHeader, new Object[] { "robot" });
assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot"));
}
@Test
public void testBuildOneHeader() throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException {