1
1
/*
2
- * Copyright 2002-2019 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.
19
19
import java .io .File ;
20
20
21
21
import org .springframework .expression .Expression ;
22
+ import org .springframework .integration .expression .FunctionExpression ;
22
23
import org .springframework .integration .util .AbstractExpressionEvaluator ;
23
24
import org .springframework .messaging .Message ;
24
25
import org .springframework .util .Assert ;
43
44
public class DefaultFileNameGenerator extends AbstractExpressionEvaluator implements FileNameGenerator {
44
45
45
46
private volatile Expression expression =
46
- EXPRESSION_PARSER . parseExpression ( "headers['" + FileHeaders .FILENAME + "']" );
47
+ new FunctionExpression < Message <?>>(( message ) -> message . getHeaders (). get ( FileHeaders .FILENAME ) );
47
48
48
49
/**
49
50
* Specify an expression to be evaluated against the Message
@@ -62,17 +63,18 @@ public void setExpression(String expression) {
62
63
*/
63
64
public void setHeaderName (String headerName ) {
64
65
Assert .notNull (headerName , "'headerName' must not be null" );
65
- this .expression = EXPRESSION_PARSER . parseExpression ( "headers['" + headerName + "']" );
66
+ this .expression = new FunctionExpression < Message <?>>(( message ) -> message . getHeaders (). get ( headerName ) );
66
67
}
67
68
68
69
@ Override
69
70
public String generateFileName (Message <?> message ) {
70
- Object filename = this . evaluateExpression (this .expression , message );
71
- if (filename instanceof String && StringUtils .hasText (( String ) filename )) {
72
- return ( String ) filename ;
71
+ Object filename = evaluateExpression (this .expression , message );
72
+ if (filename instanceof String name && StringUtils .hasText (name )) {
73
+ return name ;
73
74
}
74
- if (message .getPayload () instanceof File ) {
75
- return ((File ) message .getPayload ()).getName ();
75
+ Object payload = message .getPayload ();
76
+ if (payload instanceof File file ) {
77
+ return file .getName ();
76
78
}
77
79
return message .getHeaders ().getId () + ".msg" ;
78
80
}
0 commit comments