mirror of https://github.com/apache/jclouds.git
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:
parent
7dd111435b
commit
94b13626f1
|
@ -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. <code>{variable}</code>)
|
||||
* <p/>
|
||||
* The inputs to these variables are taken from method parameters annotated with {@code
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue