001/** 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.camel.processor; 017 018import static java.net.URLEncoder.encode; 019 020import java.io.IOException; 021 022import org.apache.camel.Exchange; 023import org.apache.camel.Message; 024import org.apache.camel.Processor; 025import org.fcrepo.camel.FcrepoHeaders; 026 027/** 028 * Represends a message processor that deletes objects from an 029 * external triplestore. 030 * 031 * @author Aaron Coburn 032 * @since Nov 8, 2014 033 */ 034public class SparqlDeleteProcessor implements Processor { 035 /** 036 * Define how the message should be processed. 037 * 038 * @param exchange the current camel message exchange 039 */ 040 public void process(final Exchange exchange) throws IOException { 041 042 final Message in = exchange.getIn(); 043 final String namedGraph = in.getHeader(FcrepoHeaders.FCREPO_NAMED_GRAPH, String.class); 044 final String subject = ProcessorUtils.getSubjectUri(in); 045 046 /* 047 * N.B. The Sparql update command below deletes all triples with 048 * the defined subject uri (coming from the FCREPO_IDENTIFIER 049 * and FCREPO_BASE_URL headers). It also deletes triples that 050 * have a subject corresponding to that Fcrepo URI plus the 051 * "/fcr:export?format=jcr/xml" string appended to it. This 052 * makes it possible to more completely remove any triples 053 * for a given resource that were added earlier. If fcrepo 054 * ever stops producing triples that are appended with 055 * /fcr:export?format..., then that extra line can be removed. 056 * It would also be possible to recursively delete triples 057 * (by removing any triple whose subject is also an object of 058 * the starting (or context) URI, but that approach tends to delete 059 * too many triples from the triplestore. This command does 060 * not remove blank nodes. 061 */ 062 final StringBuilder query = new StringBuilder(); 063 064 query.append(ProcessorUtils.deleteWhere(subject, namedGraph)); 065 query.append(";\n"); 066 query.append(ProcessorUtils.deleteWhere(subject + "/fcr:export?format=jcr/xml", namedGraph)); 067 068 in.setBody("update=" + encode(query.toString(), "UTF-8")); 069 in.setHeader(Exchange.HTTP_METHOD, "POST"); 070 in.setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded"); 071 } 072 073}