/
StandoffResponderV1.scala
170 lines (150 loc) · 6.83 KB
/
StandoffResponderV1.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
/*
* Copyright © 2015-2021 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.responders.v1
import java.util.UUID
import akka.pattern._
import org.knora.webapi._
import org.knora.webapi.exceptions.{NotFoundException, SipiException}
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.util.ResponderData
import org.knora.webapi.messages.v1.responder.ontologymessages.{
ConvertOntologyClassV2ToV1,
StandoffEntityInfoGetResponseV1
}
import org.knora.webapi.messages.v1.responder.standoffmessages._
import org.knora.webapi.messages.v1.responder.valuemessages._
import org.knora.webapi.messages.v2.responder.standoffmessages._
import org.knora.webapi.responders.Responder
import org.knora.webapi.responders.Responder.handleUnexpectedMessage
import scala.concurrent.Future
/**
* Responds to requests relating to the creation of mappings from XML elements and attributes to standoff classes and properties.
*/
class StandoffResponderV1(responderData: ResponderData) extends Responder(responderData) {
/**
* Receives a message of type [[StandoffResponderRequestV1]], and returns an appropriate response message.
*/
def receive(msg: StandoffResponderRequestV1) = msg match {
case CreateMappingRequestV1(xml, label, projectIri, mappingName, featureFactoryConfig, userProfile, uuid) =>
createMappingV1(xml, label, projectIri, mappingName, featureFactoryConfig, userProfile, uuid)
case GetMappingRequestV1(mappingIri, featureFactoryConfig, userProfile) =>
getMappingV1(mappingIri, featureFactoryConfig, userProfile)
case GetXSLTransformationRequestV1(xsltTextReprIri, featureFactoryConfig, userProfile) =>
getXSLTransformation(xsltTextReprIri, featureFactoryConfig, userProfile)
case other => handleUnexpectedMessage(other, log, this.getClass.getName)
}
val xsltCacheName = "xsltCache"
/**
* Retrieves a `knora-base:XSLTransformation` in the triplestore and requests the corresponding XSL file from Sipi.
*
* @param xslTransformationIri The IRI of the resource representing the XSL Transformation (a [[org.knora.webapi.messages.OntologyConstants.KnoraBase.XSLTransformation]]).
* @param featureFactoryConfig the feature factory configuration.
* @param userProfile The client making the request.
* @return a [[GetXSLTransformationResponseV1]].
*/
private def getXSLTransformation(xslTransformationIri: IRI,
featureFactoryConfig: FeatureFactoryConfig,
userProfile: UserADM): Future[GetXSLTransformationResponseV1] = {
val xslTransformationFuture = for {
xsltTransformation <- (responderManager ? GetXSLTransformationRequestV2(
xsltTextRepresentationIri = xslTransformationIri,
featureFactoryConfig = featureFactoryConfig,
requestingUser = userProfile
)).mapTo[GetXSLTransformationResponseV2]
} yield
GetXSLTransformationResponseV1(
xslt = xsltTransformation.xslt
)
xslTransformationFuture.recover {
case notFound: NotFoundException =>
throw SipiException(s"XSL transformation $xslTransformationIri not found: ${notFound.message}")
case other => throw other
}
}
/**
* Creates a mapping between XML elements and attributes to standoff classes and properties.
* The mapping is used to convert XML documents to [[TextValueV1]] and back.
*
* @param xml the provided mapping.
* @param featureFactoryConfig the feature factory configuration.
* @param userProfile the client that made the request.
*/
private def createMappingV1(xml: String,
label: String,
projectIri: IRI,
mappingName: String,
featureFactoryConfig: FeatureFactoryConfig,
userProfile: UserADM,
apiRequestID: UUID): Future[CreateMappingResponseV1] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val createMappingRequest = CreateMappingRequestV2(
metadata = CreateMappingRequestMetadataV2(
label = label,
projectIri = projectIri.toSmartIri,
mappingName = mappingName
),
xml = CreateMappingRequestXMLV2(xml),
featureFactoryConfig = featureFactoryConfig,
requestingUser = userProfile,
apiRequestID = apiRequestID
)
for {
mappingResponse <- (responderManager ? createMappingRequest).mapTo[CreateMappingResponseV2]
} yield
CreateMappingResponseV1(
mappingResponse.mappingIri
)
}
/**
* The name of the mapping cache.
*/
val mappingCacheName = "mappingCache"
/**
* Gets a mapping either from the cache or by making a request to the triplestore.
*
* @param mappingIri the IRI of the mapping to retrieve.
* @param featureFactoryConfig the feature factory configuration.
* @param userProfile the user making the request.
* @return a [[MappingXMLtoStandoff]].
*/
private def getMappingV1(mappingIri: IRI,
featureFactoryConfig: FeatureFactoryConfig,
userProfile: UserADM): Future[GetMappingResponseV1] = {
for {
mappingResponse: GetMappingResponseV2 <- (responderManager ? GetMappingRequestV2(
mappingIri = mappingIri,
featureFactoryConfig = featureFactoryConfig,
requestingUser = userProfile
)).mapTo[GetMappingResponseV2]
} yield
GetMappingResponseV1(
mappingIri = mappingResponse.mappingIri,
mapping = mappingResponse.mapping,
standoffEntities = StandoffEntityInfoGetResponseV1(
standoffClassInfoMap =
ConvertOntologyClassV2ToV1.classInfoMapV2ToV1(mappingResponse.standoffEntities.standoffClassInfoMap),
standoffPropertyInfoMap =
ConvertOntologyClassV2ToV1.propertyInfoMapV2ToV1(mappingResponse.standoffEntities.standoffPropertyInfoMap)
)
)
}
}