Package org.eclipse.jetty.client.util
Class MultiPartContentProvider
- java.lang.Object
-
- org.eclipse.jetty.client.util.AbstractTypedContentProvider
-
- org.eclipse.jetty.client.util.MultiPartContentProvider
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Iterable<java.nio.ByteBuffer>,ContentProvider,ContentProvider.Typed,AsyncContentProvider
public class MultiPartContentProvider extends AbstractTypedContentProvider implements AsyncContentProvider, java.io.Closeable
A
ContentProviderfor 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 Classes Modifier and Type Class Description private classMultiPartContentProvider.MultiPartIteratorprivate static classMultiPartContentProvider.Partprivate static classMultiPartContentProvider.State-
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.AsyncContentProvider
AsyncContentProvider.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.api.ContentProvider
ContentProvider.Typed
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicBooleanclosedprivate static byte[]COLON_SPACE_BYTESprivate static byte[]CR_LF_BYTESprivate java.nio.ByteBufferfirstBoundaryprivate java.nio.ByteBufferlastBoundaryprivate longlengthprivate AsyncContentProvider.Listenerlistenerprivate static LoggerLOGprivate java.nio.ByteBuffermiddleBoundaryprivate java.nio.ByteBufferonlyBoundaryprivate java.util.List<MultiPartContentProvider.Part>parts
-
Constructor Summary
Constructors Constructor Description MultiPartContentProvider()MultiPartContentProvider(java.lang.String boundary)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFieldPart(java.lang.String name, ContentProvider content, HttpFields fields)Adds a field part with the givennameas field name, and the givencontentas part content.voidaddFilePart(java.lang.String name, java.lang.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 voidaddPart(MultiPartContentProvider.Part part)private longcalculateLength()voidclose()longgetLength()java.util.Iterator<java.nio.ByteBuffer>iterator()private static java.lang.StringmakeBoundary()voidsetListener(AsyncContentProvider.Listener listener)-
Methods inherited from class org.eclipse.jetty.client.util.AbstractTypedContentProvider
getContentType
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.eclipse.jetty.client.api.ContentProvider
isReproducible
-
-
-
-
Field Detail
-
LOG
private static final Logger LOG
-
COLON_SPACE_BYTES
private static final byte[] COLON_SPACE_BYTES
-
CR_LF_BYTES
private static final byte[] CR_LF_BYTES
-
parts
private final java.util.List<MultiPartContentProvider.Part> parts
-
firstBoundary
private final java.nio.ByteBuffer firstBoundary
-
middleBoundary
private final java.nio.ByteBuffer middleBoundary
-
onlyBoundary
private final java.nio.ByteBuffer onlyBoundary
-
lastBoundary
private final java.nio.ByteBuffer lastBoundary
-
closed
private final java.util.concurrent.atomic.AtomicBoolean closed
-
listener
private AsyncContentProvider.Listener listener
-
length
private long length
-
-
Method Detail
-
makeBoundary
private static java.lang.String makeBoundary()
-
addFieldPart
public void addFieldPart(java.lang.String name, ContentProvider content, HttpFields fields)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
public void addFilePart(java.lang.String name, java.lang.String fileName, ContentProvider content, HttpFields fields)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
private void addPart(MultiPartContentProvider.Part part)
-
setListener
public void setListener(AsyncContentProvider.Listener listener)
- 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
public java.util.Iterator<java.nio.ByteBuffer> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<java.nio.ByteBuffer>
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
-