title: Java & Apache-Camel: From direct-endpoint to file-endpoint
body: <p>I've tried to build a route to copy files from one directory to an other directory. But instead of using:
  <code>from(file://source-directory).to(file://destination-directory)</code>
  I want to do something like this: </p>
  
  <pre><code>from(direct:start)
    .to(direct:doStuff)
    .to(direct:readDirectory)
    .to(file://destination-folder)
  </code></pre>
  
  <p>I've done the following stuff:</p>
  
  <p><strong>Route</strong></p>
  
  <pre><code>@Component
  public class Route extends AbstractRouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:start")
          .bean(lookup(ReadDirectory.class))
          .split(body())
            .setHeader("FILENAME", method(lookup(CreateFilename.class)))
            .to("file:///path/to/my/output/directory/?fileName=${header.FILENAME}");
    }
  </code></pre>
  
  <p><strong>Processor</strong></p>
  
  <pre><code>@Component
  public class ReadDirectory implements CamelProcessorBean {
    @Handler
    public ImmutableList&lt;File&gt; apply(@Header("SOURCE_DIR") final String sourceDir) {
      final File directory = new File(sourceDir);
      final File[] files = directory.listFiles();
      if (files == null) {
        return ImmutableList.copyOf(Lists.&lt;File&gt;newArrayList());
      }
      return ImmutableList.copyOf(files);
    }
  }
  </code></pre>
  
  <p>I can start my route by using the following pseudo-Test (The point is I can manually start my route by <code>producer.sendBodyAndHeader(..)</code>)</p>
  
  <pre><code>public class RouteIT extends StandardIT {
    @Produce
    private ProducerTemplate producer;
  
    @Test
    public void testRoute() throws Exception {
      final String uri = "direct:start";
      producer.sendBodyAndHeaders(uri, InOut, null, header());
    }
  
    private Map&lt;String, Object&gt; header() {
      final Map&lt;String, Object&gt; header = Maps.newHashMap();
      header.put("SOURCE_DIR", "/path/to/my/input/directory/");
      return header;
    }
  }
  </code></pre>
  
  <ul>
  <li><code>AbstractRouteBuilder</code>extends <code>SpringRouteBuilder</code></li>
  <li><code>CamelProcessorBean</code> is only a Marker-Interface</li>
  <li><code>StandardIT</code> loads SpringContext and stuff</li>
  </ul>
  
  <p><strong>The problem is</strong>, that I must set the filename. I've read some stuff that camel sets the header <code>CamelFileNameProduced</code> (during the file endpoint). It is a generic string with timestamp and if I don't set the filename - the written files will get this generic string as the filename.</p>
  
  <p><strong>My Question is</strong>: Is there a more beautiful solution to copy files (but starting with a direct-endpoint and read the directory in the middle of the route) and keep the filename for the destination? (I don't have to set the filename when I use <code>from("file:source").to("file:destination")</code>, why must I do it now?)</p>