@@ -53,7 +53,7 @@ import io.gatehill.imposter.plugin.soap.model.ParsedSoapMessage
5353import io.gatehill.imposter.plugin.soap.model.TypeOperationMessage
5454import io.gatehill.imposter.plugin.soap.model.WsdlOperation
5555import io.gatehill.imposter.plugin.soap.model.WsdlService
56- import io.gatehill.imposter.plugin.soap.parser.WsdlRelativeXsdEntityResolver
56+ import io.gatehill.imposter.plugin.soap.parser.SchemaContext
5757import io.gatehill.imposter.plugin.soap.util.SchemaGenerator
5858import io.gatehill.imposter.plugin.soap.util.SoapUtil
5959import io.gatehill.imposter.util.LogUtil
@@ -67,7 +67,6 @@ import org.apache.xmlbeans.XmlException
6767import org.apache.xmlbeans.XmlOptions
6868import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument
6969import org.apache.xmlbeans.impl.xsd2inst.SampleXmlUtil
70- import java.io.File
7170import javax.xml.namespace.QName
7271
7372
@@ -81,40 +80,37 @@ class SoapExampleService {
8180
8281 fun serveExample (
8382 httpExchange : HttpExchange ,
84- schemas : Array <SchemaDocument >,
85- wsdlDir : File ,
83+ schemaContext : SchemaContext ,
8684 service : WsdlService ,
8785 operation : WsdlOperation ,
8886 bodyHolder : MessageBodyHolder ,
8987 ): Boolean {
9088 logger.debug(" Generating example for {}" , operation.outputRef)
9189 val example = when (operation.style) {
92- SoapUtil .OPERATION_STYLE_DOCUMENT -> generateDocumentResponse(wsdlDir, schemas , service, operation)
93- SoapUtil .OPERATION_STYLE_RPC -> generateRpcResponse(wsdlDir, schemas , service, operation)
90+ SoapUtil .OPERATION_STYLE_DOCUMENT -> generateDocumentResponse(schemaContext , service, operation)
91+ SoapUtil .OPERATION_STYLE_RPC -> generateRpcResponse(schemaContext , service, operation)
9492 else -> throw UnsupportedOperationException (" Unsupported operation style: ${operation.style} " )
9593 }
9694 transmitExample(httpExchange, example, bodyHolder)
9795 return true
9896 }
9997
10098 private fun generateDocumentResponse (
101- wsdlDir : File ,
102- schemas : Array <SchemaDocument >,
99+ schemaContext : SchemaContext ,
103100 service : WsdlService ,
104101 operation : WsdlOperation ,
105102 ): String {
106103 return operation.outputRef?.let { message ->
107104 // no root element in document style
108- generateDocumentMessage(wsdlDir, schemas , service, message).joinToString(" \n " )
105+ generateDocumentMessage(schemaContext , service, message).joinToString(" \n " )
109106
110107 } ? : throw IllegalStateException (
111108 " No output message for operation: $operation "
112109 )
113110 }
114111
115112 private fun generateRpcResponse (
116- wsdlDir : File ,
117- schemas : Array <SchemaDocument >,
113+ schemaContext : SchemaContext ,
118114 service : WsdlService ,
119115 operation : WsdlOperation ,
120116 ): String {
@@ -129,7 +125,7 @@ class SoapExampleService {
129125 }
130126
131127 val elementSchema = SchemaGenerator .createCompositePartSchema(service.targetNamespace ? : " " , rootElement, parts)
132- val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas + elementSchema)
128+ val sts: SchemaTypeSystem = buildSchemaTypeSystem(schemaContext, elementSchema)
133129
134130 val elem = sts.documentTypes().find { it.documentElementName == rootElement }
135131 ? : throw RuntimeException (" Could not find a generated element with name \" $rootElement \" " )
@@ -138,24 +134,23 @@ class SoapExampleService {
138134 }
139135
140136 private fun generateDocumentMessage (
141- wsdlDir : File ,
142- schemas : Array <SchemaDocument >,
137+ schemaContext : SchemaContext ,
143138 service : WsdlService ,
144139 message : OperationMessage ,
145140 ): List <String > {
146141 val partXmls = mutableListOf<String >()
147142
148143 when (message) {
149144 is ElementOperationMessage -> {
150- partXmls + = generateElementExample(wsdlDir, schemas , message)
145+ partXmls + = generateElementExample(schemaContext , message)
151146 }
152147
153148 is TypeOperationMessage -> {
154- partXmls + = generateTypeExample(wsdlDir, schemas , service, message)
149+ partXmls + = generateTypeExample(schemaContext , service, message)
155150 }
156151
157152 is CompositeOperationMessage -> {
158- partXmls + = message.parts.flatMap { part -> generateDocumentMessage(wsdlDir, schemas , service, part) }
153+ partXmls + = message.parts.flatMap { part -> generateDocumentMessage(schemaContext , service, part) }
159154 }
160155
161156 else -> throw UnsupportedOperationException (
@@ -168,28 +163,24 @@ class SoapExampleService {
168163 }
169164
170165 private fun generateElementExample (
171- wsdlDir : File ,
172- schemas : Array <SchemaDocument >,
166+ schemaContext : SchemaContext ,
173167 outputRef : ElementOperationMessage ,
174168 ): String {
175- val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas)
176-
177169 val rootElementName = outputRef.elementName
178- val elem: SchemaType = sts.documentTypes().find { it.documentElementName == rootElementName }
170+ val elem: SchemaType = schemaContext. sts.documentTypes().find { it.documentElementName == rootElementName }
179171 ? : throw RuntimeException (" Could not find a global element with name \" $rootElementName \" " )
180172
181173 return SampleXmlUtil .createSampleForType(elem)
182174 }
183175
184176 private fun generateTypeExample (
185- wsdlDir : File ,
186- schemas : Array <SchemaDocument >,
177+ schemaContext : SchemaContext ,
187178 service : WsdlService ,
188179 message : TypeOperationMessage ,
189180 ): String {
190181 val elementName = message.partName
191182 val elementSchema = SchemaGenerator .createSinglePartSchema(service.targetNamespace ? : " " , message)
192- val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas + elementSchema)
183+ val sts: SchemaTypeSystem = buildSchemaTypeSystem(schemaContext, elementSchema)
193184
194185 val elem = sts.documentTypes().find { it.documentElementName.localPart == elementName }
195186 ? : throw RuntimeException (" Could not find a generated element with name \" $elementName \" " )
@@ -198,21 +189,23 @@ class SoapExampleService {
198189 }
199190
200191 private fun buildSchemaTypeSystem (
201- wsdlDir : File ,
202- schemas : Array < SchemaDocument > ,
192+ schemaContext : SchemaContext ,
193+ vararg extraSchemas : SchemaDocument ,
203194 ): SchemaTypeSystem {
204195 var sts: SchemaTypeSystem ? = null
196+
197+ val schemas = schemaContext.schemas + extraSchemas
205198 if (schemas.isNotEmpty()) {
206199 val errors = mutableListOf<XmlError >()
207200
208201 val compileOptions = XmlOptions ()
209202 .setLoadLineNumbers()
210203 .setLoadMessageDigest()
211- .setEntityResolver(WsdlRelativeXsdEntityResolver (wsdlDir) )
204+ .setEntityResolver(schemaContext.entityResolver )
212205 .setErrorListener(errors)
213206
214207 try {
215- // TODO consider reusing the SchemaTypeSystem from AbstractWsdlParser.buildXsdFromSchemas
208+ // TODO consider reusing the SchemaTypeSystem from SchemaContext
216209 sts = XmlBeans .compileXsd(schemas, XmlBeans .getBuiltinTypeSystem(), compileOptions)
217210 } catch (e: Exception ) {
218211 if (errors.isEmpty() || e !is XmlException ) {
0 commit comments