|
25 | 25 | import org.eclipse.jetty.http.MultiPartConfig; |
26 | 26 | import org.eclipse.jetty.http.MultiPartFormData; |
27 | 27 | import org.eclipse.jetty.http.MultiPartFormData.ContentSource; |
| 28 | +import org.eclipse.jetty.http.MultiPartFormData.Parts; |
28 | 29 | import org.eclipse.jetty.io.Content; |
29 | 30 | import org.eclipse.jetty.io.Content.Source; |
30 | 31 | import org.eclipse.jetty.server.Handler; |
|
37 | 38 | import org.eclipse.jetty.util.Callback; |
38 | 39 | import org.eclipse.jetty.util.Fields; |
39 | 40 | import org.eclipse.jetty.util.Fields.Field; |
40 | | -import org.eclipse.jetty.util.Promise; |
41 | 41 | import org.slf4j.Logger; |
42 | 42 | import org.slf4j.LoggerFactory; |
43 | 43 |
|
@@ -243,52 +243,86 @@ private static NGRequest multipartRequestToNGRequest( final Request jettyRequest |
243 | 243 | // The uploaded files, if any |
244 | 244 | final Map<String, UploadedFile> uploadedFiles = new HashMap<>(); |
245 | 245 |
|
246 | | - MultiPartFormData.onParts( jettyRequest, jettyRequest, contentType, config, new Promise.Invocable<MultiPartFormData.Parts>() { |
| 246 | + final Parts parts = MultiPartFormData.getParts( jettyRequest, jettyRequest, contentType, config ); |
247 | 247 |
|
248 | | - @Override |
249 | | - public void succeeded( MultiPartFormData.Parts parts ) { |
250 | | - parts.forEach( p -> { |
251 | | - final String partContentType = p.getHeaders().get( HttpHeader.CONTENT_TYPE ); |
| 248 | + parts.forEach( p -> { |
| 249 | + final String partContentType = p.getHeaders().get( HttpHeader.CONTENT_TYPE ); |
252 | 250 |
|
253 | | - final String parameterName = p.getName(); |
254 | | - final String parameterValue; |
| 251 | + final String parameterName = p.getName(); |
| 252 | + final String parameterValue; |
255 | 253 |
|
256 | | - // We're assuming that if this part does not have a content type, it's a regular ol' form value, to be added to the requests formValues map as usual. |
257 | | - if( partContentType == null ) { |
258 | | - parameterValue = p.getContentAsString( StandardCharsets.UTF_8 ); // FIXME: Hardcoding the character set is a little presumptuous // Hugi 2025-04-05 |
259 | | - } |
260 | | - else { |
261 | | - // We're generating a unique ID here to store the attachment under in the request. This value will be stored in the request's formValues, and can be used to fetch the uploaded data in the request's uploadedFiles map |
262 | | - final String uniqueID = UUID.randomUUID().toString(); |
263 | | - |
264 | | - parameterValue = uniqueID; |
| 254 | + // We're assuming that if this part does not have a content type, it's a regular ol' form value, to be added to the requests formValues map as usual. |
| 255 | + if( partContentType == null ) { |
| 256 | + parameterValue = p.getContentAsString( StandardCharsets.UTF_8 ); // FIXME: Hardcoding the character set is a little presumptuous // Hugi 2025-04-05 |
| 257 | + } |
| 258 | + else { |
| 259 | + // We're generating a unique ID here to store the attachment under in the request. This value will be stored in the request's formValues, and can be used to fetch the uploaded data in the request's uploadedFiles map |
| 260 | + final String uniqueID = UUID.randomUUID().toString(); |
265 | 261 |
|
266 | | - // Now we add the uploaded file to the request |
267 | | - final UploadedFile file = new UploadedFile( p.getFileName(), partContentType, Content.Source.asInputStream( p.getContentSource() ), p.getLength() ); |
268 | | - uploadedFiles.put( uniqueID, file ); |
269 | | - } |
| 262 | + parameterValue = uniqueID; |
270 | 263 |
|
271 | | - List<String> list = formValues.get( parameterName ); |
| 264 | + // Now we add the uploaded file to the request |
| 265 | + final UploadedFile file = new UploadedFile( p.getFileName(), partContentType, Content.Source.asInputStream( p.getContentSource() ), p.getLength() ); |
| 266 | + uploadedFiles.put( uniqueID, file ); |
| 267 | + } |
272 | 268 |
|
273 | | - if( list == null ) { |
274 | | - list = new ArrayList<>(); |
275 | | - formValues.put( p.getName(), list ); |
276 | | - } |
| 269 | + List<String> list = formValues.get( parameterName ); |
277 | 270 |
|
278 | | - list.add( parameterValue ); |
279 | | - } ); |
| 271 | + if( list == null ) { |
| 272 | + list = new ArrayList<>(); |
| 273 | + formValues.put( p.getName(), list ); |
280 | 274 | } |
281 | 275 |
|
282 | | - @Override |
283 | | - public void failed( Throwable failure ) { |
284 | | - // FIXME: This is here temporarily for some debugging. We should have better error handling overall for multipart uploads // Hugi 2025-07-19 |
285 | | - System.out.println( "================ Start multipart fail =====================" ); |
286 | | - failure.printStackTrace(); |
287 | | - System.out.println( "================ End multipart fail =====================" ); |
288 | | - // throw new RuntimeException( failure ); |
289 | | - } |
| 276 | + list.add( parameterValue ); |
290 | 277 | } ); |
291 | 278 |
|
| 279 | + // FIXME: Old method of getting the parts // Hugi 2025-07-19 |
| 280 | + // MultiPartFormData.onParts( jettyRequest, jettyRequest, contentType, config, new Promise.Invocable<MultiPartFormData.Parts>() { |
| 281 | + // |
| 282 | + // @Override |
| 283 | + // public void succeeded( MultiPartFormData.Parts parts ) { |
| 284 | + // parts.forEach( p -> { |
| 285 | + // final String partContentType = p.getHeaders().get( HttpHeader.CONTENT_TYPE ); |
| 286 | + // |
| 287 | + // final String parameterName = p.getName(); |
| 288 | + // final String parameterValue; |
| 289 | + // |
| 290 | + // // We're assuming that if this part does not have a content type, it's a regular ol' form value, to be added to the requests formValues map as usual. |
| 291 | + // if( partContentType == null ) { |
| 292 | + // parameterValue = p.getContentAsString( StandardCharsets.UTF_8 ); // FIXME: Hardcoding the character set is a little presumptuous // Hugi 2025-04-05 |
| 293 | + // } |
| 294 | + // else { |
| 295 | + // // We're generating a unique ID here to store the attachment under in the request. This value will be stored in the request's formValues, and can be used to fetch the uploaded data in the request's uploadedFiles map |
| 296 | + // final String uniqueID = UUID.randomUUID().toString(); |
| 297 | + // |
| 298 | + // parameterValue = uniqueID; |
| 299 | + // |
| 300 | + // // Now we add the uploaded file to the request |
| 301 | + // final UploadedFile file = new UploadedFile( p.getFileName(), partContentType, Content.Source.asInputStream( p.getContentSource() ), p.getLength() ); |
| 302 | + // uploadedFiles.put( uniqueID, file ); |
| 303 | + // } |
| 304 | + // |
| 305 | + // List<String> list = formValues.get( parameterName ); |
| 306 | + // |
| 307 | + // if( list == null ) { |
| 308 | + // list = new ArrayList<>(); |
| 309 | + // formValues.put( p.getName(), list ); |
| 310 | + // } |
| 311 | + // |
| 312 | + // list.add( parameterValue ); |
| 313 | + // } ); |
| 314 | + // } |
| 315 | + // |
| 316 | + // @Override |
| 317 | + // public void failed( Throwable failure ) { |
| 318 | + // // FIXME: This is here temporarily for some debugging. We should have better error handling overall for multipart uploads // Hugi 2025-07-19 |
| 319 | + // System.out.println( "================ Start multipart fail =====================" ); |
| 320 | + // failure.printStackTrace(); |
| 321 | + // System.out.println( "================ End multipart fail =====================" ); |
| 322 | + // // throw new RuntimeException( failure ); |
| 323 | + // } |
| 324 | + // } ); |
| 325 | + |
292 | 326 | final String method = jettyRequest.getMethod(); |
293 | 327 | final String uri = jettyRequest.getHttpURI().getCanonicalPath(); |
294 | 328 | final String httpVersion = jettyRequest.getConnectionMetaData().getHttpVersion().asString(); |
|
0 commit comments