Make header-enricher &header-filter functions as auto-config

* Fix Checkstyle violations in those module
This commit is contained in:
Artem Bilan
2024-01-10 10:09:31 -05:00
parent 97c3be83af
commit e1874eee92
13 changed files with 32 additions and 40 deletions

View File

@@ -4,7 +4,7 @@ This module provides a header enricher function that can be reused and composed
## Beans for injection
You can import the `HeaderEnricherFunctionConfiguration` in a Spring Boot application and then inject the following bean.
The `HeaderEnricherFunctionConfiguration` auto-configuration provides the following bean:
`headerEnricherFunction`

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 the original author or authors.
* Copyright 2020-2024 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.
@@ -23,7 +23,6 @@ import java.util.Properties;
import java.util.function.Function;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -33,6 +32,8 @@ import org.springframework.integration.transformer.support.ExpressionEvaluatingH
import org.springframework.messaging.Message;
/**
* Auto-configuration for Header Enricher function.
*
* @author Gary Russell
* @author Christian Tzolov
* @author Soby Chacko
@@ -43,18 +44,15 @@ import org.springframework.messaging.Message;
@ConditionalOnProperty(prefix = "header.enricher", value = "headers")
public class HeaderEnricherFunctionConfiguration {
@Autowired
private HeaderEnricherFunctionProperties properties;
@Bean
public Function<Message<?>, Message<?>> headerEnricherFunction(HeaderEnricher headerEnricher) {
return headerEnricher::transform;
}
@Bean
public HeaderEnricher headerEnricher(BeanFactory beanFactory) {
public HeaderEnricher headerEnricher(HeaderEnricherFunctionProperties properties, BeanFactory beanFactory) {
Map<String, ExpressionEvaluatingHeaderValueMessageProcessor<?>> headersToAdd = new HashMap<>();
Properties props = this.properties.getHeaders();
Properties props = properties.getHeaders();
Enumeration<?> enumeration = props.propertyNames();
while (enumeration.hasMoreElements()) {
String propertyName = (String) enumeration.nextElement();
@@ -64,7 +62,7 @@ public class HeaderEnricherFunctionConfiguration {
headersToAdd.put(propertyName, headerValueMessageProcessor);
}
HeaderEnricher headerEnricher = new HeaderEnricher(headersToAdd);
headerEnricher.setDefaultOverwrite(this.properties.isOverwrite());
headerEnricher.setDefaultOverwrite(properties.isOverwrite());
return headerEnricher;
}

View File

@@ -0,0 +1,4 @@
/**
* The Header Enricher function support classes.
*/
package org.springframework.cloud.fn.header.enricher;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
@@ -59,7 +59,7 @@ public class HeaderEnricherFunctionApplicationTests {
static class HeaderEnricherFunctionTestApplication {
@Bean
public String value() {
String value() {
return "beanValue";
}

View File

@@ -4,7 +4,7 @@ This module provides a header enricher function that can be reused and composed
== Beans for injection
You can import the `HeaderEnricherFunctionConfiguration` in a Spring Boot application and then inject the following bean.
The `HeaderEnricherFunctionConfiguration` auto-configuration provides the following bean:
`headerFilterFunction`

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023-2023 the original author or authors.
* Copyright 2023-2024 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.
@@ -29,7 +29,7 @@ import org.springframework.messaging.Message;
import org.springframework.util.StringUtils;
/**
* Configure a function using {@link HeaderFilter}.
* Auto-configure a function using {@link HeaderFilter}.
*
* @author Corneil du Plessis
*/
@@ -38,14 +38,9 @@ import org.springframework.util.StringUtils;
@ConditionalOnExpression("'${header.filter.remove}'!='' or '${header.filter.delete-all}' != ''")
public class HeaderFilterFunctionConfiguration {
private final HeaderFilterFunctionProperties properties;
public HeaderFilterFunctionConfiguration(HeaderFilterFunctionProperties properties) {
this.properties = properties;
}
@Bean
public Function<Message<?>, Message<?>> headerFilterFunction() {
public Function<Message<?>, Message<?>> headerFilterFunction(HeaderFilterFunctionProperties properties,
HeaderFilter headerFilter) {
if (properties.isDeleteAll()) {
return (message) -> {
var accessor = new IntegrationMessageHeaderAccessor(message);
@@ -56,12 +51,12 @@ public class HeaderFilterFunctionConfiguration {
};
}
else {
return headerFilter()::transform;
return headerFilter::transform;
}
}
@Bean
public HeaderFilter headerFilter() {
public HeaderFilter headerFilter(HeaderFilterFunctionProperties properties) {
if (properties.getRemove() != null) {
String[] remove = StringUtils.tokenizeToStringArray(properties.getRemove(), ", ", true, true);
HeaderFilter filter = new HeaderFilter(remove);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023-2023 the original author or authors.
* Copyright 2023-2024 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.
@@ -38,7 +38,7 @@ public class HeaderFilterFunctionProperties {
private String remove;
public boolean isDeleteAll() {
return deleteAll;
return this.deleteAll;
}
public void setDeleteAll(boolean deleteAll) {
@@ -46,7 +46,7 @@ public class HeaderFilterFunctionProperties {
}
public String getRemove() {
return remove;
return this.remove;
}
public void setRemove(String remove) {

View File

@@ -0,0 +1,4 @@
/**
* The Header Filter function support classes.
*/
package org.springframework.cloud.fn.header.filter;

View File

@@ -0,0 +1 @@
org.springframework.cloud.fn.header.filter.HeaderFilterFunctionConfiguration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023-2023 the original author or authors.
* Copyright 2023-2024 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.
@@ -22,7 +22,6 @@ import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.messaging.Message;
@@ -52,10 +51,6 @@ public class HeaderFilterFunctionApplicationDeleteAllTests {
@SpringBootApplication
static class HeaderFilterFunctionTestApplication {
public static void main(String[] args) throws Exception {
SpringApplication.main(args);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023-2023 the original author or authors.
* Copyright 2023-2024 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.
@@ -91,10 +91,6 @@ public class HeaderFilterFunctionApplicationTests {
@SpringBootApplication
static class HeaderFilterFunctionTestApplication {
public static void main(String[] main) {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023-2023 the original author or authors.
* Copyright 2023-2024 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.
@@ -24,7 +24,7 @@ import org.jetbrains.annotations.NotNull;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.messaging.Message;
final public class HeaderUtils {
public final class HeaderUtils {
private HeaderUtils() {
}

View File

@@ -1 +0,0 @@
logging.level.root=debug