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;
|
package org.jclouds.rest;
|
||||||
|
|
||||||
import static java.lang.annotation.ElementType.METHOD;
|
import static java.lang.annotation.ElementType.METHOD;
|
||||||
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
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
|
* 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}.
|
* {@code value}, expanding any variables annotated with {@code PathParam}.
|
||||||
|
@ -36,7 +40,7 @@ import java.lang.annotation.Target;
|
||||||
* @see PathParam
|
* @see PathParam
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
@Target(METHOD)
|
@Target( { TYPE, METHOD })
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
public @interface Header {
|
public @interface Header {
|
||||||
|
|
||||||
|
@ -45,7 +49,7 @@ public @interface Header {
|
||||||
*/
|
*/
|
||||||
String key();
|
String key();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* can be defined literally, or with enclosed variables (ex. <code>{variable}</code>)
|
* can be defined literally, or with enclosed variables (ex. <code>{variable}</code>)
|
||||||
* <p/>
|
* <p/>
|
||||||
* The inputs to these variables are taken from method parameters annotated with {@code
|
* 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)) {
|
if (declaring.isAnnotationPresent(Query.class)) {
|
||||||
Query query = declaring.getAnnotation(Query.class);
|
Query query = declaring.getAnnotation(Query.class);
|
||||||
if (query.value().equals(Query.NULL))
|
addQuery(builder, query);
|
||||||
builder.replaceQuery(query.key());
|
|
||||||
else
|
|
||||||
builder.queryParam(query.key(), query.value());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method.isAnnotationPresent(Query.class)) {
|
if (method.isAnnotationPresent(Query.class)) {
|
||||||
Query query = method.getAnnotation(Query.class);
|
Query query = method.getAnnotation(Query.class);
|
||||||
if (query.value().equals(Query.NULL))
|
addQuery(builder, query);
|
||||||
builder.replaceQuery(query.key());
|
|
||||||
else
|
|
||||||
builder.queryParam(query.key(), query.value());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Multimap<String, String> headers = buildHeaders(method, args);
|
Multimap<String, String> headers = buildHeaders(method, args);
|
||||||
|
@ -267,6 +261,13 @@ public class JaxrsAnnotationProcessor {
|
||||||
return request;
|
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) {
|
private void addFiltersIfAnnotated(Method method, HttpRequest request) {
|
||||||
if (declaring.isAnnotationPresent(RequestFilters.class)) {
|
if (declaring.isAnnotationPresent(RequestFilters.class)) {
|
||||||
for (Class<? extends HttpRequestFilter> clazz : declaring.getAnnotation(
|
for (Class<? extends HttpRequestFilter> clazz : declaring.getAnnotation(
|
||||||
|
@ -514,18 +515,26 @@ public class JaxrsAnnotationProcessor {
|
||||||
|
|
||||||
public void addHeaderIfAnnotationPresentOnMethod(Multimap<String, String> headers,
|
public void addHeaderIfAnnotationPresentOnMethod(Multimap<String, String> headers,
|
||||||
Method method, Object[] args, char... skipEncode) throws UnsupportedEncodingException {
|
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)) {
|
if (method.isAnnotationPresent(Header.class)) {
|
||||||
Header header = method.getAnnotation(Header.class);
|
Header header = method.getAnnotation(Header.class);
|
||||||
String value = header.value();
|
addHeader(headers, method, args, header);
|
||||||
for (Entry<String, Object> tokenValue : getEncodedPathParamKeyValues(method, args)
|
|
||||||
.entrySet()) {
|
|
||||||
value = value.replaceAll("\\{" + tokenValue.getKey() + "\\}", tokenValue.getValue()
|
|
||||||
.toString());
|
|
||||||
}
|
|
||||||
headers.put(header.key(), value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()) {
|
||||||
|
value = value.replaceAll("\\{" + tokenValue.getKey() + "\\}", tokenValue.getValue()
|
||||||
|
.toString());
|
||||||
|
}
|
||||||
|
headers.put(header.key(), value);
|
||||||
|
}
|
||||||
|
|
||||||
private Map<String, Object> getEncodedPathParamKeyValues(Method method, Object[] args,
|
private Map<String, Object> getEncodedPathParamKeyValues(Method method, Object[] args,
|
||||||
char... skipEncode) throws UnsupportedEncodingException {
|
char... skipEncode) throws UnsupportedEncodingException {
|
||||||
Map<String, Object> pathParamValues = Maps.newHashMap();
|
Map<String, Object> pathParamValues = Maps.newHashMap();
|
||||||
|
|
|
@ -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
|
@Test
|
||||||
public void testBuildOneHeader() throws SecurityException, NoSuchMethodException,
|
public void testBuildOneHeader() throws SecurityException, NoSuchMethodException,
|
||||||
UnsupportedEncodingException {
|
UnsupportedEncodingException {
|
||||||
|
|
Loading…
Reference in New Issue