-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
RDFTriplifier.java
134 lines (120 loc) · 4.96 KB
/
RDFTriplifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) 2024 SPARQL Anything Contributors @ http://github.com/sparql-anything
*
* Licensed 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.
*/
package io.github.sparqlanything.rdf;
import io.github.sparqlanything.model.FacadeXGraphBuilder;
import io.github.sparqlanything.model.HTTPHelper;
import io.github.sparqlanything.model.Triplifier;
import io.github.sparqlanything.model.TriplifierHTTPException;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.protocol.HTTP;
import com.google.common.collect.Sets;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFLanguages;
import org.apache.jena.sparql.core.DatasetGraph;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Properties;
import java.util.Set;
@io.github.sparqlanything.model.annotations.Triplifier
public class RDFTriplifier implements Triplifier {
// private static Logger logger = LoggerFactory.getLogger(RDFTriplifier.class);
public static Lang getRDFLang(Properties properties, String url, Header contentType) {
Lang lang = null;
// Version from HTTP content type response
if (contentType != null) {
// After issue https://github.com/SPARQL-Anything/sparql.anything/issues/317
if (contentType.getValue().indexOf(';') != -1) {
lang = RDFLanguages.contentTypeToLang(contentType.getValue().substring(0, contentType.getValue().indexOf(';')));
} else {
lang = RDFLanguages.contentTypeToLang(contentType.getValue());
}
}
// Version from expected content type (HTTP accept header)
if (lang == null && properties.containsKey(HTTPHelper.HTTPHEADER_PREFIX + "accept")) {
lang = RDFLanguages.contentTypeToLang(properties.getProperty(HTTPHelper.HTTPHEADER_PREFIX + "accept"));
}
if (lang == null) {
// Version from location file extension
lang = RDFLanguages.filenameToLang(url);
}
if (lang == null) {
log.warn("Failed to determine RDF lang");
}
// XXX The CLI registers JSON with the JSON-LD parser to support cases where
// the served content-type is application/json but the expected content is RDF
// However, the JSON is not a proper LANG, therefore, we rewrite it here
// See #356
// See io.github.sparqlanything.cli.SPARQLAnything.initSPARQLAnythingEngine()
if(lang.getLabel().equals("JSON")){
lang = Lang.JSONLD;
}
log.trace("Lang {}", lang);
return lang;
}
@Override
public void triplify(Properties properties, FacadeXGraphBuilder builder) throws IOException {
URL url = Triplifier.getLocation(properties);
if (url == null) return;
DatasetGraph dg = builder.getDatasetGraph();
log.trace("URL {}", url);
try {
InputStream is;
Header contentType;
if (url.getProtocol().equals("http") || url.getProtocol().equals("https")) {
CloseableHttpResponse response = HTTPHelper.getInputStream(url, properties);
if (!HTTPHelper.isSuccessful(response)) {
log.warn("Request unsuccessful: {}", response.getStatusLine().toString());
if (log.isTraceEnabled()) {
log.trace("Response: {}", response);
log.trace("Response body: {}", IOUtils.toString(response.getEntity().getContent(), Charset.defaultCharset()));
}
throw new TriplifierHTTPException(response.getStatusLine().toString());
}
is = response.getEntity().getContent();
contentType = response.getFirstHeader(HTTP.CONTENT_TYPE);
} else {
is = Triplifier.getInputStream(properties);
contentType = null;
}
Lang l = getRDFLang(properties, url.toString(), contentType);
if (Sets.newHashSet(Lang.RDFXML, Lang.TTL, Lang.NT, Lang.JSONLD, Lang.JSONLD11, Lang.RDFJSON).contains(l)) {
Graph g = dg.getDefaultGraph();
RDFDataMgr.read(g, is, l);
dg.addGraph(NodeFactory.createURI(url.toString()), g);
} else {
RDFDataMgr.read(dg, is, l);
}
} catch (TriplifierHTTPException e) {
log.error("{}", e.getMessage());
throw new IOException(e);
}
}
@Override
public Set<String> getMimeTypes() {
return Sets.newHashSet("application/rdf+thrift", "application/trix+xml", "application/n-quads", "text/trig", "application/owl+xml", "text/turtle", "application/rdf+xml", "application/n-triples", "application/ld+json");
}
@Override
public Set<String> getExtensions() {
return Sets.newHashSet("rdf", "ttl", "nt", "jsonld", "owl", "trig", "nq", "trix", "trdf");
}
}