calaxb_2.10.0-RC1.1.0.0-RC1.source-code.soap.scala.template Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalaxb_2.10.0-RC1 Show documentation
Show all versions of scalaxb_2.10.0-RC1 Show documentation
scalaxb is an XML data-binding tool for Scala that supports W3C XML Schema (xsd) and wsdl.
The newest version!
package scalaxb
trait HttpClients {
val httpClient: HttpClient
trait HttpClient {
def request(in: String, address: java.net.URI, headers: Map[String, String]): String
}
}
case class Fault[+A](original: Any, detail: Option[A], headers: scala.xml.NodeSeq) {
def asFault[B: scalaxb.XMLFormat] = Fault(original, detail map {
case x: soapenvelope12.Detail => x.any.head.value match {
case node: scala.xml.Node => scalaxb.fromXML[B](node)
case _ => error("unsupported fault: " + toString)
}
case _ => error("unsupported fault: " + toString)
}, headers)
}
trait SoapClients { this: HttpClients =>
import soapenvelope12.{Envelope, Body, Detail, Header}
lazy val soapClient: SoapClient = new SoapClient {}
val baseAddress: java.net.URI
trait SoapClient {
val SOAP_ENVELOPE_URI = "http://www.w3.org/2003/05/soap-envelope"
def soapRequest(in: Option[Envelope], scope: scala.xml.NamespaceBinding,
address: java.net.URI, webMethod: String, action: Option[java.net.URI]): Envelope = {
val merged = scalaxb.toScope(((Some("soap12") -> "http://www.w3.org/2003/05/soap-envelope") ::
scalaxb.fromScope(scope)).distinct: _*)
val r = in map { scalaxb.toXML(_, Some(SOAP_ENVELOPE_URI), Some("Envelope"), merged) match {
case elem: scala.xml.Elem => elem
case x => error("unexpected non-elem: " + x.toString)
}}
val contentType = "application/soap+xml; charset=utf-8" +
(action map {"""; action="%s"""" format(_)} getOrElse {""})
val headers = Map[String, String]("Content-Type" -> contentType)
val s = httpClient.request(r map {_.toString} getOrElse {""}, address, headers)
try {
val response = scala.xml.XML.loadString(s)
scalaxb.fromXML[Envelope](response)
}
catch {
case e: Exception => error(e.toString + ": " + s)
}
}
def requestResponse(body: scala.xml.NodeSeq, headers: scala.xml.NodeSeq, scope: scala.xml.NamespaceBinding,
address: java.net.URI, webMethod: String, action: Option[java.net.URI]):
Either[Fault[Detail], (scala.xml.NodeSeq, scala.xml.NodeSeq)] = {
val bodyRecords = body.toSeq map {DataRecord(None, None, _)}
val headerOption = headers.toSeq.headOption map { _ =>
Header(headers.toSeq map {DataRecord(None, None, _)}, Map())
}
val envelope = Envelope(headerOption, Body(bodyRecords, Map()), Map())
buildResponse(soapRequest(Some(envelope), scope, address, webMethod, action))
}
def soapResponse(location: Option[String], params: Map[String, Any],
address: java.net.URI, webMethod: String, action: Option[java.net.URI]):
Either[Fault[Detail], (scala.xml.NodeSeq, scala.xml.NodeSeq)]= {
buildResponse(soapRequest(None, scala.xml.TopScope, address, webMethod, action))
}
def buildResponse(soapResponse: Envelope):
Either[Fault[Detail], (scala.xml.NodeSeq, scala.xml.NodeSeq)] = {
val header: scala.xml.NodeSeq =
soapResponse.Header.toSeq flatMap { header =>
header.any collect {
case DataRecord(_, _, x: scala.xml.Node) => x
}
}
soapResponse.Body.any.headOption match {
case Some(DataRecord(_, _, x: scala.xml.Elem)) if (x.label == "Fault") &&
(x.scope.getURI(x.prefix) == SOAP_ENVELOPE_URI) =>
val fault = scalaxb.fromXML[soapenvelope12.Fault](x)
Left(Fault(fault, fault.Detail, header))
case _ =>
Right(header, soapResponse.Body.any collect {
case DataRecord(_, _, x: scala.xml.Node) => x
})
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy