4 Replies Latest reply on Aug 29, 2014 12:57 AM by mlybarger

    http service access binary content

    mlybarger

      i'm looking to put together a crude image upload flow.  the ultimate use case is that i have an http client application that will submit binary data to an http endpoint. so, to start, i want to post binary data, an image file and then just save the content on the server.

       

      My client is:

      curl -X POST --data-binary @KT-6341.jpg http://localhost:8080/http-binding/image

       

      I have an ImageBean that is promoted by an http binding.  I'm struggling with how to get the binary content.  When my client is invoked the service executes, but nothing is stored in a.jpg.  Any insights would be most helpful.

       

      Thanks,

      -mark-

       

       

      package org.switchyard.quickstarts.http.binding;
      
      
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      
      
      import javax.inject.Inject;
      
      
      import org.switchyard.Context;
      import org.switchyard.component.bean.Service;
      
      
      @Service(Image.class)
      public class ImageBean implements Image {
      
        @Inject
        private Context context;
      
          public String storeImage(InputStream is) {
         
          System.out.println("stored");
          storeToFile(is);
          return "stored";
         
          }
          
          private void storeToFile( InputStream is ){
          OutputStream os = null;
          try {
        os = new FileOutputStream("/a.jpg");
        int read = 0;
        byte[] bytes = new byte[1024];
         
        while ((read = is.read(bytes)) != -1) {
        os.write(bytes, 0, read);
        }
      
        } catch (Exception e) {
        e.printStackTrace();
        } finally {
        if (os != null) {
        try {
        os.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }
         
          }
      
      
      
      }
      
        • 1. Re: http service access binary content
          jorgemoralespou_2

          Hi,

          I'm not an expert, but some suggestions.

          • Use a camel route, it is much easier for this use case. (composite service binding: http, and composite reference binding: file), So your route could be:
          from("switchyard://YourHttpService").to("switchyard://YourFileReference")
          
          • Use ESB interface (as you do not need an java contract) --> inbound: java.lang.Object
          • Enable message tracing to see what is getting there, and where is the first point of error

           

          Hope that with this 3 tips, you can do it.

           

          Cheers,

          • 2. Re: http service access binary content
            mlybarger

            Thank you for responding Jorge. I did get a sample from JBoss support showing me to use a rest binding that took as input a InputStream.  That seemed to work. 

             

            As far as storing the bits to a file, that was just for an example, I needed to be able to process binary data posted to a url.  My end service wll transform the binary data (protobuf) to xml and post it to a queue.

            • 3. Re: http service access binary content
              jorgemoralespou_2

              HI Mark,

              Im glad if you have it working. It would be nice if you could explain your problem hand solution here in the community, as people may face similar problems but might not have support.

               

              cheers,

              • 4. Re: http service access binary content
                mlybarger

                You can checkout the http-binding project under branch 1.1.0.Final-ML on my fork of the switchyard quickstarts at: https://github.com/mlybarger/quickstarts.git

                 

                I used a rest binding to my service and took as input an HttpServletRequest object.  see the FileEchoServiceBean.java.  I also needed to use an http session to maintain state between client interactions.  You can notice the request.getSession interaction in the code.  To test the service, I used curl as my client program:

                 

                curl -b foo -c foo -H MSG:bar -X POST -d HI http://localhost:8080/rest/b/d

                 

                My example is just reading text, but as you can see, it has access to the request inputStream so, any data posted can be read.

                 

                This was developed on JBDS 7.1.1.GA using jboss fsw 6.0.0.GA,

                 

                HTH