Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@
import com.oracle.bmc.http.client.HttpResponse;
import com.oracle.bmc.http.client.Method;
import com.oracle.bmc.http.client.RequestInterceptor;
import io.micronaut.buffer.netty.NettyByteBufferFactory;
import io.micronaut.buffer.netty.NettyReadBufferFactory;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NextMajorVersion;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.io.buffer.ByteArrayBufferFactory;
import io.micronaut.core.io.buffer.ReadBufferFactory;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.body.AvailableByteBody;
import io.micronaut.http.body.ByteBody;
import io.micronaut.http.body.ByteBodyFactory;
import io.micronaut.http.body.CloseableByteBody;
import io.micronaut.http.body.stream.AvailableByteArrayBody;
import io.micronaut.http.body.stream.InputStreamByteBody;
import io.micronaut.http.netty.body.AvailableNettyByteBody;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.ByteBufAllocator;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -55,10 +58,13 @@
import static io.micronaut.oraclecloud.httpclient.netty.NettyClientProperties.CLASS_AND_METHOD_KEY_NAME;

@Internal
@NextMajorVersion("Get rid of micronaut-http-client dependency and replace it with micronaut-http-client-core")
final class MicronautHttpRequest implements HttpRequest {

private static final long UNKNOWN_CONTENT_LENGTH = -1;

private static final ReadBufferFactory READ_BUFFER_FACTORY;

private final NettyHttpClient client;

private final Map<String, Object> attributes;
Expand All @@ -76,6 +82,16 @@ final class MicronautHttpRequest implements HttpRequest {
@Nullable
private CloseableByteBody byteBody;

static {
ReadBufferFactory rbf;
try {
rbf = NettyReadBufferFactory.of(ByteBufAllocator.DEFAULT);
} catch (LinkageError e) {
rbf = ReadBufferFactory.getJdkFactory();
}
READ_BUFFER_FACTORY = rbf;
}

public MicronautHttpRequest(NettyHttpClient nettyHttpClient, Method method) {
client = nettyHttpClient;
this.uri = new StringBuilder(client.baseUri());
Expand Down Expand Up @@ -137,13 +153,13 @@ public HttpRequest body(Object body) {
byteBody.close();
}

if (body instanceof String) {
byteBody = new AvailableNettyByteBody(ByteBufUtil.encodeString(client.alloc(), CharBuffer.wrap((CharSequence) body), StandardCharsets.UTF_8));
if (body instanceof String s) {
byteBody = AvailableByteArrayBody.create(READ_BUFFER_FACTORY.copyOf(s, StandardCharsets.UTF_8));
returningBody = body;
} else if (body instanceof InputStream) {
body((InputStream) body, UNKNOWN_CONTENT_LENGTH);
} else if (body == null) {
byteBody = AvailableNettyByteBody.empty();
byteBody = AvailableByteArrayBody.create(READ_BUFFER_FACTORY.createEmpty());
returningBody = "";
} else {
// todo: would be better to write directly to ByteBuf here, but RequestSignerImpl does not yet support
Expand All @@ -154,7 +170,7 @@ public HttpRequest body(Object body) {
} catch (IOException e) {
throw new IllegalArgumentException("Unable to process JSON body", e);
}
byteBody = new AvailableNettyByteBody(ByteBufUtil.encodeString(client.alloc(), CharBuffer.wrap(json), StandardCharsets.UTF_8));
byteBody = AvailableByteArrayBody.create(READ_BUFFER_FACTORY.copyOf(json, StandardCharsets.UTF_8));
returningBody = json;
}
return this;
Expand All @@ -166,7 +182,7 @@ public HttpRequest body(InputStream body, long contentLength) {
body,
contentLength == UNKNOWN_CONTENT_LENGTH ? OptionalLong.empty() : OptionalLong.of(contentLength),
client.blockingIoExecutor,
NettyByteBufferFactory.DEFAULT
ByteBodyFactory.createDefault(ByteArrayBufferFactory.INSTANCE)
);
returningBody = body;
return this;
Expand Down
Loading