DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Microservices With Apache Camel and Quarkus

Trending

  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • DZone's Article Types
  • Java Virtual Threads Under Load: Three Production Limits
  • How (and Why) to Build Dynamic Data Pipelines

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Remove a BOM Character From an Apache Camel Exchange Message (DSL Java)

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

By 
Anna Star
·
Updated Mar. 04, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Open Source Integration With Apache Camel and How Fuse IDE Can Help.
  • Microservices With Apache Camel.
BOM (file format) Apache Camel Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Microservices With Apache Camel and Quarkus

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook