Announcement Announcement Module
Collapse
No announcement yet.
[B]Problem handling Doc/Jpeg attachment file types in SAAJ at interceptors.[/B] Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [B]Problem handling Doc/Jpeg attachment file types in SAAJ at interceptors.[/B]

    I am using SAAJ 1.3 for sending attachments from client to server.

    My client code looks like this...
    Code:
    SOAPMessage message = messageFactory.createMessage();
    SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
    SOAPBody soapBody = envelope.getBody();
    SOAPHeader header = envelope.getHeader();
    SOAPFactory soapFactory = SOAPFactory.newInstance();
    
    DataSource dataSource = new FileDataSource("C:/test.txt");
    DataHandler dataHandler= new DataHandler(dataSource);
    ByteArrayOutputStream attachmentStream = new ByteArrayOutputStream();	
    
    InputStream is = new BufferedInputStream(dataHandler.getInputStream());
     int size = is.available();
    int bytesRead = 0;
    byte[] buffer = new byte[size];
    while ((bytesRead = is.read(buffer, 0, size)) != -1)
     {
    	attachmentStream.write(buffer, 0, bytesRead);
    }
    dataHandler.writeTo(attachmentStream);
    is.close();
    attachmentStream.close(); 
    AttachmentPart attachmentPart = message.createAttachmentPart(dataHandler);
    attachmentPart.setContentId("A1");
    message.addAttachmentPart(attachmentPart);
    message.saveChanges();
    message.writeTo(System.out);
    // and im sending the soap message to the server....
    My server-side interceptor looks like this...
    Code:
    public class HeaderLogger implements EndpointInterceptor
    {
    public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception
    {
     WebServiceMessage requestMessage = messageContext.getRequest();
     AbstractSoapMessage absSoapMessage = (AbstractSoapMessage) requestMessage;
     SaajSoapMessage tmpSaaj =(SaajSoapMessage) absSoapMessage;
     SOAPMessage tmpSoapmsg = tmpSaaj.getSaajMessage();
     java.util.Iterator iterator = tmpSoapmsg.getAttachments();
    while (iterator.hasNext()) 
    {
      AttachmentPart attachment = (AttachmentPart)iterator.next();
      Object content = attachment.getContent();
      System.out.println(attachment.getContent().toString());
    Except for a .txt file , it prints out some erroneous special characters whenever Doc/jpeg file is attached.
    Is it like we have to encode it to some other format before sending the attachment to avoid those special characters.

    I'm using springWS 1.0 rc-2 on Apache Tomcat 5.0 and the jars used for SAAJ are saaj-api-1.3.jar and saaj-impl-1.3.jar.
    Last edited by Sriram Krishnan; Oct 4th, 2007, 07:03 AM. Reason: clarity

  • #2
    Hi.

    Instead of writing it to system.out you should write it into a file. Writing binary data to the console even makes your speaker sing...

    Cheers,
    Max

    Comment


    • #3
      Hi.
      Even I tried writing to a file but the data received at the server side is truncated
      and nothing is written in to the file..

      Comment


      • #4
        Hi,

        Thanks...I changed the SpringWS version to 1.0.1 and its working now..

        Comment

        Working...
        X