Search in sources :

Example 6 with MediaType

use of com.github.ljtfreitas.julian.http.MediaType in project julian-http-client by ljtfreitas.

the class PathSerializer method write.

@Override
public void write(String boundary, MultipartFormField<Path> field, Charset charset, OutputStream output) {
    Path path = field.value;
    ContentDisposition contentDisposition = new ContentDisposition(field.name, path.getFileName().toString());
    MediaType mediaType = field.contentType.orElseGet(() -> Except.run(() -> Files.probeContentType(path)).map(MediaType::valueOf).recover(() -> MEDIA_TYPE_OCTET_STREAM));
    new MultipartFormFieldWriter(output, boundary, contentDisposition, mediaType).write(o -> Files.copy(path, output)).prop(e -> new HTTPRequestWriterException(Message.format("Cannot write multipart/form-data field {0}", field.name), e));
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) Charset(java.nio.charset.Charset) Files(java.nio.file.Files) Except(com.github.ljtfreitas.julian.Except) MediaType(com.github.ljtfreitas.julian.http.MediaType) Message(com.github.ljtfreitas.julian.Message) Path(java.nio.file.Path) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) MediaType(com.github.ljtfreitas.julian.http.MediaType)

Aggregations

MediaType (com.github.ljtfreitas.julian.http.MediaType)6 Message (com.github.ljtfreitas.julian.Message)5 HTTPRequestWriterException (com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException)5 OutputStream (java.io.OutputStream)5 Charset (java.nio.charset.Charset)5 Except (com.github.ljtfreitas.julian.Except)2 Files (java.nio.file.Files)2 DefaultHTTPRequestBody (com.github.ljtfreitas.julian.http.DefaultHTTPRequestBody)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Path (java.nio.file.Path)1