-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
e828380
commit b7bf354
Showing
20 changed files
with
414 additions
and
351 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
67 changes: 52 additions & 15 deletions
67
java/middleware/registry-middleware/jsonld-conversion/pom.xml
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,22 +1,59 @@ | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.opensaber</groupId> | ||
<artifactId>registry-middleware</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>io.opensaber.middleware</groupId> | ||
<artifactId>jsonld-conversion</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>JSON-LD Conversion</name> | ||
<description>Conversion from rdf to json-ld</description> | ||
|
||
<dependencies> | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.opensaber</groupId> | ||
<artifactId>registry-middleware</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>io.opensaber.middleware</groupId> | ||
<artifactId>jsonld-conversion</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>JSON-LD Conversion</name> | ||
<description>Conversion from rdf to json-ld</description> | ||
|
||
<properties> | ||
<junit.version>4.12</junit.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.opensaber</groupId> | ||
<artifactId>middleware-commons</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.jena</groupId> | ||
<artifactId>jena-arq</artifactId> | ||
<version>3.6.0</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.open-saber</groupId> | ||
<artifactId>jenardf4j</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.rdf4j</groupId> | ||
<artifactId>rdf4j-model</artifactId> | ||
<version>2.2.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.open-saber</groupId> | ||
<artifactId>shaclex</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
62 changes: 62 additions & 0 deletions
62
...sonld-conversion/src/main/java/io/opensaber/registry/middleware/impl/JSONLDConverter.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,62 @@ | ||
package io.opensaber.registry.middleware.impl; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.StringWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.jena.ext.com.google.common.io.ByteStreams; | ||
import org.apache.jena.query.DatasetFactory; | ||
import org.apache.jena.rdf.model.Model; | ||
import org.apache.jena.riot.JsonLDWriteContext; | ||
import org.apache.jena.riot.RDFDataMgr; | ||
import org.apache.jena.riot.WriterDatasetRIOT; | ||
import org.apache.jena.riot.system.PrefixMap; | ||
import org.apache.jena.riot.system.RiotLib; | ||
import org.apache.jena.sparql.core.DatasetGraph; | ||
|
||
import io.opensaber.converters.JenaRDF4J; | ||
import io.opensaber.registry.middleware.BaseMiddleware; | ||
import io.opensaber.registry.middleware.MiddlewareHaltException; | ||
import io.opensaber.registry.middleware.util.Constants; | ||
|
||
/** | ||
* | ||
* @author jyotsna | ||
* | ||
*/ | ||
public class JSONLDConverter implements BaseMiddleware{ | ||
|
||
private static final String INVALID_RDF_DATA = "RDF data is invalid!"; | ||
|
||
public Map<String,Object> execute(Map<String,Object> mapData) throws IOException, MiddlewareHaltException { | ||
Object responseData = mapData.get(Constants.RESPONSE_ATTRIBUTE); | ||
if(responseData != null){ | ||
if(responseData instanceof org.eclipse.rdf4j.model.Model){ | ||
Model jenaEntityModel = JenaRDF4J.asJenaModel((org.eclipse.rdf4j.model.Model)responseData); | ||
DatasetGraph g = DatasetFactory.create(jenaEntityModel).asDatasetGraph(); | ||
JsonLDWriteContext ctx = new JsonLDWriteContext(); | ||
InputStream is = this.getClass().getClassLoader().getResourceAsStream("frame.json"); | ||
String fileString = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8); | ||
ctx.setFrame(fileString); | ||
WriterDatasetRIOT w = RDFDataMgr.createDatasetWriter(org.apache.jena.riot.RDFFormat.JSONLD_FRAME_FLAT) ; | ||
PrefixMap pm = RiotLib.prefixMap(g); | ||
String base = null; | ||
StringWriter sWriterJena = new StringWriter(); | ||
w.write(sWriterJena, g, pm, base, ctx) ; | ||
String jenaJSON = sWriterJena.toString(); | ||
mapData.put(Constants.RESPONSE_ATTRIBUTE, jenaJSON); | ||
} else{ | ||
throw new MiddlewareHaltException(INVALID_RDF_DATA); | ||
} | ||
} | ||
|
||
return mapData; | ||
} | ||
|
||
public Map<String,Object> next(Map<String,Object> mapData) throws IOException { | ||
return new HashMap<String,Object>(); | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
...-conversion/src/main/java/io/opensaber/registry/middleware/impl/RdfToJsonldConverter.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
...-conversion/src/test/java/io/opensaber/registry/middleware/impl/JSONLDConversionTest.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,71 @@ | ||
package io.opensaber.registry.middleware.impl; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.eclipse.rdf4j.model.Model; | ||
import org.eclipse.rdf4j.model.impl.TreeModel; | ||
import org.eclipse.rdf4j.model.util.ModelBuilder; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import io.opensaber.converters.JenaRDF4J; | ||
import io.opensaber.registry.middleware.BaseMiddleware; | ||
import io.opensaber.registry.middleware.MiddlewareHaltException; | ||
import io.opensaber.registry.middleware.util.Constants; | ||
import io.opensaber.validators.shex.shaclex.ShaclexValidator; | ||
|
||
public class JSONLDConversionTest { | ||
|
||
private static final String TEACHER_JSONLD = "teacher.jsonld"; | ||
private static final String FORMAT = "JSON-LD"; | ||
|
||
@Rule | ||
public ExpectedException expectedEx = ExpectedException.none(); | ||
|
||
Map<String, Object> mapData; | ||
private BaseMiddleware m; | ||
private Model rdfModel; | ||
|
||
private void setup() throws IOException, URISyntaxException{ | ||
m = new JSONLDConverter(); | ||
String jsonLDData = Paths.get(getPath(TEACHER_JSONLD)).toString(); | ||
Path filePath = Paths.get(jsonLDData); | ||
String jsonld = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8); | ||
org.apache.jena.rdf.model.Model model = ShaclexValidator.parse(jsonld, FORMAT); | ||
rdfModel = JenaRDF4J.asRDF4JModel(model); | ||
} | ||
|
||
private URI getPath(String file) throws URISyntaxException { | ||
return this.getClass().getClassLoader().getResource(file).toURI(); | ||
} | ||
|
||
@Test | ||
public void testHaltIfInvalidRdf() throws MiddlewareHaltException, IOException, URISyntaxException{ | ||
expectedEx.expect(MiddlewareHaltException.class); | ||
expectedEx.expectMessage("RDF data is invalid!"); | ||
setup(); | ||
mapData = new HashMap<String,Object>(); | ||
mapData.put(Constants.RESPONSE_ATTRIBUTE, "{}"); | ||
m.execute(mapData); | ||
} | ||
|
||
@Test | ||
public void testValidRdf() throws URISyntaxException, IOException, MiddlewareHaltException{ | ||
setup(); | ||
mapData = new HashMap<String,Object>(); | ||
mapData.put(Constants.RESPONSE_ATTRIBUTE, rdfModel); | ||
m.execute(mapData); | ||
} | ||
|
||
} |
Oops, something went wrong.