diff --git a/README.adoc b/README.adoc
index 60a3bad9f..3d99877fe 100644
--- a/README.adoc
+++ b/README.adoc
@@ -195,5 +195,180 @@ from the `file` menu.
:spring-cloud-build-branch: master
-Please refer to https://github.com/spring-cloud/spring-cloud-build#contributing[this Spring Cloud documentation]
-to learn more about the licensing, Contributor License Agreement, Code of Conduct, Code Conventions and Checkstyle Configuration.
+Spring Cloud is released under the non-restrictive Apache 2.0 license,
+and follows a very standard Github development process, using Github
+tracker for issues and merging pull requests into master. If you want
+to contribute even something trivial please do not hesitate, but
+follow the guidelines below.
+
+=== Sign the Contributor License Agreement
+Before we accept a non-trivial patch or pull request we will need you to sign the
+https://cla.pivotal.io/sign/spring[Contributor License Agreement].
+Signing the contributor's agreement does not grant anyone commit rights to the main
+repository, but it does mean that we can accept your contributions, and you will get an
+author credit if we do. Active contributors might be asked to join the core team, and
+given the ability to merge pull requests.
+
+=== Code of Conduct
+This project adheres to the Contributor Covenant https://github.com/spring-cloud/spring-cloud-build/blob/master/docs/src/main/asciidoc/code-of-conduct.adoc[code of
+conduct]. By participating, you are expected to uphold this code. Please report
+unacceptable behavior to spring-code-of-conduct@pivotal.io.
+
+=== Code Conventions and Housekeeping
+None of these is essential for a pull request, but they will all help. They can also be
+added after the original pull request but before a merge.
+
+* Use the Spring Framework code format conventions. If you use Eclipse
+ you can import formatter settings using the
+ `eclipse-code-formatter.xml` file from the
+ https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-dependencies-parent/eclipse-code-formatter.xml[Spring
+ Cloud Build] project. If using IntelliJ, you can use the
+ https://plugins.jetbrains.com/plugin/6546[Eclipse Code Formatter
+ Plugin] to import the same file.
+* Make sure all new `.java` files to have a simple Javadoc class comment with at least an
+ `@author` tag identifying you, and preferably at least a paragraph on what the class is
+ for.
+* Add the ASF license header comment to all new `.java` files (copy from existing files
+ in the project)
+* Add yourself as an `@author` to the .java files that you modify substantially (more
+ than cosmetic changes).
+* Add some Javadocs and, if you change the namespace, some XSD doc elements.
+* A few unit tests would help a lot as well -- someone has to do it.
+* If no-one else is using your branch, please rebase it against the current master (or
+ other target branch in the main project).
+* When writing a commit message please follow https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html[these conventions],
+ if you are fixing an existing issue please add `Fixes gh-XXXX` at the end of the commit
+ message (where XXXX is the issue number).
+
+=== Checkstyle
+
+Spring Cloud Build comes with a set of checkstyle rules. You can find them in the `spring-cloud-build-tools` module. The most notable files under the module are:
+
+.spring-cloud-build-tools/
+----
+└── src
+ ├── checkstyle
+ │ └── checkstyle-suppressions.xml <3>
+ └── main
+ └── resources
+ ├── checkstyle-header.txt <2>
+ └── checkstyle.xml <1>
+----
+<1> Default Checkstyle rules
+<2> File header setup
+<3> Default suppression rules
+
+==== Checkstyle configuration
+
+Checkstyle rules are *disabled by default*. To add checkstyle to your project just define the following properties and plugins.
+
+.pom.xml
+----
+
+true <1>
+ true
+ <2>
+ true
+ <3>
+
+
+
+
+ <4>
+ io.spring.javaformat
+ spring-javaformat-maven-plugin
+
+ <5>
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+
+
+
+
+
+ <5>
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+
+
+
+
+----
+<1> Fails the build upon Checkstyle errors
+<2> Fails the build upon Checkstyle violations
+<3> Checkstyle analyzes also the test sources
+<4> Add the Spring Java Format plugin that will reformat your code to pass most of the Checkstyle formatting rules
+<5> Add checkstyle plugin to your build and reporting phases
+
+If you need to suppress some rules (e.g. line length needs to be longer), then it's enough for you to define a file under `${project.root}/src/checkstyle/checkstyle-suppressions.xml` with your suppressions. Example:
+
+.projectRoot/src/checkstyle/checkstyle-suppresions.xml
+----
+
+
+
+
+
+
+----
+
+It's advisable to copy the `${spring-cloud-build.rootFolder}/.editorconfig` and `${spring-cloud-build.rootFolder}/.springformat` to your project. That way, some default formatting rules will be applied. You can do so by running this script:
+
+```bash
+$ curl https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/.editorconfig -o .editorconfig
+$ touch .springformat
+```
+
+=== IDE setup
+
+==== Intellij IDEA
+
+In order to setup Intellij you should import our coding conventions, inspection profiles and set up the checkstyle plugin.
+The following files can be found in the https://github.com/spring-cloud/spring-cloud-build/tree/master/spring-cloud-build-tools[Spring Cloud Build] project.
+
+.spring-cloud-build-tools/
+----
+└── src
+ ├── checkstyle
+ │ └── checkstyle-suppressions.xml <3>
+ └── main
+ └── resources
+ ├── checkstyle-header.txt <2>
+ ├── checkstyle.xml <1>
+ └── intellij
+ ├── Intellij_Project_Defaults.xml <4>
+ └── Intellij_Spring_Boot_Java_Conventions.xml <5>
+----
+<1> Default Checkstyle rules
+<2> File header setup
+<3> Default suppression rules
+<4> Project defaults for Intellij that apply most of Checkstyle rules
+<5> Project style conventions for Intellij that apply most of Checkstyle rules
+
+.Code style
+
+image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring-cloud-build-branch}/docs/src/main/asciidoc/images/intellij-code-style.png[Code style]
+
+Go to `File` -> `Settings` -> `Editor` -> `Code style`. There click on the icon next to the `Scheme` section. There, click on the `Import Scheme` value and pick the `Intellij IDEA code style XML` option. Import the `spring-cloud-build-tools/src/main/resources/intellij/Intellij_Spring_Boot_Java_Conventions.xml` file.
+
+.Inspection profiles
+
+image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring-cloud-build-branch}/docs/src/main/asciidoc/images/intellij-inspections.png[Code style]
+
+Go to `File` -> `Settings` -> `Editor` -> `Inspections`. There click on the icon next to the `Profile` section. There, click on the `Import Profile` and import the `spring-cloud-build-tools/src/main/resources/intellij/Intellij_Project_Defaults.xml` file.
+
+.Checkstyle
+
+To have Intellij work with Checkstyle, you have to install the `Checkstyle` plugin. It's advisable to also install the `Assertions2Assertj` to automatically convert the JUnit assertions
+
+image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring-cloud-build-branch}/docs/src/main/asciidoc/images/intellij-checkstyle.png[Checkstyle]
+
+Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on the `+` icon in the `Configuration file` section. There, you'll have to define where the checkstyle rules should be picked from. In the image above, we've picked the rules from the cloned Spring Cloud Build repository. However, you can point to the Spring Cloud Build's GitHub repository (e.g. for the `checkstyle.xml` : `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle.xml`). We need to provide the following variables:
+
+- `checkstyle.header.file` - please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` URL.
+- `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL.
+- `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`.
+
+IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.
\ No newline at end of file
diff --git a/docs/src/main/asciidoc/spring-cloud-function.adoc b/docs/src/main/asciidoc/spring-cloud-function.adoc
index fa3f965f9..d1e065adc 100644
--- a/docs/src/main/asciidoc/spring-cloud-function.adoc
+++ b/docs/src/main/asciidoc/spring-cloud-function.adoc
@@ -254,6 +254,8 @@ completely.
== Standalone Web Applications
+Functions could be automatically exported as HTTP endpoints.
+
The `spring-cloud-function-web` module has autoconfiguration that
activates when it is included in a Spring Boot web application (with
MVC support). There is also a `spring-cloud-starter-function-web` to
@@ -263,7 +265,7 @@ getting started experience.
With the web configurations activated your app will have an MVC
endpoint (on "/" by default, but configurable with
`spring.cloud.function.web.path`) that can be used to access the
-functions in the application context. The supported content types are
+functions in the application context where function name becomes part of the URL path. The supported content types are
plain text and JSON.
|===
@@ -278,16 +280,31 @@ plain text and JSON.
|===
-As the table above shows the behaviour of the endpoint depends on the method and also the type of incoming request data. When the incoming data is single valued, and the target function is declared as obviously single valued (i.e. not returning a collection or `Flux`), then the response will also contain a single value.
+As the table above shows the behaviour of the endpoint depends on the method and also the type of incoming request data. When the incoming data
+is single valued, and the target function is declared as obviously single valued (i.e. not returning a collection or `Flux`), then the response
+will also contain a single value.
For multi-valued responses the client can ask for a server-sent event stream by sending `Accept: text/event-stream".
-If there is only a single function (consumer etc.) in the catalog, the name in the path is optional.
-Composite functions can be addressed using pipes or commas to separate function names (pipes are legal in URL paths, but a bit awkward to type on the command line).
+Functions and consumers that are declared with input and output in `Message>` will see the request headers on the input messages, and the output message headers will be converted to HTTP headers.
-For cases where there is more then a single function in catalog and you want to map a specific function to the root
-path (e.g., "/"), or you want to compose several functions and then map to the root path you can do so by providing
-`spring.cloud.function.definition` property which essentially used by spring-cloud-function-web module to provide
-default mapping for cases where there is some type of a conflict (e.g., more then one function available etc).
+When POSTing text the response format might be different with Spring Boot 2.0 and older versions, depending on the content negotiation (provide content type and accept headers for the best results).
+
+See <> to see the details and example on how to test such application.
+
+
+=== Function Mapping rules
+
+If there is only a single function (consumer etc.) in the catalog, the name in the path is optional.
+In other words, providing you only have `uppercase` function in catalog
+`curl -H "Content-Type: text/plain" localhost:8080/uppercase -d hello` and `curl -H "Content-Type: text/plain" localhost:8080/ -d hello` calls are identical.
+
+Composite functions can be addressed using pipes or commas to separate function names (pipes are legal in URL paths, but a bit awkward to type on the command line).
+For example, `curl -H "Content-Type: text/plain" localhost:8080/uppercase,reverse -d hello`.
+
+For cases where there is more then a single function in catalog, each function will be exported and mapped with function name being
+part of the path (e.g., `localhost:8080/uppercase`).
+In this scenario you can still map specific function or function composition to the root path by providing
+`spring.cloud.function.definition` property
For example,
----
@@ -296,11 +313,24 @@ For example,
The above property will compose 'foo' and 'bar' function and map the composed function to the "/" path.
-Functions and consumers that are declared with input and output in `Message>` will see the request headers on the input messages, and the output message headers will be converted to HTTP headers.
+=== Function Filtering rules
-When POSTing text the response format might be different with Spring Boot 2.0 and older versions, depending on the content negotiation (provide content type and accpt headers for the best results).
+In situations where there are more then one function in catalog there may be a need to only export certain functions or function compositions. In that case you can use
+the same `spring.cloud.function.definition` property listing functions you intend to export delimited by `;`.
+Note that in this case nothing will be mapped to the root path and functions that are not listed (including compositions) are not going to be exported
-See <> to see the details and example on how to test such application.
+For example,
+----
+--spring.cloud.function.definition=foo;bar
+----
+
+This will only export function `foo` and function `bar` regardless how many functions are available in catalog (e.g., `localhost:8080/foo`).
+
+----
+--spring.cloud.function.definition=foo|bar;baz
+----
+
+This will only export function composition `foo|bar` and function `baz` regardless how many functions are available in catalog (e.g., `localhost:8080/foo,bar`).
== Standalone Streaming Applications
diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/SpringBootRequestHandlerTests.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/SpringBootRequestHandlerTests.java
index db468e547..abe3fc8f1 100644
--- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/SpringBootRequestHandlerTests.java
+++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/SpringBootRequestHandlerTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2019 the original author or authors.
+ * Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.cloud.function.adapter.aws;
import java.util.function.Function;
+import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
@@ -36,6 +37,11 @@ public class SpringBootRequestHandlerTests {
private SpringBootRequestHandler handler;
+ @Before
+ public void after() {
+ System.clearProperty("spring.cloud.function.definition");
+ }
+
@Test
public void functionBean() throws Exception {
this.handler = new SpringBootRequestHandler(FunctionConfig.class);
diff --git a/spring-cloud-function-context/pom.xml b/spring-cloud-function-context/pom.xml
index 37ebefe64..f6d6f25bf 100644
--- a/spring-cloud-function-context/pom.xml
+++ b/spring-cloud-function-context/pom.xml
@@ -59,9 +59,6 @@
test
-
-
-
com.fasterxml.jackson.core
jackson-databind
@@ -72,7 +69,6 @@
org.apache.maven.plugins
maven-compiler-plugin
- 3.5.1
diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java
index 38892a167..893116d09 100644
--- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java
+++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistry.java
@@ -24,6 +24,7 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -51,6 +52,7 @@ import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.cloud.function.context.FunctionCatalog;
@@ -94,7 +96,7 @@ import org.springframework.util.StringUtils;
* @since 3.0
*/
public class BeanFactoryAwareFunctionRegistry
- implements FunctionRegistry, FunctionInspector, ApplicationContextAware {
+ implements FunctionRegistry, FunctionInspector, ApplicationContextAware, InitializingBean {
private static Log logger = LogFactory.getLog(BeanFactoryAwareFunctionRegistry.class);
@@ -118,10 +120,22 @@ public class BeanFactoryAwareFunctionRegistry
private final CompositeMessageConverter messageConverter;
+ private List declaredFunctionDefinitions;
+
public BeanFactoryAwareFunctionRegistry(ConversionService conversionService,
@Nullable CompositeMessageConverter messageConverter) {
this.conversionService = conversionService;
this.messageConverter = messageConverter;
+
+ }
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ String userDefinition = this.applicationContext.getEnvironment().getProperty("spring.cloud.function.definition");
+ this.declaredFunctionDefinitions = StringUtils.hasText(userDefinition) ? Arrays.asList(userDefinition.split(";")) : Collections.emptyList();
+ if (this.declaredFunctionDefinitions.contains(RoutingFunction.FUNCTION_NAME)) {
+ Assert.isTrue(this.declaredFunctionDefinitions.size() == 1, "It is illegal to declare more then one function when using RoutingFunction");
+ }
}
@Override
@@ -139,9 +153,36 @@ public class BeanFactoryAwareFunctionRegistry
@Override
@SuppressWarnings("unchecked")
public T lookup(String definition, String... acceptedOutputTypes) {
- if (!StringUtils.hasText(definition)) {
- definition = this.applicationContext.getEnvironment().getProperty("spring.cloud.function.definition");
+ definition = StringUtils.hasText(definition) ? definition.replaceAll(",", "|") : "";
+
+ boolean routing = definition.contains(RoutingFunction.FUNCTION_NAME)
+ || this.declaredFunctionDefinitions.contains(RoutingFunction.FUNCTION_NAME);
+
+ if (!routing && this.declaredFunctionDefinitions.size() > 0) {
+ if (StringUtils.hasText(definition)) {
+ if (this.declaredFunctionDefinitions.size() > 1 &&!this.declaredFunctionDefinitions.contains(definition)) {
+ logger.warn("Attempted to access un-declared function definition '" + definition + "'. Declared functions are '" + this.declaredFunctionDefinitions
+ + "' specified via `spring.cloud.function.definition` property. If the intention is to access "
+ + "any function available in FunctionCatalog, please remove `spring.cloud.function.definition` property.");
+ return null;
+ }
+ }
+ else {
+ if (this.declaredFunctionDefinitions.size() == 1) {
+ definition = this.declaredFunctionDefinitions.get(0);
+ }
+ else if (this.declaredFunctionDefinitions.size() > 1) {
+ logger.warn("Default function can not be mapped since multiple functions are declared " + this.declaredFunctionDefinitions);
+ return null;
+ }
+ else {
+ logger.warn("Default function can not be mapped since multiple functions are available in FunctionCatalog. "
+ + "Please use 'spring.cloud.function.definition' property.");
+ return null;
+ }
+ }
}
+
Object function = this
.proxyInvokerIfNecessary((FunctionInvocationWrapper) this.compose(null, definition, acceptedOutputTypes));
return (T) function;
diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistryTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistryTests.java
index f657db5af..27ccad0a2 100644
--- a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistryTests.java
+++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/BeanFactoryAwareFunctionRegistryTests.java
@@ -28,6 +28,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
+import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import reactor.core.publisher.Flux;
@@ -77,6 +78,11 @@ public class BeanFactoryAwareFunctionRegistryTests {
return catalog;
}
+ @Before
+ public void before() {
+ System.clearProperty("spring.cloud.function.definition");
+ }
+
@Test
public void testDefaultLookup() throws Exception {
FunctionCatalog catalog = this.configureCatalog();
@@ -84,6 +90,7 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(function).isNull();
//==
System.setProperty("spring.cloud.function.definition", "uppercase");
+ catalog = this.configureCatalog();
function = catalog.lookup("");
assertThat(function).isNotNull();
Field field = ReflectionUtils.findField(FunctionInvocationWrapper.class, "composed");
@@ -91,6 +98,7 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(((boolean) field.get(function))).isFalse();
//==
System.setProperty("spring.cloud.function.definition", "uppercase|uppercaseFlux");
+ catalog = this.configureCatalog();
function = catalog.lookup("", "application/json");
Function, Flux>> typedFunction = (Function, Flux>>) function;
Object blockFirst = typedFunction.apply(Flux.just("hello")).blockFirst();
@@ -354,7 +362,7 @@ public class BeanFactoryAwareFunctionRegistryTests {
FunctionCatalog catalog = this.configureCatalog(CollectionOutConfiguration.class);
FunctionInvocationWrapper function = catalog.lookup("parseToList", "application/json");
assertThat(function).isNotNull();
- Object result = (Message) function.apply(MessageBuilder.withPayload("1, 2, 3".getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build());
+ Object result = function.apply(MessageBuilder.withPayload("1, 2, 3".getBytes()).setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build());
assertThat(result instanceof Message).isTrue();
function = catalog.lookup("parseToListOfMessages", "application/json");