public class MultiPartContentProvider extends AbstractTypedContentProvider implements AsyncContentProvider, java.io.Closeable
A ContentProvider
for form uploads with the "multipart/form-data"
content type.
Example usage:
MultiPartContentProvider multiPart = new MultiPartContentProvider(); multiPart.addFieldPart("field", new StringContentProvider("foo"), null); multiPart.addFilePart("icon", "img.png", new PathContentProvider(Paths.get("/tmp/img.png")), null); multiPart.close(); ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) .method(HttpMethod.POST) .content(multiPart) .send();
The above example would be the equivalent of submitting this form:
<form method="POST" enctype="multipart/form-data" accept-charset="UTF-8"> <input type="text" name="field" value="foo" /> <input type="file" name="icon" /> </form>
Modifier and Type | Class and Description |
---|---|
private class |
MultiPartContentProvider.MultiPartIterator |
private static class |
MultiPartContentProvider.Part |
private static class |
MultiPartContentProvider.State |
AsyncContentProvider.Listener
ContentProvider.Typed
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.atomic.AtomicBoolean |
closed |
private static byte[] |
COLON_SPACE_BYTES |
private static byte[] |
CR_LF_BYTES |
private java.nio.ByteBuffer |
firstBoundary |
private java.nio.ByteBuffer |
lastBoundary |
private long |
length |
private AsyncContentProvider.Listener |
listener |
private static Logger |
LOG |
private java.nio.ByteBuffer |
middleBoundary |
private java.nio.ByteBuffer |
onlyBoundary |
private java.util.List<MultiPartContentProvider.Part> |
parts |
Constructor and Description |
---|
MultiPartContentProvider() |
MultiPartContentProvider(java.lang.String boundary) |
Modifier and Type | Method and Description |
---|---|
void |
addFieldPart(java.lang.String name,
ContentProvider content,
HttpFields fields)
Adds a field part with the given
name as field name, and the given
content as part content. |
void |
addFilePart(java.lang.String name,
java.lang.String fileName,
ContentProvider content,
HttpFields fields)
Adds a file part with the given
name as field name, the given
fileName as file name, and the given content as part content. |
private void |
addPart(MultiPartContentProvider.Part part) |
private long |
calculateLength() |
void |
close() |
long |
getLength() |
java.util.Iterator<java.nio.ByteBuffer> |
iterator() |
private static java.lang.String |
makeBoundary() |
void |
setListener(AsyncContentProvider.Listener listener) |
getContentType
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
isReproducible
private static final Logger LOG
private static final byte[] COLON_SPACE_BYTES
private static final byte[] CR_LF_BYTES
private final java.util.List<MultiPartContentProvider.Part> parts
private final java.nio.ByteBuffer firstBoundary
private final java.nio.ByteBuffer middleBoundary
private final java.nio.ByteBuffer onlyBoundary
private final java.nio.ByteBuffer lastBoundary
private final java.util.concurrent.atomic.AtomicBoolean closed
private AsyncContentProvider.Listener listener
private long length
public MultiPartContentProvider()
public MultiPartContentProvider(java.lang.String boundary)
private static java.lang.String makeBoundary()
public void addFieldPart(java.lang.String name, ContentProvider content, HttpFields fields)
Adds a field part with the given name
as field name, and the given
content
as part content.
The Content-Type
of this part will be obtained from:
Content-Type
header in the fields
parameter; otherwiseContentProvider.Typed.getContentType()
method if the content
parameter
implements ContentProvider.Typed
; otherwisename
- the part namecontent
- the part contentfields
- the headers associated with this partpublic void addFilePart(java.lang.String name, java.lang.String fileName, ContentProvider content, HttpFields fields)
Adds a file part with the given name
as field name, the given
fileName
as file name, and the given content
as part content.
The Content-Type
of this part will be obtained from:
Content-Type
header in the fields
parameter; otherwiseContentProvider.Typed.getContentType()
method if the content
parameter
implements ContentProvider.Typed
; otherwisename
- the part namefileName
- the file name associated to this partcontent
- the part contentfields
- the headers associated with this partprivate void addPart(MultiPartContentProvider.Part part)
public void setListener(AsyncContentProvider.Listener listener)
setListener
in interface AsyncContentProvider
listener
- the listener to be notified of content availabilityprivate long calculateLength()
public long getLength()
getLength
in interface ContentProvider
public java.util.Iterator<java.nio.ByteBuffer> iterator()
iterator
in interface java.lang.Iterable<java.nio.ByteBuffer>
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable