Skip to content

Commit

Permalink
Merge pull request #273 from assimbly/268-http-retry-impossible-to-se…
Browse files Browse the repository at this point in the history
…e-response-body-from-5xx-responses

SetOriginalMessageProcessor - set and load originalHttpMessage when retry option is used on http component
  • Loading branch information
skin27 authored Sep 18, 2024
2 parents e32e674 + 0733fa2 commit 0115eac
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.assimbly.dil.blocks.processors;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SetOriginalMessageProcessor implements Processor {

private static String DOVETAIL_ORIGINAL_HTTP_MESSAGE_METHOD_PROP = "DOVETAIL_originalHttpMessageMethod";

private static String DOVETAIL_RETRY_ATTEMPTS_HEADER = "DOVETAIL_RetryAttempts";
private static String DOVETAIL_ORIGINAL_HTTP_MESSAGE_VARIABLE_ID_HEADER = "DOVETAIL_OriginalHttpMessageVariableId";

private static String CAMEL_GLOBAL_VARIABLE_PREFIX = "global:";

public enum HttpMethod {GET, SET, DEL}

protected Logger log = LoggerFactory.getLogger(getClass());

public void process(Exchange exchange) throws Exception {
String variableId;

String methodStr = (String) exchange.getProperty(DOVETAIL_ORIGINAL_HTTP_MESSAGE_METHOD_PROP);
HttpMethod originalHttpMessageMethod = getHttpMethod(methodStr);

switch (originalHttpMessageMethod) {
case GET:
// load the original http message into the current exchange from the camel global variable

// load retry attempts
Object retryAttempts = exchange.getMessage().getHeader(DOVETAIL_RETRY_ATTEMPTS_HEADER);
int retryAttemptsInt = (Integer)retryAttempts + 1;

// load original http message variable id
variableId = (String) exchange.getMessage().getHeader(DOVETAIL_ORIGINAL_HTTP_MESSAGE_VARIABLE_ID_HEADER);

// load original http message from global variable
Message originalHttpMessage = (Message) exchange.getVariable(CAMEL_GLOBAL_VARIABLE_PREFIX + variableId);

// set retry attempts
originalHttpMessage.setHeader(DOVETAIL_RETRY_ATTEMPTS_HEADER, retryAttemptsInt);

// load original http message into exchange
exchange.setMessage(originalHttpMessage);
break;

case SET:
// set the current exchange message as the original http message in the camel global variable

// set retry attempts to 1
exchange.getMessage().setHeader(DOVETAIL_RETRY_ATTEMPTS_HEADER, 0);

// using exchangeId as http message variable id
String exchangeId = exchange.getExchangeId();
// set original http message variable id
exchange.getMessage().setHeader(DOVETAIL_ORIGINAL_HTTP_MESSAGE_VARIABLE_ID_HEADER, exchangeId);

// set original http message on a global variable
exchange.setVariable(CAMEL_GLOBAL_VARIABLE_PREFIX + exchangeId, exchange.getMessage().copy());
break;

case DEL:
// delete the original http message from the camel global variables
variableId = (String) exchange.getMessage().getHeader(DOVETAIL_ORIGINAL_HTTP_MESSAGE_VARIABLE_ID_HEADER);
exchange.removeVariable(CAMEL_GLOBAL_VARIABLE_PREFIX + variableId);
break;

default:
// do nothing
break;

}
}

private HttpMethod getHttpMethod(String methodStr) {
try {
return HttpMethod.valueOf(methodStr);
} catch (IllegalArgumentException | NullPointerException e) {
log.error("Invalid HTTP method: " + methodStr + ". Using default: " + HttpMethod.GET);
return HttpMethod.GET;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public void setDefaultBlocks() throws Exception {
registry.bind("ManageFlowProcessor", new ManageFlowProcessor());
registry.bind("multipartProcessor",new org.assimbly.multipart.processor.MultipartProcessor());
registry.bind("RoutingRulesProcessor", new RoutingRulesProcessor());
registry.bind("SetOriginalMessageProcessor", new SetOriginalMessageProcessor());
registry.bind("SetBodyProcessor", new SetBodyProcessor());
registry.bind("SetHeadersProcessor", new SetHeadersProcessor());
registry.bind("SetPatternProcessor", new SetPatternProcessor());
Expand Down

0 comments on commit 0115eac

Please sign in to comment.