Announcement Announcement Module
Collapse
No announcement yet.
binary data and web services Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • binary data and web services

    Hi there,
    I need to exchange (send/receive) files (binary data) using web services.
    Any recommendations/suggestions/samples using spring web services? We using Hessian web services for binary data transfer in our other applications, but we need to do same using web services (spring/axis)

  • #2
    I think Axis 1 supports MIME attachments, see http://www-128.ibm.com/developerwork...ry/ws-soapatt/

    Comment


    • #3
      Well...I think that the question is "best practice to handle binary data with spring-ws"...isn't it?

      Comment


      • #4
        Yes, what is the best way to handle binary data with spring-ws

        Comment


        • #5
          Ok, I misunderstood the question.

          Spring-WS 1.0-M3 supports Soap with attachments at the SoapMessage interface level. So you can
          • implement MessageEndpoint
          • get the WebServicemessage out of the context
          • cast it to a SoapMessage
          • use addAttachment and the like

          I'm currently working on supporting the MTOM stuff in JAXB2, which means that you marshal a byte array to an attachment.
          Last edited by Arjen Poutsma; Jun 14th, 2007, 04:55 PM.

          Comment


          • #6
            Thanks a lot,
            Can you please tell when support for the MTOM will be available (I know that you very busy answering forum questions), and recommendations for using binary data (size) through web services - like having file > 1 Mb switch to other technology like Hessian?

            Comment


            • #7
              1.0 RC1 is planned for the 21st of May.

              As for recommendations: it's hard to tell without your specific requirements: type of clients, amount of simultaneous requests, etc. Hessian is a perfect solution for communication between two systems within an app, but it is less suitable when you want to expose something to the internet.

              Comment


              • #8
                Arjen, I've created SWS-138.

                Comment


                • #9
                  I know this is a thread from a while ago but I need help with simple attachments (not MTOM). I am attaching a simple text file on the client side and have written a simple interceptor on the server side to obtain the simple text file.
                  The client code is very very simple

                  Code:
                                  SOAPMessage message = messageFactory.createMessage();
                          SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
                          
                          Name newInvoiceReceivedRequestName = envelope.createName("NewInvoiceReceivedRequest", PREFIX, NAMESPACE_URI);
                          SOAPBodyElement newInvoiceReceivedRequestElement = message.getSOAPBody().addBodyElement(newInvoiceReceivedRequestName);
                          
                          Name newInvoiceReceivedName = envelope.createName("NewInvoiceReceived", PREFIX, NAMESPACE_URI);
                          SOAPElement newInvoiceReceivedElement = newInvoiceReceivedRequestElement.addChildElement(newInvoiceReceivedName);
                          
                          
                          Name applicationIdName = envelope.createName("applicationId", PREFIX, NAMESPACE_URI);
                          
                          newInvoiceReceivedElement.addChildElement(applicationIdName);
                  
                          //Attachment
                          AttachmentPart ap = message.createAttachmentPart();
                          File txtFile = new File("c:\\abcd\\HelloWorld.txt"); 
                          FileInputStream fileInputStream = new FileInputStream(txtFile);
                          byte[] fileByteArray =  IOUtils.toByteArray(fileInputStream) ;
                          ap.setContent(new ByteArrayInputStream(fileByteArray), "text/html");
                          ap.setContentId("testtxt");
                          message.addAttachmentPart(ap);
                  On the server side I have an interceptor like so:-

                  Code:
                  import java.io.File;
                  
                  import org.apache.commons.io.FileUtils;
                  import org.apache.commons.io.IOUtils;
                  import org.springframework.ws.mime.Attachment;
                  import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
                  import org.springframework.ws.soap.saaj.SaajSoapMessage;
                  import org.springframework.ws.context.MessageContext;
                  
                  public class MessageEndpointInterceptorAdapter extends EndpointInterceptorAdapter {
                  	
                  	private static final String CONTENT_ID = "testtxt";
                  	
                  	@Override
                  	public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
                  		SaajSoapMessage requestMessage = (SaajSoapMessage)messageContext.getRequest();
                  		
                  		Attachment attachment = requestMessage.getAttachment(CONTENT_ID);
                  		System.out.println("attachment size: "+attachment.getSize());
                  		//Write the file to disk
                  		saveByteArrayToFile(attachment);		
                          return true;
                      }
                  	
                  	private void saveByteArrayToFile(Attachment attachment) throws Exception {
                  		byte[] bytes = IOUtils.toByteArray(attachment.getInputStream());
                  		File tmp = File.createTempFile("sometxt", ".txt");
                  		FileUtils.writeByteArrayToFile(tmp, bytes);
                  		System.out.println("Wrote tmp file to: " + tmp.getAbsolutePath());
                  	}
                  When I run my client program, I get the following :-
                  Code:
                  java.security.PrivilegedActionException: com.sun.xml.internal.messaging.saaj.SOA
                  PExceptionImpl: Error during saving a multipart message
                          at java.security.AccessController.doPrivileged(Native Method)
                          at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.cal
                  l(HttpSOAPConnection.java:151)
                          at com.markview.saasserver.ws.test.SaasServerWebServiceTest.getNewInvoic
                  eRequest(SaasServerWebServiceTest.java:161)
                          at com.markview.saasserver.ws.test.SaasServerWebServiceTest.main(SaasSer
                  verWebServiceTest.java:185)
                  Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during s
                  aving a multipart message
                          at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.writeTo(MessageI
                  mpl.java:1233)
                          at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.pos
                  t(HttpSOAPConnection.java:313)
                          at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection$Pri
                  viledgedPost.run(HttpSOAPConnection.java:180)
                          ... 4 more
                  Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME
                   type text/html
                          at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:87
                  7)
                          at javax.activation.DataHandler.writeTo(DataHandler.java:302)
                          at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyP
                  art.writeTo(MimeBodyPart.java:901)
                          at com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMulti
                  part.writeTo(MimeMultipart.java:266)
                          at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.writeTo(MessageI
                  mpl.java:1224)
                  When I googled "no object DCH for MIME type text/html" and found that I need to do something on the server side to accept text/html MIME type attachments. Where should I specify the MIME type to be accepted?

                  When I replace the "Attachment" section in my client program with the following code, things are just fine.

                  Code:
                         //Attachment
                          AttachmentPart ap = message.createAttachmentPart(); 
                          String stringContent = "Hello World";
                          ap.setContent(stringContent, "text/html");
                          ap.setContentId("testtxt");
                          message.addAttachmentPart(ap);
                  Also, I moved from Java 5 to Java 6. Is it a javax.activation issue now that is included in the Java 6 kit. Or is it related to the other libraries that might already have been included in the 6 kit.

                  Thanks in advance.

                  Comment


                  • #10
                    The reason for exception - you are sending binary data with content-type="text/html"
                    Quick fix: read the text file to string and send this string to server like you did in second client sample.
                    If you need to send binary data, look at the article 'Tip: Passing files to a Web service' at http://www.ibm.com/developerworks/xm...x-tippass.html (how to use datahandler object)
                    and at the sample mtom from samples/mtom in spring-ws.
                    Hope this will help.

                    Comment


                    • #11
                      vicchugu, you da man. I followed your link and got it working man. It was stupid of me to put a byte array for a text/html.

                      Thanks again.

                      Comment


                      • #12
                        Glad to help.

                        Comment

                        Working...
                        X