Voglio solo sapere, c'è un modo semplice per analizzare la risposta SOAP di MTOM/XOP. Il problema è che io uso semplicemente HTTP per inviare messaggi di sapone e javax.xml per la risposta di analisi. Ma alcuni servizi mi rispondono con mulipart/related e richiede una logica molto più complessa per analizzarlo (le prestazioni contano). Quindi mi chiedo se potrei in qualche modo sfruttare apache cxf, apache axiom o qualsiasi altra libreria per analizzare la risposta SOAP di MTOM/XOP?Come analizzare la risposta SOAP XOP/MTOM utilizzando java?
risposta
These unit tests mostra come utilizzare CXF per estrarre gli allegati da un messaggio MTOM. Io Inline uno dei test nel caso in cui questo link non esiste in futuro:
private MessageImpl msg;
@Before
public void setUp() throws Exception {
msg = new MessageImpl();
Exchange exchange = new ExchangeImpl();
msg.setExchange(exchange);
}
@Test
public void testDeserializerMtom() throws Exception {
InputStream is = getClass().getResourceAsStream("mimedata");
String ct = "multipart/related; type=\"application/xop+xml\"; "
+ "start=\"<[email protected]>\"; "
+ "start-info=\"text/xml; charset=utf-8\"; "
+ "boundary=\"----=_Part_4_701508.1145579811786\"";
msg.put(Message.CONTENT_TYPE, ct);
msg.setContent(InputStream.class, is);
AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
deserializer.initializeAttachments();
InputStream attBody = msg.getContent(InputStream.class);
assertTrue(attBody != is);
assertTrue(attBody instanceof DelegatingInputStream);
Collection<Attachment> atts = msg.getAttachments();
assertNotNull(atts);
Iterator<Attachment> itr = atts.iterator();
assertTrue(itr.hasNext());
Attachment a = itr.next();
assertNotNull(a);
InputStream attIs = a.getDataHandler().getInputStream();
// check the cached output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(attBody, out);
assertTrue(out.toString().startsWith("<env:Envelope"));
// try streaming a character off the wire
assertTrue(attIs.read() == '/');
assertTrue(attIs.read() == '9');
}
Nel tuo caso, il ct
proverrà dal tipo di contenuto dell'intestazione della risposta. Il "mimedata"
sarà il contenuto della risposta.
L'ho fatto allo stesso modo. Basta non avere un tempo per condividere. Grazie per la risposta! –
Caro Daniel, Potresti indicarmi il tipo di contenuto correlato alla domanda http://stackoverflow.com/questions/37455584/manually-parse-mtom-message –
Non c'è bisogno di utilizzare CXF, lo standard javax.mail.internet.MimeMultipart classe fare il lavoro ed è molto facile da usare (anche per creare richiesta MTOM).
Ecco un esempio molto semplice da decodificare parti di una risposta MTOM:
MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(data, contentType));
int count = mp.getCount();
for (int i = 0; i < count; i++) {
BodyPart bp = mp.getBodyPart(i);
bp.saveFile(filepath + "_" + i);
}
ho avuto lo stesso problema e risolto come @Nicolas Albert
public byte[] mimeParser(InputStream isMtm) {
ByteArrayOutputStream baos = null;
try {
MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(isMtm,
ct));
int count = mp.getCount();
baos = new ByteArrayOutputStream();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mp.getBodyPart(i);
if (!Part.ATTACHMENT
.equalsIgnoreCase(bodyPart.getDisposition())
&& !StringUtils.isNotBlank(bodyPart.getFileName())) {
continue; // dealing with attachments only
}
bodyPart.writeTo(baos);
}
byte[] attachment = baos.toByteArray();
FileUtils.writeByteArrayToFile(new File("E:/wss/attachment.zip"), attachment);
return attachment;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (Exception ex) {
}
}
}
return null;
}
Hai mai trovato una risposta? –