1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
38
38
import org .springframework .expression .EvaluationContext ;
39
39
import org .springframework .expression .Expression ;
40
40
import org .springframework .expression .spel .standard .SpelExpressionParser ;
41
- import org .springframework .expression .spel .support .StandardEvaluationContext ;
42
41
import org .springframework .integration .expression .ExpressionUtils ;
43
42
import org .springframework .integration .mapping .InboundMessageMapper ;
44
43
import org .springframework .integration .mapping .MessageMappingException ;
@@ -107,9 +106,7 @@ class GatewayMethodInboundMessageMapper implements InboundMessageMapper<Object[]
107
106
108
107
private Expression payloadExpression ;
109
108
110
- private EvaluationContext payloadExpressionEvaluationContext ;
111
-
112
- private BeanFactory beanFactory ;
109
+ private EvaluationContext evaluationContext ;
113
110
114
111
@ Nullable
115
112
private Expression sendTimeoutExpression ;
@@ -148,18 +145,14 @@ class GatewayMethodInboundMessageMapper implements InboundMessageMapper<Object[]
148
145
this .globalHeaderExpressions = globalHeaderExpressions ;
149
146
this .parameterList = getMethodParameterList (method );
150
147
this .payloadExpression = parsePayloadExpression (method );
151
- if (messageBuilderFactory == null ) {
152
- this .messageBuilderFactory = new DefaultMessageBuilderFactory ();
153
- }
154
- else {
155
- this .messageBuilderFactory = messageBuilderFactory ;
156
- }
157
- if (mapper == null ) {
158
- this .argsMapper = new DefaultMethodArgsMessageMapper ();
159
- }
160
- else {
161
- this .argsMapper = mapper ;
162
- }
148
+ this .messageBuilderFactory =
149
+ messageBuilderFactory == null
150
+ ? new DefaultMessageBuilderFactory ()
151
+ : messageBuilderFactory ;
152
+ this .argsMapper =
153
+ mapper == null
154
+ ? new DefaultMethodArgsMessageMapper ()
155
+ : mapper ;
163
156
}
164
157
165
158
@@ -169,8 +162,7 @@ public void setPayloadExpression(Expression expressionString) {
169
162
170
163
@ Override
171
164
public void setBeanFactory (BeanFactory beanFactory ) {
172
- this .beanFactory = beanFactory ;
173
- this .payloadExpressionEvaluationContext = ExpressionUtils .createStandardEvaluationContext (beanFactory );
165
+ this .evaluationContext = ExpressionUtils .createStandardEvaluationContext (beanFactory );
174
166
}
175
167
176
168
public void setSendTimeoutExpression (Expression sendTimeoutExpression ) {
@@ -197,38 +189,31 @@ private Message<?> mapArgumentsToMessage(Object[] arguments, @Nullable Map<Strin
197
189
try {
198
190
return this .argsMapper .toMessage (new MethodArgsHolder (this .method , arguments ), headers );
199
191
}
200
- catch (MessagingException e ) { // NOSONAR to avoid if..else
201
- throw e ;
192
+ catch (MessagingException ex ) { // NOSONAR to avoid if..else
193
+ throw ex ;
202
194
}
203
- catch (Exception e ) {
204
- throw new MessageMappingException ("Failed to map arguments: " + Arrays .toString (arguments ), e );
195
+ catch (Exception ex ) {
196
+ throw new MessageMappingException ("Failed to map arguments: " + Arrays .toString (arguments ), ex );
205
197
}
206
198
}
207
199
208
- private Map <String , Object > evaluateHeaders (EvaluationContext methodInvocationEvaluationContext ,
209
- MethodArgsHolder methodArgsHolder , Map <String , Expression > headerExpressions ) {
200
+ private Map <String , Object > evaluateHeaders (MethodArgsHolder methodArgsHolder ,
201
+ Map <String , Expression > headerExpressions ) {
210
202
211
203
Map <String , Object > evaluatedHeaders = new HashMap <>();
212
204
for (Map .Entry <String , Expression > entry : headerExpressions .entrySet ()) {
213
- Object value = entry .getValue ().getValue (methodInvocationEvaluationContext , methodArgsHolder );
205
+ Object value = entry .getValue ()
206
+ .getValue (GatewayMethodInboundMessageMapper .this .evaluationContext , methodArgsHolder );
214
207
evaluatedHeaders .put (entry .getKey (), value );
215
208
}
216
209
return evaluatedHeaders ;
217
210
}
218
211
219
- // TODO Remove in the future release. The MethodArgsHolder as a root object covers this use-case.
220
- private StandardEvaluationContext createMethodInvocationEvaluationContext (Object [] arguments ) {
221
- StandardEvaluationContext context = ExpressionUtils .createStandardEvaluationContext (this .beanFactory );
222
- context .setVariable ("args" , arguments );
223
- context .setVariable ("gatewayMethod" , this .method );
224
- return context ;
225
- }
226
-
227
212
@ Nullable
228
213
private Object evaluatePayloadExpression (String expressionString , Object argumentValue ) {
229
214
Expression expression =
230
215
this .parameterPayloadExpressions .computeIfAbsent (expressionString , PARSER ::parseExpression );
231
- return expression .getValue (this .payloadExpressionEvaluationContext , argumentValue );
216
+ return expression .getValue (this .evaluationContext , argumentValue );
232
217
}
233
218
234
219
@@ -287,23 +272,19 @@ private static Expression parsePayloadExpression(Method method) {
287
272
288
273
public class DefaultMethodArgsMessageMapper implements MethodArgsMessageMapper {
289
274
290
- private final MessageBuilderFactory msgBuilderFactory =
291
- GatewayMethodInboundMessageMapper .this .messageBuilderFactory ;
292
-
293
275
@ Override
294
276
public Message <?> toMessage (MethodArgsHolder holder , @ Nullable Map <String , Object > headersToMap ) {
295
277
Object messageOrPayload = null ;
296
278
boolean foundPayloadAnnotation = false ;
297
279
Object [] arguments = holder .getArgs ();
298
- EvaluationContext methodInvocationEvaluationContext = createMethodInvocationEvaluationContext (arguments );
299
280
Map <String , Object > headersToPopulate =
300
281
headersToMap != null
301
282
? new HashMap <>(headersToMap )
302
283
: new HashMap <>();
303
284
if (GatewayMethodInboundMessageMapper .this .payloadExpression != null ) {
304
285
messageOrPayload =
305
286
GatewayMethodInboundMessageMapper .this .payloadExpression .getValue (
306
- methodInvocationEvaluationContext , holder );
287
+ GatewayMethodInboundMessageMapper . this . evaluationContext , holder );
307
288
}
308
289
for (int i = 0 ; i < GatewayMethodInboundMessageMapper .this .parameterList .size (); i ++) {
309
290
Object argumentValue = arguments [i ];
@@ -337,8 +318,8 @@ else if (GatewayMethodInboundMessageMapper.this.payloadExpression == null) {
337
318
() -> "The 'payload' (or 'Message') for gateway [" + GatewayMethodInboundMessageMapper .this .method +
338
319
"] method call cannot be determined (must not be 'null') from the provided arguments: " +
339
320
Arrays .toString (arguments ));
340
- populateSendAndReplyTimeoutHeaders (methodInvocationEvaluationContext , holder , headersToPopulate );
341
- return buildMessage (holder , headersToPopulate , messageOrPayload , methodInvocationEvaluationContext );
321
+ populateSendAndReplyTimeoutHeaders (holder , headersToPopulate );
322
+ return buildMessage (holder , headersToPopulate , messageOrPayload );
342
323
}
343
324
344
325
private void headerOrHeaders (Map <String , Object > headersToPopulate , Object argumentValue ,
@@ -406,45 +387,46 @@ private void processMapArgument(Object messageOrPayload, boolean foundPayloadAnn
406
387
copyHeaders (argumentValue , headersToPopulate );
407
388
}
408
389
409
- private void populateSendAndReplyTimeoutHeaders (EvaluationContext methodInvocationEvaluationContext ,
410
- MethodArgsHolder methodArgsHolder , Map <String , Object > headersToPopulate ) {
390
+ private void populateSendAndReplyTimeoutHeaders (MethodArgsHolder methodArgsHolder ,
391
+ Map <String , Object > headersToPopulate ) {
411
392
412
393
if (GatewayMethodInboundMessageMapper .this .sendTimeoutExpression != null ) {
413
394
headersToPopulate .computeIfAbsent (GenericMessagingTemplate .DEFAULT_SEND_TIMEOUT_HEADER ,
414
395
v -> GatewayMethodInboundMessageMapper .this .sendTimeoutExpression
415
- .getValue (methodInvocationEvaluationContext , methodArgsHolder , Long .class ));
396
+ .getValue (GatewayMethodInboundMessageMapper .this .evaluationContext ,
397
+ methodArgsHolder , Long .class ));
416
398
}
417
399
if (GatewayMethodInboundMessageMapper .this .replyTimeoutExpression != null ) {
418
400
headersToPopulate .computeIfAbsent (GenericMessagingTemplate .DEFAULT_RECEIVE_TIMEOUT_HEADER ,
419
401
v -> GatewayMethodInboundMessageMapper .this .replyTimeoutExpression
420
- .getValue (methodInvocationEvaluationContext , methodArgsHolder , Long .class ));
402
+ .getValue (GatewayMethodInboundMessageMapper .this .evaluationContext ,
403
+ methodArgsHolder , Long .class ));
421
404
}
422
405
}
423
406
424
407
private Message <?> buildMessage (MethodArgsHolder methodArgsHolder , Map <String , Object > headers ,
425
- Object messageOrPayload , EvaluationContext methodInvocationEvaluationContext ) {
408
+ Object messageOrPayload ) {
426
409
410
+ MessageBuilderFactory msgBuilderFactory = GatewayMethodInboundMessageMapper .this .messageBuilderFactory ;
427
411
AbstractIntegrationMessageBuilder <?> builder =
428
- (messageOrPayload instanceof Message )
429
- ? this . msgBuilderFactory .fromMessage (( Message <?>) messageOrPayload )
430
- : this . msgBuilderFactory .withPayload (messageOrPayload );
412
+ (messageOrPayload instanceof Message <?> msg )
413
+ ? msgBuilderFactory .fromMessage (msg )
414
+ : msgBuilderFactory .withPayload (messageOrPayload );
431
415
builder .copyHeadersIfAbsent (headers );
432
416
// Explicit headers in XML override any @Header annotations...
433
417
if (!CollectionUtils .isEmpty (GatewayMethodInboundMessageMapper .this .headerExpressions )) {
434
- Map <String , Object > evaluatedHeaders = evaluateHeaders ( methodInvocationEvaluationContext ,
435
- methodArgsHolder , GatewayMethodInboundMessageMapper .this .headerExpressions );
418
+ Map <String , Object > evaluatedHeaders =
419
+ evaluateHeaders ( methodArgsHolder , GatewayMethodInboundMessageMapper .this .headerExpressions );
436
420
builder .copyHeaders (evaluatedHeaders );
437
421
}
438
422
// ...whereas global (default) headers do not...
439
423
if (!CollectionUtils .isEmpty (GatewayMethodInboundMessageMapper .this .globalHeaderExpressions )) {
440
- Map <String , Object > evaluatedHeaders = evaluateHeaders (methodInvocationEvaluationContext ,
441
- methodArgsHolder , GatewayMethodInboundMessageMapper .this .globalHeaderExpressions );
424
+ Map <String , Object > evaluatedHeaders =
425
+ evaluateHeaders (methodArgsHolder ,
426
+ GatewayMethodInboundMessageMapper .this .globalHeaderExpressions );
442
427
builder .copyHeadersIfAbsent (evaluatedHeaders );
443
428
}
444
- if (GatewayMethodInboundMessageMapper .this .headers != null ) {
445
- builder .copyHeadersIfAbsent (GatewayMethodInboundMessageMapper .this .headers );
446
- }
447
- return builder .build ();
429
+ return builder .copyHeadersIfAbsent (GatewayMethodInboundMessageMapper .this .headers ).build ();
448
430
}
449
431
450
432
}
0 commit comments