001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.camel.audit.triplestore;
019
020import static java.util.stream.Collectors.toList;
021import static org.apache.camel.builder.PredicateBuilder.in;
022import static org.apache.camel.builder.PredicateBuilder.not;
023import static org.apache.camel.builder.PredicateBuilder.or;
024import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
025import static org.fcrepo.camel.processor.ProcessorUtils.tokenizePropertyPlaceholder;
026import static org.slf4j.LoggerFactory.getLogger;
027
028import org.apache.camel.LoggingLevel;
029import org.apache.camel.builder.RouteBuilder;
030import org.fcrepo.camel.processor.EventProcessor;
031import org.slf4j.Logger;
032
033/**
034 * A content router for handling JMS events.
035 *
036 * @author Aaron Coburn
037 * @author escowles
038 */
039public class EventRouter extends RouteBuilder {
040
041    private static final Logger LOGGER = getLogger(EventRouter.class);
042
043    /**
044     * Configure the message route workflow.
045     */
046    public void configure() throws Exception {
047
048        /**
049         * A generic error handler (specific to this RouteBuilder)
050         */
051        onException(Exception.class)
052            .maximumRedeliveries("{{error.maxRedeliveries}}")
053            .log("Event Routing Error: ${routeId}");
054
055        /**
056         * Process a message.
057         */
058        from("{{input.stream}}")
059            .routeId("AuditFcrepoRouter")
060            .process(new EventProcessor())
061            .filter(not(in(tokenizePropertyPlaceholder(getContext(), "{{filter.containers}}", ",").stream()
062                        .map(uri -> or(
063                            header(FCREPO_URI).startsWith(constant(uri + "/")),
064                            header(FCREPO_URI).isEqualTo(constant(uri))))
065                        .collect(toList()))))
066                .to("direct:event");
067
068        from("direct:event")
069            .routeId("AuditEventRouter")
070            .setHeader(AuditHeaders.EVENT_BASE_URI, simple("{{event.baseUri}}"))
071            .process(new AuditSparqlProcessor())
072            .log(LoggingLevel.INFO, "org.fcrepo.camel.audit",
073                    "Audit Event: ${headers.CamelFcrepoUri} :: ${headers[CamelAuditEventUri]}")
074            .to("{{triplestore.baseUrl}}?useSystemProperties=true");
075    }
076}