Skip to content

Commit 4b83241

Browse files
committed
Fix new Sonar smells
1 parent 920b8ae commit 4b83241

File tree

4 files changed

+89
-54
lines changed

4 files changed

+89
-54
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/MessagingGatewayRegistrar.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import org.springframework.beans.factory.BeanDefinitionStoreException;
3030
import org.springframework.beans.factory.FactoryBean;
31-
import org.springframework.beans.factory.config.BeanDefinition;
3231
import org.springframework.beans.factory.config.BeanDefinitionHolder;
3332
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3433
import org.springframework.beans.factory.config.EmbeddedValueResolver;
@@ -57,7 +56,7 @@
5756

5857
/**
5958
* The {@link ImportBeanDefinitionRegistrar} to parse {@link MessagingGateway} and its {@code service-interface}
60-
* and to register {@link BeanDefinition} {@link GatewayProxyFactoryBean}.
59+
* and to register bean definition for {@link GatewayProxyFactoryBean}.
6160
*
6261
* @author Artem Bilan
6362
* @author Gary Russell
@@ -88,7 +87,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
8887
}
8988
}
9089

91-
public BeanDefinitionHolder gatewayProxyBeanDefinition(Map<String, Object> gatewayAttributes,
90+
public BeanDefinitionHolder gatewayProxyBeanDefinition(Map<String, Object> gatewayAttributes, // NOSONAR - complexity
9291
BeanDefinitionRegistry registry) {
9392

9493
String defaultPayloadExpression = (String) gatewayAttributes.get("defaultPayloadExpression");
@@ -116,7 +115,6 @@ public BeanDefinitionHolder gatewayProxyBeanDefinition(Map<String, Object> gatew
116115
EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver(beanFactory);
117116
Class<?> serviceInterface = getServiceInterface((String) gatewayAttributes.get("serviceInterface"), beanFactory);
118117

119-
@SuppressWarnings("unchecked")
120118
AbstractBeanDefinition beanDefinition = new RootBeanDefinition(GatewayProxyFactoryBean.class,
121119
() -> {
122120
GatewayProxyFactoryBean proxyFactoryBean = new GatewayProxyFactoryBean(serviceInterface);
@@ -152,6 +150,7 @@ else if (StringUtils.hasText(asyncExecutor)) {
152150
proxyFactoryBean.setGlobalMethodMetadata(globalMethodMetadata);
153151
}
154152

153+
@SuppressWarnings("unchecked")
155154
Map<String, AbstractBeanDefinition> methodDefinitions =
156155
(Map<String, AbstractBeanDefinition>) gatewayAttributes.get("methods");
157156

@@ -160,7 +159,8 @@ else if (StringUtils.hasText(asyncExecutor)) {
160159
methodDefinitions.entrySet()
161160
.stream()
162161
.collect(Collectors.toMap(Entry::getKey,
163-
entry -> (GatewayMethodMetadata) entry.getValue().getInstanceSupplier().get()));
162+
entry -> (GatewayMethodMetadata) entry.getValue()
163+
.getInstanceSupplier().get()));
164164

165165
proxyFactoryBean.setMethodMetadataMap(methodMetadataMap);
166166
}
@@ -223,7 +223,9 @@ private static GatewayMethodMetadata createGlobalMethodMetadata(String defaultPa
223223

224224
if (hasDefaultPayloadExpression) {
225225
String actualPayloadExpression = embeddedValueResolver.resolveStringValue(defaultPayloadExpression);
226-
gatewayMethodMetadata.setPayloadExpression(EXPRESSION_PARSER.parseExpression(actualPayloadExpression));
226+
if (actualPayloadExpression != null) {
227+
gatewayMethodMetadata.setPayloadExpression(EXPRESSION_PARSER.parseExpression(actualPayloadExpression));
228+
}
227229
}
228230

229231
if (hasDefaultHeaders) {
@@ -238,19 +240,27 @@ private static GatewayMethodMetadata createGlobalMethodMetadata(String defaultPa
238240
"is required on a gateway's header.");
239241
}
240242

241-
Expression expression =
242-
hasValue
243-
? new LiteralExpression(embeddedValueResolver.resolveStringValue(headerValue))
244-
: EXPRESSION_PARSER.parseExpression(
245-
embeddedValueResolver.resolveStringValue(headerExpression));
246-
243+
Expression expression = buildHeaderExpression(embeddedValueResolver, headerValue, headerExpression);
247244
headerExpressions.put((String) header.get("name"), expression);
248245
}
249246
gatewayMethodMetadata.setHeaderExpressions(headerExpressions);
250247
}
251248
return gatewayMethodMetadata;
252249
}
253250

251+
private static Expression buildHeaderExpression(EmbeddedValueResolver embeddedValueResolver, String headerValue,
252+
String headerExpression) {
253+
254+
if (StringUtils.hasText(headerValue)) {
255+
String resolvedValue = embeddedValueResolver.resolveStringValue(headerValue);
256+
return resolvedValue != null ? new LiteralExpression(resolvedValue) : null;
257+
}
258+
else {
259+
String resolvedValue = embeddedValueResolver.resolveStringValue(headerExpression);
260+
return resolvedValue != null ? EXPRESSION_PARSER.parseExpression(resolvedValue) : null;
261+
}
262+
}
263+
254264
/**
255265
* TODO until SPR-11710 will be resolved.
256266
* Captures the meta-annotation attribute values, in order.

spring-integration-core/src/main/java/org/springframework/integration/config/xml/GatewayParser.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ private GatewayMethodMetadata createGatewayMethodMetadata(Element element,
176176
"'payload-expression' is not allowed when a 'mapper' is provided");
177177

178178
if (StringUtils.hasText(payloadExpression)) {
179-
gatewayMethodMetadata.setPayloadExpression(
180-
EXPRESSION_PARSER.parseExpression(embeddedValueResolver.resolveStringValue(payloadExpression)));
179+
String expressionString = embeddedValueResolver.resolveStringValue(payloadExpression);
180+
if (expressionString != null) {
181+
gatewayMethodMetadata.setPayloadExpression(EXPRESSION_PARSER.parseExpression(expressionString));
182+
}
181183
}
182184

183185
List<Element> invocationHeaders = DomUtils.getChildElementsByTagName(methodElement, "header");
@@ -188,11 +190,7 @@ private GatewayMethodMetadata createGatewayMethodMetadata(Element element,
188190
for (Element headerElement : invocationHeaders) {
189191
String headerValue = headerElement.getAttribute("value");
190192
String headerExpression = headerElement.getAttribute("expression");
191-
Expression expression =
192-
StringUtils.hasText(headerValue)
193-
? new LiteralExpression(embeddedValueResolver.resolveStringValue(headerValue))
194-
: EXPRESSION_PARSER.parseExpression(
195-
embeddedValueResolver.resolveStringValue(headerExpression));
193+
Expression expression = buildHeaderExpression(embeddedValueResolver, headerValue, headerExpression);
196194

197195
headerExpressions.put(headerElement.getAttribute(AbstractBeanDefinitionParser.NAME_ATTRIBUTE),
198196
expression);
@@ -202,5 +200,18 @@ private GatewayMethodMetadata createGatewayMethodMetadata(Element element,
202200
return gatewayMethodMetadata;
203201
}
204202

203+
private static Expression buildHeaderExpression(EmbeddedValueResolver embeddedValueResolver, String headerValue,
204+
String headerExpression) {
205+
206+
if (StringUtils.hasText(headerValue)) {
207+
String resolvedValue = embeddedValueResolver.resolveStringValue(headerValue);
208+
return resolvedValue != null ? new LiteralExpression(resolvedValue) : null;
209+
}
210+
else {
211+
String resolvedValue = embeddedValueResolver.resolveStringValue(headerExpression);
212+
return resolvedValue != null ? EXPRESSION_PARSER.parseExpression(resolvedValue) : null;
213+
}
214+
}
215+
205216

206217
}

spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayMethodMetadata.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,10 +54,11 @@ public Expression getPayloadExpression() {
5454
return this.payloadExpression;
5555
}
5656

57-
public void setPayloadExpression(Expression payloadExpression) {
57+
public void setPayloadExpression(@Nullable Expression payloadExpression) {
5858
this.payloadExpression = payloadExpression;
5959
}
6060

61+
@Nullable
6162
public Map<String, Expression> getHeaderExpressions() {
6263
return this.headerExpressions;
6364
}
@@ -85,19 +86,21 @@ public void setReplyChannelName(String replyChannelName) {
8586
this.replyChannelName = replyChannelName;
8687
}
8788

89+
@Nullable
8890
public String getRequestTimeout() {
8991
return this.requestTimeout;
9092
}
9193

92-
public void setRequestTimeout(String requestTimeout) {
94+
public void setRequestTimeout(@Nullable String requestTimeout) {
9395
this.requestTimeout = requestTimeout;
9496
}
9597

98+
@Nullable
9699
public String getReplyTimeout() {
97100
return this.replyTimeout;
98101
}
99102

100-
public void setReplyTimeout(String replyTimeout) {
103+
public void setReplyTimeout(@Nullable String replyTimeout) {
101104
this.replyTimeout = replyTimeout;
102105
}
103106

spring-integration-http/src/main/java/org/springframework/integration/http/config/IntegrationGraphControllerRegistrar.java

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,51 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
8080
String path = (String) annotationAttributes.get("value");
8181
String[] allowedOrigins = (String[]) annotationAttributes.get("allowedOrigins");
8282
if (allowedOrigins != null && allowedOrigins.length > 0) {
83-
AbstractBeanDefinition controllerCorsConfigurer = null;
84-
if (HttpContextUtils.WEB_MVC_PRESENT) {
85-
controllerCorsConfigurer = webMvcControllerCorsConfigurerBean(path, allowedOrigins);
86-
}
87-
else if (HttpContextUtils.WEB_FLUX_PRESENT) {
88-
controllerCorsConfigurer = webFluxControllerCorsConfigurerBean(path, allowedOrigins);
89-
}
90-
91-
if (controllerCorsConfigurer != null) {
92-
BeanDefinitionReaderUtils.registerWithGeneratedName(controllerCorsConfigurer, registry);
93-
}
94-
else {
95-
LOGGER.warn("Nor Spring MVC, neither WebFlux is present to configure CORS origins " +
96-
"for Integration Graph Controller.");
97-
}
83+
registerControlerCorsConfigurer(registry, path, allowedOrigins);
9884
}
9985

10086
if (!registry.containsBeanDefinition(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME)) {
101-
Map<String, Object> properties = annotationAttributes;
102-
AbstractBeanDefinition controllerPropertiesPopulator =
103-
BeanDefinitionBuilder.genericBeanDefinition(GraphControllerPropertiesPopulator.class,
104-
() -> new GraphControllerPropertiesPopulator(properties))
105-
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
106-
.getBeanDefinition();
107-
BeanDefinitionReaderUtils.registerWithGeneratedName(controllerPropertiesPopulator, registry);
108-
109-
BeanDefinition graphController =
110-
new RootBeanDefinition(IntegrationGraphController.class, () ->
111-
new IntegrationGraphController(
112-
((BeanFactory) registry)
113-
.getBean(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME,
114-
IntegrationGraphServer.class)));
115-
116-
registry.registerBeanDefinition(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME, graphController);
87+
registerIntegrationGraphController(registry, annotationAttributes);
88+
}
89+
}
90+
91+
private static void registerIntegrationGraphController(BeanDefinitionRegistry registry,
92+
Map<String, Object> properties) {
93+
94+
AbstractBeanDefinition controllerPropertiesPopulator =
95+
BeanDefinitionBuilder.genericBeanDefinition(GraphControllerPropertiesPopulator.class,
96+
() -> new GraphControllerPropertiesPopulator(properties))
97+
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
98+
.getBeanDefinition();
99+
BeanDefinitionReaderUtils.registerWithGeneratedName(controllerPropertiesPopulator, registry);
100+
101+
BeanDefinition graphController =
102+
new RootBeanDefinition(IntegrationGraphController.class, () ->
103+
new IntegrationGraphController(
104+
((BeanFactory) registry)
105+
.getBean(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME,
106+
IntegrationGraphServer.class)));
107+
108+
registry.registerBeanDefinition(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME, graphController);
109+
}
110+
111+
private static void registerControlerCorsConfigurer(BeanDefinitionRegistry registry, String path,
112+
String[] allowedOrigins) {
113+
114+
AbstractBeanDefinition controllerCorsConfigurer = null;
115+
if (HttpContextUtils.WEB_MVC_PRESENT) {
116+
controllerCorsConfigurer = webMvcControllerCorsConfigurerBean(path, allowedOrigins);
117+
}
118+
else if (HttpContextUtils.WEB_FLUX_PRESENT) {
119+
controllerCorsConfigurer = webFluxControllerCorsConfigurerBean(path, allowedOrigins);
120+
}
121+
122+
if (controllerCorsConfigurer != null) {
123+
BeanDefinitionReaderUtils.registerWithGeneratedName(controllerCorsConfigurer, registry);
124+
}
125+
else {
126+
LOGGER.warn("Nor Spring MVC, neither WebFlux is present to configure CORS origins " +
127+
"for Integration Graph Controller.");
117128
}
118129
}
119130

0 commit comments

Comments
 (0)