|
29 | 29 | import java.util.Map; |
30 | 30 | import java.util.Optional; |
31 | 31 | import java.util.TreeMap; |
| 32 | +import java.util.concurrent.ExecutionException; |
32 | 33 | import java.util.regex.Matcher; |
33 | 34 | import java.util.regex.Pattern; |
| 35 | +import java.util.stream.StreamSupport; |
34 | 36 | import org.eclipse.jetty.http.HttpField; |
35 | 37 | import org.eclipse.jetty.http.HttpHeader; |
36 | 38 | import org.eclipse.jetty.http.MimeTypes; |
| 39 | +import org.eclipse.jetty.http.MimeTypes.Type; |
| 40 | +import org.eclipse.jetty.http.MultiPart; |
37 | 41 | import org.eclipse.jetty.http.MultiPart.Part; |
| 42 | +import org.eclipse.jetty.http.MultiPartFormData; |
38 | 43 | import org.eclipse.jetty.io.Content; |
39 | 44 | import org.eclipse.jetty.server.Request; |
40 | 45 | import org.eclipse.jetty.util.Fields.Field; |
@@ -75,7 +80,21 @@ public Map<String, List<String>> getQueryParameters() { |
75 | 80 |
|
76 | 81 | @Override |
77 | 82 | public Map<String, HttpPart> getParts() { |
78 | | - // TODO |
| 83 | + |
| 84 | + // TODO initiate reading the parts asynchronously before invocation |
| 85 | + String contentType = request.getHeaders().get(HttpHeader.CONTENT_TYPE); |
| 86 | + if (Type.MULTIPART_FORM_DATA.is(MimeTypes.getContentTypeWithoutCharset(contentType))) { |
| 87 | + String boundary = MultiPart.extractBoundary(contentType); |
| 88 | + try { |
| 89 | + MultiPartFormData.Parts parts = |
| 90 | + MultiPartFormData.from(request, boundary, parser -> parser.parse(request)).get(); |
| 91 | + return StreamSupport.stream(parts.spliterator(), false) |
| 92 | + .map(HttpPartImpl::new) |
| 93 | + .collect(toMap(HttpPartImpl::getName, p -> p)); |
| 94 | + } catch (InterruptedException | ExecutionException e) { |
| 95 | + throw new RuntimeException(e); |
| 96 | + } |
| 97 | + } |
79 | 98 | return Collections.emptyMap(); |
80 | 99 | } |
81 | 100 |
|
@@ -123,6 +142,10 @@ private HttpPartImpl(Part part) { |
123 | 142 | this.part = part; |
124 | 143 | } |
125 | 144 |
|
| 145 | + public String getName() { |
| 146 | + return part.getName(); |
| 147 | + } |
| 148 | + |
126 | 149 | @Override |
127 | 150 | public Optional<String> getFileName() { |
128 | 151 | return Optional.ofNullable(part.getFileName()); |
|
0 commit comments