
org.fcrepo.camel.processor.SparqlInsertProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fcrepo-camel Show documentation
Show all versions of fcrepo-camel Show documentation
Camel Component for interacting with a Fedora Repository
/**
* Copyright 2015 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.camel.processor;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.camel.processor.ProcessorUtils.insertData;
import static org.fcrepo.camel.processor.ProcessorUtils.langFromMimeType;
import static java.net.URLEncoder.encode;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import com.hp.hpl.jena.rdf.model.Model;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.fcrepo.camel.FcrepoHeaders;
/**
* Represents a processor for creating the sparql-update message to
* be passed to an external triplestore.
*
* @author Aaron Coburn
* @since Nov 8, 2014
*/
public class SparqlInsertProcessor implements Processor {
/**
* Define how the message is processed.
*
* @param exchange the current camel message exchange
*/
public void process(final Exchange exchange) throws IOException {
final Message in = exchange.getIn();
final ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
final String subject = ProcessorUtils.getSubjectUri(in);
final String namedGraph = in.getHeader(FcrepoHeaders.FCREPO_NAMED_GRAPH, String.class);
final Model model = createDefaultModel().read(in.getBody(InputStream.class), subject,
langFromMimeType(in.getHeader(Exchange.CONTENT_TYPE, String.class)));
model.write(serializedGraph, "N-TRIPLE");
exchange.getIn().setBody("update=" +
encode(insertData(serializedGraph.toString("UTF-8"), namedGraph), "UTF-8"));
exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy