Class StreamDataBodyPart

All Implemented Interfaces:
EntityPart

public class StreamDataBodyPart extends FormDataBodyPart
Represents an InputStream based file submission as a part of the multipart/form-data.

It sets the InputStream as a body part with the default MediaType.APPLICATION_OCTET_STREAM_TYPE (if not specified by the user).

Note: The MIME type of the entity cannot be automatically predicted as in case of FileDataBodyPart.

The filename of the attachment is set by the user or defaults to the part's name.

Author:
Pedro Kowalski (pallipp at gmail.com), Michal Gajdos
See Also:
  • Constructor Details

    • StreamDataBodyPart

      public StreamDataBodyPart()
      Default constructor which forces user to manually set the required (name and streamEntity) properties.

      setFilename(String) can be used to set user-specified attachment filename instead of the default one.

      See Also:
    • StreamDataBodyPart

      public StreamDataBodyPart(String name, InputStream streamEntity)
      Convenience constructor which assumes the defaults for: filename (part's name) and mediaType ( MediaType.APPLICATION_OCTET_STREAM_TYPE).

      It builds the requested body part and makes the part ready for submission.

      Parameters:
      name - name of the form-data field.
      streamEntity - entity to be set as a body part.
    • StreamDataBodyPart

      public StreamDataBodyPart(String name, InputStream streamEntity, String filename)
      Convenience constructor which assumes the defaults for the mediaType (MediaType.APPLICATION_OCTET_STREAM_TYPE).

      It builds the requested body part and makes the part ready for submission.

      Parameters:
      name - name of the form-data field.
      streamEntity - entity to be set as a body part.
      filename - filename of the sent attachment (to be set as a part of content-disposition).
    • StreamDataBodyPart

      public StreamDataBodyPart(String name, InputStream streamEntity, String filename, MediaType mediaType)
      All-arguments constructor with all requested parameters set by the caller.

      It builds the requested body part and makes the part ready for submission.

      Parameters:
      name - name of the form-data field.
      streamEntity - entity to be set as a body part.
      filename - filename of the sent attachment (to be set as a part of content-disposition).
      mediaType - MIME type of the streamEntity attachment.
      Throws:
      IllegalArgumentException - if name or streamEntity are null.
  • Method Details

    • setValue

      public void setValue(MediaType mediaType, Object value) throws UnsupportedOperationException
      This operation is not supported from this implementation.
      Overrides:
      setValue in class FormDataBodyPart
      Parameters:
      mediaType - the media type for this field value.
      value - the field value as a Java object.
      Throws:
      UnsupportedOperationException - Operation not supported.
      See Also:
    • setValue

      public void setValue(String value)
      This operation is not supported from this implementation.
      Overrides:
      setValue in class FormDataBodyPart
      Parameters:
      value - the field value.
      Throws:
      UnsupportedOperationException - Operation not supported.
      See Also:
    • setEntity

      public void setEntity(Object entity) throws UnsupportedOperationException
      This operation is not supported from this implementation.
      Overrides:
      setEntity in class BodyPart
      Parameters:
      entity - the new entity object.
      Throws:
      UnsupportedOperationException - Operation not supported.
      See Also:
    • setStreamEntity

      public void setStreamEntity(InputStream streamEntity)
      Allows to explicitly set the body part entity. This method assumes the default MediaType.APPLICATION_OCTET_STREAM MIME type and doesn't have to be invoked if one of the non-default constructors was already called.

      Either this method or setStreamEntity(InputStream, MediaType) must be invoked if the default constructor was called.

      Parameters:
      streamEntity - entity to be set as a body part.
    • setStreamEntity

      public void setStreamEntity(InputStream streamEntity, MediaType mediaType)
      Allows to explicitly set the value and the MIME type of the body part entity. This method doesn't have to be invoked if one of the non-default constructors was already called.

      Either this method or setStreamEntity(InputStream) must be invoked if the default constructor was called.

      Parameters:
      streamEntity - entity to be set as a body part.
      mediaType - MIME type of the streamEntity attachment.
    • buildContentDisposition

      protected FormDataContentDisposition buildContentDisposition()
      Builds the body part content-disposition header which the specified filename (or the default one if unspecified).
      Returns:
      ready to use content-disposition header.
    • getDefaultMediaType

      protected static MediaType getDefaultMediaType()
      Gets the default MediaType to be used if the user didn't specify any.
      Returns:
      default MediaType for this body part entity.
    • setFilename

      public void setFilename(String filename)
      Sets the body part entity filename value to be used in the content-disposition header.
      Parameters:
      filename - name to be used.
    • getStreamEntity

      public InputStream getStreamEntity()
      Gets the underlying stream entity which will form the body part entity.
      Returns:
      underlying stream.
    • getFilename

      public String getFilename()
      Gets the filename value which is to be used in the content-disposition header of this body part entity.
      Returns:
      filename.
    • getFileName

      public Optional<String> getFileName()
      Specified by:
      getFileName in interface EntityPart
      Overrides:
      getFileName in class FormDataBodyPart