Skip to content

Commit

Permalink
Merge pull request #272 from RayDNoper/271-rest-with-dynamic-body
Browse files Browse the repository at this point in the history
271 Add Ruuter REST calls with predefined (dynamic) request structure
  • Loading branch information
RayDNoper authored Apr 15, 2024
2 parents 7a14e57 + f6bc7a8 commit bd685dd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
11 changes: 11 additions & 0 deletions DSL/POST/dynamic-body.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dynamicTest:
call: http.post
args:
url: http://host.docker.internal:9001/test_endpoint
dynamicParameters: true
body:
dynamicBody: ${incoming.body.input}
result: value

returnStep:
return: ${value}
17 changes: 17 additions & 0 deletions samples/steps/http-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ return_value:
return: ${the_message.response}
```
### POST step with "dynamic body"
It is possible to send a pre-defined json body with POST by storing it in
`body.dynamicBody` value and setting `args.dynamicParameters` value to true:
```yaml
dynamicTest:
call: http.post
args:
url: http://host.docker.internal:9001/test_endpoint
dynamicParameters: true
body:
dynamicBody: ${incoming.body.input}
```

This is considered unsafe and should be used only in special cases.

### HTTP Error handling

It is possible to specify the DSL step to follow when POST request gets a
Expand All @@ -126,4 +142,5 @@ error_step:
status: 500
```


[Back to Guide](../GUIDE.md#Writing-DSL-files)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ResponseEntity<Object> getRequestResponse(DslInstance di) {
Map<String, Object> evaluatedQuery = di.getScriptingHelper().evaluateScripts(args.getQuery(), di.getContext(), di.getRequestBody(), di.getRequestQuery(), di.getRequestHeaders());
Map<String, Object> evaluatedHeaders = di.getScriptingHelper().evaluateScripts(args.getHeaders(), di.getContext(), di.getRequestBody(), di.getRequestQuery(), di.getRequestHeaders());
Map<String, String> mappedHeaders = di.getMappingHelper().convertMapObjectValuesToString(evaluatedHeaders);
return di.getHttpHelper().doMethod(HttpMethod.GET, evaluatedURL, evaluatedQuery, null, mappedHeaders, null, null, getLimit(), di);
return di.getHttpHelper().doMethod(HttpMethod.GET, evaluatedURL, evaluatedQuery, null, mappedHeaders, null, null, getLimit(), di, args.isDynamicParameters());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected ResponseEntity<Object> getRequestResponse(DslInstance di) {
evaluatedQuery, evaluatedBody, mappedHeaders,
args.getContentType(),
"plaintext".equals(args.getContentType()) ? args.getPlaintext() : null,
getLimit(), di);
getLimit(), di,
args.isDynamicParameters());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class HttpQueryArgs {
private String contentType;
private String originalUrl = "";

protected boolean dynamicParameters;

public void addHeaders(Map<String, Object> newHeaders) {
newHeaders.forEach(headers::putIfAbsent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public ResponseEntity<Object> forwardRequest(String dsl, Map<String, Object> req
.toUriString();

if (methodType.equals(HttpMethod.POST.name())) {
return httpHelper.doMethod(HttpMethod.POST,forwardingUrl, query, body, headers, contentType, null, null, di);
return httpHelper.doMethod(HttpMethod.POST,forwardingUrl, query, body, headers, contentType, null, null, di, false);
}
if (methodType.equals(HttpMethod.GET.name())) {
return httpHelper.doMethod(HttpMethod.GET, forwardingUrl, query,null, headers, null, null, null, di);
return httpHelper.doMethod(HttpMethod.GET, forwardingUrl, query,null, headers, null, null, null, di, false);
}
throw new InvalidHttpMethodTypeException(methodType);
}
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/ee/buerokratt/ruuter/helper/HttpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ee.buerokratt.ruuter.configuration.ApplicationProperties;
import ee.buerokratt.ruuter.domain.DslInstance;
import ee.buerokratt.ruuter.util.LoggingUtils;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
Expand Down Expand Up @@ -40,27 +41,27 @@ public class HttpHelper {

final private ScriptingHelper scriptingHelper;

public ResponseEntity<Object> doPost(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, DslInstance di) {
return doPost(url, body, query, headers, this.getClass().getName(), di);
public ResponseEntity<Object> doPost(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, DslInstance di, boolean dynamicBody) {
return doPost(url, body, query, headers, this.getClass().getName(), di, dynamicBody);
}
public ResponseEntity<Object> doPost(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, String contentType, DslInstance di) {
return doMethod(POST, url, query, body,headers, contentType, null, null, di);
public ResponseEntity<Object> doPost(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, String contentType, DslInstance di, boolean dynamicBody) {
return doMethod(POST, url, query, body,headers, contentType, null, null, di, dynamicBody);
}

public ResponseEntity<Object> doPostPlaintext(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, String plaintext, DslInstance di) {
return doMethod(POST, url, body, query, headers, "plaintext", plaintext, null, di);
return doMethod(POST, url, body, query, headers, "plaintext", plaintext, null, di, false);
}

public ResponseEntity<Object> doGet(String url, Map<String, Object> query, Map<String, String> headers, DslInstance di) {
return doMethod(HttpMethod.GET, url, query, null, headers, null, null, null, di);
return doMethod(HttpMethod.GET, url, query, null, headers, null, null, null, di, false);
}

public ResponseEntity<Object> doPut(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, String contentType, DslInstance di) {
return doMethod(HttpMethod.PUT, url, query, body,headers, contentType, null, null, di);
public ResponseEntity<Object> doPut(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, String contentType, DslInstance di, boolean dynamicBody) {
return doMethod(HttpMethod.PUT, url, query, body,headers, contentType, null, null, di, dynamicBody);
}

public ResponseEntity<Object> doDelete(String url, Map<String, Object> body, Map<String, Object> query, Map<String, String> headers, String contentType, DslInstance di) {
return doMethod(HttpMethod.DELETE, url, query, body, headers, contentType, null, null, di);
return doMethod(HttpMethod.DELETE, url, query, body, headers, contentType, null, null, di, false);
}

public ResponseEntity<Object> doMethod(HttpMethod method,
Expand All @@ -71,14 +72,14 @@ public ResponseEntity<Object> doMethod(HttpMethod method,
String contentType,
String plaintextValue,
Integer limit,
DslInstance instance) {
DslInstance instance,
boolean dynamicBody) {
try {
MultiValueMap<String, String> qp = new LinkedMultiValueMap<>(
query.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e-> Arrays.asList(e.getValue().toString()))));

BodyInserter bodyValue;
String mediaType;

if (method == POST &&
"plaintext".equals(contentType) && plaintextValue != null) {
bodyValue = BodyInserters.fromValue(plaintextValue);;
Expand Down Expand Up @@ -112,7 +113,12 @@ public ResponseEntity<Object> doMethod(HttpMethod method,
bodyValue = BodyInserters.empty();
mediaType = MediaType.APPLICATION_JSON_VALUE;
} else {
bodyValue = BodyInserters.fromValue(body);
if (dynamicBody) {
log.warn("Sending dynamic body request is considered unsecure");
bodyValue = BodyInserters.fromValue(body.get("dynamicBody"));
} else {
bodyValue = BodyInserters.fromValue(body);
}
mediaType = MediaType.APPLICATION_JSON_VALUE;
}

Expand Down

0 comments on commit bd685dd

Please sign in to comment.