4 Replies Latest reply on Jan 31, 2005 9:10 AM by Thomas Diesler

    XML Payload Best Practice

    Johan Borchers Newbie

      That is also what I use. Sending a XML document in a String return value. You can read about that at the following link.

      http://java.sun.com/blueprints/guidelines/designing_webservices/html/webservdesign8.html#1135159

      Johan.

        • 1. Re: XML Payload Best Practice
          Thomas Diesler Master

          The upcomming jboss-4.0.1 release maps xsd:anyType to org.w3c.dom.Element which makes it easier to exchange arbitary XML fragments. This however goes beyond the JAXRPC spec and is therefore not portable and maybe not interoperable.

          If your .NET client is BasicProfile-1.0 compliant you should expect good results with SOAP with Attachments (SwA). This would be the way to do it.

          Returning an XML fragment as string is quite different from the idea of exchanging business documents. The WSDL and its associated schemas (which is the abstract contract between the parties) will not define the structure of the "string" and the receiving side has no way to validate it.

          Resorting to xsd:anyType or string should only be considered if you have truely unstructured (random) XML fragments to exchange.

          • 2. Re: XML Payload Best Practice
            Yevgen Bushuyev Newbie

            If you need to upload/download attachments you can do it with jboss 4.0.0:

            /**
             * @return
             * @throws RemoteException
             * @ ejb.interface-method
             */
             public FileInfo downloadFile(int id) throws RemoteException {
             try {
             File[] files = (new File(FILES_DIR)).listFiles();
            
             MessageContext msgContext = MessageContext.getCurrentContext();
             Message reqMsg = msgContext.getResponseMessage();
            
             reqMsg.getAttachmentsImpl().setSendType(
             org.apache.axis.attachments.Attachments.SEND_TYPE_DIME);
            
             DataHandler dh = new DataHandler(new URL("file:"
             + files[id].getAbsolutePath()));
             AttachmentPart attachmentPart = new AttachmentPartImpl(dh);
            
             reqMsg.addAttachmentPart(attachmentPart);
            
             FileInfo fileInfo = new FileInfo();
             fileInfo.setId(id);
             fileInfo.setName(files[id].getName());
             fileInfo.setSize(files[id].length());
            
             return fileInfo;
            
             } catch (ArrayIndexOutOfBoundsException aie) {
             throw new RemoteException("Can't find file with id " + id);
            
             } catch (MalformedURLException e) {
             throw new RemoteException(e.getMessage(), e);
            
             }
             }
            
             /**
             * @return
             * @throws RemoteException
             * @ejb.interface-method
             */
             public void uploadFile(String file) throws RemoteException {
             try {
             MessageContext msgContext = MessageContext.getCurrentContext();
             Message reqMsg = msgContext.getRequestMessage();
             Attachments messageAttachments = reqMsg.getAttachmentsImpl();
            
             if (null == messageAttachments) {
             throw new RemoteException("No attachments found");
            
             }
            
             int attachmentCount = messageAttachments.getAttachmentCount();
            
             if (attachmentCount == 0) {
             throw new RemoteException("No attachments found");
             }
            
             Iterator it = messageAttachments.getAttachments().iterator();
            
             int count = 0;
             while (it.hasNext()) {
             AttachmentPart part = (AttachmentPart) it.next();
            
             InputStream is = part.getDataHandler().getInputStream();
            
             FileOutputStream fos = new FileOutputStream(FILES_DIR
             + File.separator + file);
             int b = 0;
             byte[] buff = new byte[10240];
             while ((b = is.read(buff)) > 0) {
             fos.write(buff, 0, b);
             }
             fos.flush();
             fos.close();
             }
            
             } catch (SOAPException e) {
             throw new RemoteException(e.getMessage(), e);
            
             } catch (FileNotFoundException e) {
             throw new RemoteException(e.getMessage(), e);
            
             } catch (IOException e) {
             throw new RemoteException(e.getMessage(), e);
            
             }
            
             }
            
            


            Corresponding C# code:


             private void testUpload(Test1.localhost1.CRTServiceWse crtWs)
             {
            
             FileStream fs = File.OpenRead(@"C:\tmp\test.pdf");
            
             byte[] buffer = new byte[32768];
             MemoryStream ms = new MemoryStream();
            
             while (true)
             {
             int read = fs.Read (buffer, 0, buffer.Length);
             if (read <= 0)
             break;
             ms.Write (buffer, 0, read);
             }
            
             fs.Close();
            
            
             //
             // Create a new Attachment class, and add the buffer to that
             //
             Attachment attachment = new Attachment("text/pdf", ms);
             crtWs.RequestSoapContext.Attachments.Add(attachment);
            
             crtWs.uploadFile(new System.Byte[3]);
            
             }
            
             private void testDownload(Test1.localhost1.CRTServiceWse crtWs )
             {
             crtWs.downloadFile(new System.Byte[3]);
             System.Collections.IEnumerator e = crtWs.ResponseSoapContext.Attachments.GetEnumerator();
             System.Console.WriteLine(crtWs.ResponseSoapContext.Attachments.Count);
             while(e.MoveNext())
             {
             DimeAttachment da = (DimeAttachment)e.Current;
            
             string fname = @"C:\tmp\save"+da.Id+".jar";
            
             FileStream fs = File.Create(fname);
             byte[] buffer = new byte[32768];
             while (true)
             {
             int read = da.Stream.Read (buffer, 0, buffer.Length);
             if (read <= 0)
             break;
             fs.Write (buffer, 0, read);
             }
            
             fs.Close();
            
            
             }
             }
            
            


            wbr, eugen.

            • 3. Re: XML Payload Best Practice
              Steven Wisener Newbie

               

              "thomas.diesler@jboss.com" wrote:
              The upcomming jboss-4.0.1 release maps xsd:anyType to org.w3c.dom.Element which makes it easier to exchange arbitary XML fragments. This however goes beyond the JAXRPC spec and is therefore not portable and maybe not interoperable.


              Thomas, did this not make it into the final release of 4.0.1? I've tried to map xsd:anyType to Element, but I'm not having any luck. I've looked through the source and can't see anywhere where this mapping might be taking place, though I might be looking in the wrong place...what serializer/deserializer is it supposed to use?