Real world example of Web Service

Introduction

Today in this article we will look at a real world technical problem in an enterprise and how web service helps us in solving the problem. This is one of the main questions while appearing for Web Service interview. You can also get videos on Java Design Pattern, J2EE, Struts 2, Hibernate, Spring, Web services using Java/JaxWS/JAX RS.

If you are new to web services then view the following starter video for the same: –


Problem:

OAx organization is making use of Content management system (CMS) named Ex for storing documents pertaining to one application.


Due to the performance given by te Ex it was decided to replace other existing applications making use of other CMS to above one.

Possible solution 1

To achieve the above said solution, existing applications can make use of Ex api for doing operations with their documents.

Directly making CMS api had following disadvantages:

      Tomorrow if the above CMS tool has to be replaced by other one due to some reason , will imply changing the code in all the applications making use of above CMS tool.
      The Ex api has extensible support for Java but does not provide solution for Microsoft technologies. There are many applications already developed using .Net and it is not possible to completely rewrite the same.

Web service in action

The above problem can be solved using Web service.

Functionalities which are to be used by various applications will be now exposed as Web service.

Since web service is based on the principle of separation between interface and implementation. the implementation part of Web service will make use of core Ex api to achieve the required functionality.

Since web service provides transparency w.r.t programming language, applications written in various programming language can access the web service without any hindrance.

Hence using web service serves as the best solution in this scenario.

JAX WS—A hero in web service world

JAX WS helps in developing web service in the Java side.

Following are the things which indicate JAX WS as best solution: –

      Annotation driven: ease of development using annotation.
      MTOM: Message transmission optimization mechanism which provides advantage over previous Base 64 encoding for attachments.

Web service scenario exposing functionalities as service

Implementation details:

Let consider one of the functions to be provided by web service

        Insertion of pdf data in CMS (In our case we will store it in some location instead of CMS to keep it simple)
      We will implement the same using Eclipse Helios. In the example instead of storing the pdf file in CMS we will store the same at some location in hard drive

Step 1: Creating a Java project and name the project as DocumentProject

Step 2: Create a DocumentInt class within a package com.questpond.service with following method signature:

package com.questpond.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface DocumentInt {
	@WebMethod	public String upload(byte data[]) ;

}

Step 3: Create a DocumentImpl class implementing DocumentInt and giving body to upload . The function takes byte array and converts the same to a file at location
“D:/eclipse/Example.pdf”. MTOM is used to support optimization while transmitting binary data.

package com.questpond.service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;

@MTOM
@WebService(endpointInterface = "com.questpond.service.DocumentInt")
public class DocumentImpl implements DocumentInt {
@Override
public String upload(byte data[]) {		
OutputStream  fos;
try {
	fos = new FileOutputStream(new File("D://eclipse//Example.pdf"));
	fos.write(data);
	fos.flush();
        fos.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
				e.printStackTrace();
		}		
	return "success";

	}

}

Step 4: Create DocumentPublisher class which will publish the created web service. Put the following code inside the same:

package com.questpond.service;
import javax.xml.ws.Endpoint;

public class DocumentPublisher {
 public static void main(String[] args) {
Endpoint.publish("http://localhost:7656/questpond/image", new DocumentImpl());
 System.out.println("Server is published!");
   }
}

Step 5: Execute the publisher class so that web service is exposed. Confirm the same by putting following url in Web browser to get the exposed WSDL file. A snippet of the same is shown:


Step 6:
Create a client for web service .Let’s name the class as Main.

The Main class will be responsible to invoke the web service and since the parameter to the upload function is a byte array which contains file data, the client will convert file to a byte array and will pass the same to upload function which will convert the byte array to file. Put the following code with main function of Main class.

public static void main(String[] args) {
		URL url;
		byte[] bytes = new byte[5000];
		try {
		url = new URL("http://localhost:7655/questpond/image?wsdl");
		QName qname = new QName("http://service.questpond.com/",
					"DocumentImplService");
		Service service = Service.create(url, qname);
		DocumentInt server = service.getPort(DocumentInt.class);
		File file = new File("E:/Documents/view_pdf.pdf");
		bytes = new byte[(int) file.length()];
			try {
			FileInputStream fileInputStream = new FileInputStream(file);
			fileInputStream.read(bytes);
			} catch (FileNotFoundException e) {
				System.out.println("File Not Found.");
				e.printStackTrace();
			} catch (IOException e1) {
				System.out.println("Error Reading The File.");
				e1.printStackTrace();
			}

			server.upload(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

The above function reads a file from E:/Documents/view_pdf.pdf and converts the file to a byte array. This byte array is then used to invoke the upload function.

On executing the client we will get a file created in the location which we specified in upload function.

Conclusion:

Thus we have seen a real world scenario where web service is used and a partial implementation of the same. You can further enhance your knowledge on Web service by watching my videos on Web services and their implementation in Java using JAXWS and JAXRS.

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s