All Downloads are FREE. Search and download functionalities are using the official Maven repository.

calaxb_2.13.1.12.1.source-code.soap12_async.scala.template Maven / Gradle / Ivy

Go to download

scalaxb is an XML data-binding tool for Scala that supports W3C XML Schema (xsd) and wsdl.

The newest version!
package scalaxb

import scala.concurrent.{ Future, ExecutionContext }

case class Fault[+A](original: Any, detail: Option[A], headers: scala.xml.NodeSeq) extends Exception {
  def asFault[B: scalaxb.XMLFormat]: Fault[B] = Fault(original, detail map {
    case x: soapenvelope12.Detail => x.any.head.value match {
      case node: scala.xml.Node => scalaxb.fromXML[B](node)
      case _ => sys.error("unsupported fault: " + toString)
    }
    case _ => sys.error("unsupported fault: " + toString)
  }, headers)
  def asNillableFault[B: scalaxb.XMLFormat]: Fault[Option[B]] = Fault(original, detail map {
    case x: soapenvelope12.Detail => x.any.head.value match {
      case node: scala.xml.Node if scalaxb.Helper.isNil(node) => None
      case node: scala.xml.Node                               => Some(scalaxb.fromXML[B](node))
      case _ => sys.error("unsupported fault: " + toString)
    }
    case _ => sys.error("unsupported fault: " + toString)
  }, headers)
  override def toString: String = s"""Fault($original, $detail, $headers)"""
}

trait SoapClientsAsync {
  this: HttpClientsAsync =>

  lazy val soapClient: SoapClientAsync = new SoapClientAsync {}
  def baseAddress: java.net.URI

  trait SoapClientAsync {
    import soapenvelope12.{Fault => _, _}
    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])
                    (implicit ec: ExecutionContext): Future[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 => sys.error("unexpected non-elem: " + x.toString)
      }}
      val contentType = "application/soap+xml; charset=utf-8" +
        (action map {uri => """; action="%s"""".format(uri.toASCIIString)} getOrElse {""})
      val headers = Map[String, String]("Content-Type" -> contentType)
      val ftr: Future[String] = httpClient.request(r map {_.toString} getOrElse {""}, address, headers)
      ftr map { (s: String) =>
        try {
          val response = scala.xml.XML.loadString(s)
          scalaxb.fromXML[Envelope](response)
        }
        catch {
          case e: Exception => sys.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])
                       (implicit ec: ExecutionContext):
        Future[(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])
                    (implicit ec: ExecutionContext):
        Future[(scala.xml.NodeSeq, scala.xml.NodeSeq)]= {
      buildResponse(soapRequest(None, scala.xml.TopScope, address, webMethod, action))
    }

    def buildResponse(futureSoapResponse: Future[Envelope])(implicit ec: ExecutionContext):
        Future[(scala.xml.NodeSeq, scala.xml.NodeSeq)] = {
      futureSoapResponse.map(soapResponse => {
        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)
            throw Fault(fault, fault.Detail, header)
          case _ =>
            (header, soapResponse.Body.any collect {
              case DataRecord(_, _, x: scala.xml.Node) => x
            })
        }
      })
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy