Module methanol

Class MultipartBodyPublisher.Builder

  • Enclosing class:
    MultipartBodyPublisher

    public static final class MultipartBodyPublisher.Builder
    extends java.lang.Object
    A builder of MultipartBodyPublisher instances. The default multipart subtype used by the builder is form-data.

    Any part added with a MimeBodyPublisher will have it's Content-Type header automatically appended to the part's headers during transmission.

    • Constructor Detail

      • Builder

        public Builder()
    • Method Detail

      • boundary

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder boundary​(java.lang.String boundary)
        Sets the boundary of the multipart body.
        Throws:
        java.lang.IllegalArgumentException - if the boundary is invalid
      • mediaType

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder mediaType​(MediaType mediaType)
        Sets the media type of the multipart body. If the given media type has a boundary parameter, it will be used as the body's boundary.
        Throws:
        java.lang.IllegalArgumentException - if the given media type is not a multipart type or if it has an invalid boundary parameter
      • formPart

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder formPart​(java.lang.String name,
                                                       java.net.http.HttpRequest.BodyPublisher bodyPublisher)
        Adds a form field with the given name and body.
      • formPart

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder formPart​(java.lang.String name,
                                                       java.lang.String filename,
                                                       java.net.http.HttpRequest.BodyPublisher bodyPublisher)
        Adds a form field with the given name, filename and body.
      • formPart

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder formPart​(java.lang.String name,
                                                       java.lang.String filename,
                                                       java.net.http.HttpRequest.BodyPublisher bodyPublisher,
                                                       MediaType mediaType)
        Adds a form field with the given name, filename, body and media type.
      • textPart

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder textPart​(java.lang.String name,
                                                       java.lang.Object value)
        Adds a text/plain form field with the given name and value. UTF-8 is used for encoding the field's body.
      • textPart

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder textPart​(java.lang.String name,
                                                       java.lang.Object value,
                                                       java.nio.charset.Charset charset)
        Adds a text/plain form field with the given name and value, using the given charset for encoding the field's value.
      • filePart

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder filePart​(java.lang.String name,
                                                       java.nio.file.Path file)
                                                throws java.io.FileNotFoundException
        Adds a file form field with the given name and file. The field's filename property will be that of the given path's filename component. The given path will be used to probe the part's media type. If probing fails, either by throwing an exception or returning null, application/octet-stream will be used.
        Throws:
        java.io.FileNotFoundException - if a file with the given path cannot be found
      • filePart

        @CanIgnoreReturnValue
        public MultipartBodyPublisher.Builder filePart​(java.lang.String name,
                                                       java.nio.file.Path file,
                                                       MediaType mediaType)
                                                throws java.io.FileNotFoundException
        Adds a file form field with given name, file and media type. The field's filename property will be that of the given path's filename compontent.
        Throws:
        java.io.FileNotFoundException - if a file with the given path cannot be found
      • build

        public MultipartBodyPublisher build()
        Returns a new MultipartBodyPublisher. If no boundary has been set, a randomly generated one is used.
        Throws:
        java.lang.IllegalStateException - if no part was added