|
44 | 44 | package io.gatehill.imposter.plugin.soap.service |
45 | 45 |
|
46 | 46 | import io.gatehill.imposter.http.HttpExchange |
| 47 | +import io.gatehill.imposter.plugin.soap.model.CompositeOperationMessage |
47 | 48 | import io.gatehill.imposter.plugin.soap.model.ElementOperationMessage |
48 | 49 | import io.gatehill.imposter.plugin.soap.model.MessageBodyHolder |
49 | | -import io.gatehill.imposter.plugin.soap.model.OperationMessage |
50 | 50 | import io.gatehill.imposter.plugin.soap.model.ParsedRawBody |
51 | 51 | import io.gatehill.imposter.plugin.soap.model.ParsedSoapMessage |
52 | 52 | import io.gatehill.imposter.plugin.soap.model.TypeOperationMessage |
| 53 | +import io.gatehill.imposter.plugin.soap.model.WsdlOperation |
53 | 54 | import io.gatehill.imposter.plugin.soap.model.WsdlService |
54 | 55 | import io.gatehill.imposter.plugin.soap.parser.WsdlRelativeXsdEntityResolver |
55 | 56 | import io.gatehill.imposter.plugin.soap.util.SchemaGenerator |
@@ -82,57 +83,147 @@ class SoapExampleService { |
82 | 83 | schemas: Array<SchemaDocument>, |
83 | 84 | wsdlDir: File, |
84 | 85 | service: WsdlService, |
85 | | - outputMessage: OperationMessage, |
| 86 | + operation: WsdlOperation, |
86 | 87 | bodyHolder: MessageBodyHolder, |
87 | 88 | ): Boolean { |
88 | | - logger.debug("Generating example for {}", outputMessage) |
89 | | - val example = generateInstanceFromSchemas(schemas, wsdlDir, service, outputMessage) |
| 89 | + logger.debug("Generating example for {}", operation.outputRef) |
| 90 | + val example = when (operation.style) { |
| 91 | + SoapUtil.OPERATION_STYLE_DOCUMENT -> generateDocumentResponse(operation, wsdlDir, schemas) |
| 92 | + SoapUtil.OPERATION_STYLE_RPC -> generateRpcResponse(service, operation, wsdlDir, schemas) |
| 93 | + else -> throw UnsupportedOperationException("Unsupported operation style: ${operation.style}") |
| 94 | + } |
90 | 95 | transmitExample(httpExchange, example, bodyHolder) |
91 | 96 | return true |
92 | 97 | } |
93 | 98 |
|
94 | | - private fun generateInstanceFromSchemas( |
95 | | - schemas: Array<SchemaDocument>, |
| 99 | + private fun generateDocumentResponse( |
| 100 | + operation: WsdlOperation, |
96 | 101 | wsdlDir: File, |
97 | | - service: WsdlService, |
98 | | - message: OperationMessage, |
| 102 | + schemas: Array<SchemaDocument>, |
99 | 103 | ): String { |
100 | | - when (message) { |
| 104 | + when (val message = operation.outputRef) { |
101 | 105 | is ElementOperationMessage -> { |
102 | | - val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas) |
103 | | - |
104 | | - // TODO should this use the qualified name instead? |
105 | | - val rootElementName = message.elementName.localPart |
106 | | - val elem: SchemaType = sts.documentTypes().find { it.documentElementName.localPart == rootElementName } |
107 | | - ?: throw RuntimeException("Could not find a global element with name \"$rootElementName\"") |
108 | | - |
109 | | - return SampleXmlUtil.createSampleForType(elem) |
| 106 | + return generateElementExample(wsdlDir, schemas, message) |
110 | 107 | } |
111 | 108 |
|
112 | 109 | is TypeOperationMessage -> { |
113 | | - // by convention, the suffix 'Response' is added to the operation name |
114 | | - val elementName = message.operationName + "Response" |
115 | | - val rootElement = if (service.targetNamespace?.isNotBlank() == true) { |
116 | | - QName(service.targetNamespace, elementName, "tns") |
117 | | - } else { |
118 | | - QName(elementName) |
| 110 | + return generateTypeExample(wsdlDir, schemas, message) |
| 111 | + } |
| 112 | + |
| 113 | + is CompositeOperationMessage -> { |
| 114 | + val partXmls = message.parts.map { part -> |
| 115 | + val partXml = when (part) { |
| 116 | + is ElementOperationMessage -> { |
| 117 | + generateElementExample(wsdlDir, schemas, part) |
| 118 | + } |
| 119 | + |
| 120 | + is TypeOperationMessage -> { |
| 121 | + generateTypeExample(wsdlDir, schemas, part) |
| 122 | + } |
| 123 | + |
| 124 | + else -> throw UnsupportedOperationException( |
| 125 | + "Unsupported output message part: ${part::class.java.canonicalName}" |
| 126 | + ) |
| 127 | + } |
| 128 | + logger.trace("Generated document part XML: {}", partXml) |
| 129 | + partXml |
119 | 130 | } |
120 | 131 |
|
121 | | - val parts = mutableMapOf<String, QName>() |
| 132 | + // no root element in document style |
| 133 | + return partXmls.joinToString("\n") |
| 134 | + } |
| 135 | + |
| 136 | + else -> throw UnsupportedOperationException("Unsupported output message: ${operation.outputRef}") |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private fun generateRpcResponse( |
| 141 | + service: WsdlService, |
| 142 | + operation: WsdlOperation, |
| 143 | + wsdlDir: File, |
| 144 | + schemas: Array<SchemaDocument>, |
| 145 | + ): String { |
| 146 | + val parts = mutableMapOf<String, QName>() |
| 147 | + |
| 148 | + when (val message = operation.outputRef) { |
| 149 | + is ElementOperationMessage -> { |
| 150 | + // TODO generate wrapper root element and place element part XML within it |
| 151 | + TODO("Element parts in RPC bindings are not supported") |
| 152 | + } |
| 153 | + |
| 154 | + is TypeOperationMessage -> { |
122 | 155 | parts[message.partName] = message.typeName |
| 156 | + } |
123 | 157 |
|
124 | | - val elementSchema = SchemaGenerator.createElementSchema(rootElement, parts) |
125 | | - val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas + elementSchema) |
| 158 | + is CompositeOperationMessage -> { |
| 159 | + parts += message.parts.associate { part -> |
| 160 | + when (part) { |
| 161 | + is ElementOperationMessage -> { |
| 162 | + // TODO generate wrapper root element and place element part XML within it |
| 163 | + TODO("Element parts in composite messages are not supported") |
| 164 | + } |
126 | 165 |
|
127 | | - val elem = sts.documentTypes().find { it.documentElementName == rootElement } |
128 | | - ?: throw RuntimeException("Could not find a generated element with name \"$rootElement\"") |
| 166 | + is TypeOperationMessage -> { |
| 167 | + part.partName to part.typeName |
| 168 | + } |
129 | 169 |
|
130 | | - return SampleXmlUtil.createSampleForType(elem) |
| 170 | + else -> throw UnsupportedOperationException( |
| 171 | + "Unsupported output message part: ${part::class.java.canonicalName}" |
| 172 | + ) |
| 173 | + } |
| 174 | + } |
131 | 175 | } |
132 | 176 |
|
133 | | - // TODO support CompositeOperationMessage |
134 | | - else -> throw UnsupportedOperationException("Unsupported output message: ${message::class.java.canonicalName}") |
| 177 | + else -> throw UnsupportedOperationException( |
| 178 | + "Unsupported output message: $message" |
| 179 | + ) |
| 180 | + } |
| 181 | + |
| 182 | + // by convention, the suffix 'Response' is added to the operation name |
| 183 | + val rootElementName = operation.name + "Response" |
| 184 | + val rootElement = if (service.targetNamespace?.isNotBlank() == true) { |
| 185 | + QName(service.targetNamespace, rootElementName, "tns") |
| 186 | + } else { |
| 187 | + QName(rootElementName) |
135 | 188 | } |
| 189 | + |
| 190 | + val elementSchema = SchemaGenerator.createCompositePartSchema(rootElement, parts) |
| 191 | + val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas + elementSchema) |
| 192 | + |
| 193 | + val elem = sts.documentTypes().find { it.documentElementName == rootElement } |
| 194 | + ?: throw RuntimeException("Could not find a generated element with name \"$rootElement\"") |
| 195 | + |
| 196 | + return SampleXmlUtil.createSampleForType(elem) |
| 197 | + } |
| 198 | + |
| 199 | + private fun generateElementExample( |
| 200 | + wsdlDir: File, |
| 201 | + schemas: Array<SchemaDocument>, |
| 202 | + outputRef: ElementOperationMessage, |
| 203 | + ): String { |
| 204 | + val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas) |
| 205 | + |
| 206 | + // TODO should this use the qualified name instead? |
| 207 | + val rootElementName = outputRef.elementName.localPart |
| 208 | + val elem: SchemaType = sts.documentTypes().find { it.documentElementName.localPart == rootElementName } |
| 209 | + ?: throw RuntimeException("Could not find a global element with name \"$rootElementName\"") |
| 210 | + |
| 211 | + return SampleXmlUtil.createSampleForType(elem) |
| 212 | + } |
| 213 | + |
| 214 | + private fun generateTypeExample( |
| 215 | + wsdlDir: File, |
| 216 | + schemas: Array<SchemaDocument>, |
| 217 | + message: TypeOperationMessage, |
| 218 | + ): String { |
| 219 | + val elementName = message.partName |
| 220 | + val elementSchema = SchemaGenerator.createSinglePartSchema(elementName, message.typeName) |
| 221 | + val sts: SchemaTypeSystem = buildSchemaTypeSystem(wsdlDir, schemas + elementSchema) |
| 222 | + |
| 223 | + val elem = sts.documentTypes().find { it.documentElementName.localPart == elementName } |
| 224 | + ?: throw RuntimeException("Could not find a generated element with name \"$elementName\"") |
| 225 | + |
| 226 | + return SampleXmlUtil.createSampleForType(elem) |
136 | 227 | } |
137 | 228 |
|
138 | 229 | private fun buildSchemaTypeSystem( |
|
0 commit comments