Created
January 13, 2016 20:22
-
-
Save ern/d6b4466b4df8326427a9 to your computer and use it in GitHub Desktop.
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
#!/bin/sh | |
exec scala "$0" "$@" | |
!# | |
import scala.xml.{Elem,XML} | |
class SoapClient { | |
private def error(msg: String) = { | |
println("SoapClient error: " + msg) | |
} | |
def wrap(xml: Elem) : String = { | |
val buf = new StringBuilder | |
buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") | |
buf.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.sakaiproject.org/\">\n") | |
buf.append("<soapenv:Header/>\n") | |
buf.append("<soapenv:Body>\n") | |
buf.append(xml.toString) | |
buf.append("\n</soapenv:Body>\n") | |
buf.append("</soapenv:Envelope>\n") | |
buf.toString | |
} | |
def sendMessage(host: String, req: Elem) : Option[Elem] = { | |
val url = new java.net.URL(host) | |
val outs = wrap(req).getBytes | |
val conn = url.openConnection.asInstanceOf[java.net.HttpURLConnection] | |
try { | |
conn.setRequestMethod("POST") | |
conn.setDoOutput(true) | |
conn.setRequestProperty("Content-Length", outs.length.toString) | |
conn.setRequestProperty("Content-Type", "text/xml") | |
conn.getOutputStream.write(outs) | |
conn.getOutputStream.close | |
Some(XML.load(conn.getInputStream)) | |
} | |
catch { | |
case e: Exception => error("post: " + e) | |
error("post:" + scala.io.Source.fromInputStream(conn.getErrorStream).mkString) | |
None | |
} | |
} | |
} | |
object SoapCall { | |
def makeXml(sessionid: String, siteid: String, hidden: String) = | |
<web:siteHideResources> | |
<sessionid>{ sessionid }</sessionid> | |
<siteid>{ siteid }</siteid> | |
<hidden>{ hidden }</hidden> | |
</web:siteHideResources>; | |
def doHttpCall(xml: Elem) { | |
val host = "http://localhost:8080/sakai-ws/soap/contenthosting" | |
val cli = new SoapClient | |
println("##### request:\n" + cli.wrap(xml)) | |
val resp = cli.sendMessage(host, xml) | |
if (resp.isDefined) { | |
println("##### response:") | |
(resp.get \\ "return").foreach(elem => { println(elem.text) }) | |
} | |
} | |
def main(args: Array[String]): Unit = { | |
doHttpCall(makeXml(args(0), args(1), args(2))) | |
} | |
} | |
SoapCall.main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A successful request would look something like: