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.fixity;
019
020import static org.slf4j.LoggerFactory.getLogger;
021
022import org.apache.camel.LoggingLevel;
023import org.apache.camel.builder.RouteBuilder;
024import org.apache.camel.builder.xml.Namespaces;
025import org.fcrepo.camel.RdfNamespaces;
026import org.slf4j.Logger;
027
028/**
029 * A content router for checking fixity of Binary resources.
030 *
031 * @author Aaron Coburn
032 * @since 2015-06-18
033 */
034public class FixityRouter extends RouteBuilder {
035
036    private static final Logger LOGGER = getLogger(FixityRouter.class);
037
038    /**
039     * Configure the message route workflow.
040     */
041    public void configure() throws Exception {
042
043        final Namespaces ns = new Namespaces("rdf", RdfNamespaces.RDF);
044        ns.add("premis", "http://www.loc.gov/premis/rdf/v1#");
045
046        /**
047         * A generic error handler (specific to this RouteBuilder)
048         */
049        onException(Exception.class)
050            .maximumRedeliveries("{{error.maxRedeliveries}}")
051            .log("Index Routing Error: ${routeId}");
052
053        /**
054         * Handle fixity events
055         */
056        from("{{fixity.stream}}")
057            .routeId("FcrepoFixity")
058            .to("fcrepo:{{fcrepo.baseUrl}}?preferInclude=ServerManged&accept=application/rdf+xml")
059            .filter().xpath(
060                    "/rdf:RDF/rdf:Description/rdf:type" +
061                    "[@rdf:resource='" + RdfNamespaces.REPOSITORY + "Binary']", ns)
062            .log(LoggingLevel.INFO, LOGGER,
063                    "Checking Fixity for ${headers[CamelFcrepoIdentifier]}")
064            .delay(simple("{{fixity.delay}}"))
065            .to("fcrepo:{{fcrepo.baseUrl}}?fixity=true&accept=application/rdf+xml")
066            .choice()
067                .when().xpath(
068                        "/rdf:RDF/rdf:Description/premis:hasEventOutcome" +
069                        "[text()='SUCCESS']", ns)
070                    .to("{{fixity.success}}")
071                .otherwise()
072                    .log(LoggingLevel.WARN, LOGGER,
073                        "Fixity error on ${headers[CamelFcrepoIdentifier]}")
074                    .to("{{fixity.failure}}");
075    }
076}