-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging settings to soap methods
- Loading branch information
1 parent
eca4e63
commit 34f136e
Showing
6 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
.../src/main/kotlin/org/taymyr/lagom/soap/interceptor/AbstractLoggingShadowingInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package org.taymyr.lagom.soap.interceptor | ||
|
||
import org.apache.cxf.helpers.XPathUtils | ||
import org.apache.cxf.message.Message | ||
import org.apache.cxf.message.Message.PROTOCOL_HEADERS | ||
import org.apache.cxf.phase.AbstractPhaseInterceptor | ||
import org.w3c.dom.Document | ||
import org.w3c.dom.Element | ||
import org.w3c.dom.Node | ||
import org.w3c.dom.NodeList | ||
import java.io.ByteArrayInputStream | ||
import java.io.StringWriter | ||
import javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD | ||
import javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA | ||
import javax.xml.XMLConstants.ACCESS_EXTERNAL_STYLESHEET | ||
import javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING | ||
import javax.xml.parsers.DocumentBuilder | ||
import javax.xml.parsers.DocumentBuilderFactory | ||
import javax.xml.transform.TransformerFactory | ||
import javax.xml.transform.dom.DOMSource | ||
import javax.xml.transform.stream.StreamResult | ||
import javax.xml.xpath.XPathConstants.NODE | ||
import javax.xml.xpath.XPathConstants.NODESET | ||
|
||
const val SOAP_ACTION = "org.taymyr.lagom.soap.interceptor.soapAction" | ||
|
||
@Suppress("unchecked_cast") | ||
val Message.soapAction: String? get() = (this[PROTOCOL_HEADERS] as Map<String, List<String>>)["SOAPAction"]?.firstOrNull() | ||
|
||
private val DOCUMENT_BUILDER: DocumentBuilder = DocumentBuilderFactory.newInstance().apply { | ||
setAttribute(ACCESS_EXTERNAL_DTD, "") | ||
setAttribute(ACCESS_EXTERNAL_SCHEMA, "") | ||
setFeature(FEATURE_SECURE_PROCESSING, true) | ||
setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) | ||
}.newDocumentBuilder() | ||
|
||
private val TRANSFORMER = TransformerFactory.newInstance().apply { | ||
setAttribute(ACCESS_EXTERNAL_DTD, "") | ||
setAttribute(ACCESS_EXTERNAL_STYLESHEET, "") | ||
}.newTransformer() | ||
|
||
private val xPathUtils: XPathUtils = XPathUtils() | ||
|
||
abstract class AbstractLoggingShadowingInterceptor( | ||
phase: String, | ||
protected val shadowingSettings: List<ShadowingSettings> | ||
) : AbstractPhaseInterceptor<Message>(phase) { | ||
|
||
companion object { | ||
fun shadowStringXml( | ||
sourceXml: String, | ||
shadowingConfig: ShadowingSettings?, | ||
configSelector: (ShadowingSettings) -> List<String> | ||
): String = | ||
if (shadowingConfig != null) { | ||
replaceAllEntrance( | ||
xmlString = sourceXml, | ||
replacingValue = shadowingConfig.replacingSymbols, | ||
regexps = configSelector(shadowingConfig) | ||
) | ||
} else sourceXml | ||
|
||
private tailrec fun replaceAllEntrance( | ||
xmlString: String, | ||
regexps: List<String>, | ||
replacingValue: String | ||
): String = | ||
if (regexps.isNotEmpty()) | ||
replaceAllEntrance( | ||
replaceXml(xmlString, regexps.first(), replacingValue), | ||
regexps.drop(1), | ||
replacingValue | ||
) | ||
else | ||
xmlString | ||
|
||
private fun replaceXml(xmlString: String, xmlPathExp: String, replacingValue: String): String = | ||
runCatching { | ||
val doc = DOCUMENT_BUILDER.parse(ByteArrayInputStream(xmlString.toByteArray())) | ||
// node and nodes are mutual exclusive in common | ||
val node = xPathUtils.getValue(xmlPathExp, doc, NODE) as? Element | ||
val nodes = xPathUtils.getValue(xmlPathExp, doc, NODESET) as? NodeList | ||
when { | ||
nodes != null -> { | ||
(0..nodes.length).mapNotNull { nodes.item(it) as? Element }.forEach { | ||
replaceNode(replacingValue = replacingValue, sourceDocument = doc, foundNode = it) | ||
} | ||
getXmlWithReplacing(doc) | ||
} | ||
|
||
(node != null) -> { | ||
replaceNode(replacingValue = replacingValue, sourceDocument = doc, foundNode = node) | ||
getXmlWithReplacing(sourceDocument = doc) | ||
} | ||
|
||
else -> xmlString | ||
} | ||
}.recover { xmlString } | ||
.getOrThrow() | ||
|
||
private fun replaceNode(replacingValue: String, sourceDocument: Document, foundNode: Node) { | ||
val fieldName = foundNode.nodeName | ||
val fragmentDoc = | ||
DOCUMENT_BUILDER.parse(ByteArrayInputStream("<$fieldName>$replacingValue</$fieldName>".toByteArray())) | ||
val injectedNode = sourceDocument.adoptNode(fragmentDoc.firstChild) | ||
val parentNode = foundNode.parentNode | ||
parentNode.removeChild(foundNode) | ||
parentNode.appendChild(injectedNode) | ||
} | ||
|
||
private fun getXmlWithReplacing(sourceDocument: Document): String { | ||
val result = StreamResult(StringWriter()) | ||
TRANSFORMER.transform(DOMSource(sourceDocument), result) | ||
return result.writer.toString() | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
java/src/main/kotlin/org/taymyr/lagom/soap/interceptor/ShadowingLoggingInInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.taymyr.lagom.soap.interceptor | ||
|
||
import org.apache.cxf.helpers.IOUtils | ||
import org.apache.cxf.interceptor.LoggingInInterceptor | ||
import org.apache.cxf.io.CachedOutputStream | ||
import org.apache.cxf.message.Message | ||
import org.apache.cxf.phase.Phase.RECEIVE | ||
import java.io.ByteArrayInputStream | ||
import java.io.InputStream | ||
import java.io.SequenceInputStream | ||
import kotlin.Int.Companion.MAX_VALUE | ||
|
||
private const val SOURCE_INPUT_STREAM = "org.taymyr.lagom.soap.interceptor.sourceInputStream" | ||
|
||
/** | ||
* Wraps logging income inputstream to overwrite data before logging | ||
*/ | ||
class PreShadowingLoggingInInterceptor( | ||
shadowingSettings: List<ShadowingSettings>, | ||
) : AbstractLoggingShadowingInterceptor(phase = RECEIVE, shadowingSettings = shadowingSettings) { | ||
|
||
init { | ||
addBefore(LoggingInInterceptor::class.java.name) | ||
} | ||
|
||
override fun handleMessage(message: Message) { | ||
val soapAction = message.exchange[SOAP_ACTION] | ||
if (soapAction != null) { | ||
val bis = message.getContent(InputStream::class.java) | ||
val bos = CachedOutputStream() | ||
// copy all to process all xml string | ||
IOUtils.copyAtLeast(bis, bos, MAX_VALUE) | ||
bos.flush() | ||
val result = bos.inputStream.toString() | ||
message[SOURCE_INPUT_STREAM] = ByteArrayInputStream(result.toByteArray()) | ||
val bais = ByteArrayInputStream( | ||
( | ||
shadowStringXml( | ||
sourceXml = result, | ||
shadowingConfig = shadowingSettings.firstOrNull { it.soapMethod == soapAction } | ||
) { it.responsePatterns } | ||
).toByteArray() | ||
) | ||
message.setContent(InputStream::class.java, bais) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Wraps logging income inputstream to overwrite data after logging | ||
*/ | ||
class PostShadowingLoggingInInterceptor( | ||
shadowingSettings: List<ShadowingSettings>, | ||
maxSize: Int? | ||
) : AbstractLoggingShadowingInterceptor(phase = RECEIVE, shadowingSettings = shadowingSettings) { | ||
private val maxSize = maxSize ?: (1024 * 48) | ||
init { | ||
addAfter(LoggingInInterceptor::class.java.name) | ||
} | ||
|
||
override fun handleMessage(message: Message) { | ||
val soapAction = message.exchange[SOAP_ACTION] | ||
if (soapAction != null) { | ||
val bis = message[SOURCE_INPUT_STREAM] as InputStream | ||
val bos = CachedOutputStream() | ||
IOUtils.copyAtLeast(bis, bos, if (maxSize == -1) MAX_VALUE else maxSize) | ||
bos.flush() | ||
val bais = SequenceInputStream(bos.getInputStream(), bis) | ||
message.setContent(InputStream::class.java, bais) | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
java/src/main/kotlin/org/taymyr/lagom/soap/interceptor/ShadowingLoggingOutInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.taymyr.lagom.soap.interceptor | ||
|
||
import org.apache.cxf.interceptor.LoggingOutInterceptor | ||
import org.apache.cxf.io.CacheAndWriteOutputStream | ||
import org.apache.cxf.io.CachedOutputStream | ||
import org.apache.cxf.io.CachedOutputStreamCallback | ||
import org.apache.cxf.message.Message | ||
import org.apache.cxf.phase.Phase.PRE_STREAM | ||
import org.taymyr.lagom.soap.interceptor.AbstractLoggingShadowingInterceptor.Companion.shadowStringXml | ||
import java.io.ByteArrayOutputStream | ||
import java.io.OutputStream | ||
|
||
/** | ||
* Wraps logging outcome callback for logging fields with shadowing | ||
*/ | ||
class ShadowingLoggingOutInterceptor( | ||
shadowingSettings: List<ShadowingSettings>, | ||
maxSize: Int?, | ||
) : AbstractLoggingShadowingInterceptor(phase = PRE_STREAM, shadowingSettings = shadowingSettings) { | ||
private val maxSize = maxSize ?: (1024 * 48) | ||
|
||
init { | ||
addAfter(LoggingOutInterceptor::class.java.name) | ||
} | ||
|
||
override fun handleMessage(message: Message) { | ||
val os = message.getContent(OutputStream::class.java) as CacheAndWriteOutputStream | ||
// Here we have one org.apache.cxf.interceptor.LoggingOutInterceptor.LoggingCallback that we are going to wrap | ||
val callbacks = os.callbacks.toMutableList().onEach { | ||
os.deregisterCallback(it) | ||
} | ||
message.exchange[SOAP_ACTION] = message.soapAction | ||
os.registerCallback( | ||
NameShadowingCallback( | ||
originalMessage = message, | ||
shadowingSettings = shadowingSettings, | ||
loggingCallback = callbacks, | ||
maxSize = maxSize | ||
) | ||
) | ||
} | ||
} | ||
|
||
class NameShadowingCallback( | ||
private val originalMessage: Message, | ||
private val loggingCallback: List<CachedOutputStreamCallback>, | ||
private val shadowingSettings: List<ShadowingSettings>, | ||
private val maxSize: Int, | ||
) : CachedOutputStreamCallback { | ||
|
||
override fun onClose(cos: CachedOutputStream) { | ||
val sb = StringBuilder() | ||
cos.writeCacheTo(sb) | ||
val result = sb.toString() | ||
val baos = ByteArrayOutputStream(maxSize) | ||
baos.write( | ||
( | ||
shadowStringXml( | ||
result, | ||
shadowingSettings.firstOrNull { it.soapMethod == originalMessage.soapAction } | ||
) { it.requestPatterns } | ||
).toByteArray() | ||
) | ||
val shadowedCos = CacheAndWriteOutputStream(baos) | ||
shadowedCos.resetOut(baos, true) | ||
loggingCallback.forEach { | ||
it.onClose(shadowedCos) | ||
} | ||
} | ||
|
||
override fun onFlush(os: CachedOutputStream?) {} | ||
} |
8 changes: 8 additions & 0 deletions
8
java/src/main/kotlin/org/taymyr/lagom/soap/interceptor/ShadowingSettings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.taymyr.lagom.soap.interceptor | ||
|
||
data class ShadowingSettings( | ||
val soapMethod: String, | ||
val replacingSymbols: String, | ||
val requestPatterns: List<String>, | ||
val responsePatterns: List<String> | ||
) |