Added: stanbol/trunk/entityhub/web/clerezza/pom.xml URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/clerezza/pom.xml?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/clerezza/pom.xml (added) +++ stanbol/trunk/entityhub/web/clerezza/pom.xml Mon Dec 16 09:18:20 2013 @@ -0,0 +1,178 @@ +<?xml version="1.0"?> + <!-- + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the + NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF + licenses this file to You 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.stanbol</groupId> + <artifactId>apache-stanbol-entityhub</artifactId> + <version>1.0.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + + <artifactId>org.apache.stanbol.entityhub.web.clerezza</artifactId> + <packaging>bundle</packaging> + + <name>Apache Stanbol Entityhub Web: Clerezza Model Serializer</name> + <description>Provides RDF serializers for the Clerezza Model implementation</description> + + <licenses> + <license> + <name>Apache Software License, Version 2.0</name> + <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> + <distribution>repo</distribution> + <comments>A business-friendly OSS license</comments> + </license> + </licenses> + + <scm> + <connection> + scm:svn:http://svn.apache.org/repos/asf/stanbol/trunk/entityhub/web/clerezza + </connection> + <developerConnection> + scm:svn:https://svn.apache.org/repos/asf/stanbol/trunk/entityhub/web/clerezza + </developerConnection> + <url>http://stanbol.apache.org</url> + </scm> + + <build> + <!-- make it an OSGi bundle --> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Import-Package> + * + </Import-Package> + <Private-Package> + org.apache.stanbol.entityhub.web.writer.clerezza; version=${project.version} + </Private-Package> + </instructions> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + </plugin> + </plugins> + </build> + + <dependencies> + + <!-- dependencies on other Stanbol modules --> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.commons.web.base</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.commons.indexedgraph</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <!-- Jersey and Freemarker --> + <!-- dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.commons.web.viewable</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency --> + + <!-- dependencies on other Entityhub modules --> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.servicesapi</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <!-- dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.core</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency --> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.model.clerezza</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.query.clerezza</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.ldpath</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.jersey</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + + + <!-- LD Path --> + <!-- imported via entityhub.ldpath + <dependency> + <groupId>at.newmedialab.ldpath</groupId> + <artifactId>ldpath-api</artifactId> + </dependency> + <dependency> + <groupId>at.newmedialab.ldpath</groupId> + <artifactId>ldpath-core-bundle</artifactId> + </dependency> --> + + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + + + <!-- Servlet API for handling ServletContext --> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + </dependency> + + <!-- OSGi tax --> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + + <!-- for tests --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project>
Added: stanbol/trunk/entityhub/web/clerezza/src/license/THIRD-PARTY.properties URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/clerezza/src/license/THIRD-PARTY.properties?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/clerezza/src/license/THIRD-PARTY.properties (added) +++ stanbol/trunk/entityhub/web/clerezza/src/license/THIRD-PARTY.properties Mon Dec 16 09:18:20 2013 @@ -0,0 +1,29 @@ +# Generated by org.codehaus.mojo.license.AddThirdPartyMojo +#------------------------------------------------------------------------------- +# Already used licenses in project : +# - Apache Software License +# - Apache Software License, Version 2.0 +# - BSD License +# - Common Development And Distribution License (CDDL), Version 1.0 +# - Common Development And Distribution License (CDDL), Version 1.1 +# - Common Public License, Version 1.0 +# - Eclipse Public License, Version 1.0 +# - GNU General Public License (GPL), Version 2 with classpath exception +# - GNU Lesser General Public License (LGPL) +# - GNU Lesser General Public License (LGPL), Version 2.1 +# - ICU License +# - MIT License +# - New BSD License +# - Public Domain License +# - Similar to Apache License but with the acknowledgment clause removed +#------------------------------------------------------------------------------- +# Please fill the missing licenses for dependencies : +# +# +#Tue Jan 22 15:36:38 CET 2013 +asm--asm--3.1=BSD License +javax.servlet--servlet-api--2.5=Common Development And Distribution License (CDDL), Version 1.0 +org.codehaus.jettison--jettison--1.3=The Apache Software License, Version 2.0 +org.osgi--org.osgi.compendium--4.1.0=The Apache Software License, Version 2.0 +org.osgi--org.osgi.core--4.1.0=The Apache Software License, Version 2.0 +xerces--xercesImpl--2.7.1=The Apache Software License, Version 2.0 Added: stanbol/trunk/entityhub/web/clerezza/src/main/java/org/apache/stanbol/entityhub/web/writer/clerezza/ClerezzaModelWriter.java URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/clerezza/src/main/java/org/apache/stanbol/entityhub/web/writer/clerezza/ClerezzaModelWriter.java?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/clerezza/src/main/java/org/apache/stanbol/entityhub/web/writer/clerezza/ClerezzaModelWriter.java (added) +++ stanbol/trunk/entityhub/web/clerezza/src/main/java/org/apache/stanbol/entityhub/web/writer/clerezza/ClerezzaModelWriter.java Mon Dec 16 09:18:20 2013 @@ -0,0 +1,305 @@ +package org.apache.stanbol.entityhub.web.writer.clerezza; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; + +import org.apache.clerezza.rdf.core.LiteralFactory; +import org.apache.clerezza.rdf.core.MGraph; +import org.apache.clerezza.rdf.core.Triple; +import org.apache.clerezza.rdf.core.TripleCollection; +import org.apache.clerezza.rdf.core.TypedLiteral; +import org.apache.clerezza.rdf.core.UriRef; +import org.apache.clerezza.rdf.core.impl.TripleImpl; +import org.apache.clerezza.rdf.core.serializedform.Serializer; +import org.apache.clerezza.rdf.core.serializedform.SupportedFormat; +import org.apache.clerezza.rdf.ontologies.RDF; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.ReferenceCardinality; +import org.apache.felix.scr.annotations.Service; +import org.apache.stanbol.commons.indexedgraph.IndexedMGraph; +import org.apache.stanbol.commons.namespaceprefix.NamespacePrefixService; +import org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation; +import org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory; +import org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList; +import org.apache.stanbol.entityhub.servicesapi.defaults.NamespaceEnum; +import org.apache.stanbol.entityhub.servicesapi.model.Entity; +import org.apache.stanbol.entityhub.servicesapi.model.Representation; +import org.apache.stanbol.entityhub.servicesapi.model.rdf.RdfResourceEnum; +import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery; +import org.apache.stanbol.entityhub.servicesapi.query.QueryResultList; +import org.apache.stanbol.entityhub.web.ModelWriter; +import org.apache.stanbol.entityhub.web.fieldquery.FieldQueryToJsonUtils; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(immediate=true) +@Service +public class ClerezzaModelWriter implements ModelWriter { + + private final Logger log = LoggerFactory.getLogger(ClerezzaModelWriter.class); + + /** + * {@link MediaType} instance for {@link SupportedFormat#TURTLE} + */ + public static final MediaType TURTLE_TYPE = MediaType.valueOf(SupportedFormat.TURTLE); + /** + * {@link MediaType} instance for <code>application/ld+json</code> + */ + public static final MediaType JSONLD_TYPE = MediaType.valueOf("application/ld+json"); + /** + * {@link MediaType} instance for {@link SupportedFormat#N3} + */ + public static final MediaType N3_TYPE = MediaType.valueOf(SupportedFormat.N3); + /** + * {@link MediaType} instance for {@link SupportedFormat#RDF_JSON} + */ + public static final MediaType RDF_JSON_TYPE = MediaType.valueOf(SupportedFormat.RDF_JSON); + /** + * {@link MediaType} instance for {@link SupportedFormat#RDF_XML} + */ + public static final MediaType RDF_XML_TYPE = MediaType.valueOf(SupportedFormat.RDF_XML); + /** + * {@link MediaType} instance for {@link SupportedFormat#X_TURTLE} + */ + public static final MediaType X_TURTLE_TYPE = MediaType.valueOf(SupportedFormat.X_TURTLE); + /** + * Read-only list of the supported RDF formats + */ + public static final List<MediaType> SUPPORTED_RDF_TYPES = Collections.unmodifiableList( + Arrays.asList(TURTLE_TYPE, JSONLD_TYPE, N3_TYPE, RDF_JSON_TYPE, RDF_XML_TYPE, X_TURTLE_TYPE)); + + //some Concepts and Relations we use to represent Entities + private final static UriRef FOAF_DOCUMENT = new UriRef(NamespaceEnum.foaf+"Document"); + private final static UriRef FOAF_PRIMARY_TOPIC = new UriRef(NamespaceEnum.foaf+"primaryTopic"); + private final static UriRef FOAF_PRIMARY_TOPIC_OF = new UriRef(NamespaceEnum.foaf+"isPrimaryTopicOf"); + private final static UriRef SIGN_SITE = new UriRef(RdfResourceEnum.site.getUri()); +// private final static UriRef ENTITY_TYPE = new UriRef(RdfResourceEnum.Entity.getUri()); + private final static RdfValueFactory valueFactory = RdfValueFactory.getInstance(); + /** + * The URI used for the query result list (static for all responses) + */ + private static final UriRef QUERY_RESULT_LIST = new UriRef(RdfResourceEnum.QueryResultSet.getUri()); + /** + * The property used for all results + */ + private static final UriRef QUERY_RESULT = new UriRef(RdfResourceEnum.queryResult.getUri()); + /** + * The property used for the JSON serialised FieldQuery (STANBOL-298) + */ + private static final UriRef FIELD_QUERY = new UriRef(RdfResourceEnum.query.getUri()); + + /** + * This Serializer only supports UTF-8 + */ + public static final String CHARSET = Charset.forName("UTF-8").toString(); + + /** + * The literal factory used (currently {@link LiteralFactory#getInstance()}, + * but we might use a custom one for Stanbol therefore it is better to + * have it as a field + */ + static final LiteralFactory literalFactory = LiteralFactory.getInstance(); + + /** + * The Clerezza {@link Serializer} service + */ + @Reference + protected Serializer ser; + + @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY) + protected NamespacePrefixService nsPrefixService; + + @Override + public Class<? extends Representation> getNativeType() { + return RdfRepresentation.class; + } + + @Override + public List<MediaType> supportedMediaTypes() { + return SUPPORTED_RDF_TYPES; + } + + @Override + public MediaType getBestMediaType(MediaType mediaType) { + for(MediaType supported : SUPPORTED_RDF_TYPES){ + if(supported.isCompatible(mediaType)){ + return supported; + } + } + return null; + } + + @Override + public void write(Representation rep, OutputStream out, MediaType mediaType) throws WebApplicationException, + IOException { + writeRdf(toRDF(rep), out, mediaType); + } + + @Override + public void write(Entity entity, OutputStream out, MediaType mediaType) throws WebApplicationException, + IOException { + writeRdf(toRDF(entity),out,mediaType); + + } + + @Override + public void write(QueryResultList<?> result, OutputStream out, MediaType mediaType) throws WebApplicationException, + IOException { + MGraph queryRdf = toRDF(result); + //we need also to the JSON formatted FieldQuery as a literal to the + //RDF data. + FieldQuery query = result.getQuery(); + if(query != null){ + try { + JSONObject fieldQueryJson = FieldQueryToJsonUtils.toJSON(query, + nsPrefixService); + if(fieldQueryJson != null){ + //add the triple with the fieldQuery + queryRdf.add(new TripleImpl(QUERY_RESULT_LIST, FIELD_QUERY, + literalFactory.createTypedLiteral(fieldQueryJson.toString()))); + } + } catch (JSONException e) { + log.warn(String.format("Unable to serialize Fieldquery '%s' to JSON! " + + "Query response will not contain the serialized query.", + query),e); + } + } + //now serialise the data + writeRdf(queryRdf,out,mediaType); + } + + /** + * @param tc + * @param out + * @param mediaType + */ + private void writeRdf(TripleCollection tc, OutputStream out, MediaType mediaType) { + String charset = mediaType.getParameters().get("charset"); + if(charset == null){ + charset = ModelWriter.DEFAULT_CHARSET; + } + if(!CHARSET.equalsIgnoreCase(charset)){ + log.warn("Unsupported Charset {} requested (will use {})!",charset,CHARSET); + } + ser.serialize(out, tc , new StringBuilder(mediaType.getType()) + .append('/').append(mediaType.getSubtype()).toString()); + } + + private MGraph toRDF(Representation representation) { + MGraph graph = new IndexedMGraph(); + addRDFTo(graph, representation); + return graph; + } + + private void addRDFTo(MGraph graph, Representation representation) { + graph.addAll(valueFactory.toRdfRepresentation(representation).getRdfGraph()); + } + + private TripleCollection toRDF(Entity entity) { + MGraph graph = new IndexedMGraph(); + addRDFTo(graph, entity); + return graph; + } + + private void addRDFTo(MGraph graph, Entity entity) { + addRDFTo(graph, entity.getRepresentation()); + addRDFTo(graph, entity.getMetadata()); + //now add some triples that represent the Sign + addEntityTriplesToGraph(graph, entity); + } + + + /** + * Adds the Triples that represent the Sign to the parsed graph. Note that + * this method does not add triples for the representation. However it adds + * the triple (sign,singRepresentation,representation) + * + * @param graph the graph to add the triples + * @param sign the sign + */ + private void addEntityTriplesToGraph(MGraph graph, Entity sign) { + UriRef id = new UriRef(sign.getId()); + UriRef metaId = new UriRef(sign.getMetadata().getId()); + //add the FOAF triples between metadata and content + graph.add(new TripleImpl(id, FOAF_PRIMARY_TOPIC_OF, metaId)); + graph.add(new TripleImpl(metaId, FOAF_PRIMARY_TOPIC, metaId)); + graph.add(new TripleImpl(metaId, RDF.type, FOAF_DOCUMENT)); + //add the site to the metadata + //TODO: this should be the HTTP URI and not the id of the referenced site + TypedLiteral siteName = literalFactory.createTypedLiteral(sign.getSite()); + graph.add(new TripleImpl(metaId, SIGN_SITE, siteName)); + + } + + private MGraph toRDF(QueryResultList<?> resultList) { + final MGraph resultGraph; + Class<?> type = resultList.getType(); + if (String.class.isAssignableFrom(type)) { + resultGraph = new IndexedMGraph(); //create a new Graph + for (Object result : resultList) { + //add a triple to each reference in the result set + resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, new UriRef(result.toString()))); + } + } else { + //first determine the type of the resultList + final boolean isSignType; + if (Representation.class.isAssignableFrom(type)) { + isSignType = false; + } else if (Representation.class.isAssignableFrom(type)) { + isSignType = true; + } else { + //incompatible type -> throw an Exception + throw new IllegalArgumentException("Parsed type " + type + " is not supported"); + } + //special treatment for RdfQueryResultList for increased performance + if (resultList instanceof RdfQueryResultList) { + resultGraph = ((RdfQueryResultList) resultList).getResultGraph(); + if (isSignType) { //if we build a ResultList for Signs, that we need to do more things + //first remove all triples representing results + Iterator<Triple> resultTripleIt = resultGraph.filter(QUERY_RESULT_LIST, QUERY_RESULT, null); + while (resultTripleIt.hasNext()) { + resultTripleIt.next(); + resultTripleIt.remove(); + } + //now add the Sign specific triples and add result triples + //to the Sign IDs + for (Object result : resultList) { + UriRef signId = new UriRef(((Entity) result).getId()); + addEntityTriplesToGraph(resultGraph, (Entity) result); + resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, signId)); + } + } + } else { //any other implementation of the QueryResultList interface + resultGraph = new IndexedMGraph(); //create a new graph + if (Representation.class.isAssignableFrom(type)) { + for (Object result : resultList) { + UriRef resultId; + if (!isSignType) { + addRDFTo(resultGraph, (Representation) result); + resultId = new UriRef(((Representation) result).getId()); + } else { + addRDFTo(resultGraph, (Entity) result); + resultId = new UriRef(((Entity) result).getId()); + } + //Note: In case of Representation this Triple points to + // the representation. In case of Signs it points to + // the sign. + resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, resultId)); + } + } + } + } + return resultGraph; + } +} Added: stanbol/trunk/entityhub/web/clerezza/src/main/resources/OSGI-INF/metatype/metatype.properties URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/clerezza/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/clerezza/src/main/resources/OSGI-INF/metatype/metatype.properties (added) +++ stanbol/trunk/entityhub/web/clerezza/src/main/resources/OSGI-INF/metatype/metatype.properties Mon Dec 16 09:18:20 2013 @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + + +org.apache.stanbol.entityhub.web.writer.clerezza.ClerzzaModelWriter.name=Apache Stanbol Entityhub IO ModelWriter for Clerezza +org.apache.stanbol.entityhub.web.writer.clerezza.ClerzzaModelWriter.description=ModelWirter for various RDF formats \ +based on Apache Clerezza. Provides native support for serializing the Clerezza Model implementation. Propchange: stanbol/trunk/entityhub/web/sesame/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Mon Dec 16 09:18:20 2013 @@ -0,0 +1,7 @@ +.project + +target + +.settings + +.classpath Added: stanbol/trunk/entityhub/web/sesame/pom.xml URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/sesame/pom.xml?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/sesame/pom.xml (added) +++ stanbol/trunk/entityhub/web/sesame/pom.xml Mon Dec 16 09:18:20 2013 @@ -0,0 +1,152 @@ +<?xml version="1.0"?> + <!-- + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the + NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF + licenses this file to You 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.stanbol</groupId> + <artifactId>apache-stanbol-entityhub</artifactId> + <version>1.0.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + + <artifactId>org.apache.stanbol.entityhub.web.sesame</artifactId> + <packaging>bundle</packaging> + + <name>Apache Stanbol Entityhub Web: Sesame Model Serializer</name> + <description>Provides RDF serializers based on the Sesame Model implementation</description> + + <licenses> + <license> + <name>Apache Software License, Version 2.0</name> + <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> + <distribution>repo</distribution> + <comments>A business-friendly OSS license</comments> + </license> + </licenses> + + <scm> + <connection> + scm:svn:http://svn.apache.org/repos/asf/stanbol/trunk/entityhub/web/sesame + </connection> + <developerConnection> + scm:svn:https://svn.apache.org/repos/asf/stanbol/trunk/entityhub/web/sesame + </developerConnection> + <url>http://stanbol.apache.org</url> + </scm> + + <build> + <!-- make it an OSGi bundle --> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Import-Package> + * + </Import-Package> + <Private-Package> + org.apache.stanbol.entityhub.web.writer.sesame; version=${project.version} + </Private-Package> + </instructions> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + </plugin> + </plugins> + </build> + + <dependencies> + + <!-- dependencies on other Stanbol modules --> + + <!-- dependencies on other Entityhub modules --> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.servicesapi</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.model.sesame</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.query.sesame</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.ldpath</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.stanbol</groupId> + <artifactId>org.apache.stanbol.entityhub.jersey</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + + + <!-- Sesame IO --> + <dependency> + <groupId>org.openrdf.sesame</groupId> + <artifactId>sesame-rio-api</artifactId> + </dependency> + + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + </dependency> + + + <!-- Servlet API for handling ServletContext --> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + </dependency> + + <!-- OSGi tax --> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + + <!-- for tests --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> Added: stanbol/trunk/entityhub/web/sesame/src/license/THIRD-PARTY.properties URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/sesame/src/license/THIRD-PARTY.properties?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/sesame/src/license/THIRD-PARTY.properties (added) +++ stanbol/trunk/entityhub/web/sesame/src/license/THIRD-PARTY.properties Mon Dec 16 09:18:20 2013 @@ -0,0 +1,29 @@ +# Generated by org.codehaus.mojo.license.AddThirdPartyMojo +#------------------------------------------------------------------------------- +# Already used licenses in project : +# - Apache Software License +# - Apache Software License, Version 2.0 +# - BSD License +# - Common Development And Distribution License (CDDL), Version 1.0 +# - Common Development And Distribution License (CDDL), Version 1.1 +# - Common Public License, Version 1.0 +# - Eclipse Public License, Version 1.0 +# - GNU General Public License (GPL), Version 2 with classpath exception +# - GNU Lesser General Public License (LGPL) +# - GNU Lesser General Public License (LGPL), Version 2.1 +# - ICU License +# - MIT License +# - New BSD License +# - Public Domain License +# - Similar to Apache License but with the acknowledgment clause removed +#------------------------------------------------------------------------------- +# Please fill the missing licenses for dependencies : +# +# +#Tue Jan 22 15:36:38 CET 2013 +asm--asm--3.1=BSD License +javax.servlet--servlet-api--2.5=Common Development And Distribution License (CDDL), Version 1.0 +org.codehaus.jettison--jettison--1.3=The Apache Software License, Version 2.0 +org.osgi--org.osgi.compendium--4.1.0=The Apache Software License, Version 2.0 +org.osgi--org.osgi.core--4.1.0=The Apache Software License, Version 2.0 +xerces--xercesImpl--2.7.1=The Apache Software License, Version 2.0 Added: stanbol/trunk/entityhub/web/sesame/src/main/java/org/apache/stanbol/entityhub/web/writer/sesame/SesameModelWriter.java URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/sesame/src/main/java/org/apache/stanbol/entityhub/web/writer/sesame/SesameModelWriter.java?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/sesame/src/main/java/org/apache/stanbol/entityhub/web/writer/sesame/SesameModelWriter.java (added) +++ stanbol/trunk/entityhub/web/sesame/src/main/java/org/apache/stanbol/entityhub/web/writer/sesame/SesameModelWriter.java Mon Dec 16 09:18:20 2013 @@ -0,0 +1,265 @@ +package org.apache.stanbol.entityhub.web.writer.sesame; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; + +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.ReferenceCardinality; +import org.apache.felix.scr.annotations.Service; +import org.apache.stanbol.commons.namespaceprefix.NamespacePrefixService; +import org.apache.stanbol.entityhub.model.sesame.RdfRepresentation; +import org.apache.stanbol.entityhub.model.sesame.RdfValueFactory; +import org.apache.stanbol.entityhub.servicesapi.defaults.NamespaceEnum; +import org.apache.stanbol.entityhub.servicesapi.model.Entity; +import org.apache.stanbol.entityhub.servicesapi.model.Representation; +import org.apache.stanbol.entityhub.servicesapi.model.rdf.RdfResourceEnum; +import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery; +import org.apache.stanbol.entityhub.servicesapi.query.QueryResultList; +import org.apache.stanbol.entityhub.web.ModelWriter; +import org.apache.stanbol.entityhub.web.fieldquery.FieldQueryToJsonUtils; +import org.apache.stanbol.entityhub.yard.sesame.SesameQueryResultList; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; +import org.openrdf.model.Literal; +import org.openrdf.model.Model; +import org.openrdf.model.URI; +import org.openrdf.model.ValueFactory; +import org.openrdf.model.impl.LinkedHashModel; +import org.openrdf.model.impl.ValueFactoryImpl; +import org.openrdf.rio.RDFFormat; +import org.openrdf.rio.RDFHandlerException; +import org.openrdf.rio.Rio; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(immediate=true) +@Service +public class SesameModelWriter implements ModelWriter { + + private final Logger log = LoggerFactory.getLogger(SesameModelWriter.class); + + public static final MediaType TURTLE = MediaType.valueOf("text/turtle"); + public static final MediaType JSONLD = MediaType.valueOf("application/ld+json"); + public static final MediaType N3 = MediaType.valueOf("text/rdf+n3"); + public static final MediaType N_TRIPLE = MediaType.valueOf("text/rdf+nt"); + public static final MediaType RDF_JSON = MediaType.valueOf("application/rdf+json"); + public static final MediaType RDF_XML = MediaType.valueOf("application/rdf+xml"); + public static final MediaType X_TURTLE = MediaType.valueOf("application/x-turtle"); + + public static final List<MediaType> SUPPORTED_RDF_TYPES = Collections.unmodifiableList( + Arrays.asList(TURTLE, JSONLD, N3, N_TRIPLE, RDF_JSON, RDF_XML, X_TURTLE)); + + private final static RdfValueFactory valueFactory = RdfValueFactory.getInstance(); + private final static ValueFactory sesameFactory = ValueFactoryImpl.getInstance(); + + private final static URI FOAF_DOCUMENT = sesameFactory.createURI(NamespaceEnum.foaf+"Document"); + private final static URI FOAF_PRIMARY_TOPIC = sesameFactory.createURI(NamespaceEnum.foaf+"primaryTopic"); + private final static URI FOAF_PRIMARY_TOPIC_OF = sesameFactory.createURI(NamespaceEnum.foaf+"isPrimaryTopicOf"); + private final static URI RDF_TYPE = sesameFactory.createURI(NamespaceEnum.rdf+"type"); + private final static URI EH_SIGN_SITE = sesameFactory.createURI(RdfResourceEnum.site.getUri()); + + /** + * The URI used for the query result list (static for all responses) + */ + private static final URI QUERY_RESULT_LIST = sesameFactory.createURI(RdfResourceEnum.QueryResultSet.getUri()); + /** + * The property used for all results + */ + private static final URI QUERY_RESULT = sesameFactory.createURI(RdfResourceEnum.queryResult.getUri()); + /** + * The property used for the JSON serialised FieldQuery (STANBOL-298) + */ + private static final URI FIELD_QUERY = sesameFactory.createURI(RdfResourceEnum.query.getUri()); + + + @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY) + NamespacePrefixService nsPrefixService; + + @Override + public Class<? extends Representation> getNativeType() { + return RdfRepresentation.class; + } + + @Override + public List<MediaType> supportedMediaTypes() { + return SUPPORTED_RDF_TYPES; + } + + @Override + public MediaType getBestMediaType(MediaType mediaType) { + for(MediaType supported : SUPPORTED_RDF_TYPES){ + if(supported.isCompatible(mediaType)){ + return supported; + } + } + return null; + } + + @Override + public void write(Representation rep, OutputStream out, MediaType mediaType) throws WebApplicationException, + IOException { + writeRdf(toRDF(rep), out, mediaType); + } + + @Override + public void write(Entity entity, OutputStream out, MediaType mediaType) throws WebApplicationException, + IOException { + writeRdf(toRDF(entity), out, mediaType); + } + + @Override + public void write(QueryResultList<?> result, OutputStream out, MediaType mediaType) throws WebApplicationException, + IOException { + Model queryRdf = toRDF(result); + //we need also to the JSON formatted FieldQuery as a literal to the + //RDF data. + FieldQuery query = result.getQuery(); + if(query != null){ + try { + JSONObject fieldQueryJson = FieldQueryToJsonUtils.toJSON(query, + nsPrefixService); + if(fieldQueryJson != null){ + //add the triple with the fieldQuery + queryRdf.add(QUERY_RESULT_LIST, FIELD_QUERY, + sesameFactory.createLiteral(fieldQueryJson.toString())); + } + } catch (JSONException e) { + log.warn(String.format("Unable to serialize Fieldquery '%s' to JSON! " + + "Query response will not contain the serialized query.", + query),e); + } + } + //now serialise the data + writeRdf(queryRdf,out,mediaType); + + } + + /** + * @param data + * @param out + * @param mediaType + */ + private void writeRdf(Model data, OutputStream out, MediaType mediaType) { + RDFFormat rdfFormat = Rio.getWriterFormatForMIMEType(mediaType.toString()); + if(rdfFormat == null){ + throw new IllegalStateException("JAX-RS called for unsupported mediaType '" + + mediaType +"'! If this is a valid RDF type this indicates a missing " + + "Sesame Serializer implementation. Otherwise please report this " + + "as a bug for the Stanbol Issue Tracker."); + } + try { + Rio.write(data, out, rdfFormat); + } catch (RDFHandlerException e) { + throw new WebApplicationException("Unable to serialize QueryResultList with requested Format '" + + rdfFormat +"'!", e); + } + } + + private Model toRDF(Representation representation) { + return valueFactory.toRdfRepresentation(representation).getModel(); + } + + private void addRDFTo(Model graph, Representation representation) { + graph.addAll(valueFactory.toRdfRepresentation(representation).getModel()); + } + + private Model toRDF(Entity entity) { + Model graph = new LinkedHashModel(); + addRDFTo(graph, entity); + return graph; + } + + private void addRDFTo(Model graph, Entity entity) { + addRDFTo(graph, entity.getRepresentation()); + addRDFTo(graph, entity.getMetadata()); + //now add some triples that represent the Sign + addEntityTriplesToGraph(graph, entity); + } + + + /** + * Adds the Triples that represent the Sign to the parsed graph. Note that + * this method does not add triples for the representation. However it adds + * the triple (sign,singRepresentation,representation) + * + * @param graph the graph to add the triples + * @param sign the sign + */ + private void addEntityTriplesToGraph(Model graph, Entity sign) { + URI id = sesameFactory.createURI(sign.getId()); + URI metaId = sesameFactory.createURI(sign.getMetadata().getId()); + //add the FOAF triples between metadata and content + graph.add(id, FOAF_PRIMARY_TOPIC_OF, metaId); + graph.add(metaId, FOAF_PRIMARY_TOPIC, metaId); + graph.add(metaId, RDF_TYPE, FOAF_DOCUMENT); + //add the site to the metadata + //TODO: this should be the HTTP URI and not the id of the referenced site + Literal siteName = sesameFactory.createLiteral(sign.getSite()); + graph.add(metaId, EH_SIGN_SITE, siteName); + + } + + private Model toRDF(QueryResultList<?> resultList) { + final Model resultGraph; + Class<?> type = resultList.getType(); + if (String.class.isAssignableFrom(type)) { + resultGraph = new LinkedHashModel(); //create a new Graph + for (Object result : resultList) { + //add a triple to each reference in the result set + resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, sesameFactory.createURI(result.toString())); + } + } else { + //first determine the type of the resultList + final boolean isSignType; + if (Representation.class.isAssignableFrom(type)) { + isSignType = false; + } else if (Representation.class.isAssignableFrom(type)) { + isSignType = true; + } else { + //incompatible type -> throw an Exception + throw new IllegalArgumentException("Parsed type " + type + " is not supported"); + } + //special treatment for RdfQueryResultList for increased performance + if (resultList instanceof SesameQueryResultList) { + resultGraph = ((SesameQueryResultList) resultList).getModel(); + if (isSignType) { //if we build a ResultList for Signs, we need to do more things + //first remove all triples representing results + resultGraph.filter(null, QUERY_RESULT, null).clear(); + //now add the Sign specific triples and add result triples + //to the Sign IDs + for (Object result : resultList) { + URI signId = sesameFactory.createURI(((Entity) result).getId()); + addEntityTriplesToGraph(resultGraph, (Entity) result); + resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, signId); + } + } + } else { //any other implementation of the QueryResultList interface + resultGraph = new LinkedHashModel(); //create a new graph + if (Representation.class.isAssignableFrom(type)) { + for (Object result : resultList) { + URI resultId; + if (!isSignType) { + addRDFTo(resultGraph, (Representation) result); + resultId = sesameFactory.createURI(((Representation) result).getId()); + } else { + addRDFTo(resultGraph, (Entity) result); + resultId = sesameFactory.createURI(((Entity) result).getId()); + } + //Note: In case of Representation this Triple points to + // the representation. In case of Signs it points to + // the sign. + resultGraph.add(QUERY_RESULT_LIST, QUERY_RESULT, resultId); + } + } + } + } + return resultGraph; + } +} Added: stanbol/trunk/entityhub/web/sesame/src/main/resources/OSGI-INF/metatype/metatype.properties URL: http://svn.apache.org/viewvc/stanbol/trunk/entityhub/web/sesame/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1551134&view=auto ============================================================================== --- stanbol/trunk/entityhub/web/sesame/src/main/resources/OSGI-INF/metatype/metatype.properties (added) +++ stanbol/trunk/entityhub/web/sesame/src/main/resources/OSGI-INF/metatype/metatype.properties Mon Dec 16 09:18:20 2013 @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + + +org.apache.stanbol.entityhub.web.writer.sesame.SesameModelWriter.name=Apache Stanbol Entityhub IO ModelWriter for Clerezza +org.apache.stanbol.entityhub.web.writer.sesame.ClerzzaModelWriter.description=ModelWirter for various RDF formats \ +based on Apache Clerezza. Provides native support for serializing the Clerezza Model implementation.
