Package jakarta.ws.rs.core
Interface EntityPart
-
public interface EntityPart
AEntityPart
is one part of a multipart entity. As defined in RFC 7578, a multipart request or response must have a content type of "multipart/form-data" with aboundary
parameter indicating where one part ends the next may begin.Multipart entities may be received in a resource method as a collection of parts (e.g.
List<EntityPart>
) or as a form parameter (ex:@FormParam("part1Name") EntityPart part1
).Likewise, a client may receive a multipart response by reading the returned entity as a collection of EntityParts (ex:
response.readEntity(new GenericType<List<EntityPart>>() {})
).In order to send a multipart entity either as a client request or a response from a resource method, you may create the Lists using
EntityPart.Builder
. For example:Client c = ClientBuilder.newClient(); WebTarget target = c.target(someURL); List<EntityPart> parts = Arrays.asList( EntityPart.withName("name1").fileName("file1.doc").content(stream1).build(), EntityPart.withName("name2").fileName("file2.doc").content(stream2).build(), EntityPart.withName("name3").fileName("file3.xml").content(myObject, MyClass.class).mediaType("application/xml").build()); GenericEntity<List<EntityPart>> genericEntity = new GenericEntity<>(parts){}; Entity entity = Entity.entity(genericEntity, MediaType.MULTIPART_FORM_DATA); Response r = target.request().post(entity);
Note that when building a EntityPart, the name and content are required. Other properties such as headers, file name, and media type are optional. It is the responsibility of the implementation code to close the content input streams when sending the multipart content. Closing the stream before the implementation has sent it could result in unexpected exceptions. It is the responsibility of the calling code to close the stream when receiving the multipart content.- Since:
- 3.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
EntityPart.Builder
Builder forEntityPart
instances.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description InputStream
getContent()
Returns the input stream for this part.<T> T
getContent(GenericType<T> type)
Converts the content stream for this part to the specified type and returns it.<T> T
getContent(Class<T> type)
Converts the content stream for this part to the specified class and returns it.Optional<String>
getFileName()
Returns the filename of this part.MultivaluedMap<String,String>
getHeaders()
Returns an immutable multivalued map of headers for this specific part.MediaType
getMediaType()
Returns the content type of this part, and equivalent to callingMediaType.valueOf(part.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE))
.String
getName()
Returns the name of this part within the multipart entity.static EntityPart.Builder
withFileName(String partAndFileName)
Creates a newEntityPart.Builder
instance that sets the partname
andfileName
to the passed inpartAndFileName
value.static EntityPart.Builder
withName(String partName)
Creates a newEntityPart.Builder
instance.
-
-
-
Method Detail
-
withName
static EntityPart.Builder withName(String partName)
Creates a newEntityPart.Builder
instance.- Parameters:
partName
- name of the part to create within the multipart entity- Returns:
EntityPart.Builder
for building newEntityPart
instances
-
withFileName
static EntityPart.Builder withFileName(String partAndFileName)
Creates a newEntityPart.Builder
instance that sets the partname
andfileName
to the passed inpartAndFileName
value.Logically, this is the same as
EntityPart.withName(x).fileName(x)
.- Parameters:
partAndFileName
- name and filename of the part to create within the multipart entity- Returns:
EntityPart.Builder
for building newEntityPart
instances
-
getName
String getName()
Returns the name of this part within the multipart entity. This will be the "name" attribute of theContent-Disposition
header for this part.- Returns:
- the part name
-
getFileName
Optional<String> getFileName()
Returns the filename of this part. This will be the "filename" attribute of theContent-Disposition
header for this part. A filename is not required in a part, so if a filename is not present it will returnOptional.empty()
.- Returns:
- an
Optional<String>
indicating the filename if present
-
getContent
InputStream getContent()
Returns the input stream for this part. This is the content body of the part and is accessed as a stream to avoid loading potentially large amounts of data into the heap. It is the responsibility of the calling code to close this stream after receiving it.- Returns:
- an
InputStream
representing the content of this part
-
getContent
<T> T getContent(Class<T> type) throws IllegalArgumentException, IllegalStateException, IOException, WebApplicationException
Converts the content stream for this part to the specified class and returns it. The implementation must convert the stream by finding aMessageBodyReader
that handles the specified type as well as theMediaType
of the part. If noMessageBodyReader
can be found to perform the conversion, this method will throw anIllegalArgumentException
. The implementation is required to close the content stream when this method is invoked, so it may only be invoked once. Subsequent invocations will result in anIllegalStateException
. Likewise this method will throw anIllegalStateException
if it is called after callinggetContent()
orgetContent(GenericType)
.- Type Parameters:
T
- the entity type- Parameters:
type
- theClass
that the implementation should convert this part to- Returns:
- an instance of the specified
Class
representing the content of this part - Throws:
IllegalArgumentException
- if noMessageBodyReader
can handle the conversion of this part to the specified typeIllegalStateException
- if this method or any of the othergetContent
methods has already been invokedIOException
- if theMessageBodyReader.readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], MediaType, MultivaluedMap, InputStream)
method throws anIOException
WebApplicationException
- if theMessageBodyReader.readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], MediaType, MultivaluedMap, InputStream)
method throws anWebApplicationException
-
getContent
<T> T getContent(GenericType<T> type) throws IllegalArgumentException, IllegalStateException, IOException, WebApplicationException
Converts the content stream for this part to the specified type and returns it. The implementation must convert the stream by finding aMessageBodyReader
that handles the specified type as well as theMediaType
of the part. If noMessageBodyReader
can be found to perform the conversion, this method will throw anIllegalArgumentException
. The implementation is required to close the content stream when this method is invoked, so it may only be invoked once. Subsequent invocations will result in anIllegalStateException
. Likewise this method will throw anIllegalStateException
if it is called after callinggetContent()
orgetContent(Class)
.- Type Parameters:
T
- the entity type- Parameters:
type
- the generic type that the implementation should convert this part to- Returns:
- an instance of the specified generic type representing the content of this part
- Throws:
IllegalArgumentException
- if noMessageBodyReader
can handle the conversion of this part to the specified typeIllegalStateException
- if this method or any of the othergetContent
methods has already been invokedIOException
- if theMessageBodyReader.readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], MediaType, MultivaluedMap, InputStream)
method throws anIOException
WebApplicationException
- if theMessageBodyReader.readFrom(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], MediaType, MultivaluedMap, InputStream)
method throws anWebApplicationException
-
getHeaders
MultivaluedMap<String,String> getHeaders()
Returns an immutable multivalued map of headers for this specific part.- Returns:
- immutable
MultivaluedMap<String, String>
of part headers
-
getMediaType
MediaType getMediaType()
Returns the content type of this part, and equivalent to callingMediaType.valueOf(part.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE))
.- Returns:
- the media type for this part
-
-