Skip to content

Commit 5608eb3

Browse files
predic8rrayst
andauthored
fix: body access for SpEL e.g. ${body} (membrane#1698)
* fix: body access for SpEL e.g. ${body} * refactor: minor * return error in case body could not be decoded --------- Co-authored-by: Tobias Polley <mail@tobias-polley.de>
1 parent b7d1c01 commit 5608eb3

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

core/src/main/java/com/predic8/membrane/core/lang/spel/SpELExchangeEvaluationContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class SpELExchangeEvaluationContext extends StandardEvaluationContext {
3939

4040
private final Exchange exchange;
4141
private final Message message;
42+
private final SpELBody body; // Is used by SpEL in scripts
4243

4344
// Avoid the common plural error
4445
private final SpELLablePropertyAware headers;
@@ -55,7 +56,7 @@ public class SpELExchangeEvaluationContext extends StandardEvaluationContext {
5556

5657
private int statusCode;
5758

58-
private String scopes;
59+
private String scopes; // Is used by SpEL in scripts
5960

6061
private SpELMessageWrapper request;
6162
private SpELMessageWrapper response;
@@ -67,6 +68,7 @@ public SpELExchangeEvaluationContext(Exchange exchange, Flow flow) {
6768
this.exchange = exchange;
6869
this.flow = flow;
6970
this.message = exchange.getMessage(flow);
71+
this.body = new SpELBody(message);
7072

7173
pathParam = new SpELPathParameters(exchange);
7274
properties = new SpELProperties(exchange.getProperties());
@@ -88,6 +90,7 @@ private void addTypeConverters() {
8890
GenericConversionService cs = new DefaultConversionService();
8991
cs.addConverter(new SpELHeaderToStringTypeConverter());
9092
cs.addConverter(new SpELMapToStringTypeConverter());
93+
cs.addConverter(new SpELBodyToStringTypeConverter());
9194
setTypeConverter(new StandardTypeConverter(cs));
9295
}
9396

@@ -154,6 +157,8 @@ public Message getMessage() {
154157
return message;
155158
}
156159

160+
public SpELBody getBody() { return body; }
161+
157162
public String getPath() {
158163
return path;
159164
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright 2025 predic8 GmbH, www.predic8.com
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
package com.predic8.membrane.core.lang.spel.spelable;
15+
16+
import com.predic8.membrane.core.http.*;
17+
18+
public class SpELBody {
19+
20+
/**
21+
* Store message instead of body to be able to extract even zipped bodies
22+
*/
23+
Message message;
24+
25+
public SpELBody(Message msg) {
26+
message = msg;
27+
}
28+
29+
public Message getMessage() {
30+
return message;
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Copyright 2025 predic8 GmbH, www.predic8.com
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
package com.predic8.membrane.core.lang.spel.typeconverters;
15+
16+
import com.predic8.membrane.core.lang.spel.spelable.*;
17+
import com.predic8.membrane.core.util.*;
18+
import org.slf4j.*;
19+
import org.springframework.core.convert.converter.*;
20+
21+
public class SpELBodyToStringTypeConverter implements Converter<SpELBody, String> {
22+
23+
private static final Logger log = LoggerFactory.getLogger(SpELBodyToStringTypeConverter.class.getName());
24+
25+
@Override
26+
public String convert(SpELBody body) {
27+
try {
28+
return new String(MessageUtil.getContent(body.getMessage()));
29+
} catch (Exception e) {
30+
log.warn("Cannot log body content", e);
31+
throw new RuntimeException(e);
32+
}
33+
34+
}
35+
}

core/src/test/java/com/predic8/membrane/core/lang/spel/SpELExchangeEvaluationContextTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,9 @@ void json() {
159159
void jsonUnknown() {
160160
assertEquals("", keyExpression("json['unknown']"));
161161
}
162+
163+
@Test
164+
void body() {
165+
assertTrue(keyExpression("body").contains("Snake oil"));
166+
}
162167
}

0 commit comments

Comments
 (0)