Rename groovy-transformer to groovy-processor
This can be then used as a comnmon groovy-processor app that can be used in lieu of the current groovy-transform and groovy-filter processor apps.
This commit is contained in:
13
applications/processor/groovy-processor/README.adoc
Normal file
13
applications/processor/groovy-processor/README.adoc
Normal file
@@ -0,0 +1,13 @@
|
||||
//tag::ref-doc[]
|
||||
= Groovy Processor
|
||||
|
||||
A Processor that applies a Groovy script against messages.
|
||||
|
||||
== Options
|
||||
|
||||
The **$$groovy-processor** $$processor$$ has the following options:
|
||||
|
||||
//tag::configuration-properties[]
|
||||
//end::configuration-properties[]
|
||||
|
||||
//end::ref-doc[]
|
||||
130
applications/processor/groovy-processor/pom.xml
Normal file
130
applications/processor/groovy-processor/pom.xml
Normal file
@@ -0,0 +1,130 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>groovy-transform-processor</artifactId>
|
||||
<name>groovy-processor</name>
|
||||
<description>groovy processor apps</description>
|
||||
<version>3.0.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud.stream.app</groupId>
|
||||
<artifactId>stream-applications-core</artifactId>
|
||||
<version>3.0.0-SNAPSHOT</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<apache-ivy.version>2.4.0</apache-ivy.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-groovy</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-json</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-nio</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-templates</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-xml</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-dateutil</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-datetime</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-jaxb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.ivy</groupId>
|
||||
<artifactId>ivy</artifactId>
|
||||
<version>${apache-ivy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>payload-converter-function</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-app-starter-doc-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.cloud.stream.app.plugin</groupId>
|
||||
<artifactId>spring-cloud-stream-app-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<generatedApp>
|
||||
<name>groovy-transform</name>
|
||||
<type>processor</type>
|
||||
<version>${project.version}</version>
|
||||
<configClass>
|
||||
org.springframework.cloud.stream.app.groovy.processor.GroovyProcessorConfiguration.class
|
||||
</configClass>
|
||||
<functionDefinition>byteArrayTextToString|groovyProcessorFunction</functionDefinition>
|
||||
</generatedApp>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.stream.app</groupId>
|
||||
<artifactId>groovy-processor</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/libs-snapshot-local</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/libs-milestone-local</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2015-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.app.groovy.processor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import org.springframework.integration.groovy.GroovyScriptExecutingMessageProcessor;
|
||||
import org.springframework.integration.handler.MessageProcessor;
|
||||
import org.springframework.integration.scripting.DefaultScriptVariableGenerator;
|
||||
import org.springframework.integration.scripting.ScriptVariableGenerator;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.scripting.support.ResourceScriptSource;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* A Processor app that transforms messages using a Groovy script.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Mark Fisher
|
||||
* @author Marius Bogoevici
|
||||
* @author Gary Russell
|
||||
* @author Christian Tzolov
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(GroovyProcessorProperties.class)
|
||||
public class GroovyProcessorConfiguration {
|
||||
|
||||
@Autowired
|
||||
private GroovyProcessorProperties properties;
|
||||
|
||||
@Bean
|
||||
public Function<Message<?>, Object> groovyProcessorFunction(ScriptVariableGenerator scriptVariableGenerator) {
|
||||
return message -> transformer(scriptVariableGenerator).processMessage(message);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageProcessor<?> transformer(ScriptVariableGenerator scriptVariableGenerator) {
|
||||
return new GroovyScriptExecutingMessageProcessor(
|
||||
new ResourceScriptSource(properties.getScript()), scriptVariableGenerator);
|
||||
}
|
||||
|
||||
@Bean(name = "variableGenerator")
|
||||
public ScriptVariableGenerator scriptVariableGenerator() throws IOException {
|
||||
Map<String, Object> variables = new HashMap<>();
|
||||
CollectionUtils.mergePropertiesIntoMap(properties.getVariables(), variables);
|
||||
if (properties.getVariablesLocation() != null) {
|
||||
CollectionUtils.mergePropertiesIntoMap(
|
||||
PropertiesLoaderUtils.loadProperties(properties.getVariablesLocation()), variables);
|
||||
}
|
||||
return new DefaultScriptVariableGenerator(variables);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.app.groovy.processor;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
* Configuration properties for the Groovy Transform Processor module.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
@ConfigurationProperties("groovy-processor")
|
||||
@Validated
|
||||
public class GroovyProcessorProperties {
|
||||
|
||||
/**
|
||||
* Reference to a script used to process messages.
|
||||
*/
|
||||
private Resource script;
|
||||
|
||||
/**
|
||||
* Variable bindings as a new line delimited string of name-value pairs, e.g. 'foo=bar\n baz=car'.
|
||||
*/
|
||||
private Properties variables;
|
||||
|
||||
/**
|
||||
* The location of a properties file containing custom script variable bindings.
|
||||
*/
|
||||
private Resource variablesLocation;
|
||||
|
||||
@NotNull
|
||||
public Resource getScript() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public void setScript(Resource script) {
|
||||
this.script = script;
|
||||
}
|
||||
|
||||
|
||||
public Properties getVariables() {
|
||||
return variables;
|
||||
}
|
||||
|
||||
public void setVariables(Properties variables) {
|
||||
this.variables = variables;
|
||||
}
|
||||
|
||||
public Resource getVariablesLocation() {
|
||||
return variablesLocation;
|
||||
}
|
||||
|
||||
public void setVariablesLocation(Resource variablesLocation) {
|
||||
this.variablesLocation = variablesLocation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
configuration-properties.classes=org.springframework.cloud.stream.app.groovy.processor.GroovyProcessorProperties
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2015-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.app.groovy.processor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.stream.binder.test.InputDestination;
|
||||
import org.springframework.cloud.stream.binder.test.OutputDestination;
|
||||
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class GroovyProcessorIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void testGroovyProcessorWithScript() {
|
||||
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
|
||||
TestChannelBinderConfiguration.getCompleteConfiguration(GroovyProcessorTestAppConfiguration.class))
|
||||
.web(WebApplicationType.NONE)
|
||||
.run("--spring.cloud.function.definition=byteArrayTextToString|groovyProcessorFunction",
|
||||
"--groovy-processor.script=script.groovy",
|
||||
"--groovy-processor.variables=limit=5\n foo=\\\40WORLD")) {
|
||||
|
||||
InputDestination processorInput = context.getBean(InputDestination.class);
|
||||
OutputDestination processorOutput = context.getBean(OutputDestination.class);
|
||||
|
||||
String inMessage = "hello world";
|
||||
processorInput.send(new GenericMessage<>(inMessage.getBytes(StandardCharsets.UTF_8)));
|
||||
Message<byte[]> sourceMessage = processorOutput.receive(10000);
|
||||
assertThat(new String(sourceMessage.getPayload())).isEqualTo("hello WORLD");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroovyProcessorWithGrab() {
|
||||
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
|
||||
TestChannelBinderConfiguration.getCompleteConfiguration(GroovyProcessorTestAppConfiguration.class))
|
||||
.web(WebApplicationType.NONE)
|
||||
.run("--spring.cloud.function.definition=byteArrayTextToString|groovyProcessorFunction",
|
||||
"--groovy-processor.script=script-with-grab.groovy")) {
|
||||
|
||||
InputDestination processorInput = context.getBean(InputDestination.class);
|
||||
OutputDestination processorOutput = context.getBean(OutputDestination.class);
|
||||
|
||||
String inMessage = "def foo=bar";
|
||||
processorInput.send(new GenericMessage<>(inMessage.getBytes(StandardCharsets.UTF_8)));
|
||||
Message<byte[]> sourceMessage = processorOutput.receive(10000);
|
||||
assertThat(new String(sourceMessage.getPayload())).isEqualTo("var foo = bar;" + System.lineSeparator());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Import({GroovyProcessorConfiguration.class})
|
||||
public static class GroovyProcessorTestAppConfiguration {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
@Grab('org.grooscript:grooscript:1.3.0')
|
||||
import org.grooscript.GrooScript
|
||||
@Grab('org.grooscript:grooscript:1.3.0')
|
||||
import org.grooscript.GrooScript
|
||||
|
||||
GrooScript.convert(new String(payload))
|
||||
@@ -0,0 +1 @@
|
||||
payload.substring(0, limit as int) + foo
|
||||
Reference in New Issue
Block a user