Skip to content

Instantly share code, notes, and snippets.

@ern
Created January 13, 2016 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ern/d6b4466b4df8326427a9 to your computer and use it in GitHub Desktop.
Save ern/d6b4466b4df8326427a9 to your computer and use it in GitHub Desktop.
#!/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)
@ern
Copy link
Author

ern commented Jan 14, 2016

This is a sample script to use call it with the following params
./soapSiteHideResources sessionid siteid true|false
i.e.
./soapSiteHideResources 197ce6a3-3ac8-4ec0-a59f-c08bff541f7f mercury true

@ern
Copy link
Author

ern commented Jan 14, 2016

A successful request would look something like:

##### request:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservices.sakaiproject.org/">
<soapenv:Header/>
<soapenv:Body>
<web:siteHideResources>
            <sessionid>197ce6a3-3ac8-4ec0-a59f-c08bff541f7f</sessionid>
            <siteid>mercury</siteid>
            <hidden>true</hidden>
        </web:siteHideResources>
</soapenv:Body>
</soapenv:Envelope>

##### response:
success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment