You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Andrew Smith <as...@smithdot.com> on 2003/09/26 17:05:25 UTC

Document Style Service with Attachments

I have a very simple web service that echos back the xml given that
looks like this:

public class MessageService 
{
    public Document echoDocument(Document doc) throws Exception 
    {
      return doc;
    }
} 

On the client I have the various methods setup to retrieve the result
vector and print out the document.

So far so good. 

Now when I try to add an attachment from the server the result vector on
the client is empty. Using TCPMonitor I can see that the mime message is
coming through just fine. A perl client using SOAP::Lite is able to see
the data too.

public class MessageService 
{
    public Document echoDocument(Document doc) throws Exception 
    {
      DataSource ds = new FileDataSource("/tmp/msg.pdf");
      AttachmentPart att = new AttachmentPart(new DataHandler(ds));
      Message msg =
	(Message)MessageContext.getCurrentContext().getResponseMessage();
      msg.addAttachmentPart(att);
      msg.saveChanges();

      return doc;
    }
} 

Has anyone written an axis client for a document style service that can
handle an attachment?

-a.


For completeness here is the MessageClient:

public class MessageClient 
{
    public static void main(String[] args) throws Exception 
    {
      DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.parse(args[0]);

      System.out.println("sending " + XMLUtils.DocumentToString(doc));

      Service  service = new Service();
      Call call = (Call) service.createCall();
      String endpoint =
"http://localhost:8080/msgservice/services/MessageService";
      call.setTargetEndpointAddress(new URL(endpoint));
      call.setOperation(new QName(endpoint, "echoDocument"),
"echoDocument"); 

      Object ret = call.invoke(new Object[] {new
SOAPBodyElement(doc.getDocumentElement())});
      Vector result = (Vector) call.invoke(new Object[] {new
SOAPBodyElement(doc.getDocumentElement())});      
      System.out.println(result);
      /*
      SOAPBodyElement sbe = (SOAPBodyElement) result.get(0);
      Document retDoc = sbe.getAsDocument();
      System.out.println("received " +
XMLUtils.DocumentToString(retDoc));
      */
    }
}


Re: Document Style Service with Attachments

Posted by Andrew Smith <as...@smithdot.com>.
Figured this out yesterday.

The server piece is fine. On the server in order to attach documents
with axis you just do the following:

  DataSource ds = new FileDataSource("/tmp/msg.pdf");
  AttachmentPart att = new AttachmentPart(new DataHandler(ds));
  Message msg =
	(Message)MessageContext.getCurrentContext().getResponseMessage();
  msg.addAttachmentPart(att);
  msg.saveChanges();

To see these at the client you need the following code:

Iterator iterator = call.getResponseMessage().getAttachments();
if (iterator.hasNext())
{
  AttachmentPart part     = (AttachmentPart) iterator.next();
  DataHandler    dh       = part.getDataHandler();
  dh.writeTo(new java.io.FileOutputStream("somefilename"));
}

-a.

On Fri, 2003-09-26 at 11:05, Andrew Smith wrote:
> I have a very simple web service that echos back the xml given that
> looks like this:
> 
> public class MessageService 
> {
>     public Document echoDocument(Document doc) throws Exception 
>     {
>       return doc;
>     }
> } 
> 
> On the client I have the various methods setup to retrieve the result
> vector and print out the document.
> 
> So far so good. 
> 
> Now when I try to add an attachment from the server the result vector on
> the client is empty. Using TCPMonitor I can see that the mime message is
> coming through just fine. A perl client using SOAP::Lite is able to see
> the data too.
> 
> public class MessageService 
> {
>     public Document echoDocument(Document doc) throws Exception 
>     {
>       DataSource ds = new FileDataSource("/tmp/msg.pdf");
>       AttachmentPart att = new AttachmentPart(new DataHandler(ds));
>       Message msg =
> 	(Message)MessageContext.getCurrentContext().getResponseMessage();
>       msg.addAttachmentPart(att);
>       msg.saveChanges();
> 
>       return doc;
>     }
> } 
> 
> Has anyone written an axis client for a document style service that can
> handle an attachment?
> 
> -a.
> 
> 
> For completeness here is the MessageClient:
> 
> public class MessageClient 
> {
>     public static void main(String[] args) throws Exception 
>     {
>       DocumentBuilder builder =
> DocumentBuilderFactory.newInstance().newDocumentBuilder();
>       Document doc = builder.parse(args[0]);
> 
>       System.out.println("sending " + XMLUtils.DocumentToString(doc));
> 
>       Service  service = new Service();
>       Call call = (Call) service.createCall();
>       String endpoint =
> "http://localhost:8080/msgservice/services/MessageService";
>       call.setTargetEndpointAddress(new URL(endpoint));
>       call.setOperation(new QName(endpoint, "echoDocument"),
> "echoDocument"); 
> 
>       Object ret = call.invoke(new Object[] {new
> SOAPBodyElement(doc.getDocumentElement())});
>       Vector result = (Vector) call.invoke(new Object[] {new
> SOAPBodyElement(doc.getDocumentElement())});      
>       System.out.println(result);
>       /*
>       SOAPBodyElement sbe = (SOAPBodyElement) result.get(0);
>       Document retDoc = sbe.getAsDocument();
>       System.out.println("received " +
> XMLUtils.DocumentToString(retDoc));
>       */
>     }
> }
>