1

I'm learning Camel and created a route as follows:

from("timer:stream?period={{inbound.timer.period}}")
      ...
      .setHeader(Exchange.HTTP_URI, simple(outboundUri()))
      ...
      .multicast()
      .to(
              "stream:header",
              "file://build?autoCreate=false",
              outboundHttp
      );

The outboundUri() method returns a URI with a placeholder in the path, ${header.CamelFileName}. What I'd like to do is resolve this using a header mapper of some sort, where I can look at some headers in order of priority, and if none present, set a default value for CamelFileName.

How can I achieve this using the HTTP4 component?

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219

1 Answers1

0

I ended up implementing a Processor as follows:

public class FilenameHeaderMessageProcessor implements org.apache.camel.Processor {
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd-kkmm");

    @Override
    public void process(Exchange exchange) throws Exception {
        Message in = exchange.getIn();
        Map<String, Object> inHeaders = in.getHeaders();
        log.debug("In headers: {}.", inHeaders);

        Message out = exchange.getOut();

        // Without this, out body is null
        out.setBody(in.getBody());

        Object filename = inHeaders.computeIfAbsent(FILE_NAME,
                k -> Optional.ofNullable(inHeaders.get(KEY))
                        .orElse(defaultFilename())
        );

        out.setHeader(FILE_NAME, filename);
    }

    private final String defaultFilename() {
        return DATE_TIME_FORMATTER.format(LocalDateTime.now()) + ".out";
    }
}
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219