ru.tinkoff.kora.http.common.body.HttpBody Maven / Gradle / Ivy
The newest version!
package ru.tinkoff.kora.http.common.body;
import jakarta.annotation.Nullable;
import ru.tinkoff.kora.common.Context;
import java.io.Closeable;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
/**
* Русский: Описывает базовое тело HTTP запроса/ответа
*
* English: Describes the basic HTTP request/response body
*
*
* Пример / Example:
*
* {@code
* HttpBody.of("application/octet-stream", new byte{ 0x0 })
* }
*
*/
public interface HttpBody extends Closeable {
/**
* @return Русский: Возвращает длину тела запроса
*
* English: Returns the length of the body of the request
*/
long contentLength();
/**
* @return Русский: Возвращает тип тела запроса как значения HTTP заголовка
*
* English: Returns request body type as HTTP header values
*
*
* Пример / Example: application/json
*/
@Nullable
String contentType();
/**
* @return Русский: Возвращает полное тело запроса если оно доступно, иначе null
*
* English: Returns the full body of the request if available, otherwise null
*/
@Nullable
default ByteBuffer getFullContentIfAvailable() {
return null;
}
/**
* @return Русский: Возвращает пустое HTTP тело
*
* English: Returns empty HTTP body
*/
static EmptyHttpBody empty() {
return EmptyHttpBody.INSTANCE;
}
static DefaultFullHttpBody of(byte[] content) {
return new DefaultFullHttpBody(Context.current(), ByteBuffer.wrap(content), null);
}
static DefaultFullHttpBody of(ByteBuffer content) {
return new DefaultFullHttpBody(Context.current(), content, null);
}
static DefaultFullHttpBody of(@Nullable String contentType, byte[] content) {
return new DefaultFullHttpBody(Context.current(), ByteBuffer.wrap(content), contentType);
}
static DefaultFullHttpBody of(@Nullable String contentType, ByteBuffer content) {
return new DefaultFullHttpBody(Context.current(), content, contentType);
}
static DefaultFullHttpBody of(Context context, @Nullable String contentType, byte[] content) {
return new DefaultFullHttpBody(context, ByteBuffer.wrap(content), contentType);
}
static DefaultFullHttpBody of(Context context, @Nullable String contentType, ByteBuffer content) {
return new DefaultFullHttpBody(context, content, contentType);
}
static DefaultFullHttpBody octetStream(byte[] content) {
return new DefaultFullHttpBody(Context.current(), ByteBuffer.wrap(content), "application/octet-stream");
}
static DefaultFullHttpBody octetStream(ByteBuffer content) {
return new DefaultFullHttpBody(Context.current(), content, "application/octet-stream");
}
static DefaultFullHttpBody octetStream(Context context, byte[] content) {
return new DefaultFullHttpBody(context, ByteBuffer.wrap(content), "application/octet-stream");
}
static DefaultFullHttpBody octetStream(Context context, ByteBuffer content) {
return new DefaultFullHttpBody(context, content, "application/octet-stream");
}
static DefaultFullHttpBody plaintext(String content) {
return new DefaultFullHttpBody(Context.current(), ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)), "text/plain; charset=utf-8");
}
static DefaultFullHttpBody plaintext(ByteBuffer content) {
return new DefaultFullHttpBody(Context.current(), content.slice(), "text/plain; charset=utf-8");
}
static DefaultFullHttpBody plaintext(Context ctx, String content) {
return new DefaultFullHttpBody(ctx, ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)), "text/plain; charset=utf-8");
}
static DefaultFullHttpBody plaintext(Context ctx, ByteBuffer content) {
return new DefaultFullHttpBody(ctx, content.slice(), "text/plain; charset=utf-8");
}
static DefaultFullHttpBody json(String content) {
return new DefaultFullHttpBody(Context.current(), ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)), "application/json");
}
static DefaultFullHttpBody json(byte[] content) {
return new DefaultFullHttpBody(Context.current(), ByteBuffer.wrap(content), "application/json");
}
static DefaultFullHttpBody json(Context context, String content) {
return new DefaultFullHttpBody(context, ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)), "application/json");
}
static DefaultFullHttpBody json(Context context, byte[] content) {
return new DefaultFullHttpBody(context, ByteBuffer.wrap(content), "application/json");
}
}