/
RouteUtilV1.scala
369 lines (337 loc) · 14.7 KB
/
RouteUtilV1.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
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.routing
import akka.actor.ActorRef
import akka.event.LoggingAdapter
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.{RequestContext, RouteResult}
import akka.pattern._
import akka.util.Timeout
import org.knora.webapi.IRI
import org.knora.webapi.exceptions.{BadRequestException, SipiException, UnexpectedMessageException}
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.http.status.ApiStatusCodesV1
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.store.sipimessages.GetFileMetadataResponse
import org.knora.webapi.messages.util.standoff.StandoffTagUtilV2
import org.knora.webapi.messages.util.standoff.StandoffTagUtilV2.TextWithStandoffTagsV2
import org.knora.webapi.messages.v1.responder.valuemessages.{
ArchiveFileValueV1,
AudioFileValueV1,
DocumentFileValueV1,
FileValueV1,
MovingImageFileValueV1,
StillImageFileValueV1,
TextFileValueV1
}
import org.knora.webapi.messages.v1.responder.{KnoraRequestV1, KnoraResponseV1}
import org.knora.webapi.messages.v2.responder.standoffmessages.{GetMappingRequestV2, GetMappingResponseV2}
import org.knora.webapi.settings.KnoraSettingsImpl
import spray.json.{JsNumber, JsObject}
import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag
/**
* Convenience methods for Knora routes.
*/
object RouteUtilV1 {
/**
* Sends a message to a responder and completes the HTTP request by returning the response as JSON.
*
* @param requestMessage a [[KnoraRequestV1]] message that should be sent to the responder manager.
* @param requestContext the akka-http [[RequestContext]].
* @param settings the application's settings.
* @param responderManager a reference to the responder manager.
* @param log a logging adapter.
* @param timeout a timeout for `ask` messages.
* @param executionContext an execution context for futures.
* @return a [[Future]] containing a [[RouteResult]].
*/
def runJsonRoute(
requestMessage: KnoraRequestV1,
requestContext: RequestContext,
settings: KnoraSettingsImpl,
responderManager: ActorRef,
log: LoggingAdapter
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[RouteResult] = {
// Optionally log the request message. TODO: move this to the testing framework.
if (settings.dumpMessages) {
log.debug(requestMessage.toString)
}
val httpResponse: Future[HttpResponse] = for {
// Make sure the responder sent a reply of type KnoraResponseV1.
knoraResponse <- (responderManager ? requestMessage).map {
case replyMessage: KnoraResponseV1 => replyMessage
case other =>
// The responder returned an unexpected message type (not an exception). This isn't the client's
// fault, so log it and return an error message to the client.
throw UnexpectedMessageException(s"Responder sent a reply of type ${other.getClass.getCanonicalName}")
}
// Optionally log the reply message. TODO: move this to the testing framework.
_ = if (settings.dumpMessages) {
log.debug(knoraResponse.toString)
}
// The request was successful, so add a status of ApiStatusCodesV1.OK to the response.
jsonResponseWithStatus = JsObject(
knoraResponse.toJsValue.asJsObject.fields + ("status" -> JsNumber(ApiStatusCodesV1.OK.id))
)
} yield HttpResponse(
status = StatusCodes.OK,
entity = HttpEntity(
ContentTypes.`application/json`,
jsonResponseWithStatus.compactPrint
)
)
requestContext.complete(httpResponse)
}
/**
* Sends a message (resulting from a [[Future]]) to a responder and completes the HTTP request by returning the response as JSON.
*
* @param requestMessageF a [[Future]] containing a [[KnoraRequestV1]] message that should be sent to the responder manager.
* @param requestContext the akka-http [[RequestContext]].
* @param settings the application's settings.
* @param responderManager a reference to the responder manager.
* @param log a logging adapter.
* @param timeout a timeout for `ask` messages.
* @param executionContext an execution context for futures.
* @return a [[Future]] containing a [[RouteResult]].
*/
def runJsonRouteWithFuture[RequestMessageT <: KnoraRequestV1](
requestMessageF: Future[RequestMessageT],
requestContext: RequestContext,
settings: KnoraSettingsImpl,
responderManager: ActorRef,
log: LoggingAdapter
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[RouteResult] =
for {
requestMessage <- requestMessageF
routeResult <- runJsonRoute(
requestMessage = requestMessage,
requestContext = requestContext,
settings = settings,
responderManager = responderManager,
log = log
)
} yield routeResult
/**
* Sends a message to a responder and completes the HTTP request by returning the response as HTML.
*
* @tparam RequestMessageT the type of request message to be sent to the responder.
* @tparam ReplyMessageT the type of reply message expected from the responder.
* @param requestMessageF a [[Future]] containing the message that should be sent to the responder manager.
* @param viewHandler a function that can generate HTML from the responder's reply message.
* @param requestContext the [[RequestContext]].
* @param settings the application's settings.
* @param responderManager a reference to the responder manager.
* @param log a logging adapter.
* @param timeout a timeout for `ask` messages.
* @param executionContext an execution context for futures.
*/
def runHtmlRoute[RequestMessageT <: KnoraRequestV1, ReplyMessageT <: KnoraResponseV1: ClassTag](
requestMessageF: Future[RequestMessageT],
viewHandler: (ReplyMessageT, ActorRef) => String,
requestContext: RequestContext,
settings: KnoraSettingsImpl,
responderManager: ActorRef,
log: LoggingAdapter
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[RouteResult] = {
val httpResponse: Future[HttpResponse] = for {
requestMessage <- requestMessageF
// Optionally log the request message. TODO: move this to the testing framework.
_ = if (settings.dumpMessages) {
log.debug(requestMessage.toString)
}
// Make sure the responder sent a reply of type ReplyMessageT.
knoraResponse <- (responderManager ? requestMessage).map {
case replyMessage: ReplyMessageT => replyMessage
case other =>
// The responder returned an unexpected message type. This isn't the client's fault, so
// log the error and notify the client.
val msg = s"Responder sent a reply of type ${other.getClass.getCanonicalName}"
throw UnexpectedMessageException(msg)
}
// Optionally log the reply message. TODO: move this to the testing framework.
_ = if (settings.dumpMessages) {
log.debug(knoraResponse.toString)
}
} yield HttpResponse(
status = StatusCodes.OK,
entity = HttpEntity(
ContentTypes.`text/html(UTF-8)`,
viewHandler(knoraResponse, responderManager)
)
)
requestContext.complete(httpResponse)
}
/**
* Converts XML to a [[TextWithStandoffTagsV2]], representing the text and its standoff markup.
*
* @param xml the given XML to be converted to standoff.
* @param mappingIri the mapping to be used to convert the XML to standoff.
* @param acceptStandoffLinksToClientIDs if `true`, allow standoff link tags to use the client's IDs for target
* resources. In a bulk import, this allows standoff links to resources
* that are to be created by the import.
* @param userProfile the user making the request.
* @param featureFactoryConfig the feature factory configuration.
* @param settings the application's settings.
* @param responderManager a reference to the responder manager.
* @param log a logging adapter.
* @param timeout a timeout for `ask` messages.
* @param executionContext an execution context for futures.
* @return a [[TextWithStandoffTagsV2]].
*/
def convertXMLtoStandoffTagV1(
xml: String,
mappingIri: IRI,
acceptStandoffLinksToClientIDs: Boolean,
userProfile: UserADM,
featureFactoryConfig: FeatureFactoryConfig,
settings: KnoraSettingsImpl,
responderManager: ActorRef,
log: LoggingAdapter
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[TextWithStandoffTagsV2] =
for {
// get the mapping directly from v2 responder directly (to avoid useless back and forth conversions between v2 and v1 message formats)
mappingResponse: GetMappingResponseV2 <- (responderManager ? GetMappingRequestV2(
mappingIri = mappingIri,
featureFactoryConfig = featureFactoryConfig,
requestingUser = userProfile
)).mapTo[GetMappingResponseV2]
textWithStandoffTagV1 = StandoffTagUtilV2.convertXMLtoStandoffTagV2(
xml = xml,
mapping = mappingResponse,
acceptStandoffLinksToClientIDs = acceptStandoffLinksToClientIDs,
log = log
)
} yield textWithStandoffTagV1
/**
* MIME types used in Sipi to store image files.
*/
private val imageMimeTypes: Set[String] = Set(
"image/jp2",
"image/jpx"
)
/**
* MIME types used in Sipi to store text files.
*/
private val textMimeTypes: Set[String] = Set(
"application/xml",
"text/xml",
"text/csv",
"text/plain"
)
/**
* MIME types used in Sipi to store document files.
*/
private val documentMimeTypes: Set[String] = Set(
"application/pdf",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
)
/**
* MIME types used in Sipi to store audio files.
*/
private val audioMimeTypes: Set[String] = Set(
"audio/mpeg",
"audio/mp4",
"audio/wav",
"audio/x-wav",
"audio/vnd.wave"
)
/**
* MIME types used in Sipi to store video files.
*/
private val videoMimeTypes: Set[String] = Set(
"video/mp4"
)
/**
* MIME types used in Sipi to store archive files.
*/
private val archiveMimeTypes: Set[String] = Set(
"application/zip",
"application/x-tar",
"application/gzip",
"application/x-7z-compressed"
)
/**
* Converts file metadata from Sipi into a [[FileValueV1]].
*
* @param filename the filename.
* @param fileMetadataResponse the file metadata from Sipi.
* @param projectShortcode the project short code that the file value is to be created in.
* @return a [[FileValueV1]] representing the file.
*/
def makeFileValue(
filename: String,
fileMetadataResponse: GetFileMetadataResponse,
projectShortcode: String
): FileValueV1 =
if (imageMimeTypes.contains(fileMetadataResponse.internalMimeType)) {
StillImageFileValueV1(
internalFilename = filename,
internalMimeType = fileMetadataResponse.internalMimeType,
originalFilename = fileMetadataResponse.originalFilename,
originalMimeType = fileMetadataResponse.originalMimeType,
projectShortcode = projectShortcode,
dimX = fileMetadataResponse.width.getOrElse(throw SipiException(s"Sipi did not return the width of the image")),
dimY =
fileMetadataResponse.height.getOrElse(throw SipiException(s"Sipi did not return the height of the image"))
)
} else if (textMimeTypes.contains(fileMetadataResponse.internalMimeType)) {
TextFileValueV1(
internalFilename = filename,
internalMimeType = fileMetadataResponse.internalMimeType,
originalFilename = fileMetadataResponse.originalFilename,
originalMimeType = fileMetadataResponse.originalMimeType,
projectShortcode = projectShortcode
)
} else if (documentMimeTypes.contains(fileMetadataResponse.internalMimeType)) {
DocumentFileValueV1(
internalFilename = filename,
internalMimeType = fileMetadataResponse.internalMimeType,
originalFilename = fileMetadataResponse.originalFilename,
originalMimeType = fileMetadataResponse.originalMimeType,
projectShortcode = projectShortcode,
pageCount = fileMetadataResponse.pageCount,
dimX = fileMetadataResponse.width,
dimY = fileMetadataResponse.height
)
} else if (audioMimeTypes.contains(fileMetadataResponse.internalMimeType)) {
AudioFileValueV1(
internalFilename = filename,
internalMimeType = fileMetadataResponse.internalMimeType,
originalFilename = fileMetadataResponse.originalFilename,
originalMimeType = fileMetadataResponse.originalMimeType,
projectShortcode = projectShortcode,
duration = fileMetadataResponse.duration
)
} else if (videoMimeTypes.contains(fileMetadataResponse.internalMimeType)) {
MovingImageFileValueV1(
internalFilename = filename,
internalMimeType = fileMetadataResponse.internalMimeType,
originalFilename = fileMetadataResponse.originalFilename,
originalMimeType = fileMetadataResponse.originalMimeType,
projectShortcode = projectShortcode,
duration = fileMetadataResponse.duration,
fps = fileMetadataResponse.fps,
dimX = fileMetadataResponse.width.getOrElse(throw SipiException(s"Sipi did not return the width of the video")),
dimY =
fileMetadataResponse.height.getOrElse(throw SipiException(s"Sipi did not return the height of the video"))
)
} else if (archiveMimeTypes.contains(fileMetadataResponse.internalMimeType)) {
ArchiveFileValueV1(
internalFilename = filename,
internalMimeType = fileMetadataResponse.internalMimeType,
originalFilename = fileMetadataResponse.originalFilename,
originalMimeType = fileMetadataResponse.originalMimeType,
projectShortcode = projectShortcode
)
} else {
throw BadRequestException(s"MIME type ${fileMetadataResponse.internalMimeType} not supported in Knora API v1")
}
}