Class MultiPartContentProvider
java.lang.Object
org.eclipse.jetty.client.util.AbstractTypedContentProvider
org.eclipse.jetty.client.util.MultiPartContentProvider
- All Implemented Interfaces:
Closeable, AutoCloseable, Iterable<ByteBuffer>, ContentProvider, ContentProvider.Typed, AsyncContentProvider
public class MultiPartContentProvider
extends AbstractTypedContentProvider
implements AsyncContentProvider, 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>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classprivate static classprivate static enumNested classes/interfaces inherited from interface AsyncContentProvider
AsyncContentProvider.ListenerNested classes/interfaces inherited from interface ContentProvider
ContentProvider.Typed -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicBooleanprivate static final byte[]private static final byte[]private final ByteBufferprivate final ByteBufferprivate longprivate AsyncContentProvider.Listenerprivate static final Loggerprivate final ByteBufferprivate final ByteBufferprivate final List<MultiPartContentProvider.Part> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFieldPart(String name, ContentProvider content, HttpFields fields) Adds a field part with the givennameas field name, and the givencontentas part content.voidaddFilePart(String name, String fileName, ContentProvider content, HttpFields fields) Adds a file part with the givennameas field name, the givenfileNameas file name, and the givencontentas part content.private voidprivate longvoidclose()longiterator()private static StringvoidsetListener(AsyncContentProvider.Listener listener) Methods inherited from class AbstractTypedContentProvider
getContentTypeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ContentProvider
isReproducibleMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
LOG
-
COLON_SPACE_BYTES
private static final byte[] COLON_SPACE_BYTES -
CR_LF_BYTES
private static final byte[] CR_LF_BYTES -
parts
-
firstBoundary
-
middleBoundary
-
onlyBoundary
-
lastBoundary
-
closed
-
listener
-
length
private long length
-
-
Constructor Details
-
MultiPartContentProvider
public MultiPartContentProvider() -
MultiPartContentProvider
-
-
Method Details
-
makeBoundary
-
addFieldPart
Adds a field part with the given
nameas field name, and the givencontentas part content.The
Content-Typeof this part will be obtained from:- the
Content-Typeheader in thefieldsparameter; otherwise - the
ContentProvider.Typed.getContentType()method if thecontentparameter implementsContentProvider.Typed; otherwise - "text/plain"
- Parameters:
name- the part namecontent- the part contentfields- the headers associated with this part
- the
-
addFilePart
Adds a file part with the given
nameas field name, the givenfileNameas file name, and the givencontentas part content.The
Content-Typeof this part will be obtained from:- the
Content-Typeheader in thefieldsparameter; otherwise - the
ContentProvider.Typed.getContentType()method if thecontentparameter implementsContentProvider.Typed; otherwise - "application/octet-stream"
- Parameters:
name- the part namefileName- the file name associated to this partcontent- the part contentfields- the headers associated with this part
- the
-
addPart
-
setListener
- Specified by:
setListenerin interfaceAsyncContentProvider- Parameters:
listener- the listener to be notified of content availability
-
calculateLength
private long calculateLength() -
getLength
public long getLength()- Specified by:
getLengthin interfaceContentProvider- Returns:
- the content length, if known, or -1 if the content length is unknown
-
iterator
- Specified by:
iteratorin interfaceIterable<ByteBuffer>
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-