Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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 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.

7 Comments