/
ListsRouteADM.scala
278 lines (248 loc) · 10.8 KB
/
ListsRouteADM.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
/*
* 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.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{PathMatcher, Route}
import io.swagger.annotations._
import javax.ws.rs.Path
import org.knora.webapi._
import org.knora.webapi.exceptions.{BadRequestException, NotImplementedException}
import org.knora.webapi.messages.admin.responder.listsmessages._
import org.knora.webapi.routing.{Authenticator, KnoraRoute, KnoraRouteData, RouteUtilADM}
import scala.concurrent.Future
object ListsRouteADM {
val ListsBasePath: PathMatcher[Unit] = PathMatcher("admin" / "lists")
}
/**
* Provides an akka-http-routing function for API routes that deal with lists.
*/
@Api(value = "lists", produces = "application/json")
@Path("/admin/lists")
class ListsRouteADM(routeData: KnoraRouteData) extends KnoraRoute(routeData) with Authenticator with ListADMJsonProtocol {
import ListsRouteADM._
/**
* Returns the route.
*/
override def knoraApiPath: Route = getLists ~ createList ~ getList ~ updateList ~ createListChildNode ~ deleteListNode ~
getListInfo ~ getListNodeInfo
/* return all lists optionally filtered by project */
@ApiOperation(value = "Get lists", nickname = "getlists", httpMethod = "GET", response = classOf[ListsGetResponseADM])
@ApiResponses(Array(
new ApiResponse(code = 500, message = "Internal server error")
))
/* return all lists optionally filtered by project */
def getLists: Route = path(ListsBasePath) {
get {
/* return all lists */
parameters("projectIri".?) { maybeProjectIri: Option[IRI] =>
requestContext =>
val projectIri = stringFormatter.toOptionalIri(maybeProjectIri, throw BadRequestException(s"Invalid param project IRI: $maybeProjectIri"))
val requestMessage: Future[ListsGetRequestADM] = for {
requestingUser <- getUserADM(requestContext)
} yield ListsGetRequestADM(projectIri, requestingUser)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
}
/* create a new list (root node) */
@ApiOperation(value = "Add new list", nickname = "addList", httpMethod = "POST", response = classOf[ListGetResponseADM])
@ApiImplicitParams(Array(
new ApiImplicitParam(name = "body", value = "\"list\" to create", required = true,
dataTypeClass = classOf[CreateListApiRequestADM], paramType = "body")
))
@ApiResponses(Array(
new ApiResponse(code = 500, message = "Internal server error")
))
def createList: Route = path(ListsBasePath) {
post {
/* create a list */
entity(as[CreateListApiRequestADM]) { apiRequest =>
requestContext =>
val requestMessage: Future[ListCreateRequestADM] = for {
requestingUser <- getUserADM(requestContext)
} yield ListCreateRequestADM(
createListRequest = apiRequest,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
}
/* get a list */
@Path("/{IRI}")
@ApiOperation(value = "Get a list", nickname = "getlist", httpMethod = "GET", response = classOf[ListGetResponseADM])
@ApiResponses(Array(
new ApiResponse(code = 500, message = "Internal server error")
))
def getList: Route = path(ListsBasePath / Segment) { iri =>
get {
/* return a list (a graph with all list nodes) */
requestContext =>
val listIri = stringFormatter.validateAndEscapeIri(iri, throw BadRequestException(s"Invalid param list IRI: $iri"))
val requestMessage: Future[ListGetRequestADM] = for {
requestingUser <- getUserADM(requestContext)
} yield ListGetRequestADM(listIri, requestingUser)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
/**
* update list
*/
@Path("/{IRI}")
@ApiOperation(value = "Update basic list information", nickname = "putList", httpMethod = "PUT", response = classOf[ListInfoGetResponseADM])
@ApiImplicitParams(Array(
new ApiImplicitParam(name = "body", value = "\"list\" to update", required = true,
dataTypeClass = classOf[ChangeListInfoApiRequestADM], paramType = "body")
))
@ApiResponses(Array(
new ApiResponse(code = 500, message = "Internal server error")
))
def updateList: Route = path(ListsBasePath / Segment) { iri =>
put {
/* update existing list node (either root or child) */
entity(as[ChangeListInfoApiRequestADM]) { apiRequest =>
requestContext =>
val listIri = stringFormatter.validateAndEscapeIri(iri, throw BadRequestException(s"Invalid param list IRI: $iri"))
val requestMessage: Future[ListInfoChangeRequestADM] = for {
requestingUser <- getUserADM(requestContext)
} yield ListInfoChangeRequestADM(
listIri = listIri,
changeListRequest = apiRequest,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
}
/**
* create a new child node
*/
@Path("/{IRI}")
@ApiOperation(value = "Add new child node", nickname = "addListChildNode", httpMethod = "POST", response = classOf[ListNodeInfoGetResponseADM])
@ApiImplicitParams(Array(
new ApiImplicitParam(name = "body", value = "\"node\" to create", required = true,
dataTypeClass = classOf[CreateChildNodeApiRequestADM], paramType = "body")
))
@ApiResponses(Array(
new ApiResponse(code = 500, message = "Internal server error")
))
def createListChildNode: Route = path(ListsBasePath / Segment) { iri =>
post {
/* add node to existing list node. the existing list node can be either the root or a child */
entity(as[CreateChildNodeApiRequestADM]) { apiRequest =>
requestContext =>
val parentNodeIri = stringFormatter.validateAndEscapeIri(iri, throw BadRequestException(s"Invalid param list IRI: $iri"))
val requestMessage: Future[ListChildNodeCreateRequestADM] = for {
requestingUser <- getUserADM(requestContext)
} yield ListChildNodeCreateRequestADM(
parentNodeIri = parentNodeIri,
createChildNodeRequest = apiRequest,
requestingUser = requestingUser,
apiRequestID = UUID.randomUUID()
)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
}
/* delete list node which should also delete its children */
def deleteListNode: Route = path(ListsBasePath / Segment) { iri =>
delete {
/* delete (deactivate) list */
throw NotImplementedException("Method not implemented.")
???
}
}
def getListInfo: Route = path(ListsBasePath / "infos" / Segment) { iri =>
get {
/* return information about a list (without children) */
requestContext =>
val listIri = stringFormatter.validateAndEscapeIri(iri, throw BadRequestException(s"Invalid param list IRI: $iri"))
val requestMessage: Future[ListInfoGetRequestADM] = for {
requestingUser <- getUserADM(requestContext)
} yield ListInfoGetRequestADM(listIri, requestingUser)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
}
}
def getListNodeInfo: Route = path(ListsBasePath / "nodes" / Segment) { iri =>
get {
/* return information about a single node (without children) */
requestContext =>
val listIri = stringFormatter.validateAndEscapeIri(iri, throw BadRequestException(s"Invalid param list IRI: $iri"))
val requestMessage: Future[ListNodeInfoGetRequestADM] = for {
requestingUser <- getUserADM(requestContext)
} yield ListNodeInfoGetRequestADM(listIri, requestingUser)
RouteUtilADM.runJsonRoute(
requestMessage,
requestContext,
settings,
responderManager,
log
)
} ~
put {
/* update list node */
throw NotImplementedException("Method not implemented.")
???
} ~
delete {
/* delete list node */
throw NotImplementedException("Method not implemented.")
???
}
}
}