1
1
package org .baeldung .httpclient ;
2
2
3
- import static org .hamcrest .Matchers .equalTo ;
4
- import static org .junit .Assert .assertThat ;
5
- import static org .junit .Assert .assertTrue ;
6
-
7
- import java .io .BufferedReader ;
8
- import java .io .File ;
9
- import java .io .FileInputStream ;
10
- import java .io .IOException ;
11
- import java .io .InputStream ;
12
- import java .io .InputStreamReader ;
13
- import java .net .URL ;
14
- import java .util .logging .Level ;
15
- import java .util .logging .Logger ;
16
-
17
3
import org .apache .http .HttpEntity ;
18
4
import org .apache .http .HttpStatus ;
19
- import org .apache .http .client .ClientProtocolException ;
20
5
import org .apache .http .client .methods .CloseableHttpResponse ;
21
6
import org .apache .http .client .methods .HttpPost ;
22
7
import org .apache .http .entity .ContentType ;
30
15
import org .junit .Before ;
31
16
import org .junit .Test ;
32
17
18
+ import java .io .BufferedReader ;
19
+ import java .io .File ;
20
+ import java .io .FileInputStream ;
21
+ import java .io .IOException ;
22
+ import java .io .InputStream ;
23
+ import java .io .InputStreamReader ;
24
+ import java .net .URL ;
25
+ import java .util .logging .Level ;
26
+ import java .util .logging .Logger ;
27
+
28
+ import static org .hamcrest .Matchers .equalTo ;
29
+ import static org .junit .Assert .assertThat ;
30
+ import static org .junit .Assert .assertTrue ;
31
+
33
32
public class HttpClientMultipartLiveTest {
34
33
35
34
// No longer available
@@ -48,7 +47,7 @@ public class HttpClientMultipartLiveTest {
48
47
@ Before
49
48
public final void before () {
50
49
client = HttpClientBuilder .create ()
51
- .build ();
50
+ .build ();
52
51
post = new HttpPost (SERVER );
53
52
}
54
53
@@ -67,24 +66,16 @@ public final void after() throws IllegalStateException, IOException {
67
66
LOGGER .log (Level .SEVERE , e .getMessage (), e );
68
67
throw e ;
69
68
}
70
- try {
71
- final HttpEntity entity = response .getEntity ();
72
- if (entity != null ) {
73
- final InputStream instream = entity .getContent ();
74
- instream .close ();
75
- }
76
- } finally {
77
- response .close ();
78
- }
69
+ ResponseUtil .closeResponse (response );
79
70
}
80
71
81
72
// tests
82
73
83
74
@ Test
84
75
public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions () throws IOException {
85
76
final URL url = Thread .currentThread ()
86
- .getContextClassLoader ()
87
- .getResource ("uploads/" + TEXTFILENAME );
77
+ .getContextClassLoader ()
78
+ .getResource ("uploads/" + TEXTFILENAME );
88
79
89
80
final File file = new File (url .getPath ());
90
81
final FileBody fileBody = new FileBody (file , ContentType .DEFAULT_BINARY );
@@ -102,7 +93,7 @@ public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExce
102
93
response = client .execute (post );
103
94
104
95
final int statusCode = response .getStatusLine ()
105
- .getStatusCode ();
96
+ .getStatusCode ();
106
97
final String responseString = getContent ();
107
98
final String contentTypeInHeader = getContentTypeHeader ();
108
99
assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -113,10 +104,10 @@ public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExce
113
104
}
114
105
115
106
@ Test
116
- public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExeption () throws ClientProtocolException , IOException {
107
+ public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExeption () throws IOException {
117
108
final URL url = Thread .currentThread ()
118
- .getContextClassLoader ()
119
- .getResource ("uploads/" + TEXTFILENAME );
109
+ .getContextClassLoader ()
110
+ .getResource ("uploads/" + TEXTFILENAME );
120
111
final File file = new File (url .getPath ());
121
112
final String message = "This is a multipart post" ;
122
113
final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -127,7 +118,7 @@ public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody
127
118
post .setEntity (entity );
128
119
response = client .execute (post );
129
120
final int statusCode = response .getStatusLine ()
130
- .getStatusCode ();
121
+ .getStatusCode ();
131
122
final String responseString = getContent ();
132
123
final String contentTypeInHeader = getContentTypeHeader ();
133
124
assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -138,13 +129,13 @@ public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody
138
129
}
139
130
140
131
@ Test
141
- public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws ClientProtocolException , IOException {
132
+ public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws IOException {
142
133
final URL url = Thread .currentThread ()
143
- .getContextClassLoader ()
144
- .getResource ("uploads/" + ZIPFILENAME );
134
+ .getContextClassLoader ()
135
+ .getResource ("uploads/" + ZIPFILENAME );
145
136
final URL url2 = Thread .currentThread ()
146
- .getContextClassLoader ()
147
- .getResource ("uploads/" + IMAGEFILENAME );
137
+ .getContextClassLoader ()
138
+ .getResource ("uploads/" + IMAGEFILENAME );
148
139
final InputStream inputStream = new FileInputStream (url .getPath ());
149
140
final File file = new File (url2 .getPath ());
150
141
final String message = "This is a multipart post" ;
@@ -157,7 +148,7 @@ public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandA
157
148
post .setEntity (entity );
158
149
response = client .execute (post );
159
150
final int statusCode = response .getStatusLine ()
160
- .getStatusCode ();
151
+ .getStatusCode ();
161
152
final String responseString = getContent ();
162
153
final String contentTypeInHeader = getContentTypeHeader ();
163
154
assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -169,7 +160,7 @@ public final void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandA
169
160
}
170
161
171
162
@ Test
172
- public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws ClientProtocolException , IOException {
163
+ public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException () throws IOException {
173
164
final String message = "This is a multipart post" ;
174
165
final byte [] bytes = "binary code" .getBytes ();
175
166
final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -180,7 +171,7 @@ public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBod
180
171
post .setEntity (entity );
181
172
response = client .execute (post );
182
173
final int statusCode = response .getStatusLine ()
183
- .getStatusCode ();
174
+ .getStatusCode ();
184
175
final String responseString = getContent ();
185
176
final String contentTypeInHeader = getContentTypeHeader ();
186
177
assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
@@ -192,21 +183,21 @@ public final void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBod
192
183
193
184
// UTIL
194
185
195
- final String getContent () throws IOException {
186
+ private String getContent () throws IOException {
196
187
rd = new BufferedReader (new InputStreamReader (response .getEntity ()
197
- .getContent ()));
188
+ .getContent ()));
198
189
String body = "" ;
199
- String content = "" ;
190
+ StringBuilder content = new StringBuilder () ;
200
191
while ((body = rd .readLine ()) != null ) {
201
- content += body + "\n " ;
192
+ content . append ( body ). append ( "\n " ) ;
202
193
}
203
- return content .trim ();
194
+ return content .toString (). trim ();
204
195
}
205
196
206
- final String getContentTypeHeader () throws IOException {
197
+ private String getContentTypeHeader () throws IOException {
207
198
return post .getEntity ()
208
- .getContentType ()
209
- .toString ();
199
+ .getContentType ()
200
+ .toString ();
210
201
}
211
202
212
203
}
0 commit comments