Class FormDataBodyPart
- java.lang.Object
-
- org.glassfish.jersey.media.multipart.BodyPart
-
- org.glassfish.jersey.media.multipart.FormDataBodyPart
-
- All Implemented Interfaces:
jakarta.ws.rs.core.EntityPart
- Direct Known Subclasses:
FileDataBodyPart,StreamDataBodyPart
public class FormDataBodyPart extends BodyPart implements jakarta.ws.rs.core.EntityPart
Subclass ofBodyPartwith specialized support for media typemultipart/form-data. See RFC 2388 for the formal definition of this media type. For a server side application wishing to process an incomingmultipart/form-datamessage, the following features are provided:- Property accessor to retrieve the control name.
- Property accessor to retrieve the field value for a simple String field.
- Convenience accessor to retrieve the field value after conversion
through an appropriate
MessageBodyReader.
multipart/form-datamessage, the following features are provided:- Convenience constructors for named fields with either simple string values, or arbitrary entities and media types.
- Property accessor to set the control name.
- Property accessor to set the field value for a simple String field.
- Convenience accessor to set the media type and value of a "file" field.
- Author:
- Craig McClanahan, Imran M Yousuf (imran at smartitengineering.com), Paul Sandoz, Michal Gajdos
-
-
Field Summary
Fields Modifier and Type Field Description protected AtomicBooleancontentRead-
Fields inherited from class org.glassfish.jersey.media.multipart.BodyPart
contentDisposition, messageBodyWorkers
-
-
Constructor Summary
Constructors Constructor Description FormDataBodyPart()FormDataBodyPart(boolean fileNameFix)Instantiates an unnamed newFormDataBodyPartwithmediaTypeoftext/plainand setting the flag for applying the fix for erroneous file name value if content disposition header of messages coming from MS Internet Explorer (see JERSEY-759).FormDataBodyPart(jakarta.ws.rs.core.MediaType mediaType)Instantiates an unnamedFormDataBodyPartwith the specified characteristics.FormDataBodyPart(Object entity, jakarta.ws.rs.core.MediaType mediaType)Instantiates an unnamedFormDataBodyPartwith the specified characteristics.FormDataBodyPart(String name, Object entity, jakarta.ws.rs.core.MediaType mediaType)Instantiates a namedFormDataBodyPartwith the specified characteristics.FormDataBodyPart(String name, String value)Instantiates a namedFormDataBodyPartwith a media type oftext/plainand String value.FormDataBodyPart(FormDataContentDisposition formDataContentDisposition, Object entity, jakarta.ws.rs.core.MediaType mediaType)Instantiates a namedFormDataBodyPartwith the specified characteristics.FormDataBodyPart(FormDataContentDisposition formDataContentDisposition, String value)Instantiates a namedFormDataBodyPartwith the specified characteristics.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description InputStreamgetContent()<T> TgetContent(jakarta.ws.rs.core.GenericType<T> type)<T> TgetContent(Class<T> type)ContentDispositiongetContentDisposition()Overrides the behaviour onBodyPartto ensure that only instances ofFormDataContentDispositioncan be obtained.Optional<String>getFileName()FormDataContentDispositiongetFormDataContentDisposition()Gets the form data content disposition.StringgetName()Gets the control name.StringgetValue()Gets the field value for this body part.<T> TgetValueAs(Class<T> clazz)Gets the field value after appropriate conversion to the requested type.booleanisSimple()voidsetContentDisposition(ContentDisposition contentDisposition)Overrides the behaviour onBodyPartto ensure that only instances ofFormDataContentDispositioncan be set.voidsetFormDataContentDisposition(FormDataContentDisposition formDataContentDisposition)Sets the form data content disposition.voidsetName(String name)Sets the control name.voidsetValue(jakarta.ws.rs.core.MediaType mediaType, Object value)Sets the field media type and value for this body part.voidsetValue(String value)Sets the field value for this body part.-
Methods inherited from class org.glassfish.jersey.media.multipart.BodyPart
cleanup, contentDisposition, entity, getEntity, getEntityAs, getHeaders, getMediaType, getParameterizedHeaders, getParent, getProviders, setEntity, setMediaType, setMessageBodyWorkers, setParent, setProviders, type
-
-
-
-
Field Detail
-
contentRead
protected final AtomicBoolean contentRead
-
-
Constructor Detail
-
FormDataBodyPart
public FormDataBodyPart()
-
FormDataBodyPart
public FormDataBodyPart(boolean fileNameFix)
Instantiates an unnamed newFormDataBodyPartwithmediaTypeoftext/plainand setting the flag for applying the fix for erroneous file name value if content disposition header of messages coming from MS Internet Explorer (see JERSEY-759).- Parameters:
fileNameFix- If set totrue, header parser will not treat backslash as an escape character when retrieving the value offilenameparameter ofContent-Dispositionheader.
-
FormDataBodyPart
public FormDataBodyPart(jakarta.ws.rs.core.MediaType mediaType)
Instantiates an unnamedFormDataBodyPartwith the specified characteristics.- Parameters:
mediaType- theMediaTypefor this body part.
-
FormDataBodyPart
public FormDataBodyPart(Object entity, jakarta.ws.rs.core.MediaType mediaType)
Instantiates an unnamedFormDataBodyPartwith the specified characteristics.- Parameters:
entity- the entity for this body part.mediaType- theMediaTypefor this body part.
-
FormDataBodyPart
public FormDataBodyPart(String name, String value)
Instantiates a namedFormDataBodyPartwith a media type oftext/plainand String value.- Parameters:
name- the control name for this body part.value- the value for this body part.
-
FormDataBodyPart
public FormDataBodyPart(String name, Object entity, jakarta.ws.rs.core.MediaType mediaType)
Instantiates a namedFormDataBodyPartwith the specified characteristics.- Parameters:
name- the control name for this body part.entity- the entity for this body part.mediaType- theMediaTypefor this body part.
-
FormDataBodyPart
public FormDataBodyPart(FormDataContentDisposition formDataContentDisposition, String value)
Instantiates a namedFormDataBodyPartwith the specified characteristics.- Parameters:
formDataContentDisposition- the content disposition header for this body part.value- the value for this body part.
-
FormDataBodyPart
public FormDataBodyPart(FormDataContentDisposition formDataContentDisposition, Object entity, jakarta.ws.rs.core.MediaType mediaType)
Instantiates a namedFormDataBodyPartwith the specified characteristics.- Parameters:
formDataContentDisposition- the content disposition header for this body part.entity- the entity for this body part.mediaType- theMediaTypefor this body part.
-
-
Method Detail
-
getFormDataContentDisposition
public FormDataContentDisposition getFormDataContentDisposition()
Gets the form data content disposition.- Returns:
- the form data content disposition.
-
setFormDataContentDisposition
public void setFormDataContentDisposition(FormDataContentDisposition formDataContentDisposition)
Sets the form data content disposition.- Parameters:
formDataContentDisposition- the form data content disposition.
-
getContentDisposition
public ContentDisposition getContentDisposition()
Overrides the behaviour onBodyPartto ensure that only instances ofFormDataContentDispositioncan be obtained.- Overrides:
getContentDispositionin classBodyPart- Returns:
- the content disposition.
- Throws:
IllegalArgumentException- if the content disposition header cannot be parsed.
-
setContentDisposition
public void setContentDisposition(ContentDisposition contentDisposition)
Overrides the behaviour onBodyPartto ensure that only instances ofFormDataContentDispositioncan be set.- Overrides:
setContentDispositionin classBodyPart- Parameters:
contentDisposition- the content disposition which must be an instance ofFormDataContentDisposition.- Throws:
IllegalArgumentException- if the content disposition is not an instance ofFormDataContentDisposition.
-
getName
public String getName()
Gets the control name.- Specified by:
getNamein interfacejakarta.ws.rs.core.EntityPart- Returns:
- the control name.
-
getFileName
public Optional<String> getFileName()
- Specified by:
getFileNamein interfacejakarta.ws.rs.core.EntityPart
-
getContent
public InputStream getContent()
- Specified by:
getContentin interfacejakarta.ws.rs.core.EntityPart
-
getContent
public <T> T getContent(Class<T> type)
- Specified by:
getContentin interfacejakarta.ws.rs.core.EntityPart
-
getContent
public <T> T getContent(jakarta.ws.rs.core.GenericType<T> type)
- Specified by:
getContentin interfacejakarta.ws.rs.core.EntityPart
-
setName
public void setName(String name)
Sets the control name.- Parameters:
name- the control name.
-
getValue
public String getValue()
Gets the field value for this body part. This should be called only on body parts representing simple field values.- Returns:
- the simple field value.
- Throws:
jakarta.ws.rs.ProcessingException- if an IO error arises during reading the value.IllegalStateException- if called on a body part with a media type other thantext/plain
-
getValueAs
public <T> T getValueAs(Class<T> clazz)
Gets the field value after appropriate conversion to the requested type. This is useful only when the containingFormDataMultiPartinstance has been received, which causes theprovidersproperty to have been set.- Type Parameters:
T- the type of the field value.- Parameters:
clazz- Desired class into which the field value should be converted.- Returns:
- the field value.
- Throws:
jakarta.ws.rs.ProcessingException- if an IO error arises during reading an entity.IllegalArgumentException- if noMessageBodyReadercan be found to perform the requested conversion.IllegalStateException- if this method is called when theprovidersproperty has not been set or when the entity instance is not the unconverted content of the body part entity.
-
setValue
public void setValue(String value)
Sets the field value for this body part. This should be called only on body parts representing simple field values.- Parameters:
value- the field value.- Throws:
IllegalStateException- if called on a body part with a media type other thantext/plain.
-
setValue
public void setValue(jakarta.ws.rs.core.MediaType mediaType, Object value)Sets the field media type and value for this body part.- Parameters:
mediaType- the media type for this field value.value- the field value as a Java object.
-
isSimple
public boolean isSimple()
- Returns:
trueif this body part represents a simple, string-based, field value, otherwisefalse.
-
-