Example
Example
This section provides an example web service and web client that send attachments to each other.
Web Service
The web service provides two methods:
-
UploadAscii() receives an ASCII attachment and saves it.
-
DownloadBinary() sends a binary attachment to the requestor.
The class definition is as follows:
Class GSOAP.FileTransferWS Extends %SOAP.WebService
{
/// Name of the web service.
Parameter SERVICENAME = "FileTransfer";
/// SOAP namespace for the web service
Parameter NAMESPACE = "https://www.filetransfer.org";
/// Namespaces of referenced classes will be used in the WSDL.
Parameter USECLASSNAMESPACES = 1;
/// Receive an attachment and save it
Method UploadAscii(filename As %String = "sample.txt") As %Status [WebMethod]
{
//assume 1 attachment; ignore any others
Set attach=..Attachments.GetAt(1)
Set file=##class(%FileCharacterStream).%New()
Set file.Filename="c:\from-client"_$H_filename
//copy attachment into file
Set status=file.CopyFrom(attach.Body)
If $$$ISERR(status) {do $System.Status.DisplayError(status)}
Set status= file.%Save()
Quit status
}
/// Create an attachment and send it in response to the web client call
Method DownloadBinary(filename As %String = "sample.pdf") As %Status [WebMethod]
{
//use a file-type stream to read file contents
Set file=##class(%Library.FileBinaryStream).%New()
Set file.Filename="c:\"_filename
//create MIMEpart and add file to it
Set mimepart=##class(%Net.MIMEPart).%New()
Set mimepart.Body=file
//set header appropriately for binary file
Do mimepart.SetHeader("Content-Type","application/octet-stream")
Do mimepart.SetHeader("Content-Transfer-Encoding","binary")
//attach
Set status=..ResponseAttachments.Insert(mimepart)
Quit status
}
}
Web Client
The web client application provides two methods:
-
UploadAscii() sends an ASCII file to the web service.
-
DownloadBinary() calls the web service and receives an binary file in response.
The generated web client class (GSOAPClient.FileTransfer.FileTransferSoap) includes the methods UploadAscii() and DownloadBinary(), which invoke the corresponding methods of the preceding web service. This class is not shown.
The web client application also includes the following class, which uses this generated web client class:
Include %systemInclude
Class GSOAPClient.FileTransfer.UseClient
{
ClassMethod DownloadBinary(filename As %String = "sample.pdf") As %Status
{
Set client=##class(GSOAPClient.FileTransfer.FileTransferSoap).%New()
//call web method
Set ans=client.DownloadBinary(filename)
//get the attachment (assume only 1)
Set attach=client.ResponseAttachments.GetAt(1)
//create a file and copy stream contents into it
Set file=##class(%FileBinaryStream).%New()
//include $H in the filename to make filename unique
Set file.Filename="c:\from-service"_$H_filename
Set status=file.CopyFrom(attach.Body)
If $$$ISERR(status) {do $System.Status.DisplayError(status)}
Set status= file.%Save()
Quit status
}
ClassMethod UploadAscii(filename As %String = "sample.txt") As %Status
{
Set client=##class(GSOAPClient.FileTransfer.FileTransferSoap).%New()
//use a file-type stream to read file contents
Set file=##class(%Library.FileCharacterStream).%New()
Set file.Filename="c:\"_filename
//create MIME part, add file as Body, and set the header
Set mimepart=##class(%Net.MIMEPart).%New()
Set mimepart.Body=file
Do mimepart.SetHeader("Content-Transfer-Encoding","7bit")
//attach to client and call web method
Do client.Attachments.Insert(mimepart)
Set status=client.UploadAscii(filename)
Quit status
}
}