From 80af1afaffcb7c8ba81cf282a82eba9a0f39f46e Mon Sep 17 00:00:00 2001 From: kaibocai <89094811+kaibocai@users.noreply.github.com> Date: Tue, 27 Sep 2022 15:17:25 -0700 Subject: [PATCH] add middlware logics (#11) * add middlware logics * update javadoc - rename middleware chain * update javadoc * update java doc * refactor code * remove di hook interface for now * update method name --- .../internal/spi/middleware/Middleware.java | 22 +++++++ .../spi/middleware/MiddlewareChain.java | 21 +++++++ .../spi/middleware/MiddlewareContext.java | 58 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/main/java/com/microsoft/azure/functions/internal/spi/middleware/Middleware.java create mode 100644 src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareChain.java create mode 100644 src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareContext.java diff --git a/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/Middleware.java b/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/Middleware.java new file mode 100644 index 0000000..b0b4602 --- /dev/null +++ b/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/Middleware.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ +package com.microsoft.azure.functions.internal.spi.middleware; + +/** + * This interface is implemented by middlewares to include middleware core logics. + * + *
This class is internal and is hence not for public use at this time. Its APIs are unstable and can change + * at any time. + */ +public interface Middleware { + /** + * Middlewares will override this method to include their own logics. + * @param context execution context that pass along middleware chain + * @param chain middleware chain {@link MiddlewareChain} + * @throws Exception any exception that is thrown out in next middleware + */ + void invoke(MiddlewareContext context, MiddlewareChain chain) throws Exception; +} diff --git a/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareChain.java b/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareChain.java new file mode 100644 index 0000000..566b6fb --- /dev/null +++ b/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareChain.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ +package com.microsoft.azure.functions.internal.spi.middleware; + +/** + * The middleware chain. + * + *
This class is internal and is hence not for public use at this time. Its APIs are unstable and can change + * at any time. + */ +public interface MiddlewareChain { + /** + * Invokes next middleware in the chain. + * @param context execution context that pass along middleware chain + * @throws Exception any exception that happen along middleware chain + */ + void doNext(MiddlewareContext context) throws Exception; +} diff --git a/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareContext.java b/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareContext.java new file mode 100644 index 0000000..70f4a96 --- /dev/null +++ b/src/main/java/com/microsoft/azure/functions/internal/spi/middleware/MiddlewareContext.java @@ -0,0 +1,58 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +package com.microsoft.azure.functions.internal.spi.middleware; + +import com.microsoft.azure.functions.ExecutionContext; + +/** + * Middleware Execution Context + * + *
This class is internal and is hence not for public use at this time. Its APIs are unstable and can change + * at any time. + */ +public interface MiddlewareContext extends ExecutionContext { + /** + * Returns the name of parameter defined in customer function. + * The input is the simple class name of target annotation. + * @param annotationSimpleClassName the simple class name of target annotation + * @return the name of parameter defined in customer function + */ + //TODO: @Nullable + String getParameterName(String annotationSimpleClassName); + + /** + * Returns corresponding parameter value sent from host by the given the parameter name. + * The return type is Object but the real type is String (currently only support get String type, + * planning to support other types in the future.) + * Make it return Object to avoid break this API in the future. + * @param name the name of parameter + * @return an object which will be String type that represents parameter value of customer function + */ + //TODO: @Nullable + Object getParameterValue(String name); + + /** + * Updates the parameter value by parameter name. It will be the actual parameter value + * used when invoke customer function. This API give middleware ability to update function input. + * @param name the name of parameter to be updated + * @param value the value of parameter to be updated + */ + void updateParameterValue(String name, Object value); + + /** + * Returns the return value from customer function invocation. + * @return an object that is the return value of customer function + */ + //TODO: @Nullable + Object getReturnValue(); + + /** + * Updates the return value from customer function invocation. + * @param returnValue value that will be updated as function return value. + */ + void updateReturnValue(Object returnValue); +}