From 94b13626f12f9da0396f94a27f8575753d7edb81 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Thu, 3 Sep 2009 03:49:13 +0000 Subject: [PATCH] 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 --- .../main/java/org/jclouds/rest/Header.java | 8 +++- .../rest/JaxrsAnnotationProcessor.java | 39 ++++++++++++------- .../rest/JaxrsAnnotationProcessorTest.java | 19 +++++++++ 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/org/jclouds/rest/Header.java b/core/src/main/java/org/jclouds/rest/Header.java index d25435aac7..2e2b05cef8 100644 --- a/core/src/main/java/org/jclouds/rest/Header.java +++ b/core/src/main/java/org/jclouds/rest/Header.java @@ -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 { @@ -45,7 +49,7 @@ public @interface Header { */ String key(); - /** +/** * can be defined literally, or with enclosed variables (ex. {variable}) *

* The inputs to these variables are taken from method parameters annotated with {@code diff --git a/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java b/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java index fd3dd055df..7cf2c88afb 100644 --- a/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java @@ -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 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 clazz : declaring.getAnnotation( @@ -514,18 +515,26 @@ public class JaxrsAnnotationProcessor { public void addHeaderIfAnnotationPresentOnMethod(Multimap 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); - String value = header.value(); - for (Entry tokenValue : getEncodedPathParamKeyValues(method, args) - .entrySet()) { - value = value.replaceAll("\\{" + tokenValue.getKey() + "\\}", tokenValue.getValue() - .toString()); - } - headers.put(header.key(), value); + addHeader(headers, method, args, header); } } + private void addHeader(Multimap headers, Method method, Object[] args, + Header header) throws UnsupportedEncodingException { + String value = header.value(); + for (Entry tokenValue : getEncodedPathParamKeyValues(method, args).entrySet()) { + value = value.replaceAll("\\{" + tokenValue.getKey() + "\\}", tokenValue.getValue() + .toString()); + } + headers.put(header.key(), value); + } + private Map getEncodedPathParamKeyValues(Method method, Object[] args, char... skipEncode) throws UnsupportedEncodingException { Map pathParamValues = Maps.newHashMap(); diff --git a/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java b/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java index 45f1552c67..b44b16debd 100644 --- a/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java @@ -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 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 {