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;
021import static org.apache.camel.Exchange.CONTENT_TYPE;
022import static org.apache.camel.Exchange.HTTP_METHOD;
023import static org.fcrepo.camel.FcrepoHeaders.FCREPO_NAMED_GRAPH;
024import static org.fcrepo.camel.processor.ProcessorUtils.deleteWhere;
025import static org.fcrepo.camel.processor.ProcessorUtils.getSubjectUri;
026
027import java.io.IOException;
028
029import org.apache.camel.Exchange;
030import org.apache.camel.Message;
031import org.apache.camel.NoSuchHeaderException;
032import org.apache.camel.Processor;
033
034/**
035 * Represends a message processor that deletes objects from an
036 * external triplestore.
037 *
038 * @author Aaron Coburn
039 * @since Nov 8, 2014
040 */
041public class SparqlDeleteProcessor implements Processor {
042    /**
043     * Define how the message should be processed.
044     *
045     * @param exchange the current camel message exchange
046     */
047    public void process(final Exchange exchange) throws IOException, NoSuchHeaderException {
048
049        final Message in = exchange.getIn();
050        final String namedGraph = in.getHeader(FCREPO_NAMED_GRAPH, "", String.class);
051        final String subject = getSubjectUri(exchange);
052
053        in.setBody("update=" + encode(deleteWhere(subject, namedGraph), "UTF-8"));
054        in.setHeader(HTTP_METHOD, "POST");
055        in.setHeader(CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
056   }
057
058}