Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): update dependency io.micronaut:micronaut-core-bom to v4.5.1 #1106

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import io.micronaut.http.MutableHttpHeaders;
import io.micronaut.http.MutableHttpParameters;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.ServerHttpRequest;
import io.micronaut.http.body.ByteBody;
import io.micronaut.http.body.CloseableAvailableByteBody;
import io.micronaut.http.body.CloseableByteBody;
import io.micronaut.http.cookie.Cookie;
import io.micronaut.http.cookie.Cookies;
import io.micronaut.http.simple.SimpleHttpParameters;
Expand All @@ -45,21 +49,27 @@
import io.micronaut.servlet.http.ServletExchange;
import io.micronaut.servlet.http.ServletHttpRequest;
import io.micronaut.servlet.http.ServletHttpResponse;
import io.micronaut.servlet.http.body.InputStreamByteBody;
import io.netty.handler.codec.http.QueryStringDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.function.Supplier;

import static io.micronaut.servlet.http.BodyBuilder.isFormSubmission;
Expand All @@ -75,6 +85,7 @@
final class GoogleFunctionHttpRequest<B> implements
ServletHttpRequest<com.google.cloud.functions.HttpRequest, B>,
ServletExchange<com.google.cloud.functions.HttpRequest, com.google.cloud.functions.HttpResponse>,
ServerHttpRequest<B>,
FullHttpRequest<B>,
ParsedBodyHolder<B> {

Expand All @@ -85,6 +96,7 @@ final class GoogleFunctionHttpRequest<B> implements
private final HttpMethod method;
private final GoogleFunctionHeaders headers;
private final GoogleFunctionHttpResponse<?> googleResponse;
private final Supplier<ByteBody> byteBody;
private MutableHttpParameters httpParameters;
private MutableConvertibleValues<Object> attributes;
private B parsedBody;
Expand All @@ -93,8 +105,6 @@ final class GoogleFunctionHttpRequest<B> implements

private ConversionService conversionService;

private ByteArrayByteBuffer<B> servletByteBuffer;

/**
* Default constructor.
*
Expand All @@ -107,7 +117,8 @@ final class GoogleFunctionHttpRequest<B> implements
com.google.cloud.functions.HttpRequest googleRequest,
GoogleFunctionHttpResponse<?> googleResponse,
ConversionService conversionService,
BodyBuilder bodyBuilder) {
BodyBuilder bodyBuilder,
Executor ioExecutor) {
this.googleRequest = googleRequest;
this.googleResponse = googleResponse;
this.uri = URI.create(googleRequest.getUri());
Expand All @@ -125,20 +136,43 @@ final class GoogleFunctionHttpRequest<B> implements
B built = parsedBody != null ? parsedBody : (B) bodyBuilder.buildBody(this::getInputStream, this);
return Optional.ofNullable(built);
});
this.byteBody = SupplierUtil.memoized(() -> {
try {
return InputStreamByteBody.create(
googleRequest.getInputStream(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when/how is this closed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not, but i assume it doesn't really matter for functions

OptionalLong.of(googleRequest.getContentLength()),
ioExecutor
);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}

public byte[] getBodyBytes() throws IOException {
return googleRequest.getInputStream().readAllBytes();
try (CloseableByteBody streaming = byteBody().split(ByteBody.SplitBackpressureMode.FASTEST);
CloseableAvailableByteBody buffered = streaming.buffer().get()) {
return buffered.toByteArray();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new InterruptedIOException();
} catch (ExecutionException e) {
if (e.getCause() instanceof IOException ioe) {
throw ioe;
} else {
throw new RuntimeException(e);
}
}
}

@Override
public InputStream getInputStream() throws IOException {
return servletByteBuffer != null ? servletByteBuffer.toInputStream() : new ByteArrayInputStream(getBodyBytes());
return byteBody().split(ByteBody.SplitBackpressureMode.FASTEST).toInputStream();
}

@Override
public BufferedReader getReader() throws IOException {
return googleRequest.getReader();
return new BufferedReader(new InputStreamReader(getInputStream(), getCharacterEncoding()));
}

@Override
Expand Down Expand Up @@ -263,10 +297,7 @@ public void setParsedBody(B body) {
@Override
public @Nullable ByteBuffer<?> contents() {
try {
if (servletByteBuffer == null) {
this.servletByteBuffer = new ByteArrayByteBuffer<>(getInputStream().readAllBytes());
}
return servletByteBuffer;
return new ByteArrayByteBuffer<>(getBodyBytes());
} catch (IOException e) {
throw new IllegalStateException("Error getting all body contents", e);
}
Expand All @@ -277,6 +308,11 @@ public void setParsedBody(B body) {
return ExecutionFlow.just(contents());
}

@Override
public @NonNull ByteBody byteBody() {
return byteBody.get();
}

/**
* Models the headers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MutableHttpHeaders;
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.cookie.Cookie;
import io.micronaut.http.cookie.ServerCookieEncoder;
import io.micronaut.servlet.http.ServletHttpResponse;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -47,7 +47,6 @@
final class GoogleFunctionHttpResponse<B> implements ServletHttpResponse<HttpResponse, B> {

private final HttpResponseWrapper response;
private final MediaTypeCodecRegistry mediaTypeCodecRegistry;

private final ConversionService conversionService;
private MutableConvertibleValues<Object> attributes;
Expand All @@ -59,11 +58,9 @@ final class GoogleFunctionHttpResponse<B> implements ServletHttpResponse<HttpRes
* Default constructor.
*
* @param response The Google response object
* @param mediaTypeCodecRegistry The media type codec registry
*/
GoogleFunctionHttpResponse(HttpResponse response, MediaTypeCodecRegistry mediaTypeCodecRegistry, ConversionService conversionService) {
GoogleFunctionHttpResponse(HttpResponse response, ConversionService conversionService) {
this.response = new HttpResponseWrapper(response);
this.mediaTypeCodecRegistry = mediaTypeCodecRegistry;
this.conversionService = conversionService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.type.Argument;
import io.micronaut.function.executor.FunctionInitializer;
import io.micronaut.http.*;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.HttpRequestFactory;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.codec.MediaTypeCodec;
import io.micronaut.http.cookie.ClientCookieEncoder;
import io.micronaut.inject.qualifiers.Qualifiers;
import io.micronaut.json.JsonMapper;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.servlet.http.BodyBuilder;
import io.micronaut.servlet.http.DefaultServletExchange;
import io.micronaut.servlet.http.ServletExchange;
Expand All @@ -37,9 +44,25 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Executor;

/**
* Entry point into the Micronaut + GCP integration.
Expand All @@ -60,29 +83,35 @@ public class HttpFunction extends FunctionInitializer implements com.google.clou
private final ServletHttpHandler<HttpRequest, HttpResponse> httpHandler;

private final ConversionService conversionService;
private final BodyBuilder bodyBuilder;
private final Executor executor;

/**
* Default constructor.
*/
public HttpFunction() {
httpHandler = initializeHandler();
this.conversionService = applicationContext.getBean(ConversionService.class);
this.bodyBuilder = applicationContext.getBean(BodyBuilder.class);
this.executor = applicationContext.getBean(Executor.class, Qualifiers.byName(TaskExecutors.BLOCKING));
}

public HttpFunction(ApplicationContext context) {
super(context);
httpHandler = initializeHandler();
this.conversionService = applicationContext.getBean(ConversionService.class);
this.bodyBuilder = applicationContext.getBean(BodyBuilder.class);
this.executor = applicationContext.getBean(Executor.class, Qualifiers.byName(TaskExecutors.BLOCKING));
}

private ServletHttpHandler<HttpRequest, HttpResponse> initializeHandler() {
final ServletHttpHandler<HttpRequest, HttpResponse> httpHandler = new ServletHttpHandler<HttpRequest, HttpResponse>(applicationContext) {
@Override
protected ServletExchange<HttpRequest, HttpResponse> createExchange(HttpRequest request, HttpResponse response) {
final GoogleFunctionHttpResponse<Object> res =
new GoogleFunctionHttpResponse<>(response, getMediaTypeCodecRegistry(), conversionService);
new GoogleFunctionHttpResponse<>(response, conversionService);
final GoogleFunctionHttpRequest<Object> req =
new GoogleFunctionHttpRequest<>(request, res, conversionService, applicationContext.getBean(BodyBuilder.class));
new GoogleFunctionHttpRequest<>(request, res, conversionService, bodyBuilder, executor);

return new DefaultServletExchange<>(req, res);
}
Expand Down
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[versions]
micronaut-docs = "2.0.0"
micronaut = "4.4.0"
micronaut = "4.5.1"
groovy = "4.0.14"
spock = "2.3-groovy-4.0"

Expand Down Expand Up @@ -30,13 +30,13 @@ micronaut-jackson-xml = "4.3.0"
micronaut-logging = "1.3.0"
micronaut-reactor = "3.3.0"
micronaut-rxjava3 = "3.3.0"
micronaut-serde = "2.9.0"
micronaut-servlet = "4.7.0"
micronaut-tracing = "6.5.0"
micronaut-serde = "2.10.1"
micronaut-servlet = "4.9.1"
micronaut-tracing = "6.6.0"
micronaut-test = "4.3.0"
micronaut-discovery = "4.3.0"
micronaut-test-resources="2.5.2"
micronaut-validation = "4.5.0"
micronaut-validation = "4.6.0"

# Micronaut
micronaut-gradle-plugin = "4.4.0"
Expand Down
Loading