Skip to Content

Suppose that a scenerio needs a way to send data to a remote service to do some specific work for you. To reach that goal, there are many ways; may be the most popular one in this SOA driven world is the web services. Think of a moment that you also need to send binary data with plain text ( for instance a pdf document, a jpg image, etc.) in this scenerio. What’s the right way, or what can you do if the only way is integrating with the web services.

 

You can send this binary data with the web service, more specifically, with what’s called SOAP with Attachments, sometimes referred as “web services with attachments” in the forums. And the good news is for String and byte[] parameters, you can use this feature with Netweaver Developer Studio and SAP Web AS.

Our little task is as follows :  

Scenerio: Sending product specification with a product image to the headquarters.
Implementation : SOAP with Attachments
Result : The specifications and the image(BLOB) is stored in the company’s database for later use.

Lets start with the web service generation: In my previous blog How-To Create Dynamic & Configurable Web Services Easily With Netweaver Development Components (Part I) I simply talked about creating a web service within minutes. Our approach will be the same with small differences. This time we go step by step to understand details. We start by creating an enterprise archive project. Then start to build our web service with building an EJB module project and adding an EJB(Stateless session bean) with method registerProducts.

 

This method takes two arguments : product name type: String which is our product specification (In real life scenerios you can add more features, implement them in java beans etc, for simplicity I will use only one property), and product image in binary(byte[]) format. I will return a “file saved” message with date and time if the operation is successfull.

The method simply gets the binary data from our web service and write to database.. Upps no, simple is good, to filesystem. (I won’t talk about writing it to database, there are many articles about this subject. (once you have your data as byte[] you can do anything).

 

 

Our method is as follows :

public String registerProduct(String productName, byte[] productImage) {
try{
File f = new File("D:\tmp\"+productName);
FileOutputStream fos = new FileOutputStream(f);
fos.write(productImage);
fos.flush();
fos.close();
return "file saved @"+new java.util.Date()+"";
}
catch (FileNotFoundException e) {
// XXX Auto-generated catch block
e.printStackTrace();
return "Error saving product";
}
catch (IOException e) { // XXX Auto-generated catch block
e.printStackTrace();
return "Error saving product;
}
}

 

Now for the web service part, first we need to create a virtual interface from our EJB and include our only method registerProduct.

The next thing we will do is to create a web service definition from the virtual interface. It will the base for our web service.

 

The last step is the web service deployment descriptor and adding a web service into this descriptor by selecting EJB, Web service definition we previously define and the name of the web service.

The most important part is adding a configuration, and the simple thing we should do is to create a transport binding with HTTP SOAP with Attachments. After creating this we should see Attachments property under our configuration, with our methods and parameters. That’s it. Our web service is ready to take binary data with SOAP with attachments protocol, and save it to filesystem or database.

 

One last thing to mention is to give necessary permissions to the folder you try to save the binary data.

For the sake of seeing the difference between the definitions of your web service and the others, just look at the Web Services navigator (http://local.bb.name.tr:50100/wsnavigator) (put your server host and port)

 

 

In my next blog I will create a web service, that also takes binary data(without attachments), compare the services'(SOAP with and without attachments) performance with a simple testing program which uses standalone proxy.

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

  1. Sudip Das
    Hi
       Baris, thanks for the excellent blog. My problem is how I can consume that web service (with attachment facility) from a web dynpro application. Previously I have accessed simpler web services (without attachment facility) from web dynpro application. But when I am importing (i.e creating model) that WS (with attachment facility), the wizard can’t show the Virtual Interface (VI). Is there any requirement of SOAP adapter to use these types of WS?
    (0) 
    1. Former Member Post author
      Sudip,
      Just start by creating an ordinary web service, and use the wsdl with style mime.

      By this way a logical port is created for you under web services, this is what you will use.

      Dont map the byte[] type to your controller or view context, just send it externally like this code because there’s a problem in this.

      For my attachment web service i created the code for you :


      byte[] mybytes = readFile("C:\\test.gif");
                SOAP_W_ATTACH_MODEL model = new SOAP_W_ATTACH_MODEL();
                com.sap.test.soapwithattach.wdp.modela.proxies.SOAPWAttachWSImpl service = model.getServiceImpl();
                ConfigWAttachmentBindingStub stub = (ConfigWAttachmentBindingStub) service.getLogicalPort(ConfigWAttachmentBindingStub.class);
                stub.registerProduct("Tester",mybytes);

      (0) 
      1. Former Member
        Does it have to be a WebService (with attachments) created by SAP. I wanted to check whether SAP iview could consume a WebService with Attachments created by Third Party Applications like Siebel. Would you have any example?
        (0) 
  2. Former Member
    Thinks for your document, but i want to know how to call
    the web service . I had write a class to  call the web service
    but it was failed.

    import javax.xml.namespace.QName;

    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;

    public class Testservice {

         public static void main(String[] args) throws Exception{
              String endpoint = “http://10.100.3.20:50000/FunTestWeb/Config1“;
              Service service = new Service();
              Call call = (Call)service.createCall();
              //call.setTargetEndpointAddress(endpoint);
              call.setTargetEndpointAddress(new java.net.URL(endpoint));
              call.setOperationName(new QName(“getMessage”));
              Object returnValue =
                   call.invoke(new Object[] { new String(“ANDY”) });
              
              System.out.println((String) returnValue);
              
         }
    }

    (0) 
  3. Former Member
    Hi,

    Thanks for the excellent blog.  I am fairly new to this, but our scenario is the same as your example.

    We are on 6.2 and our customer has an application that wants to call our SAP system with a web service and the result of that call are material information, with the binary data for the material.

    I have verified that 6.2 can create web services, but I need help in defining a new RFC enabled function that will send binary data.

    Any help is appreciated.

    Thanks.
    Jp

    (0) 
  4. Former Member
    Hi,

    Your blog is really interesting and resourcefull.
    However i have a requirement where i need to send the attachment from a ABAP generated web service. Is it possible to do so or is it supported only from JAVA based web services usign EJBs etc. I am not conversant with Java so any help with ABAP side will be highly appreciated.

    (0) 

Leave a Reply