diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java index 60e8a48f8..92ba13ca1 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeInitializer.java @@ -19,7 +19,6 @@ package org.springframework.cloud.function.adapter.aws; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer; import org.springframework.cloud.function.context.config.ContextFunctionCatalogInitializer; import org.springframework.cloud.function.web.source.DestinationResolver; import org.springframework.context.ApplicationContextInitializer; @@ -64,7 +63,7 @@ public class CustomRuntimeInitializer implements ApplicationContextInitializer clazz = Thread.currentThread().getContextClassLoader().loadClass(handler); - if (FunctionInvoker.class.isAssignableFrom(clazz) || AbstractSpringFunctionAdapterInitializer.class.isAssignableFrom(clazz)) { + if (FunctionInvoker.class.isAssignableFrom(clazz)) { return false; } } diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/AzureSpringBootHttpRequestHandler.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/AzureSpringBootHttpRequestHandler.java deleted file mode 100644 index 75ae9b568..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/AzureSpringBootHttpRequestHandler.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2019-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.function.adapter.azure; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -import com.microsoft.azure.functions.ExecutionContext; -import com.microsoft.azure.functions.HttpRequestMessage; -import com.microsoft.azure.functions.HttpResponseMessage; -import com.microsoft.azure.functions.HttpResponseMessage.Builder; - -import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; -import org.springframework.messaging.Message; -import org.springframework.messaging.MessageHeaders; -import org.springframework.messaging.support.GenericMessage; - -/** - * Implementation of HTTP Request Handler for Azure which supports - * HttpRequestMessage and HttpResponseMessage the types required by - * Azure Functions for HTTP-triggered functions. - * - * @param input type - * @author Markus Gulden - * - * @since 2.1 - * @deprecated since 3.2 in favor of {@link FunctionInvoker} - */ -@Deprecated -public class AzureSpringBootHttpRequestHandler extends - AzureSpringBootRequestHandler, HttpResponseMessage> { - - public AzureSpringBootHttpRequestHandler(Class configurationClass) { - super(configurationClass); - } - - public AzureSpringBootHttpRequestHandler() { - super(); - } - - @SuppressWarnings("rawtypes") - @Override - protected Object convertEvent(HttpRequestMessage event) { - - if (event.getBody() != null) { - if (functionAcceptsMessage()) { - return new GenericMessage(event.getBody(), getHeaders(event)); - } - else { - return event.getBody(); - } - } - else { - if (functionAcceptsMessage()) { - return new GenericMessage(Optional.empty(), getHeaders(event)); - } - else { - return Optional.empty(); - } - } - } - - protected boolean functionAcceptsMessage() { - - return ((FunctionInvocationWrapper) function()).isInputTypeMessage(); - } - - private MessageHeaders getHeaders(HttpRequestMessage event) { - Map headers = new HashMap(); - - if (event.getHeaders() != null) { - headers.putAll(event.getHeaders()); - } - if (event.getQueryParameters() != null) { - headers.putAll(event.getQueryParameters()); - } - if (event.getUri() != null) { - headers.put("path", event.getUri().getPath()); - } - - if (event.getHttpMethod() != null) { - headers.put("httpMethod", event.getHttpMethod().toString()); - } - - headers.put("request", event.getBody()); - return new MessageHeaders(headers); - } - - @SuppressWarnings("unchecked") - @Override - protected HttpResponseMessage convertOutput(Object input, Object output) { - HttpRequestMessage requestMessage = (HttpRequestMessage) input; - if (functionReturnsMessage(output)) { - Message message = (Message) output; - Builder builder = requestMessage - .createResponseBuilder(com.microsoft.azure.functions.HttpStatus.OK) - .body(message.getPayload()); - for (Map.Entry entry : message.getHeaders().entrySet()) { - if (entry.getValue() != null) { - builder = builder.header(entry.getKey(), entry.getValue().toString()); - } - } - return builder.build(); - } - else { - return requestMessage - .createResponseBuilder(com.microsoft.azure.functions.HttpStatus.OK) - .body(output).build(); - } - } - - @Override - public HttpResponseMessage handleRequest(HttpRequestMessage event, - ExecutionContext context) { - HttpResponseMessage result = super.handleRequest(event, context); - if (result == null) { - result = event - .createResponseBuilder(com.microsoft.azure.functions.HttpStatus.OK) - .build(); - } - return result; - } - - protected boolean functionReturnsMessage(Object output) { - return output instanceof Message; - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/AzureSpringBootRequestHandler.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/AzureSpringBootRequestHandler.java deleted file mode 100644 index 295e7b63a..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/AzureSpringBootRequestHandler.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright 2017-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.function.adapter.azure; - -import java.util.Collection; -import java.util.function.Function; -import java.util.logging.Logger; - -import com.microsoft.azure.functions.ExecutionContext; -import com.microsoft.azure.functions.HttpRequestMessage; -import com.microsoft.azure.functions.OutputBinding; -import org.reactivestreams.Publisher; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer; -import org.springframework.cloud.function.context.FunctionCatalog; -import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; -import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; -import org.springframework.messaging.Message; -import org.springframework.messaging.support.MessageBuilder; - -/** - * @param input type - * @param result type - * @author Soby Chacko - * @author Oleg Zhurakousky - * - * @deprecated since 3.2 in favor of {@link FunctionInvoker} - */ -@Deprecated -public class AzureSpringBootRequestHandler extends AbstractSpringFunctionAdapterInitializer { - - @SuppressWarnings("rawtypes") - private static AzureSpringBootRequestHandler thisInitializer; - - private static FunctionCatalog functionCatalog; - - private final static ExecutionContextDelegate EXECUTION_CTX_DELEGATE = new ExecutionContextDelegate(); - - public AzureSpringBootRequestHandler(Class configurationClass) { - super(configurationClass); - } - - public AzureSpringBootRequestHandler() { - super(); - } - - public O handleRequest(ExecutionContext context) { - return this.handleRequest(null, context); - } - - @Override - public void close() { - thisInitializer = null; - super.close(); - } - - @SuppressWarnings("unchecked") - public O handleRequest(I input, ExecutionContext context) { - EXECUTION_CTX_DELEGATE.targetContext = context; - String name = ""; - try { - if (context != null) { - name = context.getFunctionName(); - context.getLogger().info("Handler processing a request for: " + name); - } - - /* - * We need this "caching" logic to ensure that we don't reinitialize Spring Boot app on each invocation - * since Azure creates a new instance of this handler for each invocation, - * see https://github.com/spring-cloud/spring-cloud-function/issues/425 - */ - if (thisInitializer == null /*|| !thisInitializer.functionName.equals(name)*/) { - initialize(EXECUTION_CTX_DELEGATE); - functionCatalog = this.catalog; - thisInitializer = this; - return (O) thisInitializer.handleRequest(input, context); - } - else { - this.catalog = functionCatalog; - thisInitializer.clear(name); - Publisher events = input == null ? Mono.empty() : extract(convertEvent(input)); - if (events instanceof Flux) { - events = Flux.from(events).map(v -> this.toMessage(v, context)); - } - Publisher output = thisInitializer.apply(events); - O result = result(input, output); - if (context != null) { - context.getLogger().fine("Handler processed a request for: " + name); - } - return result; - } - } - catch (Throwable ex) { - if (context != null) { - context.getLogger().throwing(getClass().getName(), "handle", ex); - } - throw new RuntimeException(ex); - } - } - - public void handleOutput(I input, OutputBinding binding, - ExecutionContext context) { - O result = handleRequest(input, context); - binding.setValue(result); - } - - @Override - protected String doResolveName(Object targetContext) { - return ((ExecutionContext) targetContext).getFunctionName(); - } - - protected Object convertEvent(I input) { - return input; - } - - protected Flux extract(Object input) { - if (!isSingleInput(this.getFunction(), input)) { - return Flux.fromIterable((Iterable) input); - } - return Flux.just(input); - } - - protected boolean isSingleInput(Function function, Object input) { - if (!(input instanceof Collection)) { - return true; - } - if (function != null) { - return Collection.class - .isAssignableFrom(((FunctionInvocationWrapper) function).getRawInputType()); - } - return ((Collection) input).size() <= 1; - } - - protected boolean isSingleOutput(Function function, Object output) { - if (!(output instanceof Collection)) { - return true; - } - if (function != null) { - Class outputType = FunctionTypeUtils.getRawType(FunctionTypeUtils.getGenericType(((FunctionInvocationWrapper) function).getOutputType())); - return Collection.class.isAssignableFrom(outputType); - } - return ((Collection) output).size() <= 1; - } - - @SuppressWarnings("rawtypes") - private Message toMessage(Object value, ExecutionContext context) { - if (value instanceof Message) { - return (Message) value; - } - else { - Object payload = value; - if (value instanceof HttpRequestMessage) { - payload = ((HttpRequestMessage) value).getBody(); - if (payload == null) { - payload = ((HttpRequestMessage) value).getQueryParameters(); - } - } - return MessageBuilder.withPayload(payload) - .setHeader(AbstractSpringFunctionAdapterInitializer.TARGET_EXECUTION_CTX_NAME, context).build(); - } - } - - - private static class ExecutionContextDelegate implements ExecutionContext { - - ExecutionContext targetContext; - - @Override - public Logger getLogger() { - if (targetContext == null || targetContext.getLogger() == null) { - return Logger.getAnonymousLogger(); - } - return targetContext.getLogger(); - } - - @Override - public String getInvocationId() { - return targetContext.getInvocationId(); - } - - @Override - public String getFunctionName() { - return targetContext.getFunctionName(); - } - - @Override - public String toString() { - return "ExecutionContextDelegate over: " + this.targetContext; - } - } -} diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManager.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManager.java index 9152df071..ed208dc93 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManager.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManager.java @@ -63,7 +63,6 @@ public interface AvroSchemaServiceManager { * @param writerSchema {@link Schema} writerSchema provided at run time * @return datum reader which can be used to read Avro payload */ - @SuppressWarnings({ "unchecked", "rawtypes" }) DatumReader getDatumReader(Class type, Schema schema, Schema writerSchema); /** diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManagerImpl.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManagerImpl.java index 77faaabd9..eee848412 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManagerImpl.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/converter/avro/AvroSchemaServiceManagerImpl.java @@ -71,6 +71,7 @@ public class AvroSchemaServiceManagerImpl implements AvroSchemaServiceManager { * @param schema {@link Schema} of object which needs to be serialized * @return datum writer which can be used to write Avro payload */ + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public DatumWriter getDatumWriter(Class type, Schema schema) { DatumWriter writer; diff --git a/spring-cloud-function-samples/function-sample-azure/src/test/java/example/MapTests.java b/spring-cloud-function-samples/function-sample-azure/src/test/java/example/MapTests.java deleted file mode 100644 index c7f3e893f..000000000 --- a/spring-cloud-function-samples/function-sample-azure/src/test/java/example/MapTests.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2012-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package example; - -import com.microsoft.azure.functions.ExecutionContext; - -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import java.util.logging.Logger; -import static org.assertj.core.api.Assertions.assertThat; - -import org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler; - -/** - * @author Dave Syer - * - */ -public class MapTests { - - @Test - public void start() throws Exception { - AzureSpringBootRequestHandler handler = new AzureSpringBootRequestHandler<>( - Config.class); - ExecutionContext ec = new ExecutionContext() { - @Override - public Logger getLogger() { - return Logger.getAnonymousLogger(); - } - - @Override - public String getInvocationId() { - return "id1"; - } - - @Override - public String getFunctionName() { - return "uppercase"; - } - }; - - String result = handler.handleRequest("{\"greeting\": \"hello\",\"name\": \"your_name\"}", ec); - handler.close(); - assertThat(result).isEqualTo("{\"greeting\":\"HELLO\",\"name\":\"YOUR_NAME\"}"); - } -}