title: Apache Camel - List Aggregator with filter
body: <p>I am working on an Apache Camel project to load data and Webservice and construct a file from it. The basic job of getting data from service and loading to a CSV file is working fine. However because of a limitation in service, I have to filter the data on my end from service response.</p>
  
  <p>With my limited knowledge on Apache camel, I tried this.</p>
  
  <pre><code>from("direct:start")
              .setHeader(Exchange.HTTP_METHOD, constant("GET"))
              .setHeader(Exchange.HTTP_URI, simple(URL))
              .setHeader("Authorization", simple(KEY))
              .to(URL)
              .unmarshal(listJacksonDataFormat)
              //START - FILTER
              .split(body())
              .filter().method(DiagnosticDateFilter.class, "diagnosedAfterLastRun")
              .aggregate(constant(true), new GroupedBodyAggregationStrategy())
              .completionTimeout(1000)
              //END - FILTER
              .marshal(bindyCsvDataFormat)
              .to("file:" + FILELOC);
  </code></pre>
  
  <p><strong>// This filter checks one of the value from Webservice JSON Array response and decides if the model object needs to be added to output list during aggregation.</strong></p>
  
  <pre><code>public static class DiagnosticDateFilter {
      public boolean diagnosedAfterLastRun(@Body Feed feed) {
          return true;
      }
  }
  </code></pre>
  
  <p>public class GroupedBodyAggregationStrategy implements AggregationStrategy {</p>
  
  <pre><code>@Override
  public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
      Object newBody = newExchange.getIn().getBody();
      ArrayList&lt;Object&gt; list = null;
      if (oldExchange == null) {
          list = new ArrayList&lt;Object&gt;();
          list.add(newBody);
          newExchange.getIn().setBody(list);
          return newExchange;
      } else {
          list = oldExchange.getIn().getBody(ArrayList.class);
          list.add(newBody);
          return oldExchange;
      }
  }
  </code></pre>
  
  <p>Some reason after "filter" and "aggregate", I am loosing data in exchange and route completes without creating file.</p>
  
  <p>Is there any better way for this? Also is it possible to apply filter before <code>".unmarshal(listJacksonDataFormat)"</code> so that I don't have to include in list and then filter out? Thanks for your reply in advance!!</p>