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.http.entity.ContentType.parse; 022import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel; 023import static org.apache.jena.riot.RDFDataMgr.read; 024import static org.apache.jena.riot.RDFLanguages.contentTypeToLang; 025import static org.apache.camel.Exchange.CONTENT_TYPE; 026import static org.apache.camel.Exchange.HTTP_METHOD; 027import static org.fcrepo.camel.FcrepoHeaders.FCREPO_NAMED_GRAPH; 028import static org.fcrepo.camel.processor.ProcessorUtils.deleteWhere; 029import static org.fcrepo.camel.processor.ProcessorUtils.getSubjectUri; 030import static org.fcrepo.camel.processor.ProcessorUtils.insertData; 031 032import java.io.ByteArrayOutputStream; 033import java.io.InputStream; 034import java.io.IOException; 035 036import org.apache.jena.rdf.model.Model; 037import org.apache.camel.Exchange; 038import org.apache.camel.Message; 039import org.apache.camel.NoSuchHeaderException; 040import org.apache.camel.Processor; 041 042/** 043 * Represents a processor for creating the sparql-update message to 044 * be passed to an external triplestore. 045 * 046 * @author Aaron Coburn 047 * @since Nov 8, 2014 048 */ 049public class SparqlUpdateProcessor implements Processor { 050 /** 051 * Define how the message is processed. 052 * 053 * @param exchange the current camel message exchange 054 */ 055 public void process(final Exchange exchange) throws IOException, NoSuchHeaderException { 056 057 final Message in = exchange.getIn(); 058 059 final ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream(); 060 final String namedGraph = in.getHeader(FCREPO_NAMED_GRAPH, "", String.class); 061 final Model model = createDefaultModel(); 062 final String subject = getSubjectUri(exchange); 063 064 read(model, in.getBody(InputStream.class), 065 contentTypeToLang(parse(in.getHeader(CONTENT_TYPE, String.class)).getMimeType())); 066 067 model.write(serializedGraph, "N-TRIPLE"); 068 069 in.setBody("update=" + encode(deleteWhere(subject, namedGraph) + ";\n" + 070 insertData(serializedGraph.toString("UTF-8"), namedGraph), "UTF-8")); 071 072 in.setHeader(HTTP_METHOD, "POST"); 073 in.setHeader(CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8"); 074 } 075}