|
| 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 | +} |
0 commit comments