Skip to content
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

CAMEL-17751: Extracting Camel-Saga-Compensate & Camel-Saga-Complete f… #7118

Merged
merged 2 commits into from Mar 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,10 +16,16 @@
*/
package org.apache.camel.service.lra;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.builder.RouteBuilder;

import static org.apache.camel.service.lra.LRAConstants.PARTICIPANT_PATH_COMPENSATE;
Expand Down Expand Up @@ -59,7 +65,8 @@ public void configure() throws Exception {
}

/**
* Check if the request is pointing to an allowed URI to prevent unauthorized remote uri invocation
* Check if the request is pointing to an allowed URI to prevent unauthorized
* remote uri invocation
*/
private void verifyRequest(Exchange exchange) {
if (exchange.getIn().getHeader(Exchange.SAGA_LONG_RUNNING_ACTION) == null) {
Expand All @@ -76,11 +83,48 @@ private void verifyRequest(Exchange exchange) {
usedURIs.add(completionURI);
}

if (usedURIs.isEmpty()) {
Map<String, String> queryParams = extractQueryParamsFromHttpQueryHeader(exchange);
if (!queryParams.isEmpty()) {
compensationURI = queryParams.get(URL_COMPENSATION_KEY);
if (compensationURI != null) {
usedURIs.add(compensationURI);
exchange.getIn().setHeader(URL_COMPENSATION_KEY, compensationURI);
}
completionURI = queryParams.get(URL_COMPLETION_KEY);
if (completionURI != null) {
usedURIs.add(completionURI);
exchange.getIn().setHeader(URL_COMPLETION_KEY, completionURI);
}
}
}

for (String uri : usedURIs) {
if (!sagaService.getRegisteredURIs().contains(uri)) {
throw new IllegalArgumentException("URI " + uri + " is not allowed");
}
}
}

private Map<String, String> extractQueryParamsFromHttpQueryHeader(Exchange exchange) {
Map<String, String> queryParams = new HashMap<>();

try {
String strQueryParams = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
if (strQueryParams != null) {
String[] pairs = strQueryParams.split("&");
for (String pair : pairs) {
String[] keyValuePair = pair.split("=");
queryParams.put(URLDecoder.decode(keyValuePair[0], StandardCharsets.UTF_8.name()),
keyValuePair.length == 2 ? URLDecoder.decode(keyValuePair[1], StandardCharsets.UTF_8.name()) : "");
}
}

} catch (UnsupportedEncodingException ex) {
throw new RuntimeCamelException("Unsupported encoding exception during " + Exchange.HTTP_QUERY + " header parsing");
}

return queryParams;
}

}