GH-1259 Fix formatting of previous commit

This commit is contained in:
Oleg Zhurakousky
2025-04-16 14:38:31 +02:00
parent 4b0636f48f
commit e460c4451a
3 changed files with 28 additions and 32 deletions

View File

@@ -33,12 +33,11 @@ import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse; import com.google.cloud.functions.HttpResponse;
import com.google.cloud.functions.RawBackgroundFunction; import com.google.cloud.functions.RawBackgroundFunction;
import reactor.core.publisher.Flux;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.WebApplicationType;
import org.springframework.cloud.function.context.FunctionCatalog; import org.springframework.cloud.function.context.FunctionCatalog;
@@ -132,12 +131,7 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
Object resultObject = function.apply(message); Object resultObject = function.apply(message);
if (resultObject != null) { if (resultObject != null) {
Message<?> result = null; Message<?> result = resultObject instanceof Publisher<?> ? getResultFromPublisher(resultObject) : (Message<?>) resultObject;
if (resultObject instanceof Publisher<?>) {
result = getResultFromPublisher(resultObject);
} else {
result = (Message<?>) resultObject;
}
buildHttpResponse(httpRequest, httpResponse, result); buildHttpResponse(httpRequest, httpResponse, result);
} }
@@ -163,7 +157,8 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
Message<byte[]> result = null; Message<byte[]> result = null;
if (resultObject instanceof Publisher<?>) { if (resultObject instanceof Publisher<?>) {
result = getResultFromPublisher(resultObject); result = getResultFromPublisher(resultObject);
} else { }
else {
result = (Message<byte[]>) resultObject; result = (Message<byte[]>) resultObject;
} }
@@ -172,19 +167,19 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
} }
} }
/** /*
* This method build the http response from service * This method build the http response from service.
*
* @throws IOException
*/ */
private void buildHttpResponse(HttpRequest httpRequest, HttpResponse httpResponse, Message<?> result) private void buildHttpResponse(HttpRequest httpRequest, HttpResponse httpResponse, Message<?> result)
throws IOException { throws IOException {
MessageHeaders headers = result.getHeaders(); MessageHeaders headers = result.getHeaders();
if (result.getHeaders().containsKey(MessageHeaders.CONTENT_TYPE)) { if (result.getHeaders().containsKey(MessageHeaders.CONTENT_TYPE)) {
httpResponse.setContentType(result.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()); httpResponse.setContentType(result.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString());
} else if (result.getHeaders().containsKey("Content-Type")) { }
else if (result.getHeaders().containsKey("Content-Type")) {
httpResponse.setContentType(result.getHeaders().get("Content-Type").toString()); httpResponse.setContentType(result.getHeaders().get("Content-Type").toString());
} else { }
else {
httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType)); httpRequest.getContentType().ifPresent(contentType -> httpResponse.setContentType(contentType));
} }
String content = result.getPayload() instanceof String strPayload ? strPayload String content = result.getPayload() instanceof String strPayload ? strPayload
@@ -196,7 +191,8 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
String headerValue = ((Collection<?>) values).stream().map(item -> item.toString()) String headerValue = ((Collection<?>) values).stream().map(item -> item.toString())
.collect(Collectors.joining(",")); .collect(Collectors.joining(","));
httpResponse.appendHeader(header.getKey(), headerValue); httpResponse.appendHeader(header.getKey(), headerValue);
} else { }
else {
httpResponse.appendHeader(header.getKey(), header.getValue().toString()); httpResponse.appendHeader(header.getKey(), header.getValue().toString());
} }
} }
@@ -204,14 +200,15 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
if (headers.containsKey(HTTP_STATUS_CODE)) { if (headers.containsKey(HTTP_STATUS_CODE)) {
if (headers.get(HTTP_STATUS_CODE) instanceof Integer) { if (headers.get(HTTP_STATUS_CODE) instanceof Integer) {
httpResponse.setStatusCode((int) headers.get(HTTP_STATUS_CODE)); httpResponse.setStatusCode((int) headers.get(HTTP_STATUS_CODE));
} else { }
else {
log.warn("The statusCode should be an Integer value"); log.warn("The statusCode should be an Integer value");
} }
} }
} }
/** /*
* This methd get the result from reactor's publisher * This methd get the result from reactor's publisher.
* *
* For reference: https://github.com/spring-cloud/spring-cloud-function/blob/main/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java * For reference: https://github.com/spring-cloud/spring-cloud-function/blob/main/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java
*/ */
@@ -223,7 +220,8 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
if (item instanceof Message<?> messageItem) { if (item instanceof Message<?> messageItem) {
results.add(convertFromJsonIfNecessary(messageItem.getPayload())); results.add(convertFromJsonIfNecessary(messageItem.getPayload()));
lastMessage = messageItem; lastMessage = messageItem;
} else { }
else {
results.add(convertFromJsonIfNecessary(item)); results.add(convertFromJsonIfNecessary(item));
} }
} }
@@ -231,9 +229,11 @@ public class FunctionInvoker implements HttpFunction, RawBackgroundFunction {
byte[] resultsPayload; byte[] resultsPayload;
if (results.size() == 1) { if (results.size() == 1) {
resultsPayload = jsonMapper.toJson(results.get(0)); resultsPayload = jsonMapper.toJson(results.get(0));
} else if (results.size() > 1) { }
else if (results.size() > 1) {
resultsPayload = jsonMapper.toJson(results); resultsPayload = jsonMapper.toJson(results);
} else { }
else {
resultsPayload = null; resultsPayload = null;
} }

View File

@@ -22,12 +22,10 @@ import java.util.function.Supplier;
import com.github.blindpirate.extensions.CaptureSystemOutput; import com.github.blindpirate.extensions.CaptureSystemOutput;
import com.google.gson.Gson; import com.google.gson.Gson;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration; import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration;
import org.springframework.cloud.function.json.JsonMapper; import org.springframework.cloud.function.json.JsonMapper;

View File

@@ -30,14 +30,12 @@ import java.util.function.Supplier;
import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse; import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson; import com.google.gson.Gson;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito; import org.mockito.Mockito;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.test.system.OutputCaptureExtension;