1818 */
1919package com .treasuredata .client ;
2020
21- import com .google .common .collect .ImmutableMap ;
2221import com .google .common .collect .ImmutableMultimap ;
2322import com .google .common .collect .Multimap ;
24-
2523import org .slf4j .Logger ;
2624import org .slf4j .LoggerFactory ;
2725
2826import java .io .File ;
2927import java .io .UnsupportedEncodingException ;
3028import java .net .URLEncoder ;
29+ import java .util .ArrayList ;
30+ import java .util .Collection ;
3131import java .util .HashMap ;
3232import java .util .Map ;
3333import java .util .Optional ;
34+ import java .util .Collections ;
3435
3536import static java .util .Objects .requireNonNull ;
3637
@@ -45,7 +46,7 @@ public class TDApiRequest
4546 private final TDHttpMethod method ;
4647 private final String path ;
4748 private final Map <String , String > queryParams ;
48- private final Multimap <String , String > headerParams ;
49+ private final Map <String , Collection < String > > headerParams ;
4950 private final Optional <String > postJson ;
5051 private final Optional <File > putFile ;
5152 private final Optional <byte []> content ;
@@ -57,7 +58,7 @@ public class TDApiRequest
5758 TDHttpMethod method ,
5859 String path ,
5960 Map <String , String > queryParams ,
60- Multimap <String , String > headerParams ,
61+ Map <String , Collection < String > > headerParams ,
6162 Optional <String > postJson ,
6263 Optional <File > putFile ,
6364 Optional <byte []> content ,
@@ -80,7 +81,7 @@ public class TDApiRequest
8081
8182 public TDApiRequest withUri (String uri )
8283 {
83- return new TDApiRequest (method , uri , ImmutableMap . copyOf ( queryParams ), ImmutableMultimap . copyOf ( headerParams ), postJson , putFile , content , contentOffset , contentLength , followRedirects );
84+ return new TDApiRequest (method , uri , Collections . unmodifiableMap ( new HashMap <>( queryParams )), Collections . unmodifiableMap ( new HashMap <>( headerParams ) ), postJson , putFile , content , contentOffset , contentLength , followRedirects );
8485 }
8586
8687 public String getPath ()
@@ -98,7 +99,21 @@ public Map<String, String> getQueryParams()
9899 return queryParams ;
99100 }
100101
102+ /**
103+ * @deprecated Use {@link #getAllHeaders()} instead.
104+ * @return
105+ */
106+ @ Deprecated
101107 public Multimap <String , String > getHeaderParams ()
108+ {
109+ ImmutableMultimap .Builder <String , String > builder = new ImmutableMultimap .Builder <>();
110+ for (Map .Entry <String , Collection <String >> e : headerParams .entrySet ()) {
111+ builder .putAll (e .getKey (), e .getValue ());
112+ }
113+ return builder .build ();
114+ }
115+
116+ public Map <String , Collection <String >> getAllHeaders ()
102117 {
103118 return headerParams ;
104119 }
@@ -135,12 +150,12 @@ public Optional<Boolean> getFollowRedirects()
135150
136151 public static class Builder
137152 {
138- private static final Map <String , String > EMPTY_MAP = ImmutableMap . of ();
139- private static final Multimap <String , String > EMPTY_HEADERS = ImmutableMultimap . of ();
153+ private static final Map <String , String > EMPTY_MAP = Collections . emptyMap ();
154+ private static final Map <String , Collection < String >> EMPTY_HEADERS = Collections . emptyMap ();
140155 private TDHttpMethod method ;
141156 private String path ;
142157 private Map <String , String > queryParams ;
143- private ImmutableMultimap . Builder <String , String > headerParams ;
158+ private HashMap <String , Collection < String > > headerParams ;
144159 private Optional <String > postJson = Optional .empty ();
145160 private Optional <File > file = Optional .empty ();
146161 private Optional <byte []> content = Optional .empty ();
@@ -175,23 +190,54 @@ public static Builder DELETE(String uri)
175190 }
176191
177192 public Builder addHeader (String key , String value )
193+ {
194+ return addHeaders (key , Collections .singletonList (value ));
195+ }
196+
197+ /**
198+ * @deprecated Use {@link #addHeaders(Map)} or {@link #addHeaders(String, Collection)} instead.
199+ * @param headers
200+ * @return
201+ */
202+ @ Deprecated
203+ public Builder addHeaders (Multimap <String , String > headers )
204+ {
205+ return this .addHeaders (headers .asMap ());
206+ }
207+
208+ public Builder addHeaders (String key , Collection <String > values )
178209 {
179210 if (headerParams == null ) {
180- headerParams = ImmutableMultimap . builder ();
211+ headerParams = new HashMap <> ();
181212 }
182- headerParams . put (key , value );
213+ addHeaderValues (key , values );
183214 return this ;
184215 }
185216
186- public Builder addHeaders (Multimap <String , String > headers )
217+ public Builder addHeaders (Map <String , ? extends Collection < String > > headers )
187218 {
188219 if (headerParams == null ) {
189- headerParams = ImmutableMultimap .builder ();
220+ headerParams = new HashMap <>();
221+ }
222+ for (Map .Entry <String , ? extends Collection <String >> e : headers .entrySet ()) {
223+ addHeaderValues (e .getKey (), e .getValue ());
190224 }
191- headerParams .putAll (headers );
192225 return this ;
193226 }
194227
228+ private void addHeaderValues (String key , Collection <String > values )
229+ {
230+ headerParams .compute (key , (unused , list ) -> {
231+ if (list == null ) {
232+ return new ArrayList <>(values );
233+ }
234+ else {
235+ list .addAll (values );
236+ return list ;
237+ }
238+ });
239+ }
240+
195241 public Builder addQueryParam (String key , String value )
196242 {
197243 if (queryParams == null ) {
@@ -233,7 +279,7 @@ public TDApiRequest build()
233279 method ,
234280 path ,
235281 queryParams != null ? queryParams : EMPTY_MAP ,
236- headerParams != null ? headerParams . build ( ) : EMPTY_HEADERS ,
282+ headerParams != null ? Collections . unmodifiableMap ( new HashMap <>( headerParams ) ) : EMPTY_HEADERS ,
237283 postJson ,
238284 file ,
239285 content ,
0 commit comments