Using SoapUI to help build a simple Web Service

Now that we have the basic code for the web service, let’s move on and clean this channel up. The results will be ready for day-to-day operations, and much easier to maintain.

Before You Start

Let’s start from scratch with a new channel:

  1. From the Dashboard, create a new channel with the following components:
    • Source = From LLP
    • Destination = To Channel
    • Filter = disabled
  2. From the To Channel destination component, open the Translator and import this project code: SoaUI_Final_Cut_Filter.zip
  3. Commit your first milestone.

How It Works

This section explains the adjustments we made to the original code:

  1. It was important that we cleaned up and streamlined the main() function. The result has a much tidier layout where the steps are self explanatory and where errors are easily handled:
    require 'node'
    require 'webserviceX'
    
    TempF = 'degreeFahrenheit'
    TempC = 'degreeCelsius'
    DegreesF = '°F'
    DegreesC = '°C'
    
    local function trace(a,b,c,d) return end
    
    function main(Data)
    
       local X = webserviceX.ConvertTemp(Data, TempF, TempC)
    
       if webserviceX.IsFault(X) then     
          -- Ooops!! Something happened so lets get the 
          -- fault message to determine what went wrong
          webserviceX.GetFault(X)     
       else
          -- Everyting is OK so lets create an       
          -- out message and push it to the queue 
          local Out = webserviceX.CreateOutMsg(Data, X, TempC):S()
          queue.push{data=Out}      
       end 
    
    end
  2. We also created a webserviceX module that contains additional functionality for error handling (along with our original web service logic). We’ve also taken the logical steps and placed them in their own functions. This results in much more manageable chunks of code.
    local function trace(a,b,c,d) return end
    
    function GetHL7Message(HL7)
       -- Lets begin by brininging in some sample data
       local Orig = hl7.parse{vmd='HL7_2.5.1.vmd',data=HL7}
       local Out = hl7.message{vmd='HL7_2.5.1.vmd',name=Orig:nodeName()}   
       Out:mapTree(Orig)
       return Out
    end
    
    function MakeRequest(HL7, TempFrom, TempTo)
    
       local P = xml.parse{data=D}
    
       P["soapenv:Envelope"]["soapenv:Body"]
       ["web:ConvertTemp"]["web:Temperature"][1]=
       HL7.PATIENT_RESULT[1].ORDER_OBSERVATION[1].OBSERVATION[2].OBX[5][1]
    
       P["soapenv:Envelope"]["soapenv:Body"]
       ["web:ConvertTemp"]["web:FromUnit"][1]=TempFrom
    
       P["soapenv:Envelope"]["soapenv:Body"]
       ["web:ConvertTemp"]["web:ToUnit"][1]=TempTo
    
       return P
    end
    
    function webserviceX.ConvertTemp(HL7,TempFrom, TempTo)
    
       local Msg = GetHL7Message(HL7)
       local P = MakeRequest(Msg, TempFrom, TempTo)
    
       -- Call the convert temperature web service 
       -- and save the result to a local variable  
       local R,C,D = net.http.post{url=Url.. 'ConvertTemperature.asmx', 
          headers={['Content-type']='text/xml;charset=UTF-8', 
             SOAPAction=SoapAction.. 'ConvertTemp'}, 
          body=P:S(),
          live=true}
    
       trace(C,D,R)
    
       -- Create variable "X" that points to our parsed soap response and 
       -- populate our outbound message with the converted temperature
       -- and units of measure
       return xml.parse{data=R} 
    
    end
    
    -- Returns nil if we do not have a SOAP fault response
    function webserviceX.IsFault(X)
       return X["soap:Envelope"]["soap:Body"]["soap:Fault"]   
    end
    
    -- Extracts the response message from a non fault response
    function GetResponse(X)
       return X["soap:Envelope"]["soap:Body"].
        ConvertTempResponse.ConvertTempResult[1]
    end
    
    -- Extracts the error message from a fault response
    function webserviceX.GetFault(X)
       return X["soap:Envelope"]["soap:Body"]
               ["soap:Fault"].faultstring[1]:S()
    end
    
    -- Create a message that will be sent to the destination endpoint
    function webserviceX.CreateOutMsg(HL7, X, TempTo)
       local Out = GetHL7Message(HL7)
    
       local TempValue
    
       if TempTo == TempC then
          TempValue = DegreesC
       else
          TempValue = DegreesF     
       end
    
       Out.PATIENT_RESULT[1].ORDER_OBSERVATION[1].OBSERVATION[2].OBX[5][1]=GetResponse(X)
       Out.PATIENT_RESULT[1].ORDER_OBSERVATION[1].OBSERVATION[2].OBX[6][2]=TempValue
    
       return Out
    end

2 Comments