-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathXSLTWriterBase.java
More file actions
362 lines (321 loc) · 13.6 KB
/
XSLTWriterBase.java
File metadata and controls
362 lines (321 loc) · 13.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright 2020 Martynas Jusevičius <martynas@atomgraph.com>.
*
* 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 com.atomgraph.linkeddatahub.writer;
import com.atomgraph.client.util.DataManager;
import com.atomgraph.client.vocabulary.AC;
import com.atomgraph.linkeddatahub.writer.factory.xslt.XsltExecutableSupplier;
import com.atomgraph.linkeddatahub.model.auth.Agent;
import com.atomgraph.linkeddatahub.vocabulary.ACL;
import com.atomgraph.linkeddatahub.vocabulary.LDH;
import com.atomgraph.linkeddatahub.vocabulary.LDHT;
import com.atomgraph.linkeddatahub.vocabulary.Google;
import com.atomgraph.linkeddatahub.vocabulary.ORCID;
import com.atomgraph.linkeddatahub.vocabulary.LAPP;
import com.atomgraph.client.vocabulary.LDT;
import com.atomgraph.core.util.Link;
import com.atomgraph.core.vocabulary.SD;
import com.atomgraph.linkeddatahub.server.security.AuthorizationContext;
import com.atomgraph.linkeddatahub.vocabulary.FOAF;
import com.atomgraph.linkeddatahub.vocabulary.LDHC;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.SecurityContext;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XdmValue;
import net.sf.saxon.s9api.XsltExecutable;
import org.apache.http.HttpHeaders;
import org.apache.jena.ontology.ObjectProperty;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.RDFLanguages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* XSLT writer subclass with LinkedDataHub specific parameters.
*
* @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
*/
public abstract class XSLTWriterBase extends com.atomgraph.client.writer.XSLTWriterBase
{
private static final Logger log = LoggerFactory.getLogger(XSLTWriterBase.class);
private static final Set<String> NAMESPACES;
/** The relative URL of the RDF file with localized labels */
public static final String TRANSLATIONS_PATH = "static/com/atomgraph/linkeddatahub/xsl/bootstrap/2.3.2/translations.rdf";
static
{
NAMESPACES = new HashSet<>();
NAMESPACES.add(AC.NS);
NAMESPACES.add(LDHT.NS);
}
@Context SecurityContext securityContext;
@Inject com.atomgraph.linkeddatahub.Application system;
@Inject jakarta.inject.Provider<Optional<com.atomgraph.linkeddatahub.apps.model.Application>> application;
@Inject jakarta.inject.Provider<DataManager> dataManager;
@Inject jakarta.inject.Provider<XsltExecutableSupplier> xsltExecSupplier;
@Inject jakarta.inject.Provider<ContainerRequestContext> crc;
@Inject jakarta.inject.Provider<Optional<AuthorizationContext>> authorizationContext;
private final MessageDigest messageDigest;
/**
* Constructs XSLT writer.
*
* @param xsltExec compiled XSLT stylesheet
* @param ontModelSpec ontology specification
* @param dataManager RDF data manager
* @param messageDigest message digest
*/
public XSLTWriterBase(XsltExecutable xsltExec, OntModelSpec ontModelSpec, DataManager dataManager, MessageDigest messageDigest)
{
super(xsltExec, ontModelSpec, dataManager); // this DataManager will be unused as we override getDataManager() with the injected (subclassed) one
this.messageDigest = messageDigest;
}
@Override
public <T extends XdmValue> Map<QName, XdmValue> getParameters(MultivaluedMap<String, Object> headerMap) throws TransformerException
{
Map<QName, XdmValue> params = super.getParameters(headerMap);
try
{
params.put(new QName("ldh", LDH.requestUri.getNameSpace(), LDH.requestUri.getLocalName()), new XdmAtomicValue(getRequestURI()));
params.put(new QName("lapp", LAPP.Context.getNameSpace(), LAPP.Context.getLocalName()),
getXsltExecutable().getProcessor().newDocumentBuilder().build(getSource(getSystem().getContextModel())));
Optional<com.atomgraph.linkeddatahub.apps.model.Application> appOpt = getApplication().get();
if (appOpt.isPresent())
{
com.atomgraph.linkeddatahub.apps.model.Application app = appOpt.get();
if (log.isDebugEnabled()) log.debug("Passing $lapp:Application to XSLT: <{}>", app);
params.put(new QName("ldt", LDT.base.getNameSpace(), LDT.base.getLocalName()), new XdmAtomicValue(app.getBaseURI()));
params.put(new QName("lapp", LAPP.origin.getNameSpace(), LAPP.origin.getLocalName()), new XdmAtomicValue(app.getOriginURI()));
params.put(new QName("ldt", LDT.ontology.getNameSpace(), LDT.ontology.getLocalName()), new XdmAtomicValue(URI.create(app.getOntology().getURI())));
}
URI endpointURI = getLinkURI(headerMap, SD.endpoint);
if (endpointURI != null) params.put(new QName("sd", SD.endpoint.getNameSpace(), SD.endpoint.getLocalName()), new XdmAtomicValue(endpointURI));
String forShapeURI = getUriInfo().getQueryParameters().getFirst(LDH.forShape.getLocalName());
if (forShapeURI != null) params.put(new QName("ldh", LDH.forShape.getNameSpace(), LDH.forShape.getLocalName()), new XdmAtomicValue(URI.create(forShapeURI)));
if (getSecurityContext() != null && getSecurityContext().getUserPrincipal() instanceof Agent)
{
Agent agent = (Agent)getSecurityContext().getUserPrincipal();
if (log.isDebugEnabled()) log.debug("Passing $foaf:Agent to XSLT: <{}>", agent);
Source source = getSource(agent.getModel());
URI agentURI = URI.create(agent.getURI());
URI agentDocUri = new URI(agentURI.getScheme(), agentURI.getSchemeSpecificPart(), null); // strip the fragment identifier
source.setSystemId(agentDocUri.toString()); // URI accessible via document-uri($foaf:Agent)
params.put(new QName("acl", ACL.agent.getNameSpace(), ACL.agent.getLocalName()),
new XdmAtomicValue(URI.create(agent.getURI())));
params.put(new QName("foaf", FOAF.Agent.getNameSpace(), FOAF.Agent.getLocalName()),
getXsltExecutable().getProcessor().newDocumentBuilder().build(source));
}
if (getAuthorizationContext().get().isPresent())
params.put(new QName("acl", ACL.mode.getNameSpace(), ACL.mode.getLocalName()),
XdmValue.makeSequence(getAuthorizationContext().get().get().getModeURIs()));
if (getHttpHeaders().getRequestHeader(HttpHeaders.REFERER) != null)
{
URI referer = URI.create(getHttpHeaders().getRequestHeader(HttpHeaders.REFERER).get(0));
if (log.isDebugEnabled()) log.debug("Passing $Referer URI to XSLT: {}", referer);
params.put(new QName("", "", "Referer"), new XdmAtomicValue(referer)); // TO-DO: move to ac: namespace
}
params.put(new QName("ldhc", LDHC.enableWebIDSignUp.getNameSpace(), LDHC.enableWebIDSignUp.getLocalName()), new XdmAtomicValue(getSystem().isEnableWebIDSignUp()));
if (getSystem().getProperty(Google.clientID.getURI()) != null)
params.put(new QName("google", Google.clientID.getNameSpace(), Google.clientID.getLocalName()), new XdmAtomicValue((String)getSystem().getProperty(Google.clientID.getURI())));
if (getSystem().getProperty(ORCID.clientID.getURI()) != null)
params.put(new QName("orcid", ORCID.clientID.getNameSpace(), ORCID.clientID.getLocalName()), new XdmAtomicValue((String)getSystem().getProperty(ORCID.clientID.getURI())));
return params;
}
catch (IOException | URISyntaxException | SaxonApiException ex)
{
if (log.isErrorEnabled()) log.error("Error reading Source stream");
throw new TransformerException(ex);
}
}
/**
* Override the Web-Client's implementation because LinkedDataHub concatenates <code>Link</code> headers into a single value.
*
* @param headerMap response headers
* @param property rel property
* @return filtered headers
* @see com.atomgraph.linkeddatahub.server.filter.response.ResponseHeadersFilter
*/
@Override
public URI getLinkURI(MultivaluedMap<String, Object> headerMap, ObjectProperty property)
{
if (headerMap.get(jakarta.ws.rs.core.HttpHeaders.LINK) == null) return null;
List<String> linkTokens = Arrays.asList(headerMap.get(jakarta.ws.rs.core.HttpHeaders.LINK).get(0).toString().split(","));
List<URI> baseLinks = linkTokens.stream().
map((String header) ->
{
try
{
return Link.valueOf(header.trim());
}
catch (URISyntaxException ex)
{
if (log.isWarnEnabled()) log.warn("Could not parse Link URI", ex);
return null;
}
}).
filter(link -> link != null && link.getRel() != null && link.getRel().equals(property.getURI())).
map(link -> link.getHref()).
collect(Collectors.toList());
if (!baseLinks.isEmpty()) return baseLinks.get(0);
return null;
}
/**
* Creates stream source from RDF model.
* The model is serialized using the RDF/XML syntax.
*
* @param model RDF model
* @return XML stream source
* @throws IOException I/O error
*/
public StreamSource getSource(Model model) throws IOException
{
if (model == null) throw new IllegalArgumentException("Model cannot be null");
try (ByteArrayOutputStream stream = new ByteArrayOutputStream())
{
model.write(stream, RDFLanguages.RDFXML.getName(), null);
return new StreamSource(new ByteArrayInputStream(stream.toByteArray()));
}
}
/**
* Returns the base URI of the main RDF/XML document being transformed.
*
* @return base URL
*/
@Override
public String getSystemId()
{
return getUriInfo().getRequestUri().toString();
}
/**
* Returns system application.
*
* @return JAX-RS application
*/
public com.atomgraph.linkeddatahub.Application getSystem()
{
return system;
}
@Override
public OntModelSpec getOntModelSpec()
{
return getSystem().getOntModelSpec();
}
/**
* Returns JAX-RS security context.
*
* @return security context
*/
public SecurityContext getSecurityContext()
{
return securityContext;
}
@Override
public DataManager getDataManager()
{
return getDataManagerProvider().get();
}
/**
* Returns a JAX-RS provider for the RDF data manager.
*
* @return provider
*/
public jakarta.inject.Provider<DataManager> getDataManagerProvider()
{
return dataManager;
}
@Override
public String getQuery()
{
if (getUriInfo().getQueryParameters().containsKey(AC.query.getLocalName()))
return getUriInfo().getQueryParameters().getFirst(AC.query.getLocalName());
return null;
}
@Override
public XsltExecutable getXsltExecutable()
{
return xsltExecSupplier.get().get();
}
@Override
public Set<String> getSupportedNamespaces()
{
return NAMESPACES;
}
/**
* Returns a JAX-RS provider for the current application.
*
* @return provider
*/
public jakarta.inject.Provider<Optional<com.atomgraph.linkeddatahub.apps.model.Application>> getApplication()
{
return application;
}
/**
* Returns optional ACL authorizationContext.
*
* @return optional authorizationContext
*/
public jakarta.inject.Provider<Optional<AuthorizationContext>> getAuthorizationContext()
{
return authorizationContext;
}
/**
* Returns message digest.
*
* @return digest
*/
public MessageDigest getMessageDigest()
{
return messageDigest;
}
/**
* Returns request context.
*
* @return request context
*/
public ContainerRequestContext getContainerRequestContext()
{
return crc.get();
}
/**
* Returns the base URI of this LinkedDataHub instance.
* It equals to the base URI of the root dataspace.
*
* @return root context URI
*/
@Override
public URI getContextURI()
{
return getSystem().getBaseURI();
}
}