Skip to content

Commit 28cc05b

Browse files
committed
ldclient json provider module
1 parent 68e9eb1 commit 28cc05b

9 files changed

Lines changed: 1369 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>ec.edu.cedia.redi</groupId>
5+
6+
<artifactId>ldclient-provider-json</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>LDClient Provider: JSON Resource Access</name>
11+
<url>https://github.com/ucuenca/ldclient-provider-json</url>
12+
13+
<description>
14+
Provides base functionality for all Linked Data resources offering data in various JSON formats. Specific
15+
data providers / parsers can derive from these base classes.
16+
</description>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<maven.compiler.source>1.7</maven.compiler.source>
21+
<maven.compiler.target>1.7</maven.compiler.target>
22+
<json.path.version>2.4.0</json.path.version>
23+
<marmotta.version>3.3.0</marmotta.version>
24+
<github.maven.repository>file:///home/cuent/NetBeansProjects/ldclient-provider-json</github.maven.repository>
25+
</properties>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.apache.marmotta</groupId>
29+
<artifactId>ldclient-api</artifactId>
30+
<version>${marmotta.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.marmotta</groupId>
34+
<artifactId>ldclient-core</artifactId>
35+
<version>${marmotta.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.jayway.jsonpath</groupId>
39+
<artifactId>json-path</artifactId>
40+
<version>${json.path.version}</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<distributionManagement>
45+
<repository>
46+
<id>maven-repository</id>
47+
<url>${github.maven.repository}</url>
48+
</repository>
49+
</distributionManagement>
50+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package ec.edu.cedia.redi.ldclient.provider.json;
19+
20+
import com.jayway.jsonpath.Configuration;
21+
import com.jayway.jsonpath.JsonPath;
22+
import com.jayway.jsonpath.Option;
23+
import com.jayway.jsonpath.ReadContext;
24+
import ec.edu.cedia.redi.ldclient.provider.json.mappers.JsonPathValueMapper;
25+
import java.io.InputStream;
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.Map;
29+
import org.apache.marmotta.ldclient.api.provider.DataProvider;
30+
import org.apache.marmotta.ldclient.exception.DataRetrievalException;
31+
import org.apache.marmotta.ldclient.services.provider.AbstractHttpProvider;
32+
import org.openrdf.model.Model;
33+
import org.openrdf.model.Resource;
34+
import org.openrdf.model.URI;
35+
import org.openrdf.model.Value;
36+
import org.openrdf.model.ValueFactory;
37+
import org.openrdf.model.impl.ValueFactoryImpl;
38+
import org.openrdf.model.vocabulary.RDF;
39+
40+
/**
41+
*
42+
* @author Xavier Sumba <xavier.sumba93@ucuenca.ec>
43+
*/
44+
public abstract class AbstractJSONDataProvider extends AbstractHttpProvider implements DataProvider {
45+
46+
protected static final String CHARSET = "UTF-8";
47+
48+
protected abstract List<String> getTypes(URI resource);
49+
50+
protected abstract Map<String, JsonPathValueMapper> getMappings(String resource, String requestUrl);
51+
52+
protected Configuration getConfiguration() {
53+
return Configuration.defaultConfiguration()
54+
.addOptions(Option.ALWAYS_RETURN_LIST)
55+
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)
56+
.addOptions(Option.SUPPRESS_EXCEPTIONS);
57+
}
58+
59+
@Override
60+
protected List<String> parseResponse(String resource, String requestUrl, Model triples, InputStream in, String contentType) throws DataRetrievalException {
61+
ReadContext ctx = JsonPath.parse(in, getConfiguration());
62+
ValueFactory vf = ValueFactoryImpl.getInstance();
63+
64+
URI subject = vf.createURI(resource);
65+
for (Map.Entry<String, JsonPathValueMapper> mapping : getMappings(resource, requestUrl).entrySet()) {
66+
URI predicate = vf.createURI(mapping.getKey());
67+
68+
List a = ctx.read(mapping.getValue().getPath());
69+
for (Object o : a) {
70+
String value;
71+
if (o == null) {
72+
continue;
73+
} else {
74+
value = String.valueOf(o);
75+
}
76+
77+
List<Value> objects = mapping.getValue().map(resource, value, vf);
78+
for (Value object : objects) {
79+
triples.add(subject, predicate, object);
80+
}
81+
}
82+
}
83+
84+
URI ptype = vf.createURI(RDF.NAMESPACE + "type");
85+
for (String typeUri : getTypes(subject)) {
86+
Resource type_resource = vf.createURI(typeUri);
87+
triples.add(vf.createStatement(subject, ptype, type_resource));
88+
}
89+
return Collections.emptyList();
90+
}
91+
92+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package ec.edu.cedia.redi.ldclient.provider.json.mappers;
7+
8+
import com.jayway.jsonpath.Predicate;
9+
import java.util.Collections;
10+
import java.util.List;
11+
import org.openrdf.model.Value;
12+
import org.openrdf.model.ValueFactory;
13+
import org.openrdf.model.vocabulary.XMLSchema;
14+
15+
/**
16+
*
17+
* @author Xavier Sumba <xavier.sumba93@ucuenca.ec>
18+
*/
19+
public class JsonPathLiteralMapper extends JsonPathValueMapper {
20+
21+
protected String datatype;
22+
23+
public JsonPathLiteralMapper(String path, String datatype, Predicate... filters) {
24+
super(path, filters);
25+
this.datatype = datatype;
26+
}
27+
28+
public JsonPathLiteralMapper(String path, Predicate... filters) {
29+
super(path, filters);
30+
}
31+
32+
@Override
33+
public List<Value> map(String resourceUri, String selectedValue, ValueFactory factory) {
34+
Value value;
35+
if (datatype != null) {
36+
value = factory.createLiteral(selectedValue.trim(), factory.createURI(XMLSchema.NAMESPACE, datatype));
37+
} else {
38+
value = factory.createLiteral(selectedValue.trim());
39+
}
40+
return Collections.singletonList(value);
41+
}
42+
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package ec.edu.cedia.redi.ldclient.provider.json.mappers;
19+
20+
import com.jayway.jsonpath.Predicate;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import org.openrdf.model.Value;
24+
import org.openrdf.model.ValueFactory;
25+
26+
/**
27+
*
28+
* @author Xavier Sumba <xavier.sumba93@ucuenca.ec>
29+
*/
30+
public class JsonPathURIMapper extends JsonPathValueMapper {
31+
32+
public JsonPathURIMapper(String path, Predicate... filters) {
33+
super(path, filters);
34+
}
35+
36+
@Override
37+
public List<Value> map(String resourceUri, String selectedValue, ValueFactory factory) {
38+
return Collections.singletonList(factory.createURI(selectedValue));
39+
}
40+
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package ec.edu.cedia.redi.ldclient.provider.json.mappers;
7+
8+
import com.jayway.jsonpath.Predicate;
9+
import org.apache.marmotta.ldclient.api.provider.ValueMapper;
10+
11+
/**
12+
*
13+
* @author Xavier Sumba <xavier.sumba93@ucuenca.ec>
14+
*/
15+
public abstract class JsonPathValueMapper implements ValueMapper {
16+
17+
private final String path;
18+
19+
protected JsonPathValueMapper(String path, Predicate... filters) {
20+
this.path = path;
21+
}
22+
23+
public String getPath() {
24+
return path;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)