001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.camel.audit.triplestore;
007
008import org.apache.camel.LoggingLevel;
009import org.apache.camel.builder.RouteBuilder;
010import org.fcrepo.camel.common.processor.AddBasicAuthProcessor;
011import org.fcrepo.camel.processor.EventProcessor;
012import org.slf4j.Logger;
013import org.springframework.beans.factory.annotation.Autowired;
014
015import static java.util.stream.Collectors.toList;
016import static org.apache.camel.builder.PredicateBuilder.in;
017import static org.apache.camel.builder.PredicateBuilder.not;
018import static org.apache.camel.builder.PredicateBuilder.or;
019import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
020import static org.fcrepo.camel.processor.ProcessorUtils.tokenizePropertyPlaceholder;
021import static org.slf4j.LoggerFactory.getLogger;
022
023/**
024 * A content router for handling JMS events.
025 *
026 * @author Aaron Coburn
027 * @author escowles
028 */
029public class EventRouter extends RouteBuilder {
030
031    private static final Logger LOGGER = getLogger(EventRouter.class);
032
033    @Autowired
034    private FcrepoAuditTriplestoreConfig config;
035
036    /**
037     * Configure the message route workflow.
038     */
039    public void configure() throws Exception {
040
041        /**
042         * A generic error handler (specific to this RouteBuilder)
043         */
044        onException(Exception.class)
045            .maximumRedeliveries(config.getMaxRedeliveries())
046            .log("Event Routing Error: ${routeId}");
047
048        /**
049         * Process a message.
050         */
051        from(config.getInputStream())
052            .routeId("AuditFcrepoRouter")
053            .process(new EventProcessor())
054            .filter(not(in(tokenizePropertyPlaceholder(getContext(), config.getFilterContainers(), ",").stream()
055                .map(uri -> or(
056                    header(FCREPO_URI).startsWith(constant(uri + "/")),
057                    header(FCREPO_URI).isEqualTo(constant(uri))))
058                .collect(toList()))))
059            .to("direct:event");
060
061        from("direct:event")
062            .routeId("AuditEventRouter")
063            .setHeader(AuditHeaders.EVENT_BASE_URI, simple(config.getEventBaseUri()))
064            .process(new AuditSparqlProcessor())
065            .log(LoggingLevel.INFO, "org.fcrepo.camel.audit",
066                "Audit Event: ${headers.CamelFcrepoUri} :: ${headers[CamelAuditEventUri]}")
067            .process(new AddBasicAuthProcessor(this.config.getTriplestoreAuthUsername(),
068                        this.config.getTriplestoreAuthPassword()))
069            .to(config.getTriplestoreBaseUrl() + "?useSystemProperties=true");
070    }
071}