-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathApplication.java
299 lines (270 loc) · 13.7 KB
/
Application.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
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
/**
* Copyright 2013 Martynas Jusevičius <[email protected]>
*
* 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.client;
import org.apache.jena.ontology.OntDocumentManager;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.LocationMapper;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.ServletConfig;
import jakarta.ws.rs.core.Context;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.RDFWriterRegistry;
import com.atomgraph.client.locator.PrefixMapper;
import com.atomgraph.client.mapper.ClientErrorExceptionMapper;
import com.atomgraph.client.mapper.NotFoundExceptionMapper;
import com.atomgraph.client.mapper.RiotExceptionMapper;
import com.atomgraph.client.model.impl.ProxyResourceBase;
import com.atomgraph.core.provider.QueryParamProvider;
import com.atomgraph.core.io.ResultSetProvider;
import com.atomgraph.core.io.UpdateRequestProvider;
import com.atomgraph.client.util.DataManager;
import com.atomgraph.client.util.DataManagerImpl;
import com.atomgraph.client.util.XsltResolver;
import com.atomgraph.client.vocabulary.AC;
import com.atomgraph.client.writer.ModelXSLTWriter;
import com.atomgraph.client.writer.ResultSetXSLTWriter;
import com.atomgraph.client.writer.function.Construct;
import com.atomgraph.client.writer.function.ConstructForClass;
import com.atomgraph.client.writer.function.UUID;
import com.atomgraph.core.client.LinkedDataClient;
import com.atomgraph.core.io.ModelProvider;
import com.atomgraph.core.vocabulary.A;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import jakarta.servlet.ServletContext;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atomgraph.core.io.DatasetProvider;
import com.atomgraph.core.io.QueryProvider;
import com.atomgraph.core.mapper.BadGatewayExceptionMapper;
import com.atomgraph.core.riot.RDFLanguages;
import com.atomgraph.core.riot.lang.RDFPostReaderFactory;
import java.util.HashMap;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.reasoner.Reasoner;
import org.apache.jena.reasoner.rulesys.RDFSRuleReasonerFactory;
import org.apache.jena.riot.RDFParserRegistry;
import org.apache.jena.vocabulary.ReasonerVocabulary;
import org.glassfish.jersey.client.ClientConfig;
import static org.glassfish.jersey.client.ClientProperties.FOLLOW_REDIRECTS;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.HttpMethodOverrideFilter;
/**
* AtomGraph Client JAX-RS application base class.
* Can be extended or used as it is (needs to be registered in web.xml).
* Needs to register JAX-RS root resource classes and providers.
*
* @author Martynas Jusevičius {@literal <[email protected]>}
* @see <a href="https://jakarta.ee/specifications/restful-ws/3.0/apidocs/jakarta/ws/rs/core/application">JAX-RS Application</a>
* @see <a href="http://docs.oracle.com/cd/E24329_01/web.1211/e24983/configure.htm#CACEAEGG">Packaging the RESTful Web Service Application Using web.xml With Application Subclass</a>
*/
public class Application extends ResourceConfig
{
private static final Logger log = LoggerFactory.getLogger(Application.class);
private final MediaTypes mediaTypes;
private final Client client;
private final DataManager dataManager;
private final Source stylesheet;
private final Boolean cacheStylesheet;
private final OntModelSpec ontModelSpec;
private final Processor xsltProc = new Processor(false);
private final XsltExecutable xsltExec;
/**
* Initializes root resource classes and provider singletons
* @param servletConfig
* @throws java.net.URISyntaxException
* @throws java.io.IOException
*/
public Application(@Context ServletConfig servletConfig) throws URISyntaxException, IOException
{
this(new MediaTypes(), getClient(new ClientConfig()),
servletConfig.getServletContext().getInitParameter(A.maxGetRequestSize.getURI()) != null ? Integer.valueOf(servletConfig.getServletContext().getInitParameter(A.maxGetRequestSize.getURI())) : null,
servletConfig.getServletContext().getInitParameter(A.preemptiveAuth.getURI()) != null ? Boolean.parseBoolean(servletConfig.getServletContext().getInitParameter(A.preemptiveAuth.getURI())) : false,
getDataManager(new PrefixMapper(servletConfig.getServletContext().getInitParameter(AC.prefixMapping.getURI()) != null ? servletConfig.getServletContext().getInitParameter(AC.prefixMapping.getURI()) : null),
com.atomgraph.client.Application.getClient(new ClientConfig()),
new MediaTypes(),
servletConfig.getServletContext().getInitParameter(A.cacheModelLoads.getURI()) != null ? Boolean.parseBoolean(servletConfig.getServletContext().getInitParameter(A.cacheModelLoads.getURI())) : false,
servletConfig.getServletContext().getInitParameter(A.preemptiveAuth.getURI()) != null ? Boolean.parseBoolean(servletConfig.getServletContext().getInitParameter(A.preemptiveAuth.getURI())) : false,
servletConfig.getServletContext().getInitParameter(AC.resolvingUncached.getURI()) != null ? Boolean.parseBoolean(servletConfig.getServletContext().getInitParameter(AC.resolvingUncached.getURI())) : false),
getSource(servletConfig.getServletContext(), servletConfig.getServletContext().getInitParameter(AC.stylesheet.getURI()) != null ? servletConfig.getServletContext().getInitParameter(AC.stylesheet.getURI()) : null),
servletConfig.getServletContext().getInitParameter(AC.cacheStylesheet.getURI()) != null ? Boolean.parseBoolean(servletConfig.getServletContext().getInitParameter(AC.cacheStylesheet.getURI())) : false,
servletConfig.getServletContext().getInitParameter(AC.resolvingUncached.getURI()) != null ? Boolean.parseBoolean(servletConfig.getServletContext().getInitParameter(AC.resolvingUncached.getURI())) : null
);
}
public Application(final MediaTypes mediaTypes, final Client client, final Integer maxGetRequestSize, final boolean preemptiveAuth,
final DataManager dataManager, final Source stylesheet, final boolean cacheStylesheet, final boolean resolvingUncached)
{
this.mediaTypes = mediaTypes;
this.client = client;
this.stylesheet = stylesheet;
this.cacheStylesheet = cacheStylesheet;
this.dataManager = dataManager;
OntDocumentManager.getInstance().setFileManager((FileManager)dataManager);
if (log.isDebugEnabled()) log.debug("OntDocumentManager.getInstance().getFileManager(): {}", OntDocumentManager.getInstance().getFileManager());
OntModelSpec rdfsReasonerSpec = new OntModelSpec(OntModelSpec.OWL_MEM);
Resource reasonerConfig = ModelFactory.createDefaultModel().
createResource().
addProperty(ReasonerVocabulary.PROPsetRDFSLevel, "simple");
Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().
create(reasonerConfig);
//reasoner.setDerivationLogging(true);
//reasoner.setParameter(ReasonerVocabulary.PROPtraceOn, Boolean.TRUE);
rdfsReasonerSpec.setReasoner(reasoner);
this.ontModelSpec = rdfsReasonerSpec;
// add RDF/POST serialization
RDFLanguages.register(RDFLanguages.RDFPOST);
RDFParserRegistry.registerLangTriples(RDFLanguages.RDFPOST, new RDFPostReaderFactory());
// register plain RDF/XML writer as default
RDFWriterRegistry.register(Lang.RDFXML, RDFFormat.RDFXML_PLAIN);
xsltProc.registerExtensionFunction(new UUID());
xsltProc.registerExtensionFunction(new Construct(xsltProc));
xsltProc.registerExtensionFunction(new ConstructForClass(xsltProc, OntDocumentManager.getInstance()));
try
{
XsltCompiler xsltComp = xsltProc.newXsltCompiler();
xsltComp.setURIResolver(new XsltResolver(LocationMapper.get(), new HashMap<>(), LinkedDataClient.create(client, mediaTypes), false, false, true));
xsltExec = xsltComp.compile(stylesheet);
}
catch (SaxonApiException ex)
{
if (log.isErrorEnabled()) log.error("System XSLT stylesheet error: {}", ex);
throw new WebApplicationException(ex);
}
}
/**
* Initializes (post construction) DataManager, its LocationMapper and Locators, and Context
*
* @see com.atomgraph.client.util.DataManager
* @see com.atomgraph.client.locator.PrefixMapper
* @see <a href="http://jena.apache.org/documentation/javadoc/jena/org/apache/jena/util/FileManager.html">FileManager</a>
* @see <a href="http://jena.apache.org/documentation/javadoc/jena/org/apache/jena/util/LocationMapper.html">LocationMapper</a>
*/
@PostConstruct
public void init()
{
register(ProxyResourceBase.class);
register(new HttpMethodOverrideFilter());
register(new ModelProvider());
register(new ResultSetProvider());
register(new QueryParamProvider());
register(new UpdateRequestProvider());
register(NotFoundExceptionMapper.class);
register(RiotExceptionMapper.class);
register(ClientErrorExceptionMapper.class);
register(BadGatewayExceptionMapper.class);
register(new ModelXSLTWriter(getXsltExecutable(), getOntModelSpec(), getDataManager())); // writes (X)HTML responses
register(new ResultSetXSLTWriter(getXsltExecutable(), getOntModelSpec(), getDataManager())); // writes (X)HTML responses
register(new AbstractBinder()
{
@Override
protected void configure()
{
bind(new MediaTypes()).to(MediaTypes.class);
}
});
register(new AbstractBinder()
{
@Override
protected void configure()
{
bind(getClient()).to(Client.class);
}
});
}
/**
* Provides XML source from filename
*
* @param servletContext
* @param path stylesheet filename
* @return XML source
* @throws FileNotFoundException
* @throws URISyntaxException
* @throws java.net.MalformedURLException
* @see <a href="http://docs.oracle.com/javase/6/docs/api/javax/xml/transform/Source.html">Source</a>
*/
public static Source getSource(ServletContext servletContext, String path) throws URISyntaxException, IOException
{
if (servletContext == null) throw new IllegalArgumentException("servletContext name cannot be null");
if (path == null) throw new IllegalArgumentException("XML file name cannot be null");
if (log.isDebugEnabled()) log.debug("Resource paths used to load Source: {} from filename: {}", servletContext.getResourcePaths("/"), path);
URL xsltUrl = servletContext.getResource(path);
if (xsltUrl == null) throw new FileNotFoundException("File '" + path + "' not found");
String xsltUri = xsltUrl.toURI().toString();
if (log.isDebugEnabled()) log.debug("XSLT stylesheet URI: {}", xsltUri);
return new StreamSource(xsltUri);
}
public static Client getClient(ClientConfig clientConfig)
{
// clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(FOLLOW_REDIRECTS, Boolean.TRUE);
clientConfig.register(new ModelProvider());
clientConfig.register(new DatasetProvider());
clientConfig.register(new ResultSetProvider());
clientConfig.register(new QueryProvider());
clientConfig.register(new UpdateRequestProvider());
Client client = ClientBuilder.newClient(clientConfig);
//if (log.isDebugEnabled()) client.addFilter(new LoggingFilter(System.out));
return client;
}
public static DataManager getDataManager(final LocationMapper mapper, final Client client, final MediaTypes mediaTypes, final boolean cacheModelLoads, final boolean preemptiveAuth, final boolean resolvingUncached)
{
return new DataManagerImpl(mapper, new HashMap<>(), LinkedDataClient.create(client, mediaTypes), cacheModelLoads, preemptiveAuth, resolvingUncached);
}
public MediaTypes getMediaTypes()
{
return mediaTypes;
}
public Client getClient()
{
return client;
}
public DataManager getDataManager()
{
return dataManager;
}
public Source getStylesheet()
{
return stylesheet;
}
public Boolean isCacheStylesheet()
{
return cacheStylesheet;
}
public OntModelSpec getOntModelSpec()
{
return ontModelSpec;
}
public XsltExecutable getXsltExecutable()
{
return xsltExec;
}
}