Skip to content

Commit bd0afdc

Browse files
committed
extract HttpServerEmbeddedServer
1 parent dcb2752 commit bd0afdc

7 files changed

+316
-257
lines changed

function-aws-api-proxy-test/src/main/java/io/micronaut/function/aws/proxy/test/AwsApiProxyTestServer.java

-209
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2017-2025 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.function.aws.proxy.test;
17+
18+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
19+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse;
20+
import com.sun.net.httpserver.HttpExchange;
21+
import io.micronaut.context.ApplicationContext;
22+
import io.micronaut.core.annotation.Internal;
23+
import io.micronaut.core.annotation.NonNull;
24+
import io.micronaut.core.util.StringUtils;
25+
import io.micronaut.function.aws.proxy.payload2.APIGatewayV2HTTPEventFunction;
26+
import io.micronaut.http.HttpHeaders;
27+
28+
import java.io.IOException;
29+
import java.io.OutputStream;
30+
import java.util.Base64;
31+
import java.util.Collections;
32+
import java.util.List;
33+
34+
@Internal
35+
class AwsProxyHttpHandler implements HttpHandlerApplicationContextAware {
36+
APIGatewayV2HTTPEventFunction handler;
37+
private final HttpExchangeToAwsProxyRequestAdapter requestAdapter;
38+
private final ContextProvider contextProvider;
39+
40+
AwsProxyHttpHandler(APIGatewayV2HTTPEventFunction handler) {
41+
this.handler = handler;
42+
ApplicationContext ctx = handler.getApplicationContext();
43+
this.contextProvider = ctx.getBean(ContextProvider.class);
44+
this.requestAdapter = ctx.getBean(HttpExchangeToAwsProxyRequestAdapter.class);
45+
}
46+
47+
@Override
48+
public void handle(HttpExchange httpExchange) throws IOException {
49+
APIGatewayV2HTTPEvent awsProxyRequest = requestAdapter.createAwsProxyRequest(httpExchange);
50+
APIGatewayV2HTTPResponse apiGatewayV2HTTPResponse = handler.handleRequest(awsProxyRequest, contextProvider.getContext());
51+
String payload = apiGatewayV2HTTPResponse.getBody();
52+
String contentLengthObject = apiGatewayV2HTTPResponse.getHeaders().get(HttpHeaders.CONTENT_LENGTH);
53+
int contentLength = StringUtils.isNotEmpty(contentLengthObject) ? Integer.parseInt(contentLengthObject) : 0;
54+
for (String headerName : apiGatewayV2HTTPResponse.getHeaders().keySet()) {
55+
String headerValue = apiGatewayV2HTTPResponse.getHeaders().get(headerName);
56+
List<String> headerValues = List.of(headerValue.split(","));
57+
httpExchange.getResponseHeaders().put(headerName, StringUtils.isEmpty(headerValue) ? Collections.emptyList() : headerValues);
58+
}
59+
httpExchange.sendResponseHeaders(apiGatewayV2HTTPResponse.getStatusCode(), contentLength);
60+
if (StringUtils.isNotEmpty(payload)) {
61+
final OutputStream output = httpExchange.getResponseBody();
62+
byte[] payloadBytes = payload.getBytes();
63+
if (apiGatewayV2HTTPResponse.getIsBase64Encoded()) {
64+
payloadBytes = Base64.getDecoder().decode(payloadBytes);
65+
}
66+
output.write(payloadBytes);
67+
output.flush();
68+
}
69+
httpExchange.close();
70+
}
71+
72+
@Override
73+
public @NonNull ApplicationContext getApplicationContext() {
74+
return handler.getApplicationContext();
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2017-2025 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.function.aws.proxy.test;
17+
18+
import io.micronaut.context.ApplicationContext;
19+
import io.micronaut.context.ApplicationContextBuilder;
20+
import io.micronaut.context.annotation.Factory;
21+
import io.micronaut.context.env.PropertySource;
22+
import io.micronaut.core.annotation.Internal;
23+
import io.micronaut.function.aws.proxy.payload2.APIGatewayV2HTTPEventFunction;
24+
import io.micronaut.http.server.HttpServerConfiguration;
25+
import io.micronaut.runtime.server.EmbeddedServer;
26+
import jakarta.inject.Singleton;
27+
28+
@Internal
29+
@Factory
30+
class EmbeddedServerFactory {
31+
32+
@Singleton
33+
HttpHandlerApplicationContextAware httpHandlerApplicationContextAware(ApplicationContext applicationContext) {
34+
APIGatewayV2HTTPEventFunction function = createLambdaHandler(applicationContext);
35+
return new AwsProxyHttpHandler(function);
36+
}
37+
38+
@Singleton
39+
EmbeddedServer createServer(HttpServerConfiguration httpServerConfiguration,
40+
HttpHandlerApplicationContextAware httpHandlerApplicationContextAware) {
41+
return new HttpServerEmbeddedServer(httpHandlerApplicationContextAware, httpServerConfiguration);
42+
}
43+
44+
private static APIGatewayV2HTTPEventFunction createLambdaHandler(ApplicationContext ctx) {
45+
ApplicationContextBuilder builder = ApplicationContext.builder();
46+
for (PropertySource propertySource : ctx.getEnvironment().getPropertySources()) {
47+
builder = builder.propertySources(propertySource);
48+
}
49+
return new APIGatewayV2HTTPEventFunction(builder.build());
50+
}
51+
}

function-aws-api-proxy-test/src/main/java/io/micronaut/function/aws/proxy/test/HttpExchangeToAwsProxyRequestAdapter.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
/*
2+
* Copyright 2017-2025 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package io.micronaut.function.aws.proxy.test;
217

318
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
419
import com.sun.net.httpserver.HttpExchange;
520

21+
/**
22+
* Creates a {@link APIGatewayV2HTTPEvent} given a {@link HttpExchange}.
23+
*/
624
@FunctionalInterface
725
public interface HttpExchangeToAwsProxyRequestAdapter {
826
APIGatewayV2HTTPEvent createAwsProxyRequest(HttpExchange httpExchange);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2017-2025 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.function.aws.proxy.test;
17+
18+
import com.sun.net.httpserver.HttpHandler;
19+
import io.micronaut.context.ApplicationContextProvider;
20+
21+
/**
22+
* An {@link HttpHandler} that is aware of the Micronaut {@link io.micronaut.context.ApplicationContext}.
23+
*/
24+
public interface HttpHandlerApplicationContextAware extends HttpHandler, ApplicationContextProvider {
25+
}

0 commit comments

Comments
 (0)