Skip to content

Commit 940a347

Browse files
Move to getParts() when consuming a multipart request
1 parent c102703 commit 940a347

File tree

1 file changed

+70
-36
lines changed

1 file changed

+70
-36
lines changed

ng-adaptor-jetty/src/main/java/ng/adaptor/jetty/NGAdaptorJetty.java

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.jetty.http.MultiPartConfig;
2626
import org.eclipse.jetty.http.MultiPartFormData;
2727
import org.eclipse.jetty.http.MultiPartFormData.ContentSource;
28+
import org.eclipse.jetty.http.MultiPartFormData.Parts;
2829
import org.eclipse.jetty.io.Content;
2930
import org.eclipse.jetty.io.Content.Source;
3031
import org.eclipse.jetty.server.Handler;
@@ -37,7 +38,6 @@
3738
import org.eclipse.jetty.util.Callback;
3839
import org.eclipse.jetty.util.Fields;
3940
import org.eclipse.jetty.util.Fields.Field;
40-
import org.eclipse.jetty.util.Promise;
4141
import org.slf4j.Logger;
4242
import org.slf4j.LoggerFactory;
4343

@@ -243,52 +243,86 @@ private static NGRequest multipartRequestToNGRequest( final Request jettyRequest
243243
// The uploaded files, if any
244244
final Map<String, UploadedFile> uploadedFiles = new HashMap<>();
245245

246-
MultiPartFormData.onParts( jettyRequest, jettyRequest, contentType, config, new Promise.Invocable<MultiPartFormData.Parts>() {
246+
final Parts parts = MultiPartFormData.getParts( jettyRequest, jettyRequest, contentType, config );
247247

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 );
252250

253-
final String parameterName = p.getName();
254-
final String parameterValue;
251+
final String parameterName = p.getName();
252+
final String parameterValue;
255253

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();
265261

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;
270263

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+
}
272268

273-
if( list == null ) {
274-
list = new ArrayList<>();
275-
formValues.put( p.getName(), list );
276-
}
269+
List<String> list = formValues.get( parameterName );
277270

278-
list.add( parameterValue );
279-
} );
271+
if( list == null ) {
272+
list = new ArrayList<>();
273+
formValues.put( p.getName(), list );
280274
}
281275

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 );
290277
} );
291278

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+
292326
final String method = jettyRequest.getMethod();
293327
final String uri = jettyRequest.getHttpURI().getCanonicalPath();
294328
final String httpVersion = jettyRequest.getConnectionMetaData().getHttpVersion().asString();

0 commit comments

Comments
 (0)