Skip to content

Commit df9726d

Browse files
committed
Merge pull request OpenFeign#204 from Netflix/adrian.headers-on-type
Allows `@Headers` to be applied to a type
2 parents 687efb9 + 531a54d commit df9726d

5 files changed

Lines changed: 68 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
### Version 8.1
2+
* Allows `@Headers` to be applied to a type
3+
14
### Version 8.0
25
* Removes Dagger 1.x Dependency
36
* Removes support for parameters annotated with `javax.inject.@Named`. Use `feign.@Param` instead.
47
* Makes body parameter type explicit.
58

9+
### Version 7.4
10+
* Allows `@Headers` to be applied to a type
11+
612
### Version 7.3
713
* Adds Request.Options support to RibbonClient
814
* Adds LBClientFactory to enable caching of Ribbon LBClients

‎core/src/main/java/feign/Contract.java‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ protected void nameParam(MethodMetadata data, String name, int i) {
130130

131131
class Default extends BaseContract {
132132

133+
@Override
134+
public MethodMetadata parseAndValidatateMetadata(Method method) {
135+
MethodMetadata data = super.parseAndValidatateMetadata(method);
136+
if (method.getDeclaringClass().isAnnotationPresent(Headers.class)) {
137+
String[] headersOnType = method.getDeclaringClass().getAnnotation(Headers.class).value();
138+
checkState(headersOnType.length > 0, "Headers annotation was empty on type %s.",
139+
method.getDeclaringClass().getName());
140+
Map<String, Collection<String>> headers = toMap(headersOnType);
141+
headers.putAll(data.template().headers());
142+
data.template().headers(null); // to clear
143+
data.template().headers(headers);
144+
}
145+
return data;
146+
}
147+
133148
@Override
134149
protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodAnnotation,
135150
Method method) {
@@ -161,21 +176,10 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
161176
data.template().bodyTemplate(body);
162177
}
163178
} else if (annotationType == Headers.class) {
164-
String[] headersToParse = Headers.class.cast(methodAnnotation).value();
165-
checkState(headersToParse.length > 0, "Headers annotation was empty on method %s.",
179+
String[] headersOnMethod = Headers.class.cast(methodAnnotation).value();
180+
checkState(headersOnMethod.length > 0, "Headers annotation was empty on method %s.",
166181
method.getName());
167-
Map<String, Collection<String>>
168-
headers =
169-
new LinkedHashMap<String, Collection<String>>(headersToParse.length);
170-
for (String header : headersToParse) {
171-
int colon = header.indexOf(':');
172-
String name = header.substring(0, colon);
173-
if (!headers.containsKey(name)) {
174-
headers.put(name, new ArrayList<String>(1));
175-
}
176-
headers.get(name).add(header.substring(colon + 2));
177-
}
178-
data.template().headers(headers);
182+
data.template().headers(toMap(headersOnMethod));
179183
}
180184
}
181185

@@ -208,7 +212,7 @@ protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[
208212
return isHttpAnnotation;
209213
}
210214

211-
private <K, V> boolean searchMapValues(Map<K, Collection<V>> map, V search) {
215+
private static <K, V> boolean searchMapValues(Map<K, Collection<V>> map, V search) {
212216
Collection<Collection<V>> values = map.values();
213217
if (values == null) {
214218
return false;
@@ -222,5 +226,20 @@ private <K, V> boolean searchMapValues(Map<K, Collection<V>> map, V search) {
222226

223227
return false;
224228
}
229+
230+
private static Map<String, Collection<String>> toMap(String[] input) {
231+
Map<String, Collection<String>>
232+
result =
233+
new LinkedHashMap<String, Collection<String>>(input.length);
234+
for (String header : input) {
235+
int colon = header.indexOf(':');
236+
String name = header.substring(0, colon);
237+
if (!result.containsKey(name)) {
238+
result.put(name, new ArrayList<String>(1));
239+
}
240+
result.get(name).add(header.substring(colon + 2));
241+
}
242+
return result;
243+
}
225244
}
226245
}

‎core/src/main/java/feign/Headers.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import java.lang.annotation.Target;
55

66
import static java.lang.annotation.ElementType.METHOD;
7+
import static java.lang.annotation.ElementType.TYPE;
78
import static java.lang.annotation.RetentionPolicy.RUNTIME;
89

910
/**
1011
* Expands headers supplied in the {@code value}. Variables are permitted as values. <br>
1112
* <pre>
13+
* &#64;Headers("Content-Type: application/xml")
14+
* interface SoapApi {
15+
* ...
1216
* &#64;RequestLine("GET /")
1317
* &#64;Headers("Cache-Control: max-age=640000")
1418
* ...
@@ -37,7 +41,7 @@
3741
* ...
3842
* </pre>
3943
*/
40-
@Target(METHOD)
44+
@Target({METHOD, TYPE})
4145
@Retention(RUNTIME)
4246
public @interface Headers {
4347

‎core/src/main/java/feign/RequestTemplate.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,7 @@ public RequestTemplate header(String name, Iterable<String> values) {
377377
* JAXRS 2.0</b><br> <br> Like {@code Invocation.Builder.headers(MultivaluedMap)}, except the
378378
* values can be templatized. <br> ex. <br>
379379
* <pre>
380-
* template.headers(ImmutableMultimap.of(&quot;X-Application-Version&quot;,
381-
* &quot;{version}&quot;));
380+
* template.headers(mapOf(&quot;X-Application-Version&quot;, asList(&quot;{version}&quot;)));
382381
* </pre>
383382
*
384383
* @param headers if null, remove all headers. else value to replace all headers with.

‎core/src/test/java/feign/DefaultContractTest.java‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void bodyWithoutParameters() throws Exception {
163163
}
164164

165165
@Test
166-
public void producesAddsContentTypeHeader() throws Exception {
166+
public void headersOnMethodAddsContentTypeHeader() throws Exception {
167167
MethodMetadata
168168
md =
169169
contract.parseAndValidatateMetadata(BodyWithoutParameters.class.getDeclaredMethod("post"));
@@ -175,6 +175,19 @@ public void producesAddsContentTypeHeader() throws Exception {
175175
);
176176
}
177177

178+
@Test
179+
public void headersOnTypeAddsContentTypeHeader() throws Exception {
180+
MethodMetadata
181+
md =
182+
contract.parseAndValidatateMetadata(HeadersOnType.class.getDeclaredMethod("post"));
183+
184+
assertThat(md.template())
185+
.hasHeaders(
186+
entry("Content-Type", asList("application/xml")),
187+
entry("Content-Length", asList(String.valueOf(md.template().body().length)))
188+
);
189+
}
190+
178191
@Test
179192
public void withPathAndURIParam() throws Exception {
180193
MethodMetadata md = contract.parseAndValidatateMetadata(
@@ -342,6 +355,14 @@ interface BodyWithoutParameters {
342355
Response post();
343356
}
344357

358+
@Headers("Content-Type: application/xml")
359+
interface HeadersOnType {
360+
361+
@RequestLine("POST /")
362+
@Body("<v01:getAccountsListOfUser/>")
363+
Response post();
364+
}
365+
345366
interface WithURIParam {
346367

347368
@RequestLine("GET /{1}/{2}")

0 commit comments

Comments
 (0)