1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.utils;
import com.yahoo.container.jdisc.HttpRequest;
import org.eclipse.jetty.http.MultiPartFormInputStream;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.TreeMap;
/**
* Wrapper around Jetty's {@link MultiPartFormInputStream}.
*
* @author bjorncs
*/
public class MultiPartFormParser {
private final MultiPartFormInputStream multipart;
public MultiPartFormParser(InputStream in, String contentType) {
this.multipart = new MultiPartFormInputStream(in, contentType, /*config*/null, /*contextTmpDir*/null);
}
public MultiPartFormParser(HttpRequest request) { this(request.getData(), request.getHeader("Content-Type")); }
public Map<String, PartItem> readParts() throws MultiPartException {
try {
Map<String, PartItem> result = new TreeMap<>();
for (Part servletPart : multipart.getParts()) {
result.put(servletPart.getName(), new PartItem(servletPart));
}
return result;
} catch (Exception e) {
throw new MultiPartException(e.getCause());
}
}
public static class PartItem {
private final String name;
private final InputStream data;
private final String contentType;
private PartItem(Part servletPart) throws IOException {
this(servletPart.getName(), servletPart.getInputStream(), servletPart.getContentType());
}
public PartItem(String name, InputStream data, String contentType) {
this.name = name;
this.data = data;
this.contentType = contentType;
}
public String name() { return name; }
public InputStream data() { return data; }
public String contentType() { return contentType; }
}
public static class MultiPartException extends IOException {
public MultiPartException(Throwable cause) { super(cause.getMessage(), cause); }
}
}
|