scalut.xml.soap.Implicits.scala Maven / Gradle / Ivy
The newest version!
package scalut.xml.soap
import javax.xml.soap.{SOAPConstants, SOAPMessage}
import com.thoughtworks.xstream.io.HierarchicalStreamDriver
import scalut.xml.DocHelper
import scalut.xml.soap.SoapHelper._
import scalut.xml.xstream.XStreamHelper
import scala.xml.NamespaceBinding
/**
* Created by vladimir on 29.11.16.
*/
object Implicits {
implicit class BodyConverters[T](body: T) {
def toSoapMessage(
soapAction: String,
soapProtocol: String = SOAPConstants.DEFAULT_SOAP_PROTOCOL,
envelopeNS: collection.Seq[NamespaceBinding] = DEFAULT_BINDINGS,
driver: HierarchicalStreamDriver = null
): SOAPMessage = {
val bodyDoc = DocHelper.fromBytes(XStreamHelper.toByteArray(body, driver))
createSOAPMessage(soapAction, bodyDoc, soapProtocol, envelopeNS)
}
}
implicit class SoapMessageConverters(message: SOAPMessage) {
def fromSOAP[T](clazz: Class[T], driver: HierarchicalStreamDriver = null): Option[T] = {
val soapBody = message.getSOAPPart.getEnvelope.getBody
if (soapBody.hasFault)
None
else
Some(XStreamHelper.fromByteArray(DocHelper.toBytes(soapBody.extractContentAsDocument()), driver, clazz))
}
}
}