-
Notifications
You must be signed in to change notification settings - Fork 170
setBody #2462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
setBody #2462
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b271a0
feat: add `setBody` interceptor for modifying HTTP body content
predic8 5f4f3e8
chore: clean up redundant Javadoc comments in `SetBodyInterceptor` an…
predic8 40cfd48
Merge branch 'master' into set-body-interceptor
predic8 3099a6c
chore: update Javadoc and comments in `SetBodyInterceptor` and `Excha…
predic8 f42bad3
Merge remote-tracking branch 'origin/set-body-interceptor' into set-b…
predic8 8d23a3d
feat: handle null expression results in `SetBodyInterceptor` and add …
predic8 2953566
fix: correct `SetBodyInterceptorTest` to validate response handling l…
predic8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
core/src/main/java/com/predic8/membrane/core/interceptor/lang/SetBodyInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package com.predic8.membrane.core.interceptor.lang; | ||
|
|
||
| import com.predic8.membrane.annot.*; | ||
| import com.predic8.membrane.core.exchange.*; | ||
| import com.predic8.membrane.core.interceptor.*; | ||
| import com.predic8.membrane.core.util.*; | ||
| import org.slf4j.*; | ||
|
|
||
| import static com.predic8.membrane.core.exceptions.ProblemDetails.*; | ||
| import static com.predic8.membrane.core.interceptor.Interceptor.Flow.*; | ||
| import static com.predic8.membrane.core.interceptor.Outcome.*; | ||
| import static com.predic8.membrane.core.interceptor.Outcome.ABORT; | ||
| import static java.nio.charset.StandardCharsets.*; | ||
|
|
||
| /** | ||
| * setBody sets the content of the HTTP message body to the specified value. The value | ||
| * can be a static string, or it can be dynamically generated by an expression. | ||
| * Different languages such as SpEL, Groovy, XMLPath or JsonPath are supported. | ||
| * setBody does not support conditional processing or loops. When you need these features, | ||
| * resort to the template plugin instead. | ||
| * | ||
| * The content of the message body is set as UTF-8 encoded bytes. Set a corresponding content type header if necessary. | ||
| */ | ||
| @MCElement(name = "setBody") | ||
| public class SetBodyInterceptor extends AbstractExchangeExpressionInterceptor { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(SetBodyInterceptor.class); | ||
|
|
||
| @Override | ||
| public Outcome handleRequest(Exchange exc) { | ||
| return handleInternal(exc, REQUEST); | ||
| } | ||
|
|
||
| @Override | ||
| public Outcome handleResponse(Exchange exc) { | ||
| return handleInternal(exc, RESPONSE); | ||
| } | ||
|
|
||
| private Outcome handleInternal(Exchange exchange, Flow flow) { | ||
| try { | ||
| // The value is typically set from YAML there we can assume UTF-8 | ||
| var result = exchangeExpression.evaluate(exchange, flow, String.class); | ||
| if (result == null) { | ||
| result = "null"; | ||
| } | ||
| exchange.getMessage(flow).setBodyContent(result.getBytes(UTF_8)); | ||
| return CONTINUE; | ||
| } catch (Exception e) { | ||
| var root = ExceptionUtil.getRootCause(e); | ||
| var message = "While evaluating expression %s: %s".formatted(expression, root.getMessage()); | ||
| log.info(message); | ||
|
|
||
| internal(getRouter().isProduction(), getDisplayName()) | ||
| .title("Error evaluating expression!") | ||
| .internal("expression", expression) | ||
| .exception(root) | ||
| .stacktrace(false) | ||
| .buildAndSetResponse(exchange); | ||
| return ABORT; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets the expression to be evaluated for modifying the HTTP message body. | ||
| * The provided string value can represent a static text or a dynamic expression | ||
| * | ||
| * @param value expression or static string | ||
| */ | ||
| @MCAttribute | ||
| public void setValue(String value) { | ||
| this.expression = value; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return expression; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "setBody"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getShortDescription() { | ||
| return "Sets the content of the HTTP message body to the expression: %s.".formatted(expression); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
core/src/main/java/com/predic8/membrane/core/lang/spel/DollarTemplateParserContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.predic8.membrane.core.lang.spel; | ||
|
|
||
| import org.springframework.expression.common.*; | ||
|
|
||
| public class DollarTemplateParserContext extends TemplateParserContext { | ||
|
|
||
| public static final DollarTemplateParserContext DOLLAR_TEMPLATE_PARSER_CONTEXT = new DollarTemplateParserContext(); | ||
|
|
||
| private DollarTemplateParserContext() { | ||
| super("${", "}"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/test/java/com/predic8/membrane/core/interceptor/lang/SetBodyInterceptorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.predic8.membrane.core.interceptor.lang; | ||
|
|
||
| import com.predic8.membrane.core.*; | ||
| import com.predic8.membrane.core.exchange.*; | ||
| import com.predic8.membrane.core.http.*; | ||
| import org.junit.jupiter.api.*; | ||
|
|
||
| import java.net.*; | ||
|
|
||
| import static com.predic8.membrane.core.http.Response.notImplemented; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| /** | ||
| * A simple test is enough to test the logic of the interceptor. | ||
| * Complex expressions are tested in the ExchangeExpressionTest, and ... | ||
| */ | ||
| class SetBodyInterceptorTest { | ||
|
predic8 marked this conversation as resolved.
|
||
|
|
||
| private SetBodyInterceptor sbi; | ||
| private Exchange exc; | ||
|
|
||
| @BeforeEach | ||
| void setup() throws URISyntaxException { | ||
| sbi = new SetBodyInterceptor(); | ||
|
|
||
| exc = Request.get("/foo").buildExchange(); | ||
| exc.setResponse(notImplemented().body("bar").build()); | ||
| } | ||
|
|
||
| @Test | ||
| void nullResult() { | ||
| sbi.setValue("null"); | ||
| sbi.init(new Router()); | ||
| sbi.handleRequest(exc); | ||
| assertEquals("null", exc.getRequest().getBodyAsStringDecoded()); | ||
| } | ||
|
|
||
| @Test | ||
| void evalOfSimpleExpression() { | ||
| sbi.setValue("${path}"); | ||
| sbi.init(new Router()); | ||
| sbi.handleRequest(exc); | ||
| assertEquals("/foo", exc.getRequest().getBodyAsStringDecoded()); | ||
| } | ||
|
|
||
| @Test | ||
| void response() { | ||
| sbi.setValue("SC: ${statusCode}"); | ||
| sbi.init(new Router()); | ||
| sbi.handleResponse(exc); | ||
| assertEquals("SC: 501", exc.getResponse().getBodyAsStringDecoded()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.