Search in sources :

Example 1 with MediaType

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

the class MapMultipartFormHTTPRequestWriter method write.

@Override
public HTTPRequestBody write(Map<String, ?> map, Charset encoding) {
    String boundary = boundaryGen.run();
    MediaType mediaType = MediaType.MULTIPART_FORM_DATA.parameter("boundary", boundary);
    return new DefaultHTTPRequestBody(mediaType, () -> serialize(map, encoding, "----" + boundary));
}
Also used : MediaType(com.github.ljtfreitas.julian.http.MediaType) DefaultHTTPRequestBody(com.github.ljtfreitas.julian.http.DefaultHTTPRequestBody)

Example 2 with MediaType

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

the class ByteArraySerializer method write.

@Override
public void write(String boundary, MultipartFormField<byte[]> field, Charset charset, OutputStream output) {
    ContentDisposition contentDisposition = new ContentDisposition(field.name);
    MediaType mediaType = field.contentType.orElse(MEDIA_TYPE_OCTET_STREAM);
    new MultipartFormFieldWriter(output, boundary, contentDisposition, mediaType).write(o -> output.write(field.value)).prop(e -> new HTTPRequestWriterException(Message.format("Cannot write multipart/form-data field {0}", field.name), e));
}
Also used : OutputStream(java.io.OutputStream) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) Charset(java.nio.charset.Charset) MediaType(com.github.ljtfreitas.julian.http.MediaType) Message(com.github.ljtfreitas.julian.Message) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) MediaType(com.github.ljtfreitas.julian.http.MediaType)

Example 3 with MediaType

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

the class ByteBufferSerializer method write.

@Override
public void write(String boundary, MultipartFormField<ByteBuffer> field, Charset charset, OutputStream output) {
    ContentDisposition contentDisposition = new ContentDisposition(field.name);
    MediaType mediaType = field.contentType.orElse(MEDIA_TYPE_OCTET_STREAM);
    new MultipartFormFieldWriter(output, boundary, contentDisposition, mediaType).write(o -> output.write(field.value.array())).prop(e -> new HTTPRequestWriterException(Message.format("Cannot write multipart/form-data field {0}", field.name), e));
}
Also used : OutputStream(java.io.OutputStream) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) Charset(java.nio.charset.Charset) MediaType(com.github.ljtfreitas.julian.http.MediaType) Message(com.github.ljtfreitas.julian.Message) ByteBuffer(java.nio.ByteBuffer) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) MediaType(com.github.ljtfreitas.julian.http.MediaType)

Example 4 with MediaType

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

the class FileSerializer method write.

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

Example 5 with MediaType

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

the class InputStreamSerializer method write.

@Override
public void write(String boundary, MultipartFormField<InputStream> field, Charset charset, OutputStream output) {
    ContentDisposition contentDisposition = new ContentDisposition(field.name);
    MediaType mediaType = field.contentType.orElse(MEDIA_TYPE_OCTET_STREAM);
    new MultipartFormFieldWriter(output, boundary, contentDisposition, mediaType).write(o -> field.value.transferTo(output)).prop(e -> new HTTPRequestWriterException(Message.format("Cannot write multipart/form-data field {0}", field.name), e));
}
Also used : OutputStream(java.io.OutputStream) HTTPRequestWriterException(com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException) Charset(java.nio.charset.Charset) MediaType(com.github.ljtfreitas.julian.http.MediaType) Message(com.github.ljtfreitas.julian.Message) InputStream(java.io.InputStream) 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