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.processor;
019
020import static java.net.URLEncoder.encode;
021
022import java.io.IOException;
023
024import org.apache.camel.Exchange;
025import org.apache.camel.Message;
026import org.apache.camel.Processor;
027import org.fcrepo.camel.FcrepoHeaders;
028
029/**
030 * Represends a message processor that deletes objects from an
031 * external triplestore.
032 *
033 * @author Aaron Coburn
034 * @since Nov 8, 2014
035 */
036public class SparqlDeleteProcessor implements Processor {
037    /**
038     * Define how the message should be processed.
039     *
040     * @param exchange the current camel message exchange
041     */
042    public void process(final Exchange exchange) throws IOException {
043
044        final Message in = exchange.getIn();
045        final String namedGraph = in.getHeader(FcrepoHeaders.FCREPO_NAMED_GRAPH, String.class);
046        final String subject = ProcessorUtils.getSubjectUri(in);
047
048        /*
049         * N.B. The Sparql update command below deletes all triples with
050         * the defined subject uri (coming from the FCREPO_IDENTIFIER
051         * and FCREPO_BASE_URL headers). It also deletes triples that
052         * have a subject corresponding to that Fcrepo URI plus the
053         * "/fcr:export?format=jcr/xml" string appended to it. This
054         * makes it possible to more completely remove any triples
055         * for a given resource that were added earlier. If fcrepo
056         * ever stops producing triples that are appended with
057         * /fcr:export?format..., then that extra line can be removed.
058         * It would also be possible to recursively delete triples
059         * (by removing any triple whose subject is also an object of
060         * the starting (or context) URI, but that approach tends to delete
061         * too many triples from the triplestore. This command does
062         * not remove blank nodes.
063         */
064        final StringBuilder query = new StringBuilder();
065
066        query.append(ProcessorUtils.deleteWhere(subject, namedGraph));
067        query.append(";\n");
068        query.append(ProcessorUtils.deleteWhere(subject + "/fcr:export?format=jcr/xml", namedGraph));
069
070        in.setBody("update=" + encode(query.toString(), "UTF-8"));
071        in.setHeader(Exchange.HTTP_METHOD, "POST");
072        in.setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
073   }
074
075}