/
NewListsRouteADMFeature.scala
456 lines (415 loc) · 15 KB
/
NewListsRouteADMFeature.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
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Copyright © 2021 Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.routing.admin.lists
import java.util.UUID
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{PathMatcher, Route}
import io.swagger.annotations._
import javax.ws.rs.Path
import org.knora.webapi.IRI
import org.knora.webapi.exceptions.{BadRequestException, ForbiddenException}
import org.knora.webapi.feature.{Feature, FeatureFactoryConfig}
import org.knora.webapi.messages.admin.responder.listsmessages.ListsErrorMessagesADM.LIST_CREATE_PERMISSION_ERROR
import org.knora.webapi.messages.admin.responder.listsmessages.NodeCreatePayloadADM.{
ChildNodeCreatePayloadADM,
ListCreatePayloadADM
}
import org.knora.webapi.messages.admin.responder.listsmessages._
import org.knora.webapi.messages.admin.responder.valueObjects.{
Comments,
Labels,
ListIRI,
ListName,
Position,
ProjectIRI
}
import org.knora.webapi.routing.{Authenticator, KnoraRoute, KnoraRouteData, RouteUtilADM}
import scala.concurrent.Future
object NewListsRouteADMFeature {
val ListsBasePath: PathMatcher[Unit] = PathMatcher("admin" / "lists")
}
/**
* A [[Feature]] that provides the new list admin API route.
*
* @param routeData the [[KnoraRouteData]] to be used in constructing the route.
*/
@Api(value = "lists (new endpoint)", produces = "application/json")
@Path("/admin/lists")
class NewListsRouteADMFeature(routeData: KnoraRouteData)
extends KnoraRoute(routeData)
with Feature
with Authenticator
with ListADMJsonProtocol {
import NewListsRouteADMFeature._
def makeRoute(featureFactoryConfig: FeatureFactoryConfig): Route =
getLists(featureFactoryConfig) ~
createListItem(featureFactoryConfig) ~
getListItem(featureFactoryConfig) ~
updateListItem(featureFactoryConfig) ~
getNodeInfo(featureFactoryConfig)
/* return all lists optionally filtered by project */
@Path("/{IRI}")
@ApiOperation(
httpMethod = "GET",
response = classOf[ListsGetResponseADM],
value = "Get lists",
nickname = "newGetLists"
)
@ApiResponses(
Array(
new ApiResponse(code = 500, message = "Internal server error")
)
)
@ApiImplicitParams(
Array(
new ApiImplicitParam(
name = "X-Knora-Feature-Toggles",
value = "new-list-admin-routes:1 = on/off",
required = true,
dataType = "string",
paramType = "header"
),
new ApiImplicitParam(
name = "projectIri",
value = "IRI of the project",
required = true,
dataType = "string",
paramType = "query"
)
)
)
/* return all lists optionally filtered by project */
private def getLists(featureFactoryConfig: FeatureFactoryConfig): Route = path(ListsBasePath) {
get {
/* return all lists */
parameters("projectIri".?) { maybeProjectIri: Option[IRI] => requestContext =>
val projectIri =
stringFormatter.validateAndEscapeOptionalIri(
maybeProjectIri,
throw BadRequestException(s"Invalid param project IRI: $maybeProjectIri")
)
val requestMessage: Future[ListsGetRequestADM] = for {
requestingUser <- getUserADM(requestContext, featureFactoryConfig)
} yield ListsGetRequestADM(
projectIri = projectIri,
featureFactoryConfig = featureFactoryConfig,
requestingUser = requestingUser
)
RouteUtilADM.runJsonRoute(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log
)
}
}
}
/* create a new list item (root or child node)*/
@ApiOperation(
value = "Add new list item",
nickname = "newAddListItem",
httpMethod = "POST",
response = classOf[ListGetResponseADM]
)
@ApiImplicitParams(
Array(
new ApiImplicitParam(
name = "body",
value = "\"list\" item to create",
required = true,
dataTypeClass = classOf[CreateNodeApiRequestADM],
paramType = "body"
),
new ApiImplicitParam(
name = "X-Knora-Feature-Toggles",
value = "new-list-admin-routes:1 = on/off",
required = true,
dataType = "string",
paramType = "header"
)
)
)
@ApiResponses(
Array(
new ApiResponse(code = 500, message = "Internal server error")
)
)
private def createListItem(featureFactoryConfig: FeatureFactoryConfig): Route = path(ListsBasePath) {
post {
/* create a list item (root or child node) */
entity(as[CreateNodeApiRequestADM]) { apiRequest => requestContext =>
val maybeId: Option[ListIRI] = apiRequest.id match {
case Some(value) => Some(ListIRI.create(value).fold(e => throw e, v => v))
case None => None
}
val maybeParentNodeIri: Option[ListIRI] = apiRequest.parentNodeIri match {
case Some(value) => Some(ListIRI.create(value).fold(e => throw e, v => v))
case None => None
}
val maybeName: Option[ListName] = apiRequest.name match {
case Some(value) => Some(ListName.create(value).fold(e => throw e, v => v))
case None => None
}
val maybePosition: Option[Position] = apiRequest.position match {
case Some(value) => Some(Position.create(value).fold(e => throw e, v => v))
case None => None
}
val labels = Labels.create(apiRequest.labels).fold(e => throw e, v => v)
val projectIri = ProjectIRI.create(apiRequest.projectIri).fold(e => throw e, v => v)
val requestMessage = for {
requestingUser <- getUserADM(requestContext, featureFactoryConfig)
_ = if (
!requestingUser.permissions.isProjectAdmin(projectIri.value) && !requestingUser.permissions.isSystemAdmin
) {
// not project or a system admin
throw ForbiddenException(LIST_CREATE_PERMISSION_ERROR)
}
// Is parent node IRI given in the payload?
createRequest =
if (apiRequest.parentNodeIri.isEmpty) {
// No, create a new list with given information of its root node.
val comments = Comments.create(apiRequest.comments).fold(e => throw e, v => v)
val createRootNodePayloadADM: ListCreatePayloadADM = ListCreatePayloadADM(
id = maybeId,
projectIri,
name = maybeName,
labels,
comments
)
ListCreateRequestADM(
createRootNode = createRootNodePayloadADM,
featureFactoryConfig = featureFactoryConfig,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
} else {
// Yes, create a new child and attach it to the parent node.
// allows to omit comments / send empty comments creating child node
val maybeComments = if (apiRequest.comments.isEmpty) {
None
} else {
Some(Comments.create(apiRequest.comments).fold(e => throw e, v => v))
}
val createChildNodePayloadADM: ChildNodeCreatePayloadADM = ChildNodeCreatePayloadADM(
id = maybeId,
parentNodeIri = maybeParentNodeIri,
projectIri,
name = maybeName,
position = maybePosition,
labels,
comments = maybeComments
)
ListChildNodeCreateRequestADM(
createChildNodeRequest = createChildNodePayloadADM,
featureFactoryConfig = featureFactoryConfig,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
}
} yield createRequest
RouteUtilADM.runJsonRoute(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log
)
}
}
}
/* get a node (root or child) */
@Path("/{IRI}")
@ApiOperation(
value = "Get a list item",
nickname = "newGetlistItem",
httpMethod = "GET",
response = classOf[ListGetResponseADM]
)
@ApiResponses(
Array(
new ApiResponse(code = 500, message = "Internal server error")
)
)
@ApiImplicitParams(
Array(
new ApiImplicitParam(
name = "X-Knora-Feature-Toggles",
value = "new-list-admin-routes:1 = on/off",
required = true,
dataType = "string",
paramType = "header"
)
)
)
private def getListItem(featureFactoryConfig: FeatureFactoryConfig): Route = path(ListsBasePath / Segment) { iri =>
get {
/* return a node, root or child, with all children */
requestContext =>
val listIri =
stringFormatter.validateAndEscapeIri(iri, throw BadRequestException(s"Invalid param list IRI: $iri"))
val requestMessage: Future[ListGetRequestADM] = for {
requestingUser <- getUserADM(requestContext, featureFactoryConfig)
} yield ListGetRequestADM(
iri = listIri,
featureFactoryConfig = featureFactoryConfig,
requestingUser = requestingUser
)
RouteUtilADM.runJsonRoute(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log
)
}
}
/**
* update list
*/
@Path("/{IRI}")
@ApiOperation(
value = "Update basic node information",
nickname = "newPutListItem",
httpMethod = "PUT",
response = classOf[NodeInfoGetResponseADM]
)
@ApiImplicitParams(
Array(
new ApiImplicitParam(
name = "body",
value = "\"list\" item to update",
required = true,
dataTypeClass = classOf[ChangeNodeInfoApiRequestADM],
paramType = "body"
),
new ApiImplicitParam(
name = "X-Knora-Feature-Toggles",
value = "new-list-admin-routes:1 = on/off",
required = true,
dataType = "string",
paramType = "header"
)
)
)
@ApiResponses(
Array(
new ApiResponse(code = 500, message = "Internal server error")
)
)
private def updateListItem(featureFactoryConfig: FeatureFactoryConfig): Route = path(ListsBasePath / Segment) { iri =>
put {
/* update existing list node (either root or child) */
entity(as[ChangeNodeInfoApiRequestADM]) { apiRequest => requestContext =>
val listIri = ListIRI.create(apiRequest.listIri).fold(e => throw e, v => v)
val projectIri = ProjectIRI.create(apiRequest.projectIri).fold(e => throw e, v => v)
val maybeHasRootNode: Option[ListIRI] = apiRequest.hasRootNode match {
case Some(value) => Some(ListIRI.create(value).fold(e => throw e, v => v))
case None => None
}
val maybeName: Option[ListName] = apiRequest.name match {
case Some(value) => Some(ListName.create(value).fold(e => throw e, v => v))
case None => None
}
val maybePosition: Option[Position] = apiRequest.position match {
case Some(value) => Some(Position.create(value).fold(e => throw e, v => v))
case None => None
}
val maybeLabels: Option[Labels] = apiRequest.labels match {
case Some(value) => Some(Labels.create(value).fold(e => throw e, v => v))
case None => None
}
val maybeComments: Option[Comments] = apiRequest.comments match {
case Some(value) => Some(Comments.create(value).fold(e => throw e, v => v))
case None => None
}
val changeNodeInfoPayloadADM: NodeInfoChangePayloadADM = NodeInfoChangePayloadADM(
listIri,
projectIri,
hasRootNode = maybeHasRootNode,
position = maybePosition,
name = maybeName,
labels = maybeLabels,
comments = maybeComments
)
val requestMessage: Future[NodeInfoChangeRequestADM] = for {
requestingUser <- getUserADM(requestContext, featureFactoryConfig)
// check if the requesting user is allowed to perform operation
_ = if (
!requestingUser.permissions.isProjectAdmin(projectIri.value) && !requestingUser.permissions.isSystemAdmin
) {
// not project or a system admin
throw ForbiddenException(LIST_CREATE_PERMISSION_ERROR)
}
} yield NodeInfoChangeRequestADM(
//TODO: why "listIri" property is doubled - here and inside "changeNodeRequest"
listIri = listIri.value,
changeNodeRequest = changeNodeInfoPayloadADM,
featureFactoryConfig = featureFactoryConfig,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log
)
}
}
}
@Path("/{IRI}/info")
@ApiOperation(
value = "Get basic node information",
nickname = "newGetNodeInfo",
httpMethod = "PUT",
response = classOf[RootNodeInfoGetResponseADM]
)
@ApiImplicitParams(
Array(
new ApiImplicitParam(
name = "X-Knora-Feature-Toggles",
value = "new-list-admin-routes:1 = on/off",
required = true,
dataType = "string",
paramType = "header"
)
)
)
@ApiResponses(
Array(
new ApiResponse(code = 500, message = "Internal server error")
)
)
private def getNodeInfo(featureFactoryConfig: FeatureFactoryConfig): Route = path(ListsBasePath / Segment / "info") {
iri =>
get {
/* return information about a node, root or child, without children */
requestContext =>
val listIri =
stringFormatter.validateAndEscapeIri(iri, throw BadRequestException(s"Invalid param list IRI: $iri"))
val requestMessage: Future[ListNodeInfoGetRequestADM] = for {
requestingUser <- getUserADM(requestContext, featureFactoryConfig)
} yield ListNodeInfoGetRequestADM(
iri = listIri,
featureFactoryConfig = featureFactoryConfig,
requestingUser = requestingUser
)
RouteUtilADM.runJsonRoute(
requestMessageF = requestMessage,
requestContext = requestContext,
featureFactoryConfig = featureFactoryConfig,
settings = settings,
responderManager = responderManager,
log = log
)
}
}
}