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 #76
Open
berezovskyi
wants to merge
5
commits into
master
Choose a base branch
from
b-universalprovider_new
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c48526
New UniversalResourceSingleProvider to support more RDF formats
berezovskyi f830142
Reformat
berezovskyi 3e3db61
Merge branch 'master' into b-universalprovider_new
berezovskyi 518a9b9
Disable Oracle JDK from the build matrix
berezovskyi fff1d8c
Ignore a failing test
berezovskyi 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: java | ||
|
||
jdk: | ||
- oraclejdk8 | ||
- openjdk8 | ||
- openjdk11 | ||
|
||
|
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
123 changes: 123 additions & 0 deletions
123
...jena-provider/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,123 @@ | ||
/*!***************************************************************************** | ||
* Copyright (c) 2019 Andrew Berezovskyi. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v. 1.0 which accompanies this distribution. | ||
* | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* Contributors: | ||
* | ||
* Andrew Berezovskyi - initial implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lyo.oslc4j.provider.jena; | ||
|
||
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.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; | ||
|
||
/** | ||
* TODO | ||
* | ||
* @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.unmarshal(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)); | ||
} | ||
} | ||
|
||
} |
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.
should be an abstract class.