2
2
3
3
import java .io .ByteArrayInputStream ;
4
4
import java .io .ByteArrayOutputStream ;
5
+ import java .io .UnsupportedEncodingException ;
5
6
import java .net .MalformedURLException ;
6
7
import java .net .URL ;
7
8
import java .net .URLEncoder ;
10
11
import java .util .HashMap ;
11
12
import java .util .List ;
12
13
import java .util .Map ;
13
- import java .util .Map .Entry ;
14
14
15
15
import org .apache .commons .codec .binary .Base64 ;
16
16
import org .apache .commons .fileupload .MultipartStream ;
22
22
import com .intuit .tank .http .json .JsonResponse ;
23
23
import com .intuit .tank .http .xml .XMLResponse ;
24
24
25
+ import static java .util .stream .Collectors .joining ;
26
+
25
27
/**
26
28
* utitly methods for tank http clients
27
29
* @author denisa
28
30
*
29
31
*/
30
32
public class TankHttpUtil {
31
-
32
33
private static Logger LOG = LogManager .getLogger (TankHttpUtil .class );
33
34
34
35
public static URL buildUrl (String protocol , String host , int port , String path , Map <String , String > urlVariables ) {
35
-
36
36
try {
37
- // no default port specified for http
38
- if (protocol .equalsIgnoreCase ("http" ) && port == -1 ) {
39
- return new URL (protocol , host , path + getQueryString (urlVariables ));
40
- } else if (protocol .equalsIgnoreCase ("https" ) && port == -1 ) {
41
- return new URL (protocol , host , path + getQueryString (urlVariables ));
42
- }
43
37
// ensure that port 80 and 8080 requests use http and not https
44
38
if (port == 80 || port == 8080 ) {
45
39
protocol = "http" ;
46
40
}
47
41
48
- return new URL (protocol , host , port , path + getQueryString (urlVariables ));
42
+ // no default port specified for http
43
+ return (port == -1 ) ?
44
+ new URL (protocol , host , path + getQueryString (urlVariables )) :
45
+ new URL (protocol , host , port , path + getQueryString (urlVariables ));
49
46
} catch (MalformedURLException e ) {
50
47
throw new RuntimeException (e );
51
48
}
52
49
}
53
50
54
51
public static String getQueryString (Map <String , String > urlVariables ) {
55
-
56
- StringBuilder queryString = new StringBuilder ();
57
-
58
- // Set the query string
59
- if (urlVariables != null ) {
60
- if (!urlVariables .isEmpty ()) {
61
-
62
- queryString .append ("?" );
63
-
64
- // Set<Map.Entry<String, String>> set = urlVariables.entrySet();
65
- // Iterator<Map.Entry<String, String>> iterator =
66
- // set.iterator();
67
- for (Entry <String , String > entry : urlVariables .entrySet ()) {
68
- try {
69
- StringBuilder nvp = new StringBuilder ();
70
- nvp .append (URLEncoder .encode (entry .getKey (), StandardCharsets .UTF_8 .toString ()));
71
- if (entry .getValue () != null ) {
72
- nvp .append ("=" );
73
- nvp .append (URLEncoder .encode (entry .getValue (), StandardCharsets .UTF_8 .toString ()));
52
+ if (urlVariables != null && !urlVariables .isEmpty ()) {
53
+ return "?" + urlVariables .entrySet ().stream ()
54
+ .map (entry -> {
55
+ try {
56
+ return URLEncoder .encode (entry .getKey (), StandardCharsets .UTF_8 .name ())
57
+ + "="
58
+ + URLEncoder .encode (entry .getValue (), StandardCharsets .UTF_8 .name ());
59
+ } catch (UnsupportedEncodingException ex ) {
60
+ LOG .warn ("Unable to set query string value: " + ex .getMessage ());
74
61
}
75
- nvp .append ("&" );
76
- queryString .append (nvp .toString ());
77
-
78
- } catch (Exception ex ) {
79
- LOG .warn ("Unable to set query string value: " + ex .getMessage ());
80
- }
81
- }
82
- }
83
- }
84
-
85
- // Remove the last &
86
- String reqQueryString = "" ;
87
- if (queryString .length () > 0 ) {
88
- if (queryString .charAt (queryString .length () - 1 ) == '&' )
89
- reqQueryString = queryString .deleteCharAt (queryString .length () - 1 ).toString ();
90
- else
91
- reqQueryString = queryString .toString ();
62
+ return "" ;
63
+ })
64
+ .collect (joining ("&" ));
92
65
}
93
-
94
- return reqQueryString ;
66
+ return "" ;
95
67
}
96
68
97
69
/**
@@ -101,13 +73,11 @@ public static String getQueryString(Map<String, String> urlVariables) {
101
73
*/
102
74
public static BaseResponse newResponseObject (String contentTypeHeader ) {
103
75
String contentType = StringUtils .isNotBlank (contentTypeHeader ) ? contentTypeHeader : "" ;
104
- if (contentType .contains ("xml" )) {
105
- return new XMLResponse ();
106
- } else if (contentType .contains ("json" )) {
107
- return new JsonResponse ();
108
- } else {
109
- return new BinaryResponse ();
110
- }
76
+ return contentType .contains ("xml" ) ?
77
+ new XMLResponse () :
78
+ contentType .contains ("json" ) ?
79
+ new JsonResponse () :
80
+ new BinaryResponse ();
111
81
}
112
82
113
83
public static List <PartHolder > getPartsFromBody (BaseRequest request ) {
@@ -196,20 +166,18 @@ public String getFileName() {
196
166
// filename="diamond-sword.png"
197
167
public String getContentType () {
198
168
String ct = headerMap .get ("Content-Type" );
199
- if (ct == null ) {
200
- ct = "text/plain" ;
201
- }
202
- return ct ;
169
+ return ct == null ?
170
+ "text/plain" :
171
+ ct ;
203
172
}
204
173
205
174
// Content-Disposition: form-data; name="uploadname1";
206
175
// filename="diamond-sword.png"
207
176
public String getContentDisposition () {
208
177
String ct = headerMap .get ("Content-Disposition" );
209
- if (ct == null ) {
210
- ct = "form-data" ;
211
- }
212
- return ct ;
178
+ return ct == null ?
179
+ "form-data" :
180
+ ct ;
213
181
}
214
182
215
183
/**
0 commit comments