This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
New UniversalResourceSingleProvider to support more RDF formats #9
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...provider.jena/src/main/java/org/eclipse/lyo/oslc4j/provider/jena/AbstractRdfProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.eclipse.lyo.oslc4j.provider.jena; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. license |
||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import org.apache.jena.rdf.model.Model; | ||
import org.apache.jena.rdf.model.ModelFactory; | ||
import org.apache.jena.rdf.model.RDFReader; | ||
import org.apache.jena.rdf.model.RDFWriter; | ||
import org.apache.jena.riot.Lang; | ||
import org.apache.jena.riot.RDFDataMgr; | ||
import org.apache.jena.riot.RDFFormat; | ||
import org.apache.jena.riot.RDFLanguages; | ||
import org.apache.jena.util.FileUtils; | ||
import org.eclipse.lyo.oslc4j.core.exception.MessageExtractor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Created on 2018-05-27 | ||
* | ||
* @author Andrew Berezovskyi ([email protected]) | ||
* @version $version-stub$ | ||
* @since 0.0.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since 4.0.0 |
||
*/ | ||
public class AbstractRdfProvider { | ||
|
||
private final static Logger log = LoggerFactory.getLogger(AbstractRdfProvider.class); | ||
|
||
protected void writeTo(final boolean queryResult, final Object[] objects, | ||
final MediaType baseMediaType, final MultivaluedMap<String, Object> map, | ||
final OutputStream outputStream) throws WebApplicationException { | ||
String descriptionURI = null; | ||
String responseInfoURI = null; | ||
|
||
if (queryResult) { | ||
throw new IllegalArgumentException("Query Result resources have to be constructed " | ||
+ "before marshalling"); | ||
} | ||
|
||
writeNonQueryObjectsTo(objects, outputStream, baseMediaType); | ||
} | ||
|
||
protected void writeNonQueryObjectsTo(final Object[] objects, final OutputStream outputStream, | ||
final MediaType serializationLanguage) { | ||
try { | ||
final Model model = JenaModelHelper.createJenaModel(objects); | ||
|
||
RDFWriter writer = getRdfWriter(serializationLanguage.toString(), model); | ||
|
||
if (serializationLanguage.equals(FileUtils.langXML) || serializationLanguage.equals( | ||
FileUtils.langXMLAbbrev)) { | ||
// XML (and Jena) default to UTF-8, but many libs default to ASCII, so need | ||
// to set this explicitly | ||
writer.setProperty("showXmlDeclaration", "false"); | ||
String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | ||
outputStream.write(xmlDeclaration.getBytes()); | ||
} | ||
writer.write(model, outputStream, null); | ||
} catch (final Exception exception) { | ||
log.warn(MessageExtractor.getMessage("ErrorSerializingResource"), exception); | ||
// TODO Andrew@2018-03-03: use another exception | ||
throw new WebApplicationException(exception); | ||
} | ||
} | ||
|
||
private RDFWriter getRdfWriter(final String serializationLanguage, final Model model) { | ||
RDFWriter writer = null; | ||
if (serializationLanguage.equals(FileUtils.langXMLAbbrev)) { | ||
// TODO Andrew@2018-05-27: do this for the RDF/XML-ABBREV only iff the users want it | ||
/* Personally, I don't like the idea of maintaining a separate ABBREV writer. I think | ||
we need to start introducing some flags where users can disable legacy functionality | ||
altogether and rely on Jena etc.*/ | ||
writer = new RdfXmlAbbreviatedWriter(); | ||
} else { | ||
final Lang lang = RDFLanguages.nameToLang(serializationLanguage); | ||
// RDFDataMgr.createGraphWriter(mapLang(lang)); | ||
// final Graph graph = model.getGraph(); | ||
writer = model.getWriter(lang.getName()); | ||
// FIXME Andrew@2018-05-27: what's the purpose of name>lang>name conversion? | ||
} | ||
return writer; | ||
} | ||
|
||
private RDFFormat mapLang(final Lang lang) { | ||
// TODO Andrew@2018-05-27: allow configuration from users (compact vs pretty etc) | ||
final RDFFormat rdfFormat = new RDFFormat(lang); | ||
return rdfFormat; | ||
} | ||
|
||
/*=== READER ================================*/ | ||
|
||
protected Object[] readFrom(final Class<?> type, final MediaType mediaType, final InputStream | ||
inputStream) | ||
throws WebApplicationException { | ||
final Model model = ModelFactory.createDefaultModel(); | ||
RDFDataMgr.read( | ||
model, | ||
inputStream, | ||
RDFDataMgr.determineLang(null, mediaType.toString(), null)); | ||
try { | ||
return JenaModelHelper.fromJenaModel(model, type); | ||
} catch (final Exception exception) { | ||
// FIXME Andrew@2018-05-27: we shall just stop the JMH blanket of exceptions craziness | ||
throw new RuntimeException(exception); | ||
// throw new WebApplicationException(exception, buildBadRequestResponse(exception, | ||
// mediaType, | ||
// map)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.ext.Provider; | ||
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType; | ||
|
||
/** | ||
* Created on 2018-03-03 | ||
|
@@ -13,6 +12,6 @@ | |
* @since 0.0.1 | ||
*/ | ||
@Provider | ||
@Produces({OslcMediaType.JSON_LD}) | ||
@Consumes({OslcMediaType.JSON_LD}) | ||
@Produces({"ld+json"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a MIME type and a const |
||
@Consumes({"ld+json"}) | ||
public class OslcJsonLdProvider extends OslcRdfXmlProvider{ } |
80 changes: 80 additions & 0 deletions
80
...a/src/main/java/org/eclipse/lyo/oslc4j/provider/jena/UniversalResourceSingleProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.eclipse.lyo.oslc4j.provider.jena; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. license |
||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyReader; | ||
import javax.ws.rs.ext.MessageBodyWriter; | ||
import javax.ws.rs.ext.Provider; | ||
import org.eclipse.lyo.oslc4j.core.model.IResource; | ||
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType; | ||
|
||
/** | ||
* JMH accepts objects in general but there is a slight conflict in the JAX pipeline with the | ||
* Collections and Arrays, which are technically also Objects. If someone needs raw Objects, they | ||
* can use old Providers. | ||
* | ||
* @author Andrew Berezovskyi ([email protected]) | ||
* @version $version-stub$ | ||
* @since 0.0.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since 4.0.0 |
||
*/ | ||
@Provider | ||
@Produces({OslcMediaType.APPLICATION_JSON_LD, OslcMediaType.TEXT_TURTLE, OslcMediaType | ||
.N_TRIPLES_MIME, OslcMediaType.RDF_JSON_MIME, OslcMediaType.RDF_THRIFT_MIME}) | ||
@Consumes({OslcMediaType.APPLICATION_JSON_LD, OslcMediaType.TEXT_TURTLE, OslcMediaType | ||
.N_TRIPLES_MIME, OslcMediaType.RDF_JSON_MIME, OslcMediaType.RDF_THRIFT_MIME}) | ||
public class UniversalResourceSingleProvider extends AbstractRdfProvider implements | ||
MessageBodyReader<IResource>, MessageBodyWriter<IResource> | ||
{ | ||
|
||
private final int CANNOT_BE_DETERMINED_IN_ADVANCE = -1; | ||
|
||
@Override | ||
public boolean isReadable(final Class<?> type, final Type genericType, | ||
final Annotation[] annotations, final MediaType mediaType) { | ||
// TODO Andrew@2018-05-27: test it | ||
return true; | ||
} | ||
|
||
@Override | ||
public IResource readFrom(final Class<IResource> type, final Type genericType, | ||
final Annotation[] annotations, final MediaType mediaType, | ||
final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) | ||
throws IOException, WebApplicationException { | ||
final Object[] objects = readFrom(type, mediaType, entityStream); | ||
if(objects.length > 1) { | ||
throw new IllegalArgumentException("Can't unmarshal a single resource from a model " | ||
+ "with multiple matching resources"); | ||
} | ||
final IResource resource = (IResource) objects[0]; | ||
return resource; | ||
} | ||
|
||
@Override | ||
public boolean isWriteable(final Class<?> type, final Type genericType, | ||
final Annotation[] annotations, final MediaType mediaType) { | ||
// TODO Andrew@2018-05-27: test | ||
return true; | ||
} | ||
|
||
@Override | ||
public long getSize(final IResource iResource, final Class<?> type, final Type genericType, | ||
final Annotation[] annotations, final MediaType mediaType) { | ||
return CANNOT_BE_DETERMINED_IN_ADVANCE; | ||
} | ||
|
||
@Override | ||
public void writeTo(final IResource iResource, final Class<?> type, final Type genericType, | ||
final Annotation[] annotations, final MediaType mediaType, | ||
final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) | ||
throws IOException, WebApplicationException { | ||
writeNonQueryObjectsTo(new Object[] {iResource}, entityStream, mediaType); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?