├── mvnBuild.bat ├── mvnBuildSkipTests.bat ├── src ├── main │ └── java │ │ └── com │ │ └── microsoft │ │ └── azure │ │ └── functions │ │ ├── package-info.java │ │ ├── annotation │ │ ├── package-info.java │ │ ├── AccessRights.java │ │ ├── Cardinality.java │ │ ├── AuthorizationLevel.java │ │ ├── StorageAccount.java │ │ ├── BindingName.java │ │ ├── HttpOutput.java │ │ ├── CustomBinding.java │ │ ├── FunctionName.java │ │ ├── EventGridTrigger.java │ │ ├── TableOutput.java │ │ ├── TwilioSmsOutput.java │ │ ├── SendGridOutput.java │ │ ├── EventHubOutput.java │ │ ├── ServiceBusTopicOutput.java │ │ ├── QueueTrigger.java │ │ ├── QueueOutput.java │ │ ├── BlobOutput.java │ │ ├── BlobInput.java │ │ ├── BlobTrigger.java │ │ ├── ServiceBusQueueOutput.java │ │ ├── ServiceBusQueueTrigger.java │ │ ├── EventHubTrigger.java │ │ ├── ServiceBusTopicTrigger.java │ │ ├── TableInput.java │ │ ├── TimerTrigger.java │ │ ├── CosmosDBInput.java │ │ ├── CosmosDBOutput.java │ │ ├── CosmosDBTrigger.java │ │ └── HttpTrigger.java │ │ ├── OutputBinding.java │ │ ├── HttpMethod.java │ │ ├── HttpStatusType.java │ │ ├── ExecutionContext.java │ │ ├── HttpStatus.java │ │ ├── HttpResponseMessage.java │ │ └── HttpRequestMessage.java └── test │ └── java │ └── com │ └── microsoft │ └── azure │ └── functions │ ├── HttpStatusTest.java │ └── annotation │ └── BindingTest.java ├── mvnGenerateArchetype.bat ├── CODEOWNERS ├── appveyor.yml ├── .gitignore ├── updateVersions.bat ├── LICENSE ├── pom.xml └── README.md /mvnBuild.bat: -------------------------------------------------------------------------------- 1 | mvn clean install -U -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B -Dgpg.skip -------------------------------------------------------------------------------- /mvnBuildSkipTests.bat: -------------------------------------------------------------------------------- 1 | mvn clean install -Dmaven.javadoc.skip=true -Dmaven.test.skip -U -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | /** 8 | * Root package 9 | */ 10 | 11 | package com.microsoft.azure.functions; 12 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | /** 8 | * Annotations and support classes for use as part of the Java API for Azure Functions. 9 | */ 10 | 11 | package com.microsoft.azure.functions.annotation; 12 | -------------------------------------------------------------------------------- /mvnGenerateArchetype.bat: -------------------------------------------------------------------------------- 1 | set atchetypeVersion=%1 2 | 3 | mvn archetype:generate -DarchetypeCatalog="local" -DarchetypeGroupId="com.microsoft.azure" -DarchetypeArtifactId="azure-functions-archetype" -DarchetypeVersion="%atchetypeVersion%" -DgroupId="com.microsoft" -DartifactId="e2etestproject" -Dversion="1.0-SNAPSHOT" -Dpackage="com.microsoft" -DappRegion="westus" -DresourceGroup="e2etest-java-functions-group" -DappName="e2etest-java-functions" -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B 4 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | # CODEOWNERS is a GitHub standard to specify who is automatically assigned pull requests to review. 2 | # This helps to prevent pull requests from languishing without review. 3 | # GitHub can also be configured to require review from code owners before a pull request can be merged. 4 | 5 | # Further reading is available from the following two URLs: 6 | # https://blog.github.com/2017-07-06-introducing-code-owners/ 7 | # https://help.github.com/articles/about-codeowners/ 8 | 9 | # Default owner for repo 10 | * @pragnagopa -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: '{build}' 2 | 3 | image: Visual Studio 2017 4 | 5 | pull_requests: 6 | do_not_increment_build_number: true 7 | 8 | branches: 9 | only: 10 | - dev 11 | - master 12 | 13 | environment: 14 | matrix: 15 | - JAVA_HOME: C:\Program Files\Java\jdk1.8.0 16 | 17 | install: 18 | - cmd: echo %JAVA_HOME% 19 | - ps: Get-Command mvn 20 | 21 | build_script: 22 | - ps: '& .\build.ps1' 23 | 24 | artifacts: 25 | - path: 'azure-functions-java-library/target/**.jar' 26 | 27 | cache: 28 | - C:\maven\ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | /.idea/* 25 | /target/* 26 | /bin 27 | /.classpath 28 | /.settings/* 29 | .project 30 | /azure-maven-archetypes/ 31 | /azure-maven-plugins/ 32 | /ciTestDir/* 33 | /target/ 34 | /Azure.Functions.Cli/* 35 | /azure-functions-java-worker/ 36 | -------------------------------------------------------------------------------- /updateVersions.bat: -------------------------------------------------------------------------------- 1 | set libraryVersion=%1 2 | set pluginVersion=%2 3 | echo setting azure.functions.java.library.version to %libraryVersion% 4 | call mvn versions:set-property -Dproperty=azure.functions.java.library.version -DnewVersion=%libraryVersion% -U -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B 5 | IF DEFINED pluginVersion ( 6 | echo setting azure.functions.maven.plugin.version to %pluginVersion% 7 | call mvn versions:set-property -Dproperty=azure.functions.maven.plugin.version -DnewVersion=%pluginVersion% -U -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B 8 | ) 9 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/AccessRights.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | /** 10 | * Azure Service Bus permission. 11 | * 12 | * @since 1.0.0 13 | */ 14 | public enum AccessRights { 15 | /** 16 | * Confers the right to manage the topology of the namespace, including creating and deleting entities. 17 | */ 18 | MANAGE, 19 | 20 | /** 21 | * Confers the right to listen (relay) or receive (queue, subscriptions) and all related message handling. 22 | */ 23 | LISTEN 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/Cardinality.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | /** 10 | *

11 | * Cardinality of the EventHubTrigger input. Choose 'ONE' if the input is a single message or 'Many' 12 | * if the input is an array of messages. 'Many' is the default if unspecified 13 | *

14 | * 15 | * @since 1.0.0 16 | */ 17 | public enum Cardinality { 18 | /** 19 | * To receive a single message, set cardinality to ONE 20 | */ 21 | ONE, 22 | 23 | /** 24 | * To receive events in a batch, set cardinality to MANY. This is the default if omitted. 25 | */ 26 | MANY 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/OutputBinding.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions; 8 | 9 | /** 10 | *

This type should be used with the parameter of output bindings.

11 | * 12 | * @since 1.0.0 13 | */ 14 | public interface OutputBinding { 15 | /** 16 | * Get the value to be passed to the output binding. 17 | * @return The actual value to be passed to the output binding. 18 | */ 19 | T getValue(); 20 | 21 | /** 22 | * Set the value to be passed to the output binding. 23 | * @param value The actual value to be passed to the output binding. 24 | */ 25 | void setValue(T value); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/AuthorizationLevel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | /** 10 | *

Azure HTTP authorization level, Determines what keys, if any, need to be present on the request in order to 11 | * invoke the function.

12 | * 13 | * @since 1.0.0 14 | */ 15 | public enum AuthorizationLevel { 16 | /** 17 | * No API key is required. 18 | */ 19 | ANONYMOUS, 20 | 21 | /** 22 | * A function-specific API key is required. This is the default value if none is provided. 23 | */ 24 | FUNCTION, 25 | 26 | /** 27 | * The master key is required. 28 | */ 29 | ADMIN 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/microsoft/azure/functions/HttpStatusTest.java: -------------------------------------------------------------------------------- 1 | package com.microsoft.azure.functions; 2 | 3 | import org.junit.*; 4 | 5 | import static junit.framework.TestCase.*; 6 | 7 | /** 8 | * Unit tests that enforce annotation contracts and conventions for Functions 9 | */ 10 | public class HttpStatusTest { 11 | @Test 12 | public void set_custom_httpstatuscode() { 13 | HttpStatusType customHttpStatus = HttpStatusType.custom(209); 14 | assertTrue(customHttpStatus.value() == 209); 15 | } 16 | 17 | @Test 18 | public void set_standard_httpstatuscode() { 19 | HttpStatusType customHttpStatus = HttpStatus.OK; 20 | assertTrue(customHttpStatus.value() == 200); 21 | } 22 | 23 | @Test(expected = IllegalArgumentException.class) 24 | public void set_invalid_httpstatuscode() { 25 | HttpStatusType.custom(-100); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/HttpMethod.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions; 8 | 9 | import java.util.Locale; 10 | 11 | /** 12 | * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See 13 | * License.txt in the project root for license information. 14 | */ 15 | 16 | public enum HttpMethod { 17 | 18 | GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE; 19 | 20 | /** 21 | * Converts passed value to upper case to extract valueOf() of this Enum. 22 | * 23 | * @param value of http method in any case 24 | * @return this enum 25 | */ 26 | public static HttpMethod value(String value) { 27 | return HttpMethod.valueOf(value.toUpperCase(Locale.ROOT)); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/HttpStatusType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions; 8 | 9 | /** 10 | * Defines an HTTP Status Type 11 | * 12 | * @since 1.0 13 | */ 14 | public interface HttpStatusType { 15 | 16 | public int value(); 17 | 18 | /** 19 | * Creates a custom (non-standard) HTTP Status code. 20 | * @param code for HttpStatusCode 21 | * @return HttpStatusType 22 | */ 23 | public static HttpStatusType custom(final int code) { 24 | if (code <= 0) { 25 | throw new IllegalArgumentException("A positive integer must be provided."); 26 | } 27 | 28 | return new HttpStatusType() { 29 | @Override 30 | public int value() { 31 | return code; 32 | } 33 | }; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/StorageAccount.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Apply this annotation to a method if you have multiple Azure Storage triggers/input/output in that method which 16 | * share the same app setting name of Azure Storage connection string.

17 | * 18 | * @since 1.0.0 19 | */ 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Target(ElementType.METHOD) 22 | public @interface StorageAccount { 23 | /** 24 | * Defines the app setting name that contains the Azure Storage connection string. 25 | * @return The app setting name of the connection string. 26 | */ 27 | String value(); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/BindingName.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would come from Azure Functions runtime. Use this 17 | * annotation when you want to get the value of trigger metadata, or when you defined your own 18 | * bindings in function.json manually. 19 | *

20 | * 21 | * @since 1.0.0 22 | */ 23 | @Target(ElementType.PARAMETER) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | public @interface BindingName { 26 | /** 27 | * Defines the trigger metadata name or binding name defined in function.json. 28 | * 29 | * @return The trigger metadata name or binding name. 30 | */ 31 | String value(); 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/ExecutionContext.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions; 8 | 9 | import java.util.logging.Logger; 10 | 11 | /** 12 | * The execution context enables interaction with the Azure Functions execution environment. 13 | * 14 | * @since 1.0.0 15 | */ 16 | public interface ExecutionContext { 17 | /** 18 | * Returns the built-in logger, which is integrated with the logging functionality provided in the Azure Functions 19 | * portal, as well as in Azure Application Insights. 20 | * 21 | * @return A Java logger that will see output directed to Azure Portal, as well as any other configured output 22 | * locations. 23 | */ 24 | Logger getLogger(); 25 | 26 | /** 27 | * Returns the invocation ID for the function call. 28 | * @return the invocation ID for the function call. 29 | */ 30 | String getInvocationId(); 31 | 32 | /** 33 | * Returns the function name. 34 | * @return the function name. 35 | */ 36 | String getFunctionName(); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/HttpOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be send back to the user as an HTTP response. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | * 23 | * 24 | * 25 | * @since 1.0.0 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 29 | public @interface HttpOutput { 30 | /** 31 | * The variable name used in function.json. 32 | * @return The variable name used in function.json. 33 | */ 34 | String name(); 35 | 36 | /** 37 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

38 | * 42 | * @return The dataType which will be used by the Functions runtime. 43 | */ 44 | String dataType() default ""; 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/CustomBinding.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | 12 | /** 13 | *

14 | * Place this on a parameter to define a custom binding

15 | * 16 | * 21 | * 22 | *

The following example shows a Java function that uses a customBinding:

23 | * 24 | *
{@literal @}FunctionName("CustomBindingTriggerSample")
25 |  * public void logCustomTriggerInput(
26 |  *    {@literal @}CustomBinding(direction = "in", name = "inputParameterName", type = "customBindingTrigger") String customTriggerInput
27 |  *    final ExecutionContext context
28 |  * ) {
29 |  *     context.getLogger().info(customTriggerInput);
30 |  * }
31 | * 32 | * @since 1.0.0 33 | */ 34 | 35 | @Retention(RetentionPolicy.RUNTIME) 36 | public @interface CustomBinding { 37 | /** 38 | * The variable name used in function.json. 39 | * @return The variable name used in function.json. 40 | */ 41 | String name(); 42 | 43 | /** 44 | * The variable name used in function.json to specify the type of the binding. 45 | * @return The type of the binding. 46 | */ 47 | String type(); 48 | 49 | /** 50 | * The variable name used in function.json to specify the direction of the binding: in or out 51 | * @return The direction of the biding. 52 | */ 53 | String direction(); 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/FunctionName.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

The {@code FunctionName} annotation is used to specify to the Azure Functions tooling what name is to be applied 16 | * to the associated function when the function is deployed onto Azure. This becomes the endpoint (in the case of an 17 | * {@link HttpTrigger http triggered} function, for example, but more generally it is what is shown to users in the 18 | * Azure Portal, so a succinct and understandable function name is useful.

19 | * 20 | *

An example of how the {@code FunctionName} annotation is shown in the code snippet below. Note that it is applied 21 | * to the function that will be called by Azure, based on the specified trigger (in the code below it is a 22 | * {@link HttpTrigger}).

23 | * 24 | *
25 |  * {@literal @}FunctionName("redirect")
26 |  *  public HttpResponseMessage<String> redirectFunction(
27 |  *    {@literal @}HttpTrigger(name = "req",
28 |  *    methods = {"get"}, authLevel = AuthorizationLevel.ANONYMOUS) 
29 |  *    HttpRequestMessage<Optional<String>> request) {
30 |  *     ....
31 |  *  }
32 | * 33 | * @since 1.0.0 34 | */ 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target(ElementType.METHOD) 37 | public @interface FunctionName { 38 | /** 39 | * The name of the function. 40 | * @return The name of the function. 41 | */ 42 | String value(); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/EventGridTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would come from EventGrid, and causing the method to run when an event is 17 | * arrived. The parameter type can be one of the following:

18 | * 19 | * 24 | * 25 | *

The following example shows a Java function that prints out an event:

26 | * 27 | *
{@literal @}FunctionName("eventGridMonitor")
28 |  * public void logEvent(
29 |  *    {@literal @}EventGridTrigger(name = "event") String content,
30 |  *     final ExecutionContext context
31 |  * ) {
32 |  *     context.getLogger().info(content);
33 |  * }
34 | * 35 | * @since 1.0.0 36 | */ 37 | @Retention(RetentionPolicy.RUNTIME) 38 | @Target({ElementType.PARAMETER}) 39 | public @interface EventGridTrigger { 40 | /** 41 | * The variable name used in function.json. 42 | * @return The variable name used in function.json. 43 | */ 44 | String name(); 45 | 46 | /** 47 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

48 | * 53 | * @return The dataType which will be used by the Functions runtime. 54 | */ 55 | String dataType() default ""; 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/com/microsoft/azure/functions/annotation/BindingTest.java: -------------------------------------------------------------------------------- 1 | package com.microsoft.azure.functions.annotation; 2 | 3 | import java.lang.annotation.Target; 4 | import java.lang.reflect.Method; 5 | import java.util.*; 6 | import java.util.stream.Collectors; 7 | 8 | import org.junit.*; 9 | import org.reflections.Reflections; 10 | 11 | import static junit.framework.TestCase.*; 12 | 13 | /** 14 | * Unit tests that enforce annotation contracts and conventions for Functions 15 | */ 16 | public class BindingTest { 17 | private static Set> annotations; 18 | private static String[] bindingAnnotationSuffix = new String[] { "Input", "Output", "Trigger" }; 19 | 20 | @Test 21 | public void every_binding_annotation_should_have_name_method() { 22 | final String methodName = "name"; 23 | 24 | for (Class annotation : annotations) { 25 | Optional method = findMethod(annotation, methodName); 26 | assertTrue(method.isPresent()); 27 | } 28 | } 29 | 30 | @Test 31 | public void every_binding_annotation_should_have_dataType_method() { 32 | final String methodName = "dataType"; 33 | 34 | for (Class annotation : annotations) { 35 | Optional method = findMethod(annotation, methodName); 36 | assertTrue(method.isPresent()); 37 | } 38 | } 39 | 40 | /** 41 | * find all annotation bindings based on annotation suffix conventions defined in bindingAnnotationSuffix array 42 | */ 43 | @Before 44 | public void findAllFunctionParameterBindingsInCore() { 45 | annotations = new Reflections(BindingTest.class.getPackage().getName()) 46 | .getTypesAnnotatedWith(Target.class) 47 | .stream() 48 | .filter(t -> t.getSimpleName().endsWith(bindingAnnotationSuffix[0]) || 49 | t.getSimpleName().endsWith(bindingAnnotationSuffix[1]) || 50 | t.getSimpleName().endsWith(bindingAnnotationSuffix[2])).collect(Collectors.toSet()); 51 | } 52 | 53 | private Optional findMethod(Class type, String name) { 54 | return Arrays.stream(type.getMethods()).filter(m -> m.getName().equals(name)).findAny(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/TableOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be written to a storage table. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | * 24 | * @since 1.0.0 25 | */ 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 28 | public @interface TableOutput { 29 | /** 30 | * The variable name used in function.json. 31 | * @return The variable name used in function.json. 32 | */ 33 | String name(); 34 | 35 | /** 36 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

37 | *
    38 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 39 | *
  • binary: treat it as a binary data whose value comes from for example OutputBinding<byte[]>
  • 40 | *
41 | * @return The dataType which will be used by the Functions runtime. 42 | */ 43 | String dataType() default ""; 44 | 45 | /** 46 | * Defines the name of the storage table to which to write. 47 | * @return The storage table name string. 48 | */ 49 | String tableName(); 50 | 51 | /** 52 | * Defines the partition key of the storage table to which to write. 53 | * @return The storage table partition key string. 54 | */ 55 | String partitionKey() default ""; 56 | 57 | /** 58 | * Defines the row key of the storage table to which to write. 59 | * @return The storage table row key string. 60 | */ 61 | String rowKey() default ""; 62 | 63 | /** 64 | * Defines the app setting name that contains the Azure Storage connection string. 65 | * @return The app setting name of the connection string. 66 | */ 67 | String connection() default ""; 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/TwilioSmsOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be sent through twilio SMS. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | * 24 | * @since 1.0.0 25 | */ 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 28 | public @interface TwilioSmsOutput { 29 | /** 30 | * The variable name used in function.json. 31 | * @return The variable name used in function.json. 32 | */ 33 | String name(); 34 | 35 | /** 36 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

37 | *
    38 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 39 | *
  • binary: treat it as a binary data whose value comes from for example OutputBinding<byte[]>
  • 40 | *
41 | * @return The dataType which will be used by the Functions runtime. 42 | */ 43 | String dataType() default ""; 44 | 45 | /** 46 | * Defines the account SID of Twilio SMS to send. 47 | * @return The Twilio SMS account SID string. 48 | */ 49 | String accountSid(); 50 | 51 | /** 52 | * Defines the authorization token of Twilio SMS to send. 53 | * @return The Twilio SMS authorization token string. 54 | */ 55 | String authToken(); 56 | 57 | /** 58 | * Defines the target of Twilio SMS to send. 59 | * @return The Twilio SMS target string. 60 | */ 61 | String to(); 62 | 63 | /** 64 | * Defines the source of Twilio SMS to send. 65 | * @return The Twilio SMS source string. 66 | */ 67 | String from(); 68 | 69 | /** 70 | * Defines the content body of Twilio SMS to send. 71 | * @return The Twilio SMS content body string. 72 | */ 73 | String body(); 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/SendGridOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be written to SendGrid. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | * 24 | * @since 1.0.0 25 | */ 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 28 | public @interface SendGridOutput { 29 | /** 30 | * The variable name used in function.json. 31 | * @return The variable name used in function.json. 32 | */ 33 | String name(); 34 | 35 | /** 36 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

37 | *
    38 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 39 | *
  • binary: treat it as a binary data whose value comes from for example OutputBinding<byte[]>
  • 40 | *
41 | * @return The dataType which will be used by the Functions runtime. 42 | */ 43 | String dataType() default ""; 44 | 45 | /** 46 | * Defines the API key of the SendGrid to which to write. 47 | * @return The SendGrid API key string. 48 | */ 49 | String apiKey(); 50 | 51 | /** 52 | * Defines the 'TO' email of the SendGrid to which to write. 53 | * @return The SendGrid 'TO' email string. 54 | */ 55 | String to(); 56 | 57 | /** 58 | * Defines the 'FROM' name of the SendGrid to which to write. 59 | * @return The SendGrid 'FROM' name string. 60 | */ 61 | String from(); 62 | 63 | /** 64 | * Defines the subject of the SendGrid email to which to write. 65 | * @return The SendGrid email subject string. 66 | */ 67 | String subject(); 68 | 69 | /** 70 | * Defines the content text of the SendGrid email to which to write. 71 | * @return The SendGrid email content string. 72 | */ 73 | String text(); 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/EventHubOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be published to the event hub. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | *

The following example shows a Java function that writes a message to an event hub:

24 | * 25 | *
{@literal @}FunctionName("sendTime")
26 |  *{@literal @}EventHubOutput(name = "event", eventHubName = "samples-workitems", connection = "AzureEventHubConnection")
27 |  * public String sendTime(
28 |  *    {@literal @}TimerTrigger(name = "sendTimeTrigger", schedule = "0 */5 * * * *") String timerInfo
29 |  * ) {
30 |  *     return LocalDateTime.now().toString();
31 |  * }
32 | * 33 | * @since 1.0.0 34 | */ 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 37 | public @interface EventHubOutput { 38 | /** 39 | * The variable name used in function.json. 40 | * @return The variable name used in function.json. 41 | */ 42 | String name(); 43 | 44 | /** 45 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

46 | *
    47 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 48 | *
  • binary: treat it as a binary data whose value comes from for example OutputBinding<byte[]>
  • 49 | *
50 | * @return The dataType which will be used by the Functions runtime. 51 | */ 52 | String dataType() default ""; 53 | 54 | /** 55 | * Defines the name of the event hub to which to publish. 56 | * @return The event hub name string. 57 | */ 58 | String eventHubName(); 59 | 60 | /** 61 | * Defines the app setting name that contains the Azure Eventhub connection string. 62 | * @return The app setting name of the connection string. 63 | */ 64 | String connection(); 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/ServiceBusTopicOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would be written to a service bus topic. The parameter type 17 | * should be OutputBinding<T>, where T could be one of: 18 | *

19 | * 20 | *
    21 | *
  • Any native Java types such as int, String, byte[]
  • 22 | *
  • Any POJO type
  • 23 | *
24 | * 25 | * 26 | * @since 1.0.0 27 | */ 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target({ ElementType.PARAMETER, ElementType.METHOD }) 30 | public @interface ServiceBusTopicOutput { 31 | /** 32 | * The variable name used in function.json. 33 | * 34 | * @return The variable name used in function.json. 35 | */ 36 | String name(); 37 | 38 | /** 39 | *

40 | * Defines how Functions runtime should treat the parameter value. Possible values are: 41 | *

42 | *
    43 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 44 | *
  • binary: treat it as a binary data whose value comes from for example 45 | * OutputBinding<byte[]>
  • 46 | *
47 | * 48 | * @return The dataType which will be used by the Functions runtime. 49 | */ 50 | String dataType() default ""; 51 | 52 | /** 53 | * Defines the name of the Service Bus topic to which to write. 54 | * 55 | * @return The Service Bus topic name string. 56 | */ 57 | String topicName(); 58 | 59 | /** 60 | * Defines the subscription name of the Service Bus topic to which to write. 61 | * 62 | * @return The Service Bus topic subscription name string. 63 | */ 64 | String subscriptionName(); 65 | 66 | /** 67 | * Defines the app setting name that contains the Service Bus connection string. 68 | * 69 | * @return The app setting name of the connection string. 70 | */ 71 | String connection(); 72 | 73 | /** 74 | * Defines the permission of the Service Bus topic to which to write. 75 | * 76 | * @return The Service Bus topic permission. 77 | */ 78 | AccessRights access() default AccessRights.MANAGE; 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/QueueTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would come from a storage queue, and causing the method to run when a new 16 | * item is pushed. The parameter type can be one of the following:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Nullable values using Optional<T>
  • 21 | *
  • Any POJO type
  • 22 | *
23 | * 24 | *

The following example shows a Java function that polls the "myqueue-items" queue and writes a log each time a 25 | * queue item is processed.

26 | * 27 | *
{@literal @}FunctionName("queueMonitor")
28 |  * public void logQueueItem(
29 |  *    {@literal @}QueueTrigger(name = "msg", queueName = "myqueue-items", connection = "AzureWebJobsStorage") 
30 |  *     String message,
31 |  *     final ExecutionContext context
32 |  * ) {
33 |  *     context.getLogger().info("Queue message processed: " + message);
34 |  * }
35 | * 36 | * @since 1.0.0 37 | */ 38 | @Retention(RetentionPolicy.RUNTIME) 39 | @Target(ElementType.PARAMETER) 40 | public @interface QueueTrigger { 41 | /** 42 | * The variable name used in function.json. 43 | * @return The variable name used in function.json. 44 | */ 45 | String name(); 46 | 47 | /** 48 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

49 | *
    50 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like POJO
  • 51 | *
  • string: always get the value as a string
  • 52 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type byte[]
  • 53 | *
54 | * @return The dataType which will be used by the Functions runtime. 55 | */ 56 | String dataType() default ""; 57 | 58 | /** 59 | * Defines the name of the storage queue to which to bind. 60 | * @return The storage queue name string. 61 | */ 62 | String queueName(); 63 | 64 | /** 65 | * Defines the app setting name that contains the Azure Storage connection string. 66 | * @return The app setting name of the connection string. 67 | */ 68 | String connection() default ""; 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/QueueOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be written to a storage queue. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | *

The following example shows a Java function that creates a queue message for each HTTP request received.

24 | * 25 | *
{@literal @}FunctionName("httpToQueue")
26 |  *{@literal @}QueueOutput(name = "item", queueName = "myqueue-items", connection = "AzureWebJobsStorage")
27 |  * public String pushToQueue(
28 |  *    {@literal @}HttpTrigger(name = "request", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
29 |  *     final String message,
30 |  *    {@literal @}HttpOutput(name = "response") final OutputBinding<String> result
31 |  * ) {
32 |  *     result.setValue(message + " has been added.");
33 |  *     return message;
34 |  * }
35 | * 36 | * @since 1.0.0 37 | */ 38 | @Retention(RetentionPolicy.RUNTIME) 39 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 40 | public @interface QueueOutput { 41 | /** 42 | * The variable name used in function.json. 43 | * @return The variable name used in function.json. 44 | */ 45 | String name(); 46 | 47 | /** 48 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

49 | *
    50 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 51 | *
  • binary: treat it as a binary data whose value comes from for example OutputBinding<byte[]>
  • 52 | *
53 | * @return The dataType which will be used by the Functions runtime. 54 | */ 55 | String dataType() default ""; 56 | 57 | /** 58 | * Defines the name of the storage queue to which to write. 59 | * @return The queue name string. 60 | */ 61 | String queueName(); 62 | 63 | /** 64 | * Defines the app setting name that contains the Azure Storage connection string. 65 | * @return The app setting name of the connection string. 66 | */ 67 | String connection() default ""; 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/BlobOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be written to a blob. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | *

The following example shows blob input and output bindings in a Java function. The function makes a copy of 24 | * a text blob. The function is triggered by a queue message that contains the name of the blob to copy. The new 25 | * blob is named {originalblobname}-Copy.

26 | * 27 | *
{@literal @}FunctionName("copyTextBlob")
28 |  *{@literal @}StorageAccount("AzureWebJobsStorage")
29 |  *{@literal @}BlobOutput(name = "target", path = "samples-workitems/{queueTrigger}-Copy")
30 |  * public String blobCopy(
31 |  *    {@literal @}QueueTrigger(name = "filename",
32 |  *                   queueName = "myqueue-items") String filename,
33 |  *    {@literal @}BlobInput(name = "source",
34 |  *                path = "samples-workitems/{queueTrigger}") String content
35 |  * ) {
36 |  *     return content;
37 |  * }
38 | * 39 | * @since 1.0.0 40 | */ 41 | @Retention(RetentionPolicy.RUNTIME) 42 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 43 | public @interface BlobOutput { 44 | /** 45 | * The variable name used in function.json. 46 | * @return The variable name used in function.json. 47 | */ 48 | String name(); 49 | 50 | /** 51 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

52 | *
    53 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 54 | *
  • binary: treat it as a binary data whose value comes from for example OutputBinding<byte[]>
  • 55 | *
56 | * @return The dataType which will be used by the Functions runtime. 57 | */ 58 | String dataType() default ""; 59 | 60 | /** 61 | * Defines the path of the blob to which to write. 62 | * @return The blob path string. 63 | */ 64 | String path(); 65 | 66 | /** 67 | * Defines the app setting name that contains the Azure Storage connection string. 68 | * @return The app setting name of the connection string. 69 | */ 70 | String connection() default ""; 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/BlobInput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would come from a blob. The parameter type can be one of the following:

16 | * 17 | *
    18 | *
  • Any native Java types such as int, String, byte[]
  • 19 | *
  • Nullable values using Optional<T>
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | *

The following example is a Java function that uses a queue trigger and an input blob binding. The queue message 24 | * contains the name of the blob, and the function logs the size of the blob.

25 | * 26 | *
{@literal @}FunctionName("getBlobSize")
27 |  *{@literal @}StorageAccount("AzureWebJobsStorage")
28 |  * public void blobSize(
29 |  *    {@literal @}QueueTrigger(name = "filename",
30 |  *                   queueName = "myqueue-items") String filename,
31 |  *    {@literal @}BlobInput(name = "file",
32 |  *                dataType = "binary",
33 |  *                path = "samples-workitems/{queueTrigger}") byte[] content,
34 |  *     final ExecutionContext context
35 |  * ) {
36 |  *     context.getLogger().info("The size of \"" + filename + "\" is: " + content.length + " bytes");
37 |  * }
38 | * 39 | * 40 | * @since 1.0.0 41 | */ 42 | @Retention(RetentionPolicy.RUNTIME) 43 | @Target(ElementType.PARAMETER) 44 | public @interface BlobInput { 45 | /** 46 | * The variable name used in function.json. 47 | * @return The variable name used in function.json. 48 | */ 49 | String name(); 50 | 51 | /** 52 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

53 | *
    54 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like POJO
  • 55 | *
  • string: always get the value as a string
  • 56 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type byte[]
  • 57 | *
58 | * @return The dataType which will be used by the Functions runtime. 59 | */ 60 | String dataType() default ""; 61 | 62 | /** 63 | * Defines the path of the blob to which to bind. 64 | * @return The blob path string. 65 | */ 66 | String path(); 67 | 68 | /** 69 | * Defines the app setting name that contains the Azure Storage connection string. 70 | * @return The app setting name of the connection string. 71 | */ 72 | String connection() default ""; 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/BlobTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would come from a blob, and causing the method to run when a blob is 16 | * uploaded. The parameter type can be one of the following:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Nullable values using Optional<T>
  • 21 | *
  • Any POJO type
  • 22 | *
23 | * 24 | * 25 | *

The following example shows a Java function that logs the filename and size when a blob is added or updated 26 | * in the "samples-workitems" container:

27 | * 28 | *
{@literal @}FunctionName("blobMonitor")
29 |  * public void blobMonitor(
30 |  *    {@literal @}BlobTrigger(name = "file",
31 |  *                  dataType = "binary",
32 |  *                  path = "samples-workitems/{name}",
33 |  *                  connection = "AzureWebJobsStorage") byte[] content,
34 |  *    {@literal @}BindingName("name") String filename,
35 |  *     final ExecutionContext context
36 |  * ) {
37 |  *     context.getLogger().info("Name: " + filename + ", Size: " + content.length + " bytes");
38 |  * }
39 | * 40 | * @see com.microsoft.azure.functions.annotation.BindingName 41 | * @since 1.0.0 42 | */ 43 | @Retention(RetentionPolicy.RUNTIME) 44 | @Target(ElementType.PARAMETER) 45 | public @interface BlobTrigger { 46 | /** 47 | * The variable name used in function.json. 48 | * @return The variable name used in function.json. 49 | */ 50 | String name(); 51 | 52 | /** 53 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

54 | *
    55 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like POJO
  • 56 | *
  • string: always get the value as a string
  • 57 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type byte[]
  • 58 | *
59 | * @return The dataType which will be used by the Functions runtime. 60 | */ 61 | String dataType() default ""; 62 | 63 | /** 64 | * Defines the path of the blob to which to bind. 65 | * @return The blob path string. 66 | */ 67 | String path(); 68 | 69 | /** 70 | * Defines the app setting name that contains the Azure Storage connection string. 71 | * @return The app setting name of the connection string. 72 | */ 73 | String connection() default ""; 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/ServiceBusQueueOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would be written to a service bus queue. 16 | * The parameter type should be OutputBinding<T>, where T could be one of:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Any POJO type
  • 21 | *
22 | * 23 | *

The following example shows a Java function that sends a Service Bus queue message:

24 | * 25 | *
{@literal @}FunctionName("httpToServiceBusQueue")
26 |  *{@literal @}ServiceBusQueueOutput(name = "message", queueName = "myqueue", connection = "AzureServiceBusConnection")
27 |  * public String pushToQueue(
28 |  *    {@literal @}HttpTrigger(name = "request", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
29 |  *     final String message,
30 |  *    {@literal @}HttpOutput(name = "response") final OutputBinding<String> result
31 |  * ) {
32 |  *     result.setValue(message + " has been sent.");
33 |  *     return message;
34 |  * }
35 | * 36 | * @since 1.0.0 37 | */ 38 | @Retention(RetentionPolicy.RUNTIME) 39 | @Target({ElementType.PARAMETER, ElementType.METHOD}) 40 | public @interface ServiceBusQueueOutput { 41 | /** 42 | * The variable name used in function.json. 43 | * @return The variable name used in function.json. 44 | */ 45 | String name(); 46 | 47 | /** 48 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

49 | *
    50 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 51 | *
  • binary: treat it as a binary data whose value comes from for example OutputBinding<byte[]>
  • 52 | *
53 | * @return The dataType which will be used by the Functions runtime. 54 | */ 55 | String dataType() default ""; 56 | 57 | /** 58 | * Defines the name of the Service Bus queue to which to write. 59 | * @return The Service Bus queue name string. 60 | */ 61 | String queueName(); 62 | 63 | /** 64 | * Defines the app setting name that contains the Service Bus connection string. 65 | * @return The app setting name of the connection string. 66 | */ 67 | String connection(); 68 | 69 | /** 70 | * Defines the permission of the Service Bus queue to which to write. 71 | * @return The Service Bus queue permission. 72 | */ 73 | AccessRights access() default AccessRights.MANAGE; 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/ServiceBusQueueTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would come from a Service Bus queue, and causing the method 17 | * to run when a new item is pushed. The parameter type can be one of the following: 18 | *

19 | * 20 | *
    21 | *
  • Any native Java types such as int, String, byte[]
  • 22 | *
  • Nullable values using Optional<T>
  • 23 | *
  • Any POJO type
  • 24 | *
25 | * 26 | *

27 | * The following example shows a Java function that logs a Service Bus queue message: 28 | *

29 | * 30 | *
31 |  * {@literal @}FunctionName("serviceBusMonitor")
32 |  * public void logServiceBusMessage(
33 |  *    {@literal @}ServiceBusQueueTrigger(name = "msg", queueName = "myqueue", connection = "AzureServiceBusConnection") 
34 |  *     final String message,
35 |  *     final ExecutionContext context
36 |  * ) {
37 |  *     context.getLogger().info("Message is received: " + message);
38 |  * }
39 |  * 
40 | * 41 | * @since 1.0.0 42 | */ 43 | @Retention(RetentionPolicy.RUNTIME) 44 | @Target(ElementType.PARAMETER) 45 | public @interface ServiceBusQueueTrigger { 46 | /** 47 | * The variable name used in function.json. 48 | * 49 | * @return The variable name used in function.json. 50 | */ 51 | String name(); 52 | 53 | /** 54 | *

55 | * Defines how Functions runtime should treat the parameter value. Possible values are: 56 | *

57 | *
    58 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like 59 | * POJO
  • 60 | *
  • string: always get the value as a string
  • 61 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type 62 | * byte[]
  • 63 | *
64 | * 65 | * @return The dataType which will be used by the Functions runtime. 66 | */ 67 | String dataType() default ""; 68 | 69 | /** 70 | * Defines the name of the Service Bus queue to which to bind. 71 | * 72 | * @return The Service Bus queue string. 73 | */ 74 | String queueName(); 75 | 76 | /** 77 | * Defines the app setting name that contains the Service Bus connection string. 78 | * 79 | * @return The app setting name of the connection string. 80 | */ 81 | String connection(); 82 | 83 | /** 84 | * Defines the permission of the Service Bus queue to which to bind. 85 | * 86 | * @return The Service Bus queue permission. 87 | */ 88 | AccessRights access() default AccessRights.MANAGE; 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/HttpStatus.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions; 8 | 9 | /** 10 | * Enum to represent HTTP Status codes. 11 | * 12 | * This enum lists all standard HTTP 1.1 status lines. For custom codes, 13 | * please refer to {@link HttpStatusType#custom(int)}. 14 | * 15 | * @author Bruno Borges 16 | * @since 1.0 17 | */ 18 | public enum HttpStatus implements HttpStatusType { 19 | 20 | // HTTP Status 100+ 21 | CONTINUE(100), SWITCHING_PROTOCOLS(101), PROCESSING(102), CHECKPOINT(103), 22 | 23 | // HTTP Status 200+ 24 | OK(200), CREATED(201), ACCEPTED(202), NON_AUTHORITATIVE_INFORMATION(203), NO_CONTENT(204), RESET_CONTENT(205), 25 | PARTIAL_CONTENT(206), MULTI_STATUS(207), ALREADY_REPORTED(208), IM_USED(226), 26 | 27 | // HTTP Status 300+ 28 | MULTIPLE_CHOICES(300), MOVED_PERMANENTLY(301), FOUND(302), SEE_OTHER(303), NOT_MODIFIED(304), 29 | TEMPORARY_REDIRECT(307), PERMANENT_REDIRECT(308), 30 | 31 | // HTTP Status 400+ 32 | BAD_REQUEST(400), UNAUTHORIZED(401), PAYMENT_REQUIRED(402), FORBIDDEN(403), NOT_FOUND(404), METHOD_NOT_ALLOWED(405), 33 | NOT_ACCEPTABLE(406), PROXY_AUTHENTICATION_REQUIRED(407), REQUEST_TIMEOUT(408), CONFLICT(409), GONE(410), 34 | LENGTH_REQUIRED(411), PRECONDITION_FAILED(412), PAYLOAD_TOO_LARGE(413), URI_TOO_LONG(414), 35 | UNSUPPORTED_MEDIA_TYPE(415), REQUESTED_RANGE_NOT_SATISFIABLE(416), EXPECTATION_FAILED(417), I_AM_A_TEAPOT(418), 36 | UNPROCESSABLE_ENTITY(422), LOCKED(423), FAILED_DEPENDENCY(424), UPGRADE_REQUIRED(426), PRECONDITION_REQUIRED(428), 37 | TOO_MANY_REQUESTS(429), REQUEST_HEADER_FIELDS_TOO_LARGE(431), UNAVAILABLE_FOR_LEGAL_REASONS(451), 38 | 39 | // HTTP Status 500+ 40 | INTERNAL_SERVER_ERROR(500), NOT_IMPLEMENTED(501), BAD_GATEWAY(502), SERVICE_UNAVAILABLE(503), GATEWAY_TIMEOUT(504), 41 | HTTP_VERSION_NOT_SUPPORTED(505), VARIANT_ALSO_NEGOTIATES(506), INSUFFICIENT_STORAGE(507), LOOP_DETECTED(508), 42 | BANDWIDTH_LIMIT_EXCEEDED(509), NOT_EXTENDED(510), NETWORK_AUTHENTICATION_REQUIRED(511); 43 | 44 | private final int value; 45 | 46 | HttpStatus(int value) { 47 | this.value = value; 48 | } 49 | 50 | /** 51 | * Returns the code of this HTTPStatus enum. 52 | * 53 | * @return int value for this http status 54 | */ 55 | public int value() { 56 | return this.value; 57 | } 58 | 59 | /** 60 | * Maps an int code to a standard HTTP status code. 61 | * 62 | * @param value for http code 63 | * @return HttpStatus enum 64 | */ 65 | public static HttpStatus valueOf(int value) { 66 | for (final HttpStatus status : HttpStatus.values()) { 67 | if (value == status.value) { 68 | return status; 69 | } 70 | } 71 | 72 | throw new IllegalArgumentException("HTTP Status code unknown: " + value); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/HttpResponseMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions; 8 | 9 | /** 10 | * An HttpResponseMessage instance is returned by Azure Functions methods that are triggered by an 11 | * {@link com.microsoft.azure.functions.annotation.HttpTrigger}. 12 | * 13 | * @see com.microsoft.azure.functions.annotation.HttpTrigger 14 | * @see HttpRequestMessage 15 | * @since 1.0.0 16 | */ 17 | public interface HttpResponseMessage { 18 | 19 | /** 20 | * Returns the HTTP status code set on the HttpResponseMessage instance. 21 | * 22 | * @return the status code set on the HttpResponseMessage instance. 23 | */ 24 | HttpStatusType getStatus(); 25 | 26 | /** 27 | * Returns the HTTP status code set on the HttpResponseMessage instance. 28 | * 29 | * @return the status code set on the HttpResponseMessage instance. 30 | */ 31 | default int getStatusCode() { 32 | return getStatus().value(); 33 | } 34 | 35 | /** 36 | * Returns a header value for the given key. 37 | * 38 | * @param key The key for which the header value is sought. 39 | * @return Returns the value if the key has previously been added, or null if it has not. 40 | */ 41 | String getHeader(String key); 42 | 43 | /** 44 | * Returns the body of the HTTP response. 45 | * 46 | * @return the body of the HTTP response. 47 | */ 48 | Object getBody(); 49 | 50 | /** 51 | * A builder to create an instance of HttpResponseMessage 52 | */ 53 | public interface Builder { 54 | 55 | /** 56 | * Sets the status code to be used in the HttpResponseMessage object. 57 | * 58 | * You can provide standard HTTP Status using enum values from {@link HttpStatus}, or you can 59 | * create a custom status code using {@link HttpStatusType#custom(int)}. 60 | * 61 | * @param status An HTTP status code representing the outcome of the HTTP request. 62 | * @return this builder 63 | */ 64 | Builder status(HttpStatusType status); 65 | 66 | /** 67 | * Adds a (key, value) header to the response. 68 | * 69 | * @param key The key of the header value. 70 | * @param value The value of the header value. 71 | * @return this builder 72 | */ 73 | Builder header(String key, String value); 74 | 75 | /** 76 | * Sets the body of the HTTP response. 77 | * 78 | * @param body The body of the HTTP response 79 | * @return this builder 80 | */ 81 | Builder body(Object body); 82 | 83 | /** 84 | * Creates an instance of HttpMessageResponse with the values configured in this builder. 85 | * 86 | * @return an HttpMessageResponse object 87 | */ 88 | HttpResponseMessage build(); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/HttpRequestMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions; 8 | 9 | import java.net.URI; 10 | import java.util.Map; 11 | 12 | /** 13 | * An HttpRequestMessage instance is provided to Azure functions that use 14 | * {@link com.microsoft.azure.functions.annotation.HttpTrigger HTTP Triggers}. For an example of how 15 | * to use the http functionality of Azure Functions, refer to the example in the 16 | * {@link com.microsoft.azure.functions.annotation.HttpTrigger} 17 | * 18 | * @see com.microsoft.azure.functions.annotation.HttpTrigger 19 | * @see HttpResponseMessage 20 | * @param The type of the body content that is expected to be received as part of this HTTP 21 | * request. 22 | * @since 1.0.0 23 | */ 24 | public interface HttpRequestMessage { 25 | /** 26 | * Returns the URI that was called that resulted in this HTTP request being submitted. 27 | * 28 | * @return the URI that was called that resulted in this HTTP request being submitted. 29 | */ 30 | URI getUri(); 31 | 32 | /** 33 | * Returns the HTTP method name as Enum 34 | * 35 | * @return type of HttpMethod 36 | */ 37 | HttpMethod getHttpMethod(); 38 | 39 | /** 40 | * Returns a map of headers that were contained within this HTTP request. 41 | * 42 | * @return a map of headers that were contained within this HTTP request. 43 | */ 44 | Map getHeaders(); 45 | 46 | /** 47 | * Returns a map of query parameters that were included with this HTTP request. 48 | * 49 | * @return a map of query parameters that were included with this HTTP request. 50 | */ 51 | Map getQueryParameters(); 52 | 53 | /** 54 | * Returns any body content that was included with this HTTP request. 55 | * 56 | * @return any body content that was included with this HTTP request. 57 | */ 58 | T getBody(); 59 | 60 | /** 61 | * Returns a {@link HttpResponseMessage.Builder} instance to build a HttpResponseMessage with 62 | * standard HTTP status code and no response body. 63 | * 64 | * @param status The HTTP status code to return to the caller of the function. 65 | * @return An {@link HttpResponseMessage.Builder} instance containing the provided status and 66 | * empty body. 67 | */ 68 | HttpResponseMessage.Builder createResponseBuilder(HttpStatus status); 69 | 70 | /** 71 | * Returns a {@link HttpResponseMessage.Builder} instance to build a HttpResponseMessage with 72 | * custome HTTP status code and no response body. 73 | * 74 | * @param status The HTTP status code to return to the caller of the function. 75 | * @return An {@link HttpResponseMessage.Builder} instance containing the provided status and 76 | * empty body. 77 | */ 78 | HttpResponseMessage.Builder createResponseBuilder(HttpStatusType status); 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/EventHubTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would come from event hub, and causing the method to run when a new event is 16 | * arrived. The parameter type can be one of the following:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Nullable values using Optional<T>
  • 21 | *
  • Any POJO type
  • 22 | *
23 | * 24 | *

The following example shows a Java function that logs the message body of the event hub trigger:

25 | * 26 | *
{@literal @}FunctionName("eventHubMonitor")
27 |  * public void logEventHubMessage(
28 |  *    {@literal @}EventHubTrigger(name = "event", 
29 |  *    eventHubName = "samples-workitems", 
30 |  *    connection = "AzureEventHubConnection") String message,
31 |  *     final ExecutionContext context
32 |  * ) {
33 |  *     context.getLogger().info("Event hub message received: " + message);
34 |  * }
35 | * 36 | * @since 1.0.0 37 | */ 38 | @Retention(RetentionPolicy.RUNTIME) 39 | @Target(ElementType.PARAMETER) 40 | public @interface EventHubTrigger { 41 | /** 42 | * The variable name used in function.json. 43 | * @return The variable name used in function.json. 44 | */ 45 | String name(); 46 | 47 | /** 48 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

49 | *
    50 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like POJO
  • 51 | *
  • string: always get the value as a string
  • 52 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type byte[]
  • 53 | *
54 | * @return The dataType which will be used by the Functions runtime. 55 | */ 56 | String dataType() default ""; 57 | 58 | /** 59 | * Defines the name of the event hub to which to bind. 60 | * @return The event hub name string. 61 | */ 62 | String eventHubName(); 63 | 64 | /** 65 | * Cardinality of the trigger input. 66 | * Choose 'One' if the input is a single message or 'Many' if the input is an array of messages. 67 | * 'Many' is the default if unspecified 68 | * @return An {@link Cardinality} value representing the Cardinality 69 | */ 70 | Cardinality cardinality() default Cardinality.MANY; 71 | 72 | 73 | /** 74 | * Defines the consumer group of the event hub to which to bind. 75 | * @return The event hub consumer group string. 76 | */ 77 | String consumerGroup() default "$Default"; 78 | 79 | /** 80 | * Defines the app setting name that contains the Azure Eventhub connection string. 81 | * @return The app setting name of the connection string. 82 | */ 83 | String connection(); 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/ServiceBusTopicTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would come from Service Bus topic, and causing the method 17 | * to run when a new item is published. The parameter type can be one of the following: 18 | *

19 | * 20 | *
    21 | *
  • Any native Java types such as int, String, byte[]
  • 22 | *
  • Nullable values using Optional<T>
  • 23 | *
  • Any POJO type
  • 24 | *
25 | * 26 | *

27 | * The following example shows a service bus topic trigger which logs the message: 28 | *

29 | * 30 | *
31 |  * {@literal @}FunctionName("sbprocessor")
32 |  * public void serviceBusProcess(
33 |  *    {@literal @}ServiceBusTopicTrigger(name = "msg",
34 |  *                             topicName = "mytopicname",
35 |  *                             subscriptionName = "mysubname",
36 |  *                             connection = "myconnvarname") String message,
37 |  *     final ExecutionContext context
38 |  * ) {
39 |  *     context.getLogger().info(message);
40 |  * }
41 |  * 
42 | * 43 | * @since 1.0.0 44 | */ 45 | @Retention(RetentionPolicy.RUNTIME) 46 | @Target(ElementType.PARAMETER) 47 | public @interface ServiceBusTopicTrigger { 48 | /** 49 | * The variable name used in function.json. 50 | * 51 | * @return The variable name used in function.json. 52 | */ 53 | String name(); 54 | 55 | /** 56 | *

57 | * Defines how Functions runtime should treat the parameter value. Possible values are: 58 | *

59 | *
    60 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like 61 | * POJO
  • 62 | *
  • string: always get the value as a string
  • 63 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type 64 | * byte[]
  • 65 | *
66 | * 67 | * @return The dataType which will be used by the Functions runtime. 68 | */ 69 | String dataType() default ""; 70 | 71 | /** 72 | * Defines the name of the Service Bus topic to which to bind. 73 | * 74 | * @return The Service Bus topic name string. 75 | */ 76 | String topicName(); 77 | 78 | /** 79 | * Defines the subscription name of the Service Bus topic to which to bind. 80 | * 81 | * @return The Service Bus topic subscription name string. 82 | */ 83 | String subscriptionName(); 84 | 85 | /** 86 | * Defines the app setting name that contains the Service Bus connection string. 87 | * 88 | * @return The app setting name of the connection string. 89 | */ 90 | String connection(); 91 | 92 | /** 93 | * Defines the permission of the Service Bus topic to which to bind. 94 | * 95 | * @return The Service Bus topic permission. 96 | */ 97 | AccessRights access() default AccessRights.MANAGE; 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/TableInput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

Place this on a parameter whose value would come from storage table. 16 | * The parameter type can be one of the following:

17 | * 18 | *
    19 | *
  • Any native Java types such as int, String, byte[]
  • 20 | *
  • Nullable values using Optional<T>
  • 21 | *
  • Any POJO type
  • 22 | *
23 | * 24 | *

The following example shows an HTTP trigger which returned the total count of the items in a table storage:

25 | * 26 | *
{@literal @}FunctionName("getallcount")
27 |  * public int run(
28 |  *    {@literal @}HttpTrigger(name = "req",
29 |  *                  methods = {"get"},
30 |  *                  authLevel = AuthorizationLevel.ANONYMOUS) Object dummyShouldNotBeUsed,
31 |  *    {@literal @}TableInput(name = "items",
32 |  *                 tableName = "mytablename",
33 |  *                 partitionKey = "myparkey",
34 |  *                 connection = "myconnvarname") MyItem[] items
35 |  * ) {
36 |  *     return items.length;
37 |  * }
38 | * 39 | * @see com.microsoft.azure.functions.annotation.HttpTrigger 40 | * @since 1.0.0 41 | */ 42 | @Retention(RetentionPolicy.RUNTIME) 43 | @Target(ElementType.PARAMETER) 44 | public @interface TableInput { 45 | /** 46 | * The variable name used in function.json. 47 | * @return The variable name used in function.json. 48 | */ 49 | String name(); 50 | 51 | /** 52 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

53 | *
    54 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like POJO
  • 55 | *
  • string: always get the value as a string
  • 56 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type byte[]
  • 57 | *
58 | * @return The dataType which will be used by the Functions runtime. 59 | */ 60 | String dataType() default ""; 61 | 62 | /** 63 | * Defines the name of the storage table to which to bind. 64 | * @return The storage table name string. 65 | */ 66 | String tableName(); 67 | 68 | /** 69 | * Defines the partition key of the storage table to which to bind. 70 | * @return The storage table partition key string. 71 | */ 72 | String partitionKey() default ""; 73 | 74 | /** 75 | * Defines the row key of the storage table to which to bind. 76 | * @return The storage table row key string. 77 | */ 78 | String rowKey() default ""; 79 | 80 | /** 81 | * Defines the filter of the storage table to which to bind. 82 | * @return The storage table filter string. 83 | */ 84 | String filter() default ""; 85 | 86 | /** 87 | * Defines the number of rows to be retrieved from the storage table to which to bind. 88 | * @return The storage table retrieving rows number string. 89 | */ 90 | String take() default ""; 91 | 92 | /** 93 | * Defines the app setting name that contains the Azure Storage connection string. 94 | * @return The app setting name of the connection string. 95 | */ 96 | String connection() default ""; 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/TimerTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | * The timer trigger lets you run a function on a schedule by specifying a CRON expression for when the function should 16 | * run. For more details and examples on how to specify a CRON expression, refer to the {@link #schedule()} attribute of 17 | * this annotation. 18 | * 19 | *

An example of using the timer trigger is shown below, where the {@code keepAlive} function is set to trigger and 20 | * execute every five minutes:

21 | * 22 | *
{@literal @}FunctionName("keepAlive")
23 |  * public void keepAlive(
24 |  *    {@literal @}TimerTrigger(name = "keepAliveTrigger", schedule = "0 */5 * * * *") String timerInfo,
25 |  *     ExecutionContext context
26 |  * ) {
27 |  *     // timeInfo is a JSON string, you can deserialize it to an object using your favorite JSON library
28 |  *     context.getLogger().info("Timer is triggered: " + timerInfo);
29 |  * }
30 | * 31 | * @since 1.0.0 32 | */ 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target(ElementType.PARAMETER) 35 | public @interface TimerTrigger { 36 | /** 37 | * The name of the variable that represents the timer object in function code. 38 | * 39 | * @return The name of the variable that represents the timer object in function code. 40 | */ 41 | String name(); 42 | 43 | /** 44 | *

Defines how Functions runtime should treat the parameter value. Possible values are:

45 | *
    46 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like POJO
  • 47 | *
  • string: always get the value as a string
  • 48 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type byte[]
  • 49 | *
50 | * @return The dataType which will be used by the Functions runtime. 51 | */ 52 | String dataType() default ""; 53 | 54 | /** 55 | * A CRON expression in the format 56 | * {@code {second} {minute} {hour} {day} {month} {day-of-week}}. 57 | * 58 | * 59 | * 60 | * 61 | * 62 | * 63 | * 64 | * 65 | * 66 | * 67 | * 68 | * 69 | * 70 | * 71 | * 72 | * 73 | * 74 | * 75 | * 76 | * 77 | * 78 | * 79 | * 80 | * 81 | * 82 | * 83 | * 84 | * 85 | * 86 | * 87 | * 88 | *
A table showing some examples of CRON expressions that could be used.
GoalCRON Expression
To trigger once every five minutes:0 */5 * * * *
To trigger once at the top of every hour:0 0 * * * *
To trigger once every two hours:0 0 */2 * * *
To trigger once every hour from 9 AM to 5 PM:0 0 9-17 * * *
To trigger at 9:30 AM every day:0 30 9 * * *
To trigger at 9:30 AM every weekday:0 30 9 * * 1-5
89 | * 90 | * @return A string representing a CRON expression that will be used to schedule a function to run. 91 | */ 92 | String schedule(); 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/CosmosDBInput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would come from CosmosDB. The parameter 17 | * type can be one of the following: 18 | *

19 | * 20 | *
    21 | *
  • Some native Java types such as String
  • 22 | *
  • Nullable values using Optional<T>
  • 23 | *
  • Any POJO type
  • 24 | *
25 | * 26 | * 27 | *

28 | * The following example shows a Java function that retrieves a single document. 29 | * The function is triggered by an HTTP request that uses a query string to 30 | * specify the ID to look up. That ID is used to retrieve a ToDoItem document 31 | * from the specified database and collection. A sample URL would be like: 32 | * http://localhost:7071/api/getItem?id=myid. 33 | *

34 | * 35 | *
 36 |  * {@literal @}FunctionName("getItem")
 37 |  * public String cosmosDbQueryById(
 38 |  *    {@literal @}HttpTrigger(name = "req",
 39 |  *                  methods = {HttpMethod.GET},
 40 |  *                  authLevel = AuthorizationLevel.ANONYMOUS) Optional<String> dummy,
 41 |  *    {@literal @}CosmosDBInput(name = "database",
 42 |  *                      databaseName = "ToDoList",
 43 |  *                      collectionName = "Items",
 44 |  *                      id = "{Query.id}",
 45 |  *                      connectionStringSetting = "AzureCosmosDBConnection") Optional<String> item
 46 |  * ) {
 47 |  *     return item.orElse("Not found");
 48 |  * }
 49 |  * 
50 | * 51 | * @since 1.0.0 52 | */ 53 | @Retention(RetentionPolicy.RUNTIME) 54 | @Target(ElementType.PARAMETER) 55 | public @interface CosmosDBInput { 56 | /** 57 | * The variable name used in function.json. 58 | * 59 | * @return The variable name used in function.json. 60 | */ 61 | String name(); 62 | 63 | /** 64 | *

65 | * Defines how Functions runtime should treat the parameter value. Possible 66 | * values are: 67 | *

68 | *
    69 | *
  • "": get the value as a string, and try to deserialize to actual parameter 70 | * type like POJO
  • 71 | *
  • string: always get the value as a string
  • 72 | *
  • binary: get the value as a binary data, and try to deserialize to actual 73 | * parameter type byte[]
  • 74 | *
75 | * 76 | * @return The dataType which will be used by the Functions runtime. 77 | */ 78 | String dataType() default ""; 79 | 80 | /** 81 | * Defines the database name of the CosmosDB to which to bind. 82 | * 83 | * @return The database name string. 84 | */ 85 | String databaseName(); 86 | 87 | /** 88 | * Defines the collection name of the CosmosDB to which to bind. 89 | * 90 | * @return The collection name string. 91 | */ 92 | String collectionName(); 93 | 94 | /** 95 | * Defines the ID of the CosmosDB to which to bind. 96 | * 97 | * @return The ID string. 98 | */ 99 | String id() default ""; 100 | 101 | /** 102 | * Defines the SQL query string to which to bind. 103 | * 104 | * @return The SQL query string. 105 | */ 106 | String sqlQuery() default ""; 107 | 108 | /** 109 | * Defines the app setting name that contains the CosmosDB connection string. 110 | * 111 | * @return The app setting name of the connection string. 112 | */ 113 | String connectionStringSetting(); 114 | 115 | /** 116 | * Defines partition key value for the lookup. May include binding parameters. 117 | * @return partition key value 118 | */ 119 | String partitionKey() default ""; 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/CosmosDBOutput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would be written to CosmosDB. The parameter type should be 17 | * OutputBinding<T>, where T could be one of: 18 | *

19 | * 20 | *
    21 | *
  • Some native Java types such as String
  • 22 | *
  • Any POJO type
  • 23 | *
24 | * 25 | *

26 | * The following example shows a Java function that adds a document to a database, using data 27 | * provided in the body of an HTTP Post request. 28 | *

29 | * 30 | *
 31 |  * {@literal @}FunctionName("addItem")
 32 |  *
 33 |  * public String cosmosDbAddItem(
 34 |  *    {@literal @}HttpTrigger(name = "request", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
 35 |  *     final String message,
 36 |  *    {@literal @}CosmosDBOutput(name = "database", databaseName = "ToDoList", collectionName = "Items", 
 37 |  *    connectionStringSetting = "AzureCosmosDBConnection")
 38 |  * ) {
 39 |  *     return "{ \"id\": \"" + System.currentTimeMillis() + "\", \"description\": \"" + message + "\" }";
 40 |  * }
 41 |  * 
42 | * 43 | * @since 1.0.0 44 | */ 45 | @Retention(RetentionPolicy.RUNTIME) 46 | @Target({ ElementType.PARAMETER, ElementType.METHOD }) 47 | public @interface CosmosDBOutput { 48 | /** 49 | * The variable name used in function.json. 50 | * 51 | * @return The variable name used in function.json. 52 | */ 53 | String name(); 54 | 55 | /** 56 | *

57 | * Defines how Functions runtime should treat the parameter value. Possible values are: 58 | *

59 | *
    60 | *
  • "" or string: treat it as a string whose value is serialized from the parameter
  • 61 | *
  • binary: treat it as a binary data whose value comes from for example 62 | * OutputBinding<byte[]>
  • 63 | *
64 | * 65 | * @return The dataType which will be used by the Functions runtime. 66 | */ 67 | String dataType() default ""; 68 | 69 | /** 70 | * Defines the database name of the CosmosDB to which to write. 71 | * 72 | * @return The database name string. 73 | */ 74 | String databaseName(); 75 | 76 | /** 77 | * Defines the collection name of the CosmosDB to which to write. 78 | * 79 | * @return The collection name string. 80 | */ 81 | String collectionName(); 82 | 83 | /** 84 | * Defines the ID of the CosmosDB to which to write. 85 | * 86 | * @return The ID string. 87 | */ 88 | boolean createIfNotExists() default false; 89 | 90 | /** 91 | * Defines the app setting name that contains the CosmosDB connection string. 92 | * 93 | * @return The app setting name of the connection string. 94 | */ 95 | String connectionStringSetting(); 96 | 97 | /** 98 | * Defines the partition key path for the created collection when createIfNotExists is set to 99 | * true. May include binding parameters. 100 | * 101 | * @return partitionKey of the created collection. 102 | */ 103 | String partitionKey() default ""; 104 | 105 | /** 106 | * If CreateIfNotExists is true, defines the throughput of the created collection. 107 | * 108 | * @return Throughput of the created collection. 109 | */ 110 | int collectionThroughput() default -1; 111 | 112 | /** 113 | * Enable to use with Multi Master accounts. 114 | * 115 | * @return whether to Multi Master accounts 116 | */ 117 | boolean useMultipleWriteLocations() default false; 118 | 119 | /** 120 | * Defines preferred locations (regions) for geo-replicated database accounts in the Azure Cosmos 121 | * DB service. Values should be comma-separated. example, PreferredLocations = "East US,South 122 | * Central US,North Europe" 123 | * 124 | * @return PreferredLocations for geo-replicated database accounts 125 | */ 126 | String preferredLocations() default ""; 127 | } 128 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | com.microsoft.azure.functions 6 | azure-functions-java-library 7 | 1.3.0-SNAPSHOT 8 | jar 9 | 10 | com.microsoft.maven 11 | java-8-parent 12 | 8.0.0 13 | 14 | 15 | Microsoft Azure Functions Java Core Types 16 | This package contains all Java interfaces and annotations to interact with Microsoft Azure functions runtime. 17 | https://azure.microsoft.com/en-us/services/functions 18 | 19 | 20 | UTF-8 21 | 22 | 23 | 24 | 25 | MIT License 26 | https://opensource.org/licenses/MIT 27 | repo 28 | 29 | 30 | 31 | 32 | scm:git:https://github.com/Azure/azure-functions-java-worker 33 | scm:git:git@github.com:Azure/azure-functions-java-worker 34 | https://github.com/Azure/azure-functions-java-worker 35 | HEAD 36 | 37 | 38 | 39 | 40 | pgopa 41 | Pragna Gopa 42 | pgopa@microsoft.com 43 | 44 | 45 | xscript 46 | Kevin Zhao 47 | kevinzha@microsoft.com 48 | 49 | 50 | 51 | 52 | 53 | ossrh 54 | Sonatype Snapshots 55 | https://oss.sonatype.org/content/repositories/snapshots/ 56 | true 57 | default 58 | 59 | 60 | 61 | 62 | 63 | maven.snapshots 64 | Maven Central Snapshot Repository 65 | https://oss.sonatype.org/content/repositories/snapshots/ 66 | 67 | false 68 | 69 | 70 | true 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | org.reflections 80 | reflections 81 | test 82 | 83 | 84 | junit 85 | junit 86 | test 87 | 88 | 89 | org.mockito 90 | mockito-core 91 | test 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | maven-compiler-plugin 100 | 101 | 102 | org.apache.maven.plugins 103 | maven-source-plugin 104 | 105 | 106 | org.apache.maven.plugins 107 | maven-javadoc-plugin 108 | 109 | 110 | org.apache.maven.plugins 111 | maven-enforcer-plugin 112 | 3.0.0-M2 113 | 114 | 115 | enforce-maven 116 | 117 | enforce 118 | 119 | 120 | 121 | 122 | 3.2.0 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | org.apache.maven.plugins 131 | maven-surefire-plugin 132 | 133 | ${project.build.directory} 134 | 135 | 136 | testing-project-jar 137 | ${project.artifactId}-${project.version}-tests.jar 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/CosmosDBTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import java.lang.annotation.ElementType; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.RetentionPolicy; 12 | import java.lang.annotation.Target; 13 | 14 | /** 15 | *

16 | * Place this on a parameter whose value would come from CosmosDB, and causing the method to run 17 | * when CosmosDB data is changed. The parameter type can be one of the following: 18 | *

19 | * 20 | *
    21 | *
  • Some native Java types such as String
  • 22 | *
  • Nullable values using Optional<T>
  • 23 | *
  • Any POJO type
  • 24 | *
25 | * 26 | *

27 | * The following example shows a Java function that is invoked when there are inserts or updates in 28 | * the specified database and collection. 29 | *

30 | * 31 | *
 32 |  * {@literal @}FunctionName("cosmosDBMonitor")
 33 |  * public void cosmosDbLog(
 34 |  *    {@literal @}CosmosDBTrigger(name = "database",
 35 |  *                      databaseName = "ToDoList",
 36 |  *                      collectionName = "Items",
 37 |  *                      leaseCollectionName = "leases",
 38 |  *                      createLeaseCollectionIfNotExists = true,
 39 |  *                      connectionStringSetting = "AzureCosmosDBConnection") 
 40 |  *                      List<Map<String, String>> items,
 41 |  *     final ExecutionContext context
 42 |  * ) {
 43 |  *     context.getLogger().info(items.size() + " item(s) is/are inserted.");
 44 |  *     if (!items.isEmpty()) {
 45 |  *         context.getLogger().info("The ID of the first item is: " + items.get(0).get("id"));
 46 |  *     }
 47 |  * }
 48 |  * 
49 | * 50 | * @since 1.0.0 51 | */ 52 | @Retention(RetentionPolicy.RUNTIME) 53 | @Target({ ElementType.PARAMETER }) 54 | public @interface CosmosDBTrigger { 55 | /** 56 | * The variable name used in function.json. 57 | * 58 | * @return The variable name used in function.json. 59 | */ 60 | String name(); 61 | 62 | /** 63 | *

64 | * Defines how Functions runtime should treat the parameter value. Possible values are: 65 | *

66 | *
    67 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like 68 | * POJO
  • 69 | *
  • string: always get the value as a string
  • 70 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type 71 | * byte[]
  • 72 | *
73 | * 74 | * @return The dataType which will be used by the Functions runtime. 75 | */ 76 | String dataType() default ""; 77 | 78 | /** 79 | * Defines the database name of the CosmosDB to which to bind. 80 | * 81 | * @return The database name string. 82 | */ 83 | String databaseName(); 84 | 85 | /** 86 | * Defines the collection name of the CosmosDB to which to bind. 87 | * 88 | * @return The collection name string. 89 | */ 90 | String collectionName(); 91 | 92 | /** 93 | * Defines Connection string for the service containing the lease collection. 94 | * 95 | * @return Connection string for the lease collection. 96 | */ 97 | String leaseConnectionStringSetting() default ""; 98 | 99 | /** 100 | * Defines the lease collection name of the CosmosDB to which to bind. 101 | * 102 | * @return The lease collection name string. 103 | */ 104 | String leaseCollectionName() default ""; 105 | 106 | /** 107 | * Defines Name of the database containing the lease collection. 108 | * 109 | * @return Name of the database for lease collection. 110 | */ 111 | String leaseDatabaseName() default ""; 112 | 113 | /** 114 | * Defines whether to create a new lease collection if not exists. 115 | * 116 | * @return configuration whether to create a new lease collection if not exists. 117 | */ 118 | boolean createLeaseCollectionIfNotExists() default false; 119 | 120 | /** 121 | * defines the throughput of the created collection.. 122 | * 123 | * @return throughput 124 | */ 125 | int leasesCollectionThroughput() default -1; 126 | 127 | /** 128 | * Defines a prefix to be used within a Leases collection for this Trigger. Useful when sharing 129 | * the same Lease collection among multiple Triggers. 130 | * 131 | * @return LeaseCollectionPrefix 132 | */ 133 | String leaseCollectionPrefix() default ""; 134 | 135 | /** 136 | * Customizes the amount of milliseconds between lease checkpoints. Default is always after a 137 | * Function call. 138 | * 139 | * @return checkpointInterval 140 | */ 141 | int checkpointInterval() default -1; 142 | 143 | /** 144 | * Customizes the amount of documents between lease checkpoints. Default is always after a 145 | * Function call. 146 | * 147 | * @return CheckpointDocumentCount 148 | */ 149 | int checkpointDocumentCount() default -1; 150 | 151 | /** 152 | * Customizes the delay in milliseconds in between polling a partition for new changes on the 153 | * feed, after all current changes are drained. Default is 5000 (5 seconds). 154 | * 155 | * @return feedPollDelay 156 | */ 157 | int feedPollDelay() default 5000; 158 | 159 | /** 160 | * Defines the app setting name that contains the CosmosDB connection string. 161 | * 162 | * @return The app setting name of the connection string. 163 | */ 164 | String connectionStringSetting(); 165 | 166 | /** 167 | * Customizes the renew interval in milliseconds for all leases for partitions currently held by 168 | * the Trigger. Default is 17000 (17 seconds). 169 | * 170 | * @return renew interval in milliseconds for all leases 171 | */ 172 | int leaseRenewInterval() default 17000; 173 | 174 | /** 175 | * Customizes the interval in milliseconds to kick off a task to compute if partitions are 176 | * distributed evenly among known host instances. Default is 13000 (13 seconds). 177 | * 178 | * @return interval in milliseconds 179 | */ 180 | int leaseAcquireInterval() default 13000; 181 | 182 | /** 183 | * Customizes the interval in milliseconds for which the lease is taken on a lease representing a 184 | * partition. If the lease is not renewed within this interval, it will cause it to expire and 185 | * ownership of the partition will move to another Trigger instance. Default is 60000 (60 186 | * seconds). 187 | * 188 | * @return interval in milliseconds for which the lease is taken 189 | */ 190 | int leaseExpirationInterval() default 60000; 191 | 192 | /** 193 | * Customizes the maximum amount of items received in an invocation 194 | * 195 | * @return maximum amount of items received 196 | */ 197 | int maxItemsPerInvocation() default -1; 198 | 199 | /** 200 | * Gets or sets whether change feed in the Azure Cosmos DB service should start from beginning 201 | * (true) or from current (false). By default it's start from current (false). 202 | * 203 | * @return Configuration whether change feed should start from beginning 204 | */ 205 | boolean startFromBeginning() default false; 206 | 207 | /** 208 | * Defines preferred locations (regions) for geo-replicated database accounts in the Azure Cosmos 209 | * DB service. Values should be comma-separated. example, PreferredLocations = "East US,South 210 | * Central US,North Europe" 211 | * 212 | * @return preferred locations (regions) for geo-replicated database accounts 213 | */ 214 | String preferredLocations() default ""; 215 | } 216 | -------------------------------------------------------------------------------- /src/main/java/com/microsoft/azure/functions/annotation/HttpTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) Microsoft Corporation. All rights reserved. 3 | * Licensed under the MIT License. See License.txt in the project root for 4 | * license information. 5 | */ 6 | 7 | package com.microsoft.azure.functions.annotation; 8 | 9 | import com.microsoft.azure.functions.HttpMethod; 10 | 11 | import java.lang.annotation.ElementType; 12 | import java.lang.annotation.Retention; 13 | import java.lang.annotation.RetentionPolicy; 14 | import java.lang.annotation.Target; 15 | 16 | /** 17 | *

18 | * The HttpTrigger annotation is applied to Azure functions that will be triggered by a call to the 19 | * HTTP endpoint that the function is located at. The HttpTrigger annotation should be applied to a 20 | * method parameter of one of the following types: 21 | *

22 | * 23 | *
    24 | *
  • {@link com.microsoft.azure.functions.HttpRequestMessage HttpRequestMessage<T>}
  • 25 | *
  • Any native Java types such as int, String, byte[]
  • 26 | *
  • Nullable values using Optional<T>
  • 27 | *
  • Any POJO type
  • 28 | *
29 | * 30 | *

31 | * For example: 32 | *

33 | * 34 | *
 35 |  * {@literal @}FunctionName("hello")
 36 |  *  public HttpResponseMessage<String> helloFunction(
 37 |  *    {@literal @}HttpTrigger(name = "req",
 38 |  *                  methods = {HttpMethod.GET},
 39 |  *                  authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request
 40 |  *  ) {
 41 |  *     ....
 42 |  *  }
 43 |  * 
44 | * 45 | *

46 | * In this code snippet you will observe that we have a function annotated with 47 | * {@code @FunctionName("hello")}, which indicates that this function will be available at the 48 | * endpoint /api/hello. The name of the method itself, in this case {@code helloFunction} is 49 | * irrelevant for all intents and purposes related to Azure Functions. Note however that the method 50 | * return type is {@link com.microsoft.azure.functions.HttpResponseMessage}, and that the first 51 | * argument into the function is an {@link com.microsoft.azure.functions.HttpRequestMessage} with 52 | * generic type {@code Optional}. This indicates that the body of the request will 53 | * potentially contain a String value. 54 | *

55 | * 56 | *

57 | * Most important of all however is the {@code @HttpTrigger} annotation that has been applied to 58 | * this argument. In this annotation you'll note that it has been given a name, as well as told what 59 | * type of requests it supports (in this case, only HTTP GET requests), and that the 60 | * {@link AuthorizationLevel} is anonymous, allowing access to anyone who can call the endpoint. 61 | *

62 | * 63 | *

64 | * The {@code HttpTrigger} can be further customised by providing a custom {@link #route()}, which 65 | * allows for custom endpoints to be specified, and for these endpoints to be parameterized with 66 | * arguments being bound to arguments provided to the function at runtime. 67 | *

68 | * 69 | *

70 | * The following example shows a Java function that looks for a name parameter either in the query 71 | * string (HTTP GET) or the body (HTTP POST) of the HTTP request. Notice that the return value is 72 | * used for the output binding, but a return value attribute isn't required. 73 | *

74 | * 75 | *
 76 |  * {@literal @}FunctionName("readHttpName")
 77 |  *  public String readName(
 78 |  *    {@literal @}HttpTrigger(name = "req", 
 79 |  *          methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
 80 |  *          final HttpRequestMessage<Optional<String>> request) {
 81 |  *       String name = request.getBody().orElseGet(() -> request.getQueryParameters().get("name"));
 82 |  *       return name == null ?
 83 |  *              "Please pass a name on the query string or in the request body" :
 84 |  *              "Hello " + name;
 85 |  *  }
 86 |  * 
87 | * 88 | * @see com.microsoft.azure.functions.HttpRequestMessage 89 | * @see com.microsoft.azure.functions.HttpResponseMessage 90 | * @since 1.0.0 91 | */ 92 | @Retention(RetentionPolicy.RUNTIME) 93 | @Target(ElementType.PARAMETER) 94 | public @interface HttpTrigger { 95 | /** 96 | * The variable name used in function code for the request or request body. 97 | * 98 | * @return The variable name used in function code for the request or request body. 99 | */ 100 | String name(); 101 | 102 | /** 103 | *

104 | * Defines how Functions runtime should treat the parameter value. Possible values are: 105 | *

106 | *
    107 | *
  • "": get the value as a string, and try to deserialize to actual parameter type like 108 | * POJO
  • 109 | *
  • string: always get the value as a string
  • 110 | *
  • binary: get the value as a binary data, and try to deserialize to actual parameter type 111 | * byte[]
  • 112 | *
113 | * 114 | * @return The dataType which will be used by the Functions runtime. 115 | */ 116 | String dataType() default ""; 117 | 118 | /** 119 | *

120 | * Defines the route template, controlling which request URLs your function will respond to. The 121 | * default value if no route is provided is the function name specified in the 122 | * {@link FunctionName} annotation, applied to each Azure Function. 123 | *

124 | * 125 | *

126 | * By default when you create a function for an HTTP trigger, or WebHook, the function is 127 | * addressable with a route of the form 128 | * {@code http://<yourapp>.azurewebsites.net/api/<funcname>}. You can customize this 129 | * route using this route property. For example, a route of 130 | * {@code "products/{category:alpha}/{id:int}"} would mean that the function is now addressable 131 | * with the following route instead of the original route: 132 | * {@code http://<yourapp>.azurewebsites.net/api/products/electronics/357}, which allows the 133 | * function code to support two parameters in the address: category and id. By specifying the 134 | * route in this way, developers can then add the additional route arguments as arguments into the 135 | * function by using the {@link BindingName} annotation. For example: 136 | *

137 | * 138 | *
139 |    * {@literal @}FunctionName("routeTest")
140 |    *  public HttpResponseMessage<String> routeTest(
141 |    *      {@literal @}HttpTrigger(name = "req",
142 |    *                    methods = {HttpMethod.GET},
143 |    *                    authLevel = AuthorizationLevel.ANONYMOUS,
144 |    *                    route = "products/{category:alpha}/{id:int}") 
145 |    *                    HttpRequestMessage<Optional<String>> request,
146 |    *      {@literal @}BindingName("category") String category,
147 |    *      {@literal @}BindingName("id") int id,
148 |    *       final ExecutionContext context
149 |    *  ) {
150 |    *           ....
151 |    *           context.getLogger().info("We have " + category + " with id " + id);
152 |    *           ....
153 |    *  }
154 |    * 
155 | * 156 | *

157 | * For more details on the route syntax, refer to the 159 | * online documentation. 160 | *

161 | * 162 | * @return The route template to use for the annotated function. 163 | */ 164 | String route() default ""; 165 | 166 | /** 167 | * An array of the HTTP methods to which the function responds. If not specified, the function 168 | * responds to all HTTP methods. 169 | * 170 | * @return An array containing all valid HTTP methods. 171 | */ 172 | HttpMethod[] methods() default {}; 173 | 174 | /** 175 | *

176 | * Determines what keys, if any, need to be present on the request in order to invoke the 177 | * function. The authorization level can be one of the following values: 178 | *

179 | * 180 | *
    181 | *
  • anonymous: No API key is required.
  • 182 | *
  • function: A function-specific API key is required. This is the default 183 | * value if none is provided.
  • 184 | *
  • admin: The master key is required.
  • 185 | *
186 | * 187 | *

188 | * For more information, see the documentation 190 | * about authorization keys. 191 | *

192 | * 193 | * @return An {@link AuthorizationLevel} value representing the level required to access the 194 | * function. 195 | */ 196 | AuthorizationLevel authLevel() default AuthorizationLevel.FUNCTION; 197 | } 198 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Library for Azure Java Functions 2 | This repo contains library for building Azure Java Functions. Visit the [complete documentation of Azure Functions - Java Developer Guide](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-java) for more details. 3 | 4 | ## azure-functions-maven plugin 5 | [How to use azure-functions-maven plugin to create, update, deploy and test azure java functions](https://docs.microsoft.com/en-us/java/api/overview/azure/maven/azure-functions-maven-plugin/readme?view=azure-java-stable) 6 | 7 | ## Prerequisites 8 | 9 | * Java 8 10 | 11 | ## Parent POM 12 | 13 | Please see for details on Parent POM https://github.com/Microsoft/maven-java-parent 14 | 15 | ## Summary 16 | 17 | Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. You can write just the code you need for the problem at hand, without worrying about a whole application or the infrastructure to run it. Functions can make development even more productive.Pay only for the time your code runs and trust Azure to scale as needed. Azure Functions lets you develop [serverless](https://azure.microsoft.com/en-us/solutions/serverless/) applications on Microsoft Azure. 18 | 19 | Azure Functions supports triggers, which are ways to start execution of your code, and bindings, which are ways to simplify coding for input and output data. A function should be a stateless method to process input and produce output. Although you are allowed to write instance methods, your function must not depend on any instance fields of the class. You need to make sure all the function methods are `public` accessible and method with annotation @FunctionName is unique as that defines the entry for the the function. 20 | 21 | A deployable unit is an uber JAR containing one or more functions (see below), and a JSON file with the list of functions and triggers definitions, deployed to Azure Functions. The JAR can be created in many ways, although we recommend [Azure Functions Maven Plugin](https://docs.microsoft.com/en-us/java/api/overview/azure/maven/azure-functions-maven-plugin/readme), as it provides templates to get you started with key scenarios. 22 | 23 | All the input and output bindings can be defined in `function.json` (not recommended), or in the Java method by using annotations (recommended). All the types and annotations used in this document are included in the `azure-functions-java-library` package. 24 | 25 | ### Sample 26 | 27 | Here is an example of a HttpTrigger Azure function in Java: 28 | 29 | 30 | ```java 31 | package com.example; 32 | 33 | import com.microsoft.azure.functions.annotation.*; 34 | 35 | public class Function { 36 | @FunctionName("echo") 37 | public static String echo(@HttpTrigger(name = "req", methods = { "post" }, authLevel = AuthorizationLevel.ANONYMOUS) String in) { 38 | return "Hello, " + in + "."; 39 | } 40 | } 41 | ``` 42 | 43 | ### Adding 3rd Party Libraries 44 | 45 | Azure Functions supports the use of 3rd party libraries. If using the Maven plugin for Azure Functions, all of your dependencies specified in your `pom.xml` file will be automatically bundled during the `mvn package` step. 46 | 47 | ## Data Types 48 | 49 | You are free to use all the data types in Java for the input and output data, including native types; customized POJO types and specialized Azure types defined in this API. Azure Functions runtime will try its best to convert the actual input value to the type you need (for example, a `String` input will be treated as a JSON string and be parsed to a POJO type defined in your code). 50 | 51 | ### JSON Support 52 | The POJO types (Java classes) you may define have to be publicly accessible (`public` modifier). POJO properties/fields may be `private`. For example a JSON string `{ "x": 3 }` is able to be converted to the following POJO type: 53 | 54 | ```java 55 | public class PojoData { 56 | private int x; 57 | } 58 | ``` 59 | 60 | ### Other supported types 61 | Binary data is represented as `byte[]` or `Byte[]` in your Azure functions code. And make sure you specify `dataType = "binary"` in the corresponding triggers/bindings. 62 | 63 | Empty input values could be `null` as your functions argument, but a recommended way to deal with potential empty values is to use `Optional` type. 64 | 65 | 66 | ## Inputs 67 | 68 | Inputs are divided into two categories in Azure Functions: one is the trigger input and the other is the additional input. Trigger input is the input who triggers your function. And besides that, you may also want to get inputs from other sources (like a blob), that is the additional input. 69 | 70 | Let's take the following code snippet as an example: 71 | 72 | ```java 73 | package com.example; 74 | 75 | import com.microsoft.azure.functions.annotation.*; 76 | 77 | public class Function { 78 | @FunctionName("echo") 79 | public String echo( 80 | @HttpTrigger(name = "req", methods = { "put" }, authLevel = AuthorizationLevel.ANONYMOUS, route = "items/{id}") String in, 81 | @TableInput(name = "item", tableName = "items", partitionKey = "example", rowKey = "{id}", connection = "AzureWebJobsStorage") TestInputData inputData 82 | ) { 83 | return "Hello, " + in + " and " + inputData.getRowKey() + "."; 84 | } 85 | 86 | } 87 | 88 | public class TestInputData { 89 | public String getRowKey() { return this.rowKey; } 90 | private String rowKey; 91 | } 92 | 93 | ``` 94 | 95 | When this function is invoked, the HTTP request payload will be passed as the `String` for argument `in`; and one entry will be retrieved from the Azure Table Storage and be passed to argument `inputData` as `TestInputData` type. 96 | 97 | To receive events in a batch when using EventHubTrigger, set cardinality to many and change input type to an array or List<> 98 | 99 | ```java 100 | @FunctionName("ProcessIotMessages") 101 | public void processIotMessages( 102 | @EventHubTrigger(name = "message", eventHubName = "%AzureWebJobsEventHubPath%", connection = "AzureWebJobsEventHubSender", cardinality = Cardinality.MANY) List messages, 103 | final ExecutionContext context) 104 | { 105 | context.getLogger().info("Java Event Hub trigger received messages. Batch size: " + messages.size()); 106 | } 107 | 108 | public class TestEventData { 109 | public String id; 110 | } 111 | 112 | ``` 113 | 114 | Note: You can also bind to String[], TestEventData[] or List 115 | 116 | ## Outputs 117 | 118 | Outputs can be expressed in return value or output parameters. If there is only one output, you are recommended to use the return value. For multiple outputs, you have to use **output parameters**. 119 | 120 | Return value is the simplest form of output, you just return the value of any type, and Azure Functions runtime will try to marshal it back to the actual type (such as an HTTP response). You could apply any *output annotations* to the function method (the `name` property of the annotation has to be `$return`) to define the return value output. 121 | 122 | For example, a blob content copying function could be defined as the following code. `@StorageAccount` annotation is used here to prevent the duplicating of the `connection` property for both `@BlobTrigger` and `@BlobOutput`. 123 | 124 | ```java 125 | package com.example; 126 | 127 | import com.microsoft.azure.functions.annotation.*; 128 | 129 | public class Function { 130 | @FunctionName("copy") 131 | @StorageAccount("AzureWebJobsStorage") 132 | @BlobOutput(name = "$return", path = "samples-output-java/{name}") 133 | public String copy(@BlobTrigger(name = "blob", path = "samples-input-java/{name}") String content) { 134 | return content; 135 | } 136 | } 137 | ``` 138 | 139 | To produce multiple output values, use `OutputBinding` type defined in the `azure-functions-java-library` package. If you need to make an HTTP response and push a message to a queue, you can write something like: 140 | 141 | ```java 142 | package com.example; 143 | 144 | import com.microsoft.azure.functions.*; 145 | import com.microsoft.azure.functions.annotation.*; 146 | 147 | public class Function { 148 | @FunctionName("push") 149 | public String push( 150 | @HttpTrigger(name = "req", methods = { "post" }, authLevel = AuthorizationLevel.ANONYMOUS) String body, 151 | @QueueOutput(name = "message", queueName = "myqueue", connection = "AzureWebJobsStorage") OutputBinding queue 152 | ) { 153 | queue.setValue("This is the queue message to be pushed"); 154 | return "This is the HTTP response content"; 155 | } 156 | } 157 | ``` 158 | 159 | Use `OutputBinding` type to make a binary output value (for parameters); for return values, just use `byte[]`. 160 | 161 | ## Execution Context 162 | 163 | You interact with Azure Functions execution environment via the `ExecutionContext` object defined in the `azure-functions-java-library` package. You are able to get the invocation ID, the function name and a built-in logger (which is integrated prefectly with Azure Function Portal experience as well as AppInsights) from the context object. 164 | 165 | What you need to do is just add one more `ExecutionContext` typed parameter to your function method. Let's take a timer triggered function as an example: 166 | 167 | ```java 168 | package com.example; 169 | 170 | import com.microsoft.azure.functions.*; 171 | import com.microsoft.azure.functions.annotation.*; 172 | 173 | public class Function { 174 | @FunctionName("heartbeat") 175 | public static void heartbeat( 176 | @TimerTrigger(name = "schedule", schedule = "*/30 * * * * *") String timerInfo, 177 | ExecutionContext context 178 | ) { 179 | context.getLogger().info("Heartbeat triggered by " + context.getFunctionName()); 180 | } 181 | } 182 | ``` 183 | 184 | 185 | ## Specialized Data Types 186 | 187 | ### HTTP Request and Response 188 | 189 | Sometimes a function need to take a more detailed control of the input and output, and that's why we also provide some specialized types in the `azure-functions-java-library` package for you to manipulate: 190 | 191 | | Specialized Type | Target | Typical Usage | 192 | | ------------------------ | :-----------------: | ------------------------------ | 193 | | `HttpRequestMessage` | HTTP Trigger | Get method, headers or queries | 194 | | `HttpResponseMessage` | HTTP Output Binding | Return status other than 200 | 195 | 196 | ### Metadata 197 | 198 | Metadata comes from different sources, like HTTP headers, HTTP queries, and [trigger metadata](https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#trigger-metadata-properties). You can use `@BindingName` annotation together with the metadata name to get the value. 199 | 200 | For example, the `queryValue` in the following code snippet will be `"test"` if the requested URL is `http://{example.host}/api/metadata?name=test`. 201 | 202 | ```java 203 | package com.example; 204 | 205 | import java.util.Optional; 206 | import com.microsoft.azure.functions.annotation.*; 207 | 208 | public class Function { 209 | @FunctionName("metadata") 210 | public static String metadata( 211 | @HttpTrigger(name = "req", methods = { "get", "post" }, authLevel = AuthorizationLevel.ANONYMOUS) Optional body, 212 | @BindingName("name") String queryValue 213 | ) { 214 | return body.orElse(queryValue); 215 | } 216 | } 217 | ``` 218 | 219 | ### License 220 | 221 | This project is under the benevolent umbrella of the [.NET Foundation](http://www.dotnetfoundation.org/) and is licensed under [the MIT License](LICENSE.txt) 222 | 223 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. 224 | --------------------------------------------------------------------------------