Class FormDataBodyPart

  • Direct Known Subclasses:
    FileDataBodyPart, StreamDataBodyPart

    public class FormDataBodyPart
    extends BodyPart
    Subclass of BodyPart with specialized support for media type multipart/form-data. See RFC 2388 for the formal definition of this media type.

    For a server side application wishing to process an incoming multipart/form-data message, 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.

    For a client side application wishing to construct an outgoing multipart/form-data message, 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.
    • Field Detail

      • fileNameFix

        private final boolean fileNameFix
    • Constructor Detail

      • FormDataBodyPart

        public FormDataBodyPart()
        Instantiates an unnamed new FormDataBodyPart with a mediaType of text/plain.
      • FormDataBodyPart

        public FormDataBodyPart​(boolean fileNameFix)
        Instantiates an unnamed new FormDataBodyPart with mediaType of text/plain and 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 to true, header parser will not treat backslash as an escape character when retrieving the value of filename parameter of Content-Disposition header.
      • FormDataBodyPart

        public FormDataBodyPart​(javax.ws.rs.core.MediaType mediaType)
        Instantiates an unnamed FormDataBodyPart with the specified characteristics.
        Parameters:
        mediaType - the MediaType for this body part.
      • FormDataBodyPart

        public FormDataBodyPart​(java.lang.Object entity,
                                javax.ws.rs.core.MediaType mediaType)
        Instantiates an unnamed FormDataBodyPart with the specified characteristics.
        Parameters:
        entity - the entity for this body part.
        mediaType - the MediaType for this body part.
      • FormDataBodyPart

        public FormDataBodyPart​(java.lang.String name,
                                java.lang.String value)
        Instantiates a named FormDataBodyPart with a media type of text/plain and String value.
        Parameters:
        name - the control name for this body part.
        value - the value for this body part.
      • FormDataBodyPart

        public FormDataBodyPart​(java.lang.String name,
                                java.lang.Object entity,
                                javax.ws.rs.core.MediaType mediaType)
        Instantiates a named FormDataBodyPart with the specified characteristics.
        Parameters:
        name - the control name for this body part.
        entity - the entity for this body part.
        mediaType - the MediaType for this body part.
      • FormDataBodyPart

        public FormDataBodyPart​(FormDataContentDisposition formDataContentDisposition,
                                java.lang.String value)
        Instantiates a named FormDataBodyPart with 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,
                                java.lang.Object entity,
                                javax.ws.rs.core.MediaType mediaType)
        Instantiates a named FormDataBodyPart with the specified characteristics.
        Parameters:
        formDataContentDisposition - the content disposition header for this body part.
        entity - the entity for this body part.
        mediaType - the MediaType for 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.
      • getName

        public java.lang.String getName()
        Gets the control name.
        Returns:
        the control name.
      • setName

        public void setName​(java.lang.String name)
        Sets the control name.
        Parameters:
        name - the control name.
      • getValue

        public java.lang.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:
        javax.ws.rs.ProcessingException - if an IO error arises during reading the value.
        java.lang.IllegalStateException - if called on a body part with a media type other than text/plain
      • getValueAs

        public <T> T getValueAs​(java.lang.Class<T> clazz)
        Gets the field value after appropriate conversion to the requested type. This is useful only when the containing FormDataMultiPart instance has been received, which causes the providers property 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:
        javax.ws.rs.ProcessingException - if an IO error arises during reading an entity.
        java.lang.IllegalArgumentException - if no MessageBodyReader can be found to perform the requested conversion.
        java.lang.IllegalStateException - if this method is called when the providers property has not been set or when the entity instance is not the unconverted content of the body part entity.
      • setValue

        public void setValue​(java.lang.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:
        java.lang.IllegalStateException - if called on a body part with a media type other than text/plain.
      • setValue

        public void setValue​(javax.ws.rs.core.MediaType mediaType,
                             java.lang.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:
        true if this body part represents a simple, string-based, field value, otherwise false.