Search code examples
apache-camelsplitter

Apache Camel Split - splitting header incorrectly when only one header is provided


I have implemented endpoint which receives multiple parameters as query

i.e: /flights?flight=AB,100,12FEB18&flight=CD,200,13FEB18&flight=...&...

In camel I would like to split each flight parameter and handle it separately, so I have:

.split(header("flight"))

Case 1

Query: /flights?flight=AB,100,24FEB18&flight=AB,200,25FEB18

Splits into:
1) AB, 100, 24FEB18
2) AB, 200, 25FEB18

Result: Correct

Case 2

Query: /flights?flight=AB&flight=AB,100&flight=AB,200,26FEB18

Splits into:
1) AB
2) AB, 100
3) AB, 200, 26FEB18

Result: Correct

Case 3

Query: /flights?flight=AB,400,28FEB18

Splits into:
1) AB
2) 400
3) 28FEB18

Result: Incorrect

Expected:
1) AB, 400, 28FEB18

Why does splitter for one header works differently and how can I fix it ?


Solution

  • Well, I guess that Camel is "too smart" for your usecase and therefore you get different behaviours.

    Case 1 and 2 is a list of lists. A list of flight parameters and every parameter contains a list of comma-separated values.

    Case 3 is a simple list. A single flight parameter that contains a list of comma-separated values.

    Since the splitter wants to split things, it takes the "inner" list in Case 3 while it takes the "outer" list in Case 1 and 2.

    To solve the problem you could try several things:

    • Change the delimiter of the inner lists (use for example - instead of ,) so that Camel does not think your flight parameter contains a list in Case 3.
    • Add a flight parameter in Case 3 that is empty or contains a dummy value so that you always have an "outer" list.
      • Use a bean before the splitter and convert the parameter into a structure that is not mis-interpreted by the splitter

    If you are not in control of the URI parameters, you could use a bean to apply one of the first two suggestions.