/
SearchRouteV2.scala
444 lines (364 loc) · 20.2 KB
/
SearchRouteV2.scala
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright © 2015-2019 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.routing.v2
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import org.knora.webapi._
import org.knora.webapi.exceptions.BadRequestException
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.util.search.gravsearch.GravsearchParser
import org.knora.webapi.messages.v2.responder.searchmessages._
import org.knora.webapi.messages.{OntologyConstants, SmartIri, StringFormatter}
import org.knora.webapi.routing.{Authenticator, KnoraRoute, KnoraRouteData, RouteUtilV2}
import scala.concurrent.Future
/**
* Provides a function for API routes that deal with search.
*/
class SearchRouteV2(routeData: KnoraRouteData) extends KnoraRoute(routeData) with Authenticator {
private val LIMIT_TO_PROJECT = "limitToProject"
private val LIMIT_TO_RESOURCE_CLASS = "limitToResourceClass"
private val OFFSET = "offset"
private val LIMIT_TO_STANDOFF_CLASS = "limitToStandoffClass"
/**
* Returns the route.
*/
override def makeRoute(featureFactoryConfig: FeatureFactoryConfig): Route =
fullTextSearchCount(featureFactoryConfig) ~
fullTextSearch(featureFactoryConfig) ~
gravsearchCountGet(featureFactoryConfig) ~
gravsearchCountPost(featureFactoryConfig) ~
gravsearchGet(featureFactoryConfig) ~
gravsearchPost(featureFactoryConfig) ~
searchByLabelCount(featureFactoryConfig) ~
searchByLabel(featureFactoryConfig)
/**
* Gets the requested offset. Returns zero if no offset is indicated.
*
* @param params the GET parameters.
* @return the offset to be used for paging.
*/
private def getOffsetFromParams(params: Map[String, String]): Int = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val offsetStr = params.get(OFFSET)
offsetStr match {
case Some(offset: String) =>
val offsetInt: Int = stringFormatter.validateInt(offset, throw BadRequestException(s"offset is expected to be an Integer, but $offset given"))
if (offsetInt < 0) throw BadRequestException(s"offset must be an Integer >= 0, but $offsetInt given.")
offsetInt
case None => 0
}
}
/**
* Gets the the project the search should be restricted to, if any.
*
* @param params the GET parameters.
* @return the project Iri, if any.
*/
private def getProjectFromParams(params: Map[String, String]): Option[IRI] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val limitToProjectIriStr = params.get(LIMIT_TO_PROJECT)
val limitToProjectIri: Option[IRI] = limitToProjectIriStr match {
case Some(projectIriStr: String) =>
val projectIri = stringFormatter.validateAndEscapeIri(projectIriStr, throw BadRequestException(s"$projectIriStr is not a valid Iri"))
Some(projectIri)
case None => None
}
limitToProjectIri
}
/**
* Gets the resource class the search should be restricted to, if any.
*
* @param params the GET parameters.
* @return the internal resource class, if any.
*/
private def getResourceClassFromParams(params: Map[String, String]): Option[SmartIri] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val limitToResourceClassIriStr = params.get(LIMIT_TO_RESOURCE_CLASS)
limitToResourceClassIriStr match {
case Some(resourceClassIriStr: String) =>
val externalResourceClassIri = resourceClassIriStr.toSmartIriWithErr(throw BadRequestException(s"Invalid resource class IRI: $resourceClassIriStr"))
if (!externalResourceClassIri.isKnoraApiV2EntityIri) {
throw BadRequestException(s"$resourceClassIriStr is not a valid knora-api resource class IRI")
}
Some(externalResourceClassIri.toOntologySchema(InternalSchema))
case None => None
}
}
/**
* Gets the standoff class the search should be restricted to.
*
* @param params the GET parameters.
* @return the internal standoff class, if any.
*/
private def getStandoffClass(params: Map[String, String]): Option[SmartIri] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val limitToStandoffClassIriStr: Option[String] = params.get(LIMIT_TO_STANDOFF_CLASS)
limitToStandoffClassIriStr match {
case Some(standoffClassIriStr: String) =>
val externalStandoffClassIri = standoffClassIriStr.toSmartIriWithErr(throw BadRequestException(s"Invalid standoff class IRI: $limitToStandoffClassIriStr"))
if (!externalStandoffClassIri.getOntologySchema.contains(ApiV2Complex)) {
throw BadRequestException(s"$externalStandoffClassIri is not a valid standoff class IRI")
}
Some(externalStandoffClassIri.toOntologySchema(InternalSchema))
case None => None
}
}
private def fullTextSearchCount(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "search" / "count" / Segment) { searchStr => // TODO: if a space is encoded as a "+", this is not converted back to a space
get {
requestContext =>
if (searchStr.contains(OntologyConstants.KnoraApi.ApiOntologyHostname)) {
throw BadRequestException("It looks like you are submitting a Gravsearch request to a full-text search route")
}
val escapedSearchStr = stringFormatter.toSparqlEncodedString(searchStr, throw BadRequestException(s"Invalid search string: '$searchStr'"))
if (escapedSearchStr.length < settings.searchValueMinLength) {
throw BadRequestException(s"A search value is expected to have at least length of ${settings.searchValueMinLength}, but '$escapedSearchStr' given of length ${escapedSearchStr.length}.")
}
val params: Map[String, String] = requestContext.request.uri.query().toMap
val limitToProject: Option[IRI] = getProjectFromParams(params)
val limitToResourceClass: Option[SmartIri] = getResourceClassFromParams(params)
val limitToStandoffClass: Option[SmartIri] = getStandoffClass(params)
val requestMessage: Future[FullTextSearchCountRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield FullTextSearchCountRequestV2(
searchValue = escapedSearchStr,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass,
limitToStandoffClass = limitToStandoffClass,
requestingUser = requestingUser
)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = RouteUtilV2.getOntologySchema(requestContext),
schemaOptions = RouteUtilV2.getSchemaOptions(requestContext)
)
}
}
private def fullTextSearch(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "search" / Segment) { searchStr => // TODO: if a space is encoded as a "+", this is not converted back to a space
get {
requestContext => {
if (searchStr.contains(OntologyConstants.KnoraApi.ApiOntologyHostname)) {
throw BadRequestException("It looks like you are submitting a Gravsearch request to a full-text search route")
}
val escapedSearchStr = stringFormatter.toSparqlEncodedString(searchStr, throw BadRequestException(s"Invalid search string: '$searchStr'"))
if (escapedSearchStr.length < settings.searchValueMinLength) {
throw BadRequestException(s"A search value is expected to have at least length of ${settings.searchValueMinLength}, but '$escapedSearchStr' given of length ${escapedSearchStr.length}.")
}
val params: Map[String, String] = requestContext.request.uri.query().toMap
val offset = getOffsetFromParams(params)
val limitToProject: Option[IRI] = getProjectFromParams(params)
val limitToResourceClass: Option[SmartIri] = getResourceClassFromParams(params)
val limitToStandoffClass: Option[SmartIri] = getStandoffClass(params)
val targetSchema: ApiV2Schema = RouteUtilV2.getOntologySchema(requestContext)
val schemaOptions: Set[SchemaOption] = RouteUtilV2.getSchemaOptions(requestContext)
val requestMessage: Future[FulltextSearchRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield FulltextSearchRequestV2(
searchValue = escapedSearchStr,
offset = offset,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass,
limitToStandoffClass,
requestingUser = requestingUser,
targetSchema = targetSchema,
schemaOptions = schemaOptions
)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = targetSchema,
schemaOptions = schemaOptions
)
}
}
}
private def gravsearchCountGet(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "searchextended" / "count" / Segment) { gravsearchQuery => // Segment is a URL encoded string representing a Gravsearch query
get {
requestContext => {
val constructQuery = GravsearchParser.parseQuery(gravsearchQuery)
val requestMessage: Future[GravsearchCountRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield GravsearchCountRequestV2(constructQuery = constructQuery, requestingUser = requestingUser)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = RouteUtilV2.getOntologySchema(requestContext),
schemaOptions = RouteUtilV2.getSchemaOptions(requestContext)
)
}
}
}
private def gravsearchCountPost(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "searchextended" / "count") {
post {
entity(as[String]) { gravsearchQuery =>
requestContext => {
val constructQuery = GravsearchParser.parseQuery(gravsearchQuery)
val requestMessage: Future[GravsearchCountRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield GravsearchCountRequestV2(constructQuery = constructQuery, requestingUser = requestingUser)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = RouteUtilV2.getOntologySchema(requestContext),
schemaOptions = RouteUtilV2.getSchemaOptions(requestContext)
)
}
}
}
}
private def gravsearchGet(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "searchextended" / Segment) { sparql => // Segment is a URL encoded string representing a Gravsearch query
get {
requestContext => {
val constructQuery = GravsearchParser.parseQuery(sparql)
val targetSchema: ApiV2Schema = RouteUtilV2.getOntologySchema(requestContext)
val schemaOptions: Set[SchemaOption] = RouteUtilV2.getSchemaOptions(requestContext)
val requestMessage: Future[GravsearchRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield GravsearchRequestV2(
constructQuery = constructQuery,
targetSchema = targetSchema,
schemaOptions = schemaOptions,
requestingUser = requestingUser
)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = targetSchema,
schemaOptions = schemaOptions
)
}
}
}
private def gravsearchPost(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "searchextended") {
post {
entity(as[String]) { gravsearchQuery =>
requestContext => {
val constructQuery = GravsearchParser.parseQuery(gravsearchQuery)
val targetSchema: ApiV2Schema = RouteUtilV2.getOntologySchema(requestContext)
val schemaOptions: Set[SchemaOption] = RouteUtilV2.getSchemaOptions(requestContext)
val requestMessage: Future[GravsearchRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield GravsearchRequestV2(
constructQuery = constructQuery,
targetSchema = targetSchema,
schemaOptions = schemaOptions,
requestingUser = requestingUser
)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = targetSchema,
schemaOptions = schemaOptions
)
}
}
}
}
private def searchByLabelCount(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "searchbylabel" / "count" / Segment) { searchval => // TODO: if a space is encoded as a "+", this is not converted back to a space
get {
requestContext => {
val searchString = stringFormatter.toSparqlEncodedString(searchval, throw BadRequestException(s"Invalid search string: '$searchval'"))
if (searchString.length < settings.searchValueMinLength) {
throw BadRequestException(s"A search value is expected to have at least length of ${settings.searchValueMinLength}, but '$searchString' given of length ${searchString.length}.")
}
val params: Map[String, String] = requestContext.request.uri.query().toMap
val limitToProject: Option[IRI] = getProjectFromParams(params)
val limitToResourceClass: Option[SmartIri] = getResourceClassFromParams(params)
val requestMessage: Future[SearchResourceByLabelCountRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield SearchResourceByLabelCountRequestV2(
searchValue = searchString,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass,
requestingUser = requestingUser
)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = RouteUtilV2.getOntologySchema(requestContext),
schemaOptions = RouteUtilV2.getSchemaOptions(requestContext)
)
}
}
}
private def searchByLabel(featureFactoryConfig: FeatureFactoryConfig): Route = path("v2" / "searchbylabel" / Segment) { searchval => // TODO: if a space is encoded as a "+", this is not converted back to a space
get {
requestContext => {
val searchString = stringFormatter.toSparqlEncodedString(searchval, throw BadRequestException(s"Invalid search string: '$searchval'"))
if (searchString.length < settings.searchValueMinLength) {
throw BadRequestException(s"A search value is expected to have at least length of ${settings.searchValueMinLength}, but '$searchString' given of length ${searchString.length}.")
}
val params: Map[String, String] = requestContext.request.uri.query().toMap
val offset = getOffsetFromParams(params)
val limitToProject: Option[IRI] = getProjectFromParams(params)
val limitToResourceClass: Option[SmartIri] = getResourceClassFromParams(params)
val targetSchema: ApiV2Schema = RouteUtilV2.getOntologySchema(requestContext)
val requestMessage: Future[SearchResourceByLabelRequestV2] = for {
requestingUser <- getUserADM(requestContext)
} yield SearchResourceByLabelRequestV2(
searchValue = searchString,
offset = offset,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass,
targetSchema = targetSchema,
requestingUser = requestingUser
)
RouteUtilV2.runRdfRouteWithFuture(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log,
targetSchema = RouteUtilV2.getOntologySchema(requestContext),
schemaOptions = RouteUtilV2.getSchemaOptions(requestContext)
)
}
}
}
}