Class MultipartArgumentHelper

java.lang.Object
ru.vyarus.dropwizard.guice.test.client.builder.call.MultipartArgumentHelper

public class MultipartArgumentHelper extends Object
Helper utilities to build multipart objects for rest method analysis api.

For the most common case post(@FormDataParam("file") InputStream stream, @FormDataParam("file") FormDataContentDisposition fileDetail):

.post(multipart.fromClasspath("/some.txt"),
                   multipart.disposition("file", "some.txt"))
.

it could be a single field: post(@FormDataParam("file") FormDataBodyPart file):

.post(multipart.filePart("/some.txt"))
or to get file from classpath:
.post(multipart.streamPart("/some.txt"))
.

There might be multiple files for the same field post(@FormDataParam("file") List<FormDataBodyPart> file):

.post(Arrays.asList(
              multipart.filePart("/some.txt"),
              multipart.filePart("/other.txt")))
.

When method only accepts content-disposition mapping, it would be also used with en empty file content (as file content is not required, preserve available data): post(@FormDataParam("file") FormDataContentDisposition file)

.post(multipart.disposition("file", "some.txt"))
.

The method parameter could be a complete multipart object (with all fields inside): post(FormDataMultiPart multiPart): for such cases there is a special builder:

.post(multipart.multipart()
                  .field("foo", "val")
                  .file("file1", "/some.txt)
                  .stream("file2", "/other.txt)
                  .build())
Since:
10.10.2025
  • Constructor Details

    • MultipartArgumentHelper

      public MultipartArgumentHelper()
  • Method Details

    • createDispositionHeader

      public static String createDispositionHeader(String field, String filename)
      Header would contain both utf-8 and ascii filenames.
      Parameters:
      field - field name
      filename - file name
      Returns:
      content-disposition header
    • fromClasspath

      public InputStream fromClasspath(String path)
      Useful for rest methods declaring input stream and content disposition object.
      Parameters:
      path - classpath path
      Returns:
      input stream of classpath resource
      Throws:
      IllegalStateException - if resource not found
    • fromFile

      public InputStream fromFile(String path)
      Useful for rest methods declaring input stream and content disposition object.
      Parameters:
      path - local file path (relative to work dir)
      Returns:
      stream from local file
      Throws:
      IllegalStateException - if file does not exist
      See Also:
    • file

      public File file(String path)
      Parameters:
      path - local file path (relative to work dir)
      Returns:
      file object
      Throws:
      IllegalStateException - if file does not exist or it is a directory
    • disposition

      public org.glassfish.jersey.media.multipart.FormDataContentDisposition disposition(String field, File file)
      Create content-disposition object. Useful for resource methods with input stream and disposition object.
      Parameters:
      field - field name
      file - file to get filename from (may not exist)
      Returns:
      content-disposition object
    • disposition

      public org.glassfish.jersey.media.multipart.FormDataContentDisposition disposition(String field, String filename)
      Create content-disposition object. Useful for resource methods with input stream and disposition object. File name is applied as utf-8 and ascii.
      Parameters:
      field - field name
      filename - file name
      Returns:
      content-disposition object
    • part

      public org.glassfish.jersey.media.multipart.FormDataBodyPart part(String field, String value)
      Create simple data part. FormDataBodyPart could be declared as resource method parameter.
      Parameters:
      field - field name
      value - field value
      Returns:
      simple body part
    • filePart

      public org.glassfish.jersey.media.multipart.file.FileDataBodyPart filePart(String field, File file)
      Create file data part. FormDataBodyPart could be declared as resource method parameter (but file part can't!).
      Parameters:
      field - field name
      file - file object
      Returns:
      file body part
    • filePart

      public org.glassfish.jersey.media.multipart.file.FileDataBodyPart filePart(String field, String path)
      Create file data part from local file path. FormDataBodyPart could be declared as resource method parameter (but file part can't!).
      Parameters:
      field - field name
      path - local file path (relative to work dir)
      Returns:
      file body part
      Throws:
      IllegalStateException - if target file does not exist, or it is a directory
    • streamPart

      public org.glassfish.jersey.media.multipart.file.StreamDataBodyPart streamPart(String field, InputStream stream)
      Create stream data part. FormDataBodyPart could be declared as resource method parameter (but stream part can't!).

      Important: filename would be missed in this case!

      Parameters:
      field - field name
      stream - stream
      Returns:
      stream body part
    • streamPart

      public org.glassfish.jersey.media.multipart.file.StreamDataBodyPart streamPart(String field, InputStream stream, @Nullable String filename)
      Create stream data part. FormDataBodyPart could be declared as resource method parameter (but stream part can't!).
      Parameters:
      field - field name
      stream - stream
      filename - optional filename
      Returns:
      stream body part
    • streamPart

      public org.glassfish.jersey.media.multipart.file.StreamDataBodyPart streamPart(String field, String classpath)
      Create stream data part from classpath resource. FormDataBodyPart could be declared as resource method parameter (but stream part can't!).

      Note: filename is extracted from provided path.

      Parameters:
      field - field name
      classpath - classpath path
      Returns:
      stream body part
    • multipart

      public MultipartArgumentHelper.Builder multipart()
      Multipart object builder. Useful when resource method parameter is FormDataMultiPart (used to obtain all multipart fields).
      Returns:
      multipart object builder.