Remove a BOM Character From an Apache Camel Exchange Message (DSL Java)
Join the DZone community and get the full member experience.
Join For FreeA BOM character (Byte Order Mark) is an invisible character located at the start of a text file. A machine can identify the BOM character by its hexadecimal byte sequence, but to the user, the BOM character is invisible.
Apache Camel has a validation component that ignores BOM characters if they appear at the beginning of a file, but the component does not ignore BOM characters if they appear elsewhere. This can cause failures in an application. For example, when we use a splitter and aggregator, the exchange message can be ordered differently after aggregation is completed (i.e. the BOM character is not located at the start of a file, but somewhere else).
You may also like: How to Transform Any Type of Java Bean With BULL.
To avoid failures based on BOM characters, we create a Java Bean that scans the exchange message for the BOM character by its hexadecimal code and replaces it with an empty string.
public void detectBomCharacter(Exchange exchange) {
String newBody = ((String)exchange.getIn().getBody()).replaceAll("\\uFEFF", "");
exchange.getMessage().setBody((newBody));
}
The test using JUnit Jupiter could look as follows:
xxxxxxxxxx
import org.apache.camel.Exchange;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class BomProcessor{
Exchange exchange;
BomProcessor bomProcessor;
public void setUp(){
exchange = new DefaultExchange(new DefaultCamelContext());
bomProcessor = new BomProcessor ();
}
public void shouldRemoveBomCharacter() {
exchange.getIn().setBody("\uFEFFTest\uFEFF to remove\uFEFF BOM.\uFEFF");
bomProcessor.removeBomCharacter(exchange);
assertEquals("Test to remove BOM.", exchange.getMessage().getBody());
}
}
Further Reading
Opinions expressed by DZone contributors are their own.
Comments