Skip to content

Commit

Permalink
Make header-enricher &header-filter functions as auto-config
Browse files Browse the repository at this point in the history
* Fix Checkstyle violations in those module
  • Loading branch information
artembilan committed Jan 10, 2024
1 parent 97c3be8 commit e1874ee
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion function/spring-header-enricher-function/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,7 +23,6 @@
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;
Expand All @@ -33,6 +32,8 @@
import org.springframework.messaging.Message;

/**
* Auto-configuration for Header Enricher function.
*
* @author Gary Russell
* @author Christian Tzolov
* @author Soby Chacko
Expand All @@ -43,18 +44,15 @@
@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();
Expand All @@ -64,7 +62,7 @@ public HeaderEnricher headerEnricher(BeanFactory beanFactory) {
headersToAdd.put(propertyName, headerValueMessageProcessor);
}
HeaderEnricher headerEnricher = new HeaderEnricher(headersToAdd);
headerEnricher.setDefaultOverwrite(this.properties.isOverwrite());
headerEnricher.setDefaultOverwrite(properties.isOverwrite());
return headerEnricher;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The Header Enricher function support classes.
*/
package org.springframework.cloud.fn.header.enricher;
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -59,7 +59,7 @@ public void testDefault() {
static class HeaderEnricherFunctionTestApplication {

@Bean
public String value() {
String value() {
return "beanValue";
}

Expand Down
2 changes: 1 addition & 1 deletion function/spring-header-filter-function/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,7 @@
import org.springframework.util.StringUtils;

/**
* Configure a function using {@link HeaderFilter}.
* Auto-configure a function using {@link HeaderFilter}.
*
* @author Corneil du Plessis
*/
Expand All @@ -38,14 +38,9 @@
@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);
Expand All @@ -56,12 +51,12 @@ public Function<Message<?>, Message<?>> headerFilterFunction() {
};
}
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,15 +38,15 @@ public class HeaderFilterFunctionProperties {
private String remove;

public boolean isDeleteAll() {
return deleteAll;
return this.deleteAll;
}

public void setDeleteAll(boolean deleteAll) {
this.deleteAll = deleteAll;
}

public String getRemove() {
return remove;
return this.remove;
}

public void setRemove(String remove) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The Header Filter function support classes.
*/
package org.springframework.cloud.fn.header.filter;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.cloud.fn.header.filter.HeaderFilterFunctionConfiguration
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -22,7 +22,6 @@
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;
Expand Down Expand Up @@ -52,10 +51,6 @@ public void testRemoveLeavesIdTimestampAll() {
@SpringBootApplication
static class HeaderFilterFunctionTestApplication {

public static void main(String[] args) throws Exception {
SpringApplication.main(args);
}

}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -91,10 +91,6 @@ public void testRemoveLeavesIdTimestampAll() {
@SpringBootApplication
static class HeaderFilterFunctionTestApplication {

public static void main(String[] main) {

}

}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,7 +24,7 @@
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.messaging.Message;

final public class HeaderUtils {
public final class HeaderUtils {

private HeaderUtils() {
}
Expand Down

This file was deleted.

0 comments on commit e1874ee

Please sign in to comment.