Last active
March 25, 2020 13:24
-
-
Save chumpa/d56260226c9b664768b0ce47e2908bee to your computer and use it in GitHub Desktop.
Unzip input payload for SAP CPI, even form-data MIME
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import commons-fileupload-1.4.jar into your CPI resources | |
import com.sap.gateway.ip.core.customdev.util.Message as CpiMsg | |
import javax.activation.DataHandler | |
import org.apache.commons.fileupload.MultipartStream | |
import org.apache.commons.io.IOUtils | |
import javax.mail.internet.* | |
import java.util.zip.* | |
/** | |
* [email protected] | |
* 2020-03-25 | |
* | |
**/ | |
CpiMsg unzip1(CpiMsg msg) { | |
def mpl = messageLogFactory.getMessageLog(msg) | |
Map<String,DataHandler> atts = msg.attachments | |
def log = "Body content-type is ${msg.headers.'Content-Type'} and content-length is ${msg.headers.'Content-Length'}\n" << "Message attachments qty: ${atts.size()}\n" | |
atts.each{String k, DataHandler v -> | |
log << "Attachment $k content-type ${v.contentType}\n" | |
} | |
int cl = msg.headers."Conent-Length" ? (msg.headers."Conent-Length" as int) : 65536 | |
final ContentType ct = new ContentType(msg.headers.'Content-Type') | |
final String CD = "Content-Disposition: " | |
if (ct.primaryType=="multipart" && ct.subType=="form-data") { | |
final byte[] boundary = ct.getParameter("boundary").getBytes() | |
final MultipartStream multipartStream = new MultipartStream(msg.getBody(InputStream), boundary) | |
boolean nextPart = multipartStream.skipPreamble() | |
while (nextPart) { | |
boolean zip = false | |
String[] headers = multipartStream.readHeaders().split("\\r\\n") | |
ContentDisposition cd = null | |
for (String h: headers) { | |
if (h.startsWith(CD)) { | |
cd = new ContentDisposition(h.substring(CD.length())) | |
zip = zip || cd.getParameter("filename").endsWith(".zip") | |
} | |
} | |
if (zip) { | |
log << cd.getParameter("filename") << "\n" | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024) | |
multipartStream.readBodyData(bos) | |
bos.close() | |
ByteArrayInputStream bis2 = new ByteArrayInputStream(bos.toByteArray()) | |
ZipInputStream zis = new ZipInputStream(bis2) | |
ZipEntry ze = zis.getNextEntry() | |
while (ze!=null) { | |
log << "\t" << ze.getName() << "\n" | |
ze = zis.getNextEntry() | |
} | |
zis.close() | |
} | |
log << "\n" | |
nextPart = multipartStream.readBoundary() | |
} | |
} | |
if (ct.primaryType=="application" && ct.subType=="zip") { | |
log << ct.getParameter("filename") << "\n" | |
ZipInputStream zis = new ZipInputStream(msg.getBody(InputStream)) | |
ZipEntry ze = zis.getNextEntry() | |
while (ze!=null) { | |
log << ze.getName() << "\n" | |
ze = zis.getNextEntry() | |
} | |
zis.close() | |
} | |
msg.setBody(log as String) | |
msg | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo1 for form-data Content-Type:
Body content-type is multipart/form-data; boundary=--------------------------604670599132136208173838 and content-length is 3114794
Message attachments qty: 0
HCI Learning Material.zip
02b34619beb144b89710d7ab061158f5_content
0387b0fc31dd4ff283ad42d45f4cce8c_content
086e13052d944cfb91e63e3efdddf0f4_content
103d17cacff94fab89f9d67cb6d5a8f7_content
1050e2fe671b45ada02b1334ce7b611c_content
17f21ebea07742099ea0d5a42be3be64_content
1d18af9bd6324789ab45851bd9623c09_content
49bbc036473b4d37977459a81bc32ca2_content
4b3fbc3f6aed4172bde8d91989744255_content
4cf0b6b358924eb4827205fb6af41faf_content
518ab35d225d4c6e93490d27eff58ca5_content
5b4261edfb4448899d30a5faef03590a_content
68bef99045974525a17e29ead8386c4a_content
6d05b809b96d48de9723fb4e024c539c_content
78b695008f34431f9b909d7b3e9656e0_content
8181841817604160bbd5639fcde8ec2a_content
835c18ab4f454d4aa21d5c1941dee5de_content
8de93a9219c840e0a65c21a37532f179_content
aa8ee2e85ea349568786f80f068865f6_content
b40dc0f01cab46679b051dd5eb35b787_content
b7f985614d11479b8492a29cb2c6e443_content
bc4dc4b3923f402d88ae315fcd203fd4_content
c37bddf7d9024bd7b3e25485dd797eda_content
cc1e246f5889498189ad599920bcea62_content
f2752ad056194e979b97a1d0c3e7db20_content
resources.cnt
contentmetadata.md
hash
ExportInformation.info
Kings Bounty.zip
dosbox.conf
GAME/
GAME/256.CC
GAME/416.CC
GAME/JIRKA.DAT
GAME/KB.EXE
GAME/START.BAT
GAME/content.xml
GAME/WEB.BAT
info.txt
mapper.txt
msvcp71.dll
msvcr71.dll
Play NOW.exe
README.TXT
SDL.dll
SDL_net.dll
Demo2 for application/zip Content-Type:
Body content-type is application/zip and content-length is 414716
Message attachments qty: 0
null
02b34619beb144b89710d7ab061158f5_content
0387b0fc31dd4ff283ad42d45f4cce8c_content
086e13052d944cfb91e63e3efdddf0f4_content
103d17cacff94fab89f9d67cb6d5a8f7_content
1050e2fe671b45ada02b1334ce7b611c_content
17f21ebea07742099ea0d5a42be3be64_content
1d18af9bd6324789ab45851bd9623c09_content
49bbc036473b4d37977459a81bc32ca2_content
4b3fbc3f6aed4172bde8d91989744255_content
4cf0b6b358924eb4827205fb6af41faf_content
518ab35d225d4c6e93490d27eff58ca5_content
5b4261edfb4448899d30a5faef03590a_content
68bef99045974525a17e29ead8386c4a_content
6d05b809b96d48de9723fb4e024c539c_content
78b695008f34431f9b909d7b3e9656e0_content
8181841817604160bbd5639fcde8ec2a_content
835c18ab4f454d4aa21d5c1941dee5de_content
8de93a9219c840e0a65c21a37532f179_content
aa8ee2e85ea349568786f80f068865f6_content
b40dc0f01cab46679b051dd5eb35b787_content
b7f985614d11479b8492a29cb2c6e443_content
bc4dc4b3923f402d88ae315fcd203fd4_content
c37bddf7d9024bd7b3e25485dd797eda_content
cc1e246f5889498189ad599920bcea62_content
f2752ad056194e979b97a1d0c3e7db20_content
resources.cnt
contentmetadata.md
hash
ExportInformation.info