Hi carrier,
Although semantically correct, I believe Nathan's proposed solution is somewhat incomplete.
I guess he forgot do Base64 decode the extracted text prior to step 1. I would also include an improvement so that no human intervention is needed by verifying the temporary file using 3rd party ZIP utilities.
That would translate into accessing the temporary ZIP file and checking whether it's a valid ZIP file. Therefore, an algorithm for implementing your requirement would be:
- Access element Base64-encoded text
content and Base64 decode it
- Output ZIP raw text into a temporary file, thus creating a ZIP file
- Check if the temporarily created ZIP file is valid
With all that in mind, here's how the complete Groovy script would look like:
import org.apache.commons.codec.binary.Base64
// Step 1: Access element Base64-encoded text content and Base64 decode it
String tempZipFilename = "temp.zip"
def textBase64 = context.expand(
'${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' )
def b64 = new Base64()
def zipTextBytes = b64.decode(textBase64.getBytes())
// Step 2: Output ZIP raw text into a temporary file
def zipFile = new java.io.File(tempZipFilename)
FileOutputStream fos = new java.io.FileOutputStream(zipFile)
fos.write( zipTextBytes )
fos.flush()
fos.close()
log.info "Temporary ZIP file stored as: ${zipFile.getCanonicalPath()}"
// Step 3: Check if the temporarily created ZIP file is valid
boolean responseValid
try {
zipFile = new java.util.zip.ZipFile(tempZipFilename)
responseValid = true
log.info "Number of files in the ZIP file: ${zipFile.size()}"
} catch (java.util.zip.ZipException e) {
responseValid = false
log.error "The received response contains a bad ZIP"
}
log.info "Web service response is " + (responseValid ? "valid" : "invalid")
Please let me know if this works for you as it does for me. :-)
Cheers!
Shonzilla
p.s. I would suggest adding "ZIP" tag to this question so that people can more easily find a solution for handling ZIPs from Groovy that's embedded here.