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