001/*
002 * Licensed to Islandora Foundation under one or more contributor license
003 * agreements. See the NOTICE file distributed with this work for additional
004 * information regarding copyright ownership.
005 *
006 * The Islandora Foundation licenses this file to you under the MIT License.
007 * You may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://opensource.org/licenses/MIT
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 */
018
019package ca.islandora.indexing.triplestore;
020
021import org.apache.camel.LoggingLevel;
022import org.apache.camel.Predicate;
023import org.apache.camel.builder.PredicateBuilder;
024import org.apache.camel.builder.RouteBuilder;
025import org.fcrepo.camel.FcrepoHeaders;
026import org.fcrepo.camel.processor.SparqlUpdateProcessor;
027import org.fcrepo.camel.processor.SparqlDeleteProcessor;
028
029public class TriplestoreIndexer extends RouteBuilder {
030
031    @Override
032    public void configure() {
033
034        Predicate isTriples = header("Content-Type").isEqualTo("application/n-triples");
035        Predicate hasBaseUrl = header(FcrepoHeaders.FCREPO_BASE_URL).isNotNull();
036        Predicate hasIdentifier = header(FcrepoHeaders.FCREPO_IDENTIFIER).isNotNull();
037        Predicate hasFcrepoCamelHeaders = PredicateBuilder.and(hasBaseUrl, hasIdentifier);
038        Predicate hasAction = PredicateBuilder.or(header("action").isEqualTo("delete"), header("action").isEqualTo("upsert"));
039        Predicate isValid = PredicateBuilder.and(isTriples, hasFcrepoCamelHeaders, hasAction);
040
041        onException(Exception.class)
042            .maximumRedeliveries("{{error.maxRedeliveries}}")
043            .log(LoggingLevel.ERROR, "Error Indexing in Triplestore: ${routeId}");
044
045        from("{{input.stream}}")
046            .routeId("IslandoraTriplestoreIndexerRouter")
047            .filter(isValid)
048                .choice()
049                    .when(header("action").isEqualTo("delete"))
050                        .to("direct:triplestoreDelete")
051                    .otherwise()
052                        .to("direct:triplestoreUpsert");
053
054        from("direct:triplestoreUpsert")
055            .routeId("islandoraTripelstoreIndexerUpsert")
056            .process(new SparqlUpdateProcessor())
057            .to("http4://{{triplestore.baseUrl}}");
058
059        from("direct:triplestoreDelete")
060            .routeId("islandoraTripelstoreIndexerDelete")
061            .process(new SparqlDeleteProcessor())
062            .to("http4://{{triplestore.baseUrl}}");
063
064    }
065}