/
GroupsRouteADM.scala
447 lines (391 loc) · 16.7 KB
/
GroupsRouteADM.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
/*
* 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.admin
import java.util.UUID
import akka.actor.ActorSystem
import akka.http.scaladsl.client.RequestBuilding._
import akka.http.scaladsl.model.headers.BasicHttpCredentials
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{PathMatcher, Route}
import akka.http.scaladsl.util.FastFuture
import akka.stream.ActorMaterializer
import io.swagger.annotations._
import javax.ws.rs.Path
import org.knora.webapi.messages.admin.responder.groupsmessages._
import org.knora.webapi.routing.{Authenticator, KnoraRoute, KnoraRouteData, RouteUtilADM}
import org.knora.webapi.util.IriConversions._
import org.knora.webapi.util.clientapi.EndpointFunctionDSL._
import org.knora.webapi.util.clientapi._
import org.knora.webapi.{BadRequestException, OntologyConstants, SharedTestDataADM}
import scala.concurrent.{ExecutionContext, Future}
object GroupsRouteADM {
val GroupsBasePath: PathMatcher[Unit] = PathMatcher("admin" / "groups")
val GroupsBasePathString: String = "/admin/groups"
}
/**
* Provides a spray-routing function for API routes that deal with groups.
*/
@Api(value = "groups", produces = "application/json")
@Path("/admin/groups")
class GroupsRouteADM(routeData: KnoraRouteData) extends KnoraRoute(routeData) with Authenticator with GroupsADMJsonProtocol with ClientEndpoint {
import GroupsRouteADM._
/**
* The name of this [[ClientEndpoint]].
*/
override val name: String = "GroupsEndpoint"
/**
* The directory name to be used for this endpoint's code.
*/
override val directoryName: String = "groups"
/**
* The URL path of this [[ClientEndpoint]].
*/
override val urlPath: String = "/groups"
/**
* A description of this [[ClientEndpoint]].
*/
override val description: String = "An endpoint for working with Knora groups."
// Classes used in client function definitions.
private val GroupsResponse = classRef(OntologyConstants.KnoraAdminV2.GroupsResponse.toSmartIri)
private val GroupResponse = classRef(OntologyConstants.KnoraAdminV2.GroupResponse.toSmartIri)
private val MembersResponse = classRef(OntologyConstants.KnoraAdminV2.MembersResponse.toSmartIri)
private val CreateGroupRequest = classRef(OntologyConstants.KnoraAdminV2.CreateGroupRequest.toSmartIri)
private val UpdateGroupRequest = classRef(OntologyConstants.KnoraAdminV2.UpdateGroupRequest.toSmartIri)
private val groupIri = SharedTestDataADM.imagesReviewerGroup.id
private val groupIriEnc = java.net.URLEncoder.encode(groupIri, "utf-8")
/**
* Returns all groups
*/
private def getGroups: Route = path(GroupsBasePath) {
get {
/* return all groups */
requestContext =>
val requestMessage = for {
requestingUser <- getUserADM(requestContext)
} yield GroupsGetRequestADM(requestingUser)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
private val getGroupsFunction: ClientFunction =
"getGroups" description "Returns a list of all groups." params() doThis {
httpGet(BasePath)
} returns GroupsResponse
private def getGroupsTestResponse: Future[SourceCodeFileContent] = {
for {
responseStr <- doTestDataRequest(Get(baseApiUrl + GroupsBasePathString) ~> addCredentials(BasicHttpCredentials(SharedTestDataADM.imagesUser01.email, SharedTestDataADM.testPass)))
} yield SourceCodeFileContent(
filePath = SourceCodeFilePath.makeJsonPath("get-groups-response"),
text = responseStr
)
}
/**
* Creates a group
*/
private def createGroup: Route = path(GroupsBasePath) {
post {
/* create a new group */
entity(as[CreateGroupApiRequestADM]) { apiRequest =>
requestContext =>
val requestMessage = for {
requestingUser <- getUserADM(requestContext)
} yield GroupCreateRequestADM(
createRequest = apiRequest,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
}
private val createGroupFunction: ClientFunction =
"createGroup" description "Creates a group." params (
"group" description "The group to be created." paramType CreateGroupRequest
) doThis {
httpPost(
path = BasePath,
body = Some(arg("group"))
)
} returns GroupResponse
private def createGroupTestRequest: Future[SourceCodeFileContent] = {
FastFuture.successful(
SourceCodeFileContent(
filePath = SourceCodeFilePath.makeJsonPath("create-group-request"),
text = SharedTestDataADM.createGroupRequest
)
)
}
/**
* Returns a single group identified by IRI.
*/
private def getGroupByIri: Route = path(GroupsBasePath / Segment) { value =>
get {
/* returns a single group identified through iri */
requestContext =>
val checkedGroupIri = stringFormatter.validateAndEscapeIri(value, throw BadRequestException(s"Invalid group IRI $value"))
val requestMessage = for {
requestingUser <- getUserADM(requestContext)
} yield GroupGetRequestADM(checkedGroupIri, requestingUser)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
private val getGroupByIriFunction: ClientFunction =
"getGroupByIri" description "Gets a group by IRI." params (
"iri" description "The IRI of the group." paramType UriDatatype
) doThis {
httpGet(arg("iri"))
} returns GroupResponse
private def getGroupByIriTestResponse: Future[SourceCodeFileContent] = {
for {
responseStr <- doTestDataRequest(Get(s"$baseApiUrl$GroupsBasePathString/$groupIriEnc") ~> addCredentials(BasicHttpCredentials(SharedTestDataADM.imagesUser01.email, SharedTestDataADM.testPass)))
} yield SourceCodeFileContent(
filePath = SourceCodeFilePath.makeJsonPath("get-group-response"),
text = responseStr
)
}
/**
* Update basic group information.
*/
private def updateGroup: Route = path(GroupsBasePath / Segment) { value =>
put {
/* update a group identified by iri */
entity(as[ChangeGroupApiRequestADM]) { apiRequest =>
requestContext =>
val checkedGroupIri = stringFormatter.validateAndEscapeIri(value, throw BadRequestException(s"Invalid group IRI $value"))
/**
* The api request is already checked at time of creation.
* See case class.
*/
if (apiRequest.status.nonEmpty) {
throw BadRequestException("The status property is not allowed to be set for this route. Please use the change status route.")
}
val requestMessage = for {
requestingUser <- getUserADM(requestContext)
} yield GroupChangeRequestADM(
groupIri = checkedGroupIri,
changeGroupRequest = apiRequest,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
}
private val updateGroupFunction: ClientFunction =
"updateGroup" description "Updates a group." params(
"iri" description "The IRI of the group to be updated." paramType UriDatatype,
"groupInfo" description "The group information to be updated." paramType UpdateGroupRequest
) doThis {
httpPut(
path = arg("iri"),
body = Some(arg("groupInfo"))
)
} returns GroupResponse
private def updateGroupTestRequest: Future[SourceCodeFileContent] = {
FastFuture.successful(
SourceCodeFileContent(
filePath = SourceCodeFilePath.makeJsonPath("update-group-request"),
text = SharedTestDataADM.updateGroupRequest
)
)
}
/**
* Update the group's status.
*/
private def changeGroupStatus: Route = path(GroupsBasePath / Segment / "status") { value =>
put {
/* change the status of a group identified by iri */
entity(as[ChangeGroupApiRequestADM]) { apiRequest =>
requestContext =>
val checkedGroupIri = stringFormatter.validateAndEscapeIri(value, throw BadRequestException(s"Invalid group IRI $value"))
/**
* The api request is already checked at time of creation.
* See case class. Depending on the data sent, we are either
* doing a general update or status change. Since we are in
* the status change route, we are only interested in the
* value of the status property
*/
if (apiRequest.status.isEmpty) {
throw BadRequestException("The status property is not allowed to be empty.")
}
val requestMessage = for {
requestingUser <- getUserADM(requestContext)
} yield GroupChangeStatusRequestADM(
groupIri = checkedGroupIri,
changeGroupRequest = apiRequest,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
}
private val changeGroupStatusFunction: ClientFunction =
"updateGroupStatus" description "Updates the status of a group." params(
"iri" description "The IRI of the group to be updated." paramType UriDatatype,
"status" description "The new status of the group." paramType BooleanDatatype
) doThis {
httpPut(
path = arg("iri") / str("status"),
body = Some(json(
"status" -> arg("status")
))
)
} returns GroupResponse
private def changeGroupStatusTestRequest: Future[SourceCodeFileContent] = {
FastFuture.successful(
SourceCodeFileContent(
filePath = SourceCodeFilePath.makeJsonPath("change-group-status-request"),
text = SharedTestDataADM.changeGroupStatusRequest
)
)
}
/**
* Deletes a group (sets status to false)
*/
private def deleteGroup: Route = path(GroupsBasePath / Segment) { value =>
delete {
/* update group status to false */
requestContext =>
val checkedGroupIri = stringFormatter.validateAndEscapeIri(value, throw BadRequestException(s"Invalid group IRI $value"))
val requestMessage = for {
requestingUser <- getUserADM(requestContext)
} yield GroupChangeStatusRequestADM(
groupIri = checkedGroupIri,
changeGroupRequest = ChangeGroupApiRequestADM(status = Some(false)),
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
private val deleteGroupFunction: ClientFunction =
"deleteGroup" description "Deletes a group. This method does not actually delete a group, but sets the status to false." params (
"iri" description "The IRI of the group." paramType UriDatatype
) doThis {
httpDelete(
path = arg("iri")
)
} returns GroupResponse
/**
* Gets members of single group.
*/
private def getGroupMembers: Route = path(GroupsBasePath / Segment / "members") { value =>
get {
/* returns all members of the group identified through iri */
requestContext =>
val checkedGroupIri = stringFormatter.validateAndEscapeIri(value, throw BadRequestException(s"Invalid group IRI $value"))
val requestMessage = for {
requestingUser <- getUserADM(requestContext)
} yield GroupMembersGetRequestADM(groupIri = checkedGroupIri, requestingUser = requestingUser)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
private val getGroupMembersFunction: ClientFunction =
"getGroupMembers" description "Gets the members of a group." params (
"iri" description "The IRI of the group." paramType UriDatatype
) doThis {
httpGet(arg("iri") / str("members"))
} returns MembersResponse
private def getGroupMembersTestResponse: Future[SourceCodeFileContent] = {
for {
responseStr <- doTestDataRequest(Get(s"$baseApiUrl$GroupsBasePathString/$groupIriEnc/members") ~> addCredentials(BasicHttpCredentials(SharedTestDataADM.imagesUser01.email, SharedTestDataADM.testPass)))
} yield SourceCodeFileContent(
filePath = SourceCodeFilePath.makeJsonPath("get-group-members-response"),
text = responseStr
)
}
/**
* Returns the route.
*/
override def knoraApiPath: Route = getGroups ~ createGroup ~ getGroupByIri ~
updateGroup ~ changeGroupStatus ~ deleteGroup ~ getGroupMembers
/**
* The functions defined by this [[ClientEndpoint]].
*/
override val functions: Seq[ClientFunction] = Seq(
getGroupsFunction,
createGroupFunction,
getGroupByIriFunction,
updateGroupFunction,
changeGroupStatusFunction,
deleteGroupFunction,
getGroupMembersFunction
)
/**
* Returns test data for this endpoint.
*
* @return a set of test data files to be used for testing this endpoint.
*/
override def getTestData(implicit executionContext: ExecutionContext,
actorSystem: ActorSystem,
materializer: ActorMaterializer): Future[Set[SourceCodeFileContent]] = {
Future.sequence {
Set(
getGroupsTestResponse,
createGroupTestRequest,
getGroupByIriTestResponse,
updateGroupTestRequest,
changeGroupStatusTestRequest,
getGroupMembersTestResponse
)
}
}
}