@@ -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}
0 commit comments