Showing posts with label SOAP. Show all posts
Showing posts with label SOAP. Show all posts

Thursday, August 7, 2014

SOAP With Attachment with Java, working example

This code actually works , and produces a SOAM message with attachment of type application/octet-stream - which is totally acceptable, even if I would have preferred application/pdf.


import javax.xml.soap.*;
import java.net.URL;
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.activation.*;


// now all the SOAP code
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder builder = dbFactory.newDocumentBuilder();
// add the SOAP body as from text
StringReader sr = new StringReader(textToSend);
InputSource is = new InputSource(sr);
Document document = builder.parse(is);
SOAPBody body = envelope.getBody();
SOAPBodyElement docElement = body.addDocument(document);
// add attachment from file          

String filename = theBUSINESSIDVALUE.split("\\^")[0];
File fileAttachment = new File(rootFolderForMSS + filename + ".pdf");
FileDataSource fds = new FileDataSource(fileAttachment);
DataHandler dh = new DataHandler(fds);
out.write("content-type=" + dh.getContentType() + "
"); AttachmentPart attachment = message.createAttachmentPart(dh); String contentId = "<" + filename + ".pdf>"; attachment.setContentId(contentId); message.addAttachmentPart(attachment); SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); java.net.URL endpoint = new URL(POSTTOURL); SOAPMessage soapResponse = connection.call(message, endpoint);


I had tried this one before:

AttachmentPart attachment = message.createAttachmentPart();

String filename = theBUSINESSIDVALUE.split("\\^")[0];
File fileAttachment = new File(rootFolderForMSS + filename + ".pdf");
byte[] fileData = new byte[(int) fileAttachment.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(fileAttachment));
dis.readFully(fileData);
dis.close();
  
String contentId = "<" + filename + ".pdf>";
attachment.setContent(new ByteArrayInputStream(fileData), "application/pdf");
attachment.setContentId(contentId);
message.addAttachmentPart(attachment);


but it was failing with this error
 javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/pdf
        at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:877)
        at javax.activation.DataHandler.writeTo(DataHandler.java:302)
        at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
        at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:874)
        at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:444)
        at weblogic.xml.saaj.SOAPMessageImpl.writeMimeMessage(SOAPMessageImpl.java:959)
        at weblogic.xml.saaj.SOAPMessageImpl.writeTo(SOAPMessageImpl.java:832)
        at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:237)
        at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)



For more info see:

http://docs.oracle.com/javaee/5/tutorial/doc/bnbhr.html
http://docs.oracle.com/javase/6/docs/api/javax/activation/FileDataSource.html
http://docs.oracle.com/javase/6/docs/api/javax/activation/DataHandler.html


I would rather say that it could be all a LOT simpler.... Java Java...too verbose...

Friday, January 7, 2011

SOAP: the S stands for Simple

http://harmful.cat-v.org/software/xml/soap/simple


hahaha this is really hilarious. The people who have invented SOAP should be crucified by their balls - assuming they have 2 - and hung with their guts - sure they have, it takes guts to impose to the world such a pathetic underspecified underperforming ball of mud.

Sunday, April 11, 2010

SOAP messages patterns

a useful collection of SOAP message patterns:

http://www.w3.org/TR/xmlp-scenarios/

a message for all seasons....


Especially interesting:

http://www.w3.org/TR/xmlp-scenarios/#S807


TrackingHeader with Via Intermediary with Timestamp 
(excellent for troubleshooting and performance monitoring!)
 
<t:TrackingHeader xmlns:t="http://example.org/2001/06/tracking">
      <t:Via>
        <t:Intermediary>soap://A.com/some/endpoint</t:Intermediary>
        <t:Timestamp>2001-03-09T08:00:00Z</t:Timestamp>
      </t:Via>
 
 
 
and the response correlated to the messageId
 <n:MsgHeader xmlns:n="http://example.org/requestresponse">
      <n:MessageId>uuid:09233523-567b-2891-b623-9dke28yod7m9</n:MessageId>
      <n:ResponseTo>uuid:09233523-345b-4351-b623-5dsf35sgs5d6</n:ResponseTo>

  </n:MsgHeader>