Forum Discussion

SpiderHibs's avatar
SpiderHibs
New Contributor
16 years ago

Saving a binary file from SOAP message

Hi,

I am new to soapUI and have been impressed with what it can do. I am however having a problem with extracting some binary data from a SOAP message.

Here is my setup:

soapUI v3.5
CentOS 5.4

I have been given a WSDL by a customer which I have imported as a MockService within soapUI. The WSDL has a CreateRecord service which I send a video to. I want to test that the video I have sent in the Request is the same at when received.

A sample message is below (I have removed the content as it is too large to display here)


 
   
       
       
   

 
 


I have then written a script to go into the "AfterRequest Script" section of the mock Service (named "CreateRecordServicePortBinding MockService"):

import org.apache.commons.codec.binary.Base64
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
filename = "/tmp/soapUI.mpg"
def objFile = new java.io.File(filename)
def holder = groovyUtils.getXmlHolder("createRecord - Request 1#Request");
holder.declareNamespace('ns2','http://create.ws.test/');
def byteArray = holder.getNodeValue("//ns1:binaryData" )
def b64 = new Base64()
def textBytes = b64.decode(byteArray.getBytes())
FileOutputStream fos = new java.io.FileOutputStream(objFile);
fos.write( textBytes );
fos.flush();
fos.close();
log.info("Output file: " + filename)

I have run the script and receive an JAva.lang.NullPointerException on the getXmlHolder call. I realise that this is because it can't find the information it's looking for.

I have been reading up on the parameters but I am having trouble understanding what information should be passed as a parameter to the getXmlHolder() method. Can anyone help?

Thanks.

Can anyone

2 Replies

  • SpiderHibs's avatar
    SpiderHibs
    New Contributor
    I resolved my own issue. I'm sure that this is a bit of a convoluted way to do it but it works and I couldn't get any of the simpler methods to work for me.

    import com.eviware.soapui.model.mock.MockRunner;
    import com.eviware.soapui.model.mock.MockResult;
    import com.eviware.soapui.model.mock.MockRequest;
    import com.eviware.soapui.model.mock.MockService;
    import com.eviware.soapui.model.mock.MockOperation;
    import org.apache.commons.codec.binary.Base64;

    // Get the SOAP message
    MockRunner runner = context.getMockRunner()
    MockService service = runner.getMockService()
    MockOperation operation = service.getMockOperationByName("createRecord")
    MockResult result = operation.getLastMockResult()
    MockRequest request = result.getMockRequest()
    String content  = request.getRequestContent()

    // Open output file
    filename = "/tmp/soapUI.mpg"
    def objFile = new java.io.File(filename)

    // Find binaryData node
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
    def holder = groovyUtils.getXmlHolder(content);
    def byteArray = holder.getNodeValue("//binaryData")

    // Decode base64
    def b64 = new Base64()
    def textBytes = b64.decode(byteArray.getBytes())

    // Write & close output file
    FileOutputStream fos = new java.io.FileOutputStream(objFile);
    fos.write( textBytes );
    fos.flush();
    fos.close();
    log.info("Output file: " + filename)
  • SpiderHibs's avatar
    SpiderHibs
    New Contributor
    There is a flaw in my cunning plan. It only seems to work if there is a previous message i.e. if you psate the code in after the service has been run. If have the code in when you start the service, the first time it runs, it complains that the result.getMockRequest() fails as result is NULL.