Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swapped in clerezza rdf libraries #22

Merged
merged 2 commits into from Dec 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 37 additions & 30 deletions pom.xml
Expand Up @@ -26,11 +26,13 @@
<grizzly.version>2.3.16</grizzly.version>
<httpclient.version>4.3.5</httpclient.version>
<jena.fuseki.version>1.1.0</jena.fuseki.version>
<jena.version>2.12.1</jena.version>
<jersey.version>2.13</jersey.version>
<logback.version>1.1.2</logback.version>
<slf4j.version>1.7.7</slf4j.version>
<spring.version>4.0.7.RELEASE</spring.version>
<clerezza.rdf.jena.serializer.version>0.11</clerezza.rdf.jena.serializer.version>
<clerezza.rdf.jena.parser.version>0.12</clerezza.rdf.jena.parser.version>
<clerezza.rdf.core.version>0.14</clerezza.rdf.core.version>
<!-- plugins -->
<checkstyle.plugin.version>2.12.1</checkstyle.plugin.version>
<!-- values -->
Expand Down Expand Up @@ -103,6 +105,25 @@
<version>${camel.version}</version>
</dependency>

<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.jena.parser</artifactId>
<version>${clerezza.rdf.jena.parser.version}</version>
</dependency>

<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.jena.serializer</artifactId>
<version>${clerezza.rdf.jena.serializer.version}</version>
</dependency>

<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.core</artifactId>
<version>${clerezza.rdf.core.version}</version>
</dependency>


<!-- fcrepo -->
<dependency>
<groupId>org.fcrepo</groupId>
Expand Down Expand Up @@ -162,34 +183,7 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>

<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>${jena.version}</version>
</dependency>

<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>${jena.version}</version>
<scope>test</scope>
</dependency>

<dependency>
Expand All @@ -202,6 +196,7 @@
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>

<!-- logging -->
Expand Down Expand Up @@ -554,8 +549,20 @@
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>org.fcrepo.camel.fcrepo-camel</Bundle-SymbolicName>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Export-Service>org.apache.camel.spi.ComponentResolver;component=fcrepo</Export-Service>
<Export-Package>
org.apache.clerezza.rdf.*,
org.apache.clerezza.utils.*,
com.hp.hpl.jena.*,
org.apache.commons.codec.*,
org.apache.jena.*,
com.fasterxml.jackson.*,
com.github.jsonldjava.*,
org.osgi.service.component,
org.wymiwyg.commons.util.*,
com.ibm.icu.*,
</Export-Package>
</instructions>
</configuration>
</plugin>
Expand Down
Expand Up @@ -17,24 +17,12 @@

import static org.apache.camel.Exchange.HTTP_METHOD;
import static org.apache.camel.Exchange.CONTENT_TYPE;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;

import org.apache.camel.Processor;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.commons.lang3.StringUtils;

import com.hp.hpl.jena.graph.Node_URI;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.StmtIterator;

import java.io.InputStream;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

/**
* Represends a message processor that deletes objects from an
Expand All @@ -51,49 +39,10 @@ public void process(final Exchange exchange) throws IOException {

final Message in = exchange.getIn();
final String subject = ProcessorUtils.getSubjectUri(in);
final Model model = createDefaultModel().read(in.getBody(InputStream.class), null);
final StmtIterator triples = model.listStatements();

// build list of triples to delete
final Set<String> uris = new HashSet<String>();
while ( triples.hasNext() ) {
final Triple triple = triples.next().asTriple();

// add subject uri, if it is part of this object
if ( triple.getSubject().isURI() ) {
final String uri = ((Node_URI)triple.getSubject()).getURI();

if (uriMatches(subject, uri) ) {
uris.add(uri);
}
}

// add object uri, if it is part of this object
if ( triple.getObject().isURI() ) {
final String uri = ((Node_URI)triple.getObject()).getURI();
if (uriMatches(subject, uri) ) {
uris.add(uri);
}
}
}

// build delete commands
final List<String> commands = new ArrayList<String>();
for (final String uri : uris) {
commands.add("DELETE WHERE { <" + uri + "> ?p ?o }");
}

exchange.getIn().setBody(StringUtils.join(commands, ";\n"));
exchange.getIn().setHeader(HTTP_METHOD, "POST");
exchange.getIn().setHeader(CONTENT_TYPE, "application/sparql-update");
}
in.setBody("DELETE WHERE { <" + subject + "> ?p ?o }");
in.setHeader(HTTP_METHOD, "POST");
in.setHeader(CONTENT_TYPE, "application/sparql-update");

private static boolean uriMatches(final String resource, final String candidate) {
// All triples that will match this logic are ones that:
// - have a candidate subject or object that equals the target resource of removal, or
// - have a candidate subject or object that is prefixed with the resource of removal
// (therefore catching all children).
return resource.equals(candidate) || candidate.startsWith(resource + "/")
|| candidate.startsWith(resource + "#");
}
}
}
34 changes: 18 additions & 16 deletions src/main/java/org/fcrepo/camel/processor/SparqlInsertProcessor.java
Expand Up @@ -15,22 +15,22 @@
*/
package org.fcrepo.camel.processor;

import static org.apache.camel.Exchange.HTTP_METHOD;
import static org.apache.camel.Exchange.CONTENT_TYPE;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.apache.camel.Exchange.HTTP_METHOD;

import org.apache.camel.Processor;
import org.apache.camel.Exchange;
import org.apache.camel.Message;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.sparql.modify.request.QuadDataAcc;
import com.hp.hpl.jena.sparql.modify.request.UpdateDataInsert;
import com.hp.hpl.jena.update.UpdateRequest;
import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
import org.apache.clerezza.rdf.core.MGraph;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.jena.parser.JenaParserProvider;
import org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider;

import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

/**
* Represents a processor for creating the sparql-update message to
Expand All @@ -46,15 +46,17 @@ public class SparqlInsertProcessor implements Processor {
public void process(final Exchange exchange) throws IOException {

final Message in = exchange.getIn();
final Model model = createDefaultModel().read(in.getBody(InputStream.class), null, "N-TRIPLE");
final StmtIterator triples = model.listStatements();
final QuadDataAcc add = new QuadDataAcc();
while (triples.hasNext()) {
add.addTriple(triples.nextStatement().asTriple());
}
final UpdateRequest request = new UpdateRequest(new UpdateDataInsert(add));
final ParsingProvider parser = new JenaParserProvider();
final SerializingProvider serializer = new JenaSerializerProvider();
final MGraph graph = new SimpleMGraph();
final String contentType = in.getHeader(Exchange.CONTENT_TYPE, String.class);
final ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();

parser.parse(graph, in.getBody(InputStream.class),
"application/n-triples".equals(contentType) ? "text/rdf+nt" : contentType, null);
serializer.serialize(serializedGraph, graph.getGraph(), "text/rdf+nt");

exchange.getIn().setBody(request.toString());
exchange.getIn().setBody("INSERT DATA { " + serializedGraph.toString("UTF-8") + " }");
exchange.getIn().setHeader(HTTP_METHOD, "POST");
exchange.getIn().setHeader(CONTENT_TYPE, "application/sparql-update");
}
Expand Down
@@ -0,0 +1,66 @@
/**
* Copyright 2014 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 org.apache.camel.Exchange.CONTENT_TYPE;
import static org.apache.camel.Exchange.HTTP_METHOD;

import org.apache.camel.Processor;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
import org.apache.clerezza.rdf.core.MGraph;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.jena.parser.JenaParserProvider;
import org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider;

import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

/**
* 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 SparqlUpdateProcessor implements Processor {
/**
* Define how the message is processed.
*/
public void process(final Exchange exchange) throws IOException {

final Message in = exchange.getIn();
final ParsingProvider parser = new JenaParserProvider();
final SerializingProvider serializer = new JenaSerializerProvider();
final MGraph graph = new SimpleMGraph();
final String contentType = in.getHeader(Exchange.CONTENT_TYPE, String.class);
final ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
final String subject = ProcessorUtils.getSubjectUri(in);

parser.parse(graph, in.getBody(InputStream.class),
"application/n-triples".equals(contentType) ? "text/rdf+nt" : contentType, null);
serializer.serialize(serializedGraph, graph.getGraph(), "text/rdf+nt");

exchange.getIn().setBody("DELETE { <" + subject + "> ?p ?o }\n" +
"INSERT { " + serializedGraph.toString("UTF-8") + " }\n" +
"WHERE { <" + subject + "> ?p ?o }");
exchange.getIn().setHeader(HTTP_METHOD, "POST");
exchange.getIn().setHeader(CONTENT_TYPE, "application/sparql-update");
}
}
Expand Up @@ -89,9 +89,7 @@ public void testDelete() throws IOException, InterruptedException {

// Assertions
resultEndpoint.expectedBodiesReceived(
"DELETE WHERE { <" + base + path + "> ?p ?o };\n" +
"DELETE WHERE { <" + base + path + "/appendix> ?p ?o };\n" +
"DELETE WHERE { <" + base + path + "#appendix2> ?p ?o }");
"DELETE WHERE { <" + base + path + "> ?p ?o }");
resultEndpoint.expectedHeaderReceived(CONTENT_TYPE, "application/sparql-update");
resultEndpoint.expectedHeaderReceived(HTTP_METHOD, "POST");

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/fcrepo/camel/SparqlInsertProcessorTest.java
Expand Up @@ -73,21 +73,25 @@ public void testDescribe() throws IOException, InterruptedException {
final Map<String, Object> headers = new HashMap<>();
headers.put(FCREPO_BASE_URL, base);
headers.put(FCREPO_IDENTIFIER, path);
headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
template.sendBodyAndHeaders(document, headers);

headers.clear();
headers.put(BASE_URL_HEADER_NAME, base);
headers.put(IDENTIFIER_HEADER_NAME, path);
headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
template.sendBodyAndHeaders(document, headers);

headers.clear();
headers.put(BASE_URL_HEADER_NAME, base);
headers.put(FCREPO_IDENTIFIER, path);
headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
template.sendBodyAndHeaders(document, headers);

headers.clear();
headers.put(FCREPO_BASE_URL, base);
headers.put(IDENTIFIER_HEADER_NAME, path);
headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
template.sendBodyAndHeaders(document, headers);

// Confirm that assertions passed
Expand Down