{{announcement.body}}
{{announcement.title}}

Remove a BOM Character From an Apache Camel Exchange Message (DSL Java)

DZone 's Guide to

Remove a BOM Character From an Apache Camel Exchange Message (DSL Java)

In this article, we provide a brief tutorial on how to remove a BOM character from an Apache Camel exchange message with DSL Java.

· Java Zone ·
Free Resource

A 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. 

Java
 




x


 
1
 public void detectBomCharacter(Exchange exchange) { 
2
   String newBody = ((String)exchange.getIn().getBody()).replaceAll("\\uFEFF", "");
3
   exchange.getMessage().setBody((newBody)); 
4
 } 


 
The test using JUnit Jupiter could look as follows:

Java
 




xxxxxxxxxx
1
24


 
1
import org.apache.camel.Exchange;
2
import org.apache.camel.support.DefaultExchange;
3
import org.apache.camel.impl.DefaultCamelContext;
4
import org.junit.jupiter.api.Test;
5
import static org.junit.jupiter.api.Assertions.assertEquals;
6
 
          
7
class BomProcessor{
8
    Exchange exchange;
9
    BomProcessor bomProcessor;
10
 
          
11
    public void setUp(){
12
        exchange = new DefaultExchange(new DefaultCamelContext());
13
        bomProcessor = new BomProcessor ();
14
    }
15
 
          
16
    @Test
17
    public void shouldRemoveBomCharacter() {
18
        exchange.getIn().setBody("\uFEFFTest\uFEFF to remove\uFEFF BOM.\uFEFF");
19
        bomProcessor.removeBomCharacter(exchange);
20
        assertEquals("Test to remove BOM."exchange.getMessage().getBody());
21
    }
22
}
23
 
          
24
 




Further Reading

Topics:
apache camel ,bom ,integration ,java dsl ,java.junit

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}