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.fixity;
007
008import org.apache.camel.LoggingLevel;
009import org.apache.camel.builder.RouteBuilder;
010import org.apache.camel.support.builder.Namespaces;
011import org.apache.jena.vocabulary.RDF;
012import org.slf4j.Logger;
013import org.springframework.beans.factory.annotation.Autowired;
014
015import static org.slf4j.LoggerFactory.getLogger;
016
017/**
018 * A content router for checking fixity of Binary resources.
019 *
020 * @author Aaron Coburn
021 * @since 2015-06-18
022 */
023public class FixityRouter extends RouteBuilder {
024
025    private static final Logger LOGGER = getLogger(FixityRouter.class);
026
027    private static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";
028
029    @Autowired
030    private FcrepoFixityConfig config;
031
032    /**
033     * Configure the message route workflow.
034     */
035    public void configure() throws Exception {
036        final Namespaces ns = new Namespaces("rdf", RDF.uri);
037        ns.add("premis", "http://www.loc.gov/premis/rdf/v1#");
038
039        /**
040         * A generic error handler (specific to this RouteBuilder)
041         */
042        onException(Exception.class)
043                .maximumRedeliveries(config.getMaxRedeliveries())
044                .log("Index Routing Error: ${routeId}");
045
046        /**
047         * Handle fixity events
048         */
049        from(config.getInputStream())
050                .routeId("FcrepoFixity")
051                .to("fcrepo:" + config.getFcrepoBaseUrl() + "?preferInclude=ServerManged&accept=application/rdf+xml")
052                .filter().xpath(
053                "/rdf:RDF/rdf:Description/rdf:type" +
054                        "[@rdf:resource='" + REPOSITORY + "Binary']", ns)
055                .log(LoggingLevel.INFO, LOGGER,
056                        "Checking Fixity for ${headers[CamelFcrepoUri]}")
057                .delay(simple(String.valueOf(config.getFixityDelay())))
058                .to("fcrepo:" + config.getFcrepoBaseUrl() + "?fixity=true&accept=application/rdf+xml")
059                .choice()
060                .when().xpath(
061                "/rdf:RDF/rdf:Description/premis:hasEventOutcome" +
062                        "[text()='SUCCESS']", ns)
063                .log(LoggingLevel.INFO, LOGGER,
064                        "Fixity success on ${headers[CamelFcrepoUri]}")
065                .to(config.getFixitySuccess())
066                .otherwise()
067                .log(LoggingLevel.WARN, LOGGER,
068                        "Fixity error on ${headers[CamelFcrepoUri]}")
069                .to(config.getFixityFailure());
070
071        LOGGER.info("FixityRouter configured");
072    }
073}