8 Replies Latest reply on Feb 17, 2003 2:02 PM by didi

    Soap message creation with an attachment

    pvamstel

      Can someone give me a working example of the creation of a soap message with an example.

      I get a null pointer every time.

      Working with the axis stuff.

      Here is some code that does not work

      import java.io.BufferedReader;
      import java.io.ByteArrayInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.io.IOException;

      import javax.xml.soap.AttachmentPart;
      import javax.xml.soap.MessageFactory;
      import javax.xml.soap.SOAPEnvelope;
      import javax.xml.soap.SOAPException;
      import javax.xml.soap.SOAPMessage;

      import org.apache.axis.soap.MessageFactoryImpl;

      public class Test1 {

      public static void main(String[] args)
      {

      MessageFactory fac = new MessageFactoryImpl() ;

      SOAPMessage msg;
      try {
      msg = fac.createMessage();
      SOAPEnvelope nEnv= msg.getSOAPPart().getEnvelope();


      String pdfFileName = "e:\\tmp\\test.pdf";
      FileReader fr;
      fr = new FileReader(pdfFileName);
      BufferedReader buffr = new BufferedReader(fr);
      String sPdf="";
      String line = null;
      try {
      line = buffr.readLine();
      } catch (IOException e) {
      System.err.println(e.toString());
      }
      while(line!=null)
      {
      sPdf += line;
      try {
      line = buffr.readLine();
      } catch (IOException e) {
      System.err.println(e.toString());
      }
      }


      byte[] pdfData = sPdf.getBytes();
      ByteArrayInputStream stream = new ByteArrayInputStream(pdfData);

      //CREATE ATTACHMENT ADD THE STREAM AS CONTENT
      AttachmentPart attPDF = msg.createAttachmentPart();
      attPDF.setContent(stream, "application/pdf");
      msg.addAttachmentPart(attPDF);
      } catch (SOAPException e) {
      System.err.println(e.toString());
      } catch (FileNotFoundException e) {
      System.err.println(e.toString());
      }




      }
      }


      Get a

      java.lang.NullPointerException
      at org.apache.axis.Message.createAttachmentPart(Message.java:612)
      at Test1.main(Test1.java:62)
      Exception in thread "main"

        • 1. Re: Soap message creation with an attachment
          prabhakar

          The msg.createAttachment() method takes a javax.activation.DataHandler object as a parameter. Try creating the pdf attachment as a dataHandler object.

          -prabhakar

          • 2. Re: Soap message creation with an attachment
            pvamstel

            Did not do the trick.

            When i call addAttachmentPart with a newly created attachment. I get back a null pointer.

            I've browsed the code of apache but i'm not sure what it is doing. Has anyone have a simple example of creating a new soap message with an attachent.

            • 3. Re: Soap message creation with an attachment
              rgjawanda

              I am in the middle of trying to setup a Web Service using message based protocol. I want to send back the response with an attachment. I am having all kinds of problems.
              I am using jboss3.0.4 and have axis 1.0 installed.
              I am able to send and receive soap messages but attachments are a real real headache.
              There seems to be many ways of doing it but I can't get one way to work.

              I hope we can work together to resolve it somehow.
              I can post code or whatever.
              Ron





              • 4. Re: Soap message creation with an attachment
                pvamstel

                Happy to hear i'm not the only one not understanding the attachment stuff.

                What i'm doing does not involve a webservice. I just want to create a soap message with an attachment and then serialize the message to disk

                If you want to mail me mail me to pvamstel@hotmail.com

                grt Patrick

                • 5. Re: Soap message creation with an attachment
                  pvamstel

                  Finnally
                  I found it.


                  You have to add activation.jar and mail.jar from the tomcat distribution in your classpath

                  hopes this helps for you as well


                  • 6. Re: Soap message creation with an attachment
                    prabhakar

                    yes. you are right. The datahandler class is in the JAF package.

                    -prabhakar

                    • 7. Re: Soap message creation with an attachment
                      rgjawanda

                      From my test cases I have determined that it is in fact a bug and that attachments don't work in version 3.0.4.
                      I added soap2.2 to my deploy directory. Compiled the samples and attachments work fine.
                      Here is what I did find out.
                      Axis works find RPC, MSG services except when you try
                      to get the Soap Envelope. At this point jboss (or Axis) seems to get confused and cannot locate several methods that actually exist. This happened for a select handfull of methods. I absolutely know that I am calling them correctly because they work with clients that are calling them from a DOS java mainline program. Just not within a servlet on the jboss system.
                      SOAP 2.2 does everything correctly and works just fine.
                      You can get at the message body, header, and add attachments. I know the behaviors are different in soap but at least it works.

                      Ron

                      • 8. Re: Soap message creation with an attachment
                        didi

                        this means the posted source code from above works? (if no: can you please post the working source code? tnx)