/
GraphStoreImpl.java
317 lines (290 loc) · 10.5 KB
/
GraphStoreImpl.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Copyright 2019 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.server.model.impl;
import com.atomgraph.core.MediaTypes;
import com.atomgraph.core.riot.lang.RDFPostReader;
import static com.atomgraph.linkeddatahub.apps.model.Application.UPLOADS_PATH;
import com.atomgraph.linkeddatahub.model.Service;
import com.atomgraph.linkeddatahub.server.security.AgentContext;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.MessageDigest;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import jakarta.inject.Inject;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Request;
import jakarta.ws.rs.core.SecurityContext;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.Providers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.jena.ontology.Ontology;
import org.apache.jena.rdf.model.Model;
import org.glassfish.jersey.media.multipart.BodyPart;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* LinkedDataHub Graph Store implementation.
* We need to subclass the Core class because we're injecting a subclass of Service.
*
* @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
*/
public abstract class GraphStoreImpl extends com.atomgraph.core.model.impl.GraphStoreImpl
{
private static final Logger log = LoggerFactory.getLogger(GraphStoreImpl.class);
private final UriInfo uriInfo;
private final com.atomgraph.linkeddatahub.apps.model.Application application;
private final Ontology ontology;
private final Service service;
private final Providers providers;
private final com.atomgraph.linkeddatahub.Application system;
private final UriBuilder uploadsUriBuilder;
private final MessageDigest messageDigest;
protected final URI ownerDocURI, secretaryDocURI;
private final SecurityContext securityContext;
private final Optional<AgentContext> agentContext;
/**
* Constructs Graph Store.
*
* @param request current request
* @param uriInfo URI info of the current request
* @param mediaTypes a registry of readable/writable media types
* @param application current application
* @param ontology ontology of the current application
* @param service SPARQL service of the current application
* @param securityContext JAX-RS security context
* @param agentContext authenticated agent's context
* @param providers registry of JAX-RS providers
* @param system system application
*/
@Inject
public GraphStoreImpl(@Context Request request, @Context UriInfo uriInfo, MediaTypes mediaTypes,
com.atomgraph.linkeddatahub.apps.model.Application application, Optional<Ontology> ontology, Optional<Service> service,
@Context SecurityContext securityContext, Optional<AgentContext> agentContext,
@Context Providers providers, com.atomgraph.linkeddatahub.Application system)
{
super(request, service.get(), mediaTypes);
if (ontology.isEmpty()) throw new InternalServerErrorException("Ontology is not specified");
if (service.isEmpty()) throw new InternalServerErrorException("Service is not specified");
this.uriInfo = uriInfo;
this.application = application;
this.ontology = ontology.get();
this.service = service.get();
this.securityContext = securityContext;
this.agentContext = agentContext;
this.providers = providers;
this.system = system;
this.messageDigest = system.getMessageDigest();
uploadsUriBuilder = uriInfo.getBaseUriBuilder().path(UPLOADS_PATH);
URI ownerURI = URI.create(application.getMaker().getURI());
try
{
this.ownerDocURI = new URI(ownerURI.getScheme(), ownerURI.getSchemeSpecificPart(), null).normalize();
this.secretaryDocURI = new URI(system.getSecretaryWebIDURI().getScheme(), system.getSecretaryWebIDURI().getSchemeSpecificPart(), null).normalize();
}
catch (URISyntaxException ex)
{
throw new InternalServerErrorException(ex);
}
}
/**
* Parses multipart RDF/POST request.
*
* @param multiPart multipart form data
* @return RDF graph
* @throws URISyntaxException thrown if there is a syntax error in RDF/POST data
* @see <a href="https://atomgraph.github.io/RDF-POST/">RDF/POST Encoding for RDF</a>
*/
public Model parseModel(FormDataMultiPart multiPart) throws URISyntaxException
{
if (multiPart == null) throw new IllegalArgumentException("FormDataMultiPart cannot be null");
List<String> keys = new ArrayList<>(), values = new ArrayList<>();
Iterator<BodyPart> it = multiPart.getBodyParts().iterator(); // not using getFields() to retain ordering
while (it.hasNext())
{
FormDataBodyPart bodyPart = (FormDataBodyPart)it.next();
if (log.isDebugEnabled()) log.debug("Body part media type: {} headers: {}", bodyPart.getMediaType(), bodyPart.getHeaders());
// it's a file (if the filename is not empty)
if (bodyPart.getContentDisposition().getFileName() != null &&
!bodyPart.getContentDisposition().getFileName().isEmpty())
{
keys.add(bodyPart.getName());
if (log.isDebugEnabled()) log.debug("FormDataBodyPart name: {} value: {}", bodyPart.getName(), bodyPart.getContentDisposition().getFileName());
values.add(bodyPart.getContentDisposition().getFileName());
}
else
{
if (bodyPart.isSimple() && !bodyPart.getValue().isEmpty())
{
keys.add(bodyPart.getName());
if (log.isDebugEnabled()) log.debug("FormDataBodyPart name: {} value: {}", bodyPart.getName(), bodyPart.getValue());
values.add(bodyPart.getValue());
}
}
}
return RDFPostReader.parse(keys, values);
}
/**
* Gets a map of file parts from multipart form data.
*
* @param multiPart multipart form data
* @return map of file parts
*/
public Map<String, FormDataBodyPart> getFileNameBodyPartMap(FormDataMultiPart multiPart)
{
if (multiPart == null) throw new IllegalArgumentException("FormDataMultiPart cannot be null");
Map<String, FormDataBodyPart> fileNameBodyPartMap = new HashMap<>();
Iterator<BodyPart> it = multiPart.getBodyParts().iterator(); // not using getFields() to retain ordering
while (it.hasNext())
{
FormDataBodyPart bodyPart = (FormDataBodyPart)it.next();
if (log.isDebugEnabled()) log.debug("Body part media type: {} headers: {}", bodyPart.getMediaType(), bodyPart.getHeaders());
if (bodyPart.getContentDisposition().getFileName() != null) // it's a file
{
if (log.isDebugEnabled()) log.debug("FormDataBodyPart name: {} value: {}", bodyPart.getName(), bodyPart.getContentDisposition().getFileName());
fileNameBodyPartMap.put(bodyPart.getContentDisposition().getFileName(), bodyPart);
}
}
return fileNameBodyPartMap;
}
/**
* Returns a list of supported languages.
*
* @return list of languages
*/
@Override
public List<Locale> getLanguages()
{
return getSystem().getSupportedLanguages();
}
/**
* Returns URI builder for uploaded file resources.
*
* @return URI builder
*/
public UriBuilder getUploadsUriBuilder()
{
return uploadsUriBuilder.clone();
}
/**
* Returns message digest used in SHA1 hashing.
*
* @return message digest
*/
public MessageDigest getMessageDigest()
{
return messageDigest;
}
/**
* Returns the request URI information.
*
* @return URI info
*/
public UriInfo getUriInfo()
{
return uriInfo;
}
/**
* Returns the current application.
*
* @return application resource
*/
public com.atomgraph.linkeddatahub.apps.model.Application getApplication()
{
return application;
}
/**
* Returns the ontology of the current application.
*
* @return ontology resource
*/
public Ontology getOntology()
{
return ontology;
}
/**
* Returns the SPARQL service of the current application.
*
* @return service resource
*/
public Service getService()
{
return service;
}
/**
* Get JAX-RS security context
*
* @return security context object
*/
public SecurityContext getSecurityContext()
{
return securityContext;
}
/**
* Gets authenticated agent's context
*
* @return optional agent's context
*/
public Optional<AgentContext> getAgentContext()
{
return agentContext;
}
/**
* Returns a registry of JAX-RS providers.
*
* @return provider registry
*/
public Providers getProviders()
{
return providers;
}
/**
* Returns the system application.
*
* @return JAX-RS application
*/
public com.atomgraph.linkeddatahub.Application getSystem()
{
return system;
}
/**
* Returns URI of the WebID document of the applications owner.
*
* @return document URI
*/
public URI getOwnerDocURI()
{
return ownerDocURI;
}
/**
* Returns URI of the WebID document of the applications secretary.
*
* @return document URI
*/
public URI getSecretaryDocURI()
{
return secretaryDocURI;
}
}