BPEL HelloWorld Example with Eclipse and Apache ODE

Hi Guys as i explained in my previous post how to install Apache ODE and Eclipse BPEL Designer now im here to explain how to deploy the first BPEL process flow

Objective: deploying a BPEL process which receives an input string from a client and responses it exactly that input.
Tools: Eclipse 3.6.2, BPEL Designer 0.5.0 Plugin, Tomcat 7.0.12, ODE 1.3.5

step1.Create new BPEL project: File→New→Others→BPEL 2.0→BPEL Project.
step2.Name it BPEL_HelloWorld. Select the Target Runtime as Apache ODE 1.x Runtime and Configuration as Defaut Configuration for Apache ODE 1.x Runtime. Click Finish.

bpel_hello_world

step3.In the folder BPEL_HelloWorld\bpelContent, File → New → Other → BPEL 2.0 → New BPEL process file.
step4.Name it HelloWorld, Namespace http://helloworld.localhost and select Template Synchronous BPEL Process. Click Next.

bpel_hello_world1

step5.Edit the Service Address to : http://localhost:8080/ode/processes/HelloWorld (suppose that your Tomcat environment runs at port 8080) and keep other default declarations. Click Finish.

bpel_hello_world2

step6. Three files are created in the folder bpelContent and the HelloWorld.bpel is opened for editing the BPEL process.
step7. Click to open the Palette panel on the right of the canvas. (or right click on the canvas, select Show Pallette on Palette View)

bpel_hello_world3

step8. Drag the Assign action next to the receiveInput action and delete the FIX_ME-Add_Business_Logic_Here.

bpel_hello_world7

step9. Right click on the Assign activity. Select Show in properties.
step10.In the Properties tab, select Details. Click on the New button to define an assign operator.
step11.Assign from input→payload→input to output→payload→result. An Initializer popup dialog appear. Click Yes to initialize the output variable and save the process.

3ca9f195f7403fcc0bcb9f5a3029db16.media.900x433

step12. It’s time now to deploy the BPEL project. Click on the folder bpelContent, select File→New→Others→BPEL 2.0→Apache ODE Deployment Descriptor.
step13. Click Next. Verify the BPEL Project name as BPEL_HelloWorld. Click Finish.

bpel_hello_world5

step14 .Double click on the deploy.xml file in the bpelContent folder.
step15. In the Inbound Interfaces table, select the Partner Link as client, Associate Port as HelloWorldPort. Click on Related Service then other fields will be automatically filled. Save the deploy.xml file.

bpel_hello_world6

step16. To deploy the project on the server, right click on the Ode v1.x Server at localhost, select Add and Remove Projects.
step17.Select the BPEL_HelloWorld/HelloWorld in the Available projects box and click Add. Then, click Finish

bpel_hello_world8

step18. Start the ODE server. If the deployment was successful, you will see the following output in the console tab.

bpel_hello_world9

step19. We will test the project with Web Services Explorer provided by Eclipse. Right click on the HelloWorldArtifacts.wsdl file. Select Web Services→Test with Web Services Explorer.
step20. Select the operation process. Then, type Hello ODE in the input textbox. If your deployment was success, you will get the same string in the SOAP response.

bpel_hello_world10

We are Done
Happy Coding 🙂

Building REST web services using Jersey and Maven

Hi guys, here is my post regarding how to develop restful web services using jersey and maven. Before we go into development of service lets discuss some concepts of REST.
What Are RESTful Web Services?

RESTful web services are built to work best on the Web. Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as performance, scalability, and modifiability, that enable services to work best on the Web. In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs), typically links on the Web. The resources are acted upon by using a set of simple, well-defined operations. The REST architectural style constrains an architecture to a client/server architecture and is designed to use a stateless communication protocol, typically HTTP. In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol.

The following principles encourage RESTful applications to be simple, lightweight, and fast:

Resource identification through URI: A RESTful web service exposes a set of resources that identify the targets of the interaction with its clients. Resources are identified by URIs, which provide a global addressing space for resource and service discovery. See The @Path Annotation and URI Path Templates for more information.

Uniform interface: Resources are manipulated using a fixed set of four create, read, update, delete operations: PUT, GET, POST, and DELETE. PUT creates a new resource, which can be then deleted by using DELETE. GET retrieves the current state of a resource in some representation. POST transfers a new state onto a resource. See Responding to HTTP Methods and Requests for more information.

Self-descriptive messages: Resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others. Metadata about the resource is available and used, for example, to control caching, detect transmission errors, negotiate the appropriate representation format, and perform authentication or access control. See Responding to HTTP Methods and Requests and Using Entity Providers to Map HTTP Response and Request Entity Bodies for more information.

Summary of JAX-RS Annotations

@Path
The @Path annotation’s value is a relative URI path indicating where the Java class will be hosted: for example, /helloworld. You can also embed variables in the URIs to make a URI path template. For example, you could ask for the name of a user and pass it to the application as a variable in the URI: /helloworld/{username}.

@GET
The @GET annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP GET requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

@POST
The @POST annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP POST requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

@PUT
The @PUT annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP PUT requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

@DELETE
The @DELETE annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP DELETE requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

@HEAD
The @HEAD annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP HEAD requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

@PathParam
The @PathParam annotation is a type of parameter that you can extract for use in your resource class. URI path parameters are extracted from the request URI, and the parameter names correspond to the URI path template variable names specified in the @Path class-level annotation.

@QueryParam
The @QueryParam annotation is a type of parameter that you can extract for use in your resource class. Query parameters are extracted from the request URI query parameters.

@Consumes
The @Consumes annotation is used to specify the MIME media types of representations a resource can consume that were sent by the client.

@Produces
The @Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client: for example, “text/plain”.

@Provider
The @Provider annotation is used for anything that is of interest to the JAX-RS runtime, such as MessageBodyReader and MessageBodyWriter. For HTTP requests, the MessageBodyReader is used to map an HTTP request entity body to method parameters. On the response side, a return value is mapped to an HTTP response entity body by using a MessageBodyWriter. If the application needs to supply additional metadata, such as HTTP headers or a different status code, a method can return a Response that wraps the entity and that can be built using Response.ResponseBuilder.

Now lets see how to develop the rest service.
1.first create a maven project and add the following dependencies to your POM.xml as below –

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spark.jersy.service</groupId>
	<artifactId>JersyService</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>JersyService</name>
	<description>JersyService</description>


	<dependencies>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.17</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-servlet</artifactId>
			<version>1.17</version>
		</dependency>
		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>jsr311-api</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			<version>1.17</version>
		</dependency>
	</dependencies>
</project>

now its to code your rest service, lets name the service as RestService in the package “com.spark.rest.service” as below –

/**
 * 
 */
package com.spark.rest.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * @author Sony
 *
 */
@Path("/hello")
public class RestService {

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{name}")
	public String getMessage(@PathParam("name") String name){

		return "{'name':"+name+",'desg':'SoftWareEngg','Age':'26'}";
	}

}

lets publish our web service as a servlet so, lets code web.xml as below –

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
	<servlet>
		<servlet-name>jersey-servlet</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
		
		<init-param>
			<param-name>com.sun.jersey.config.property.packages</param-name>
			<param-value>com.spark.rest.service</param-value>
		</init-param>
		
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>jersey-servlet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

Test your service by deploying into Tomcat(any version, preferably >5.x) and request from browser as below –
http://localhost:8080/JerseyService/rest/hello/{yourname}”, in return you get a response of JSON string.

suppose, if u want to test the web service using a standalone client lets code a main method as below –

/**
 * 
 */
package com.spark.rest.service.client;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

/**
 * @author Sony
 * 
 */
public class JerseyRestClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ClientConfig clientConfig = new DefaultClientConfig();
		Client client = Client.create(clientConfig);

		WebResource webResource = client.resource(getBaseURI());

		System.out.println(webResource.path("rest").path("hello/pavan")
				.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class)
				.toString());

	}

	public static URI getBaseURI() {
		return UriBuilder.fromUri("http://localhost:8080/JersyService").build();
	}

}

References:(with courtesy)
1.http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html
2.http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/
3.http://www.vogella.com/articles/REST/article.html

Happy RESTful coding 🙂

Consuming web service using Spring 3.x

Hi guys, Here i am going to demonstrate how to consume web service using spring framework.Consuming web services with Spring framework is amazingly easy. It avoids the need of creating client side stubs during compile time and does the same at run time. A typical web service client can be configured as shown below-

inorder to get the spring capability for webservice please add the following dependencies to pom.xml as below-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spark.spring.webservice</groupId>
  <artifactId>SpringWebService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringWebService</name>
  <description>SpringWebService</description>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-core</artifactId>
  		<version>3.1.1.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>3.1.1.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context-support</artifactId>
  		<version>3.1.1.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-beans</artifactId>
  		<version>3.1.1.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.ws</groupId>
  		<artifactId>spring-ws-core</artifactId>
  		<version>2.1.1.RELEASE</version>
  	</dependency>
  </dependencies>
</project>

please configure your applicationContext.xml as below-

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

	<bean id="myWebService"
		class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
		<property name="serviceInterface" value="com.spark.webservice.TemperatureConverter" />
		<property name="wsdlDocumentUrl" value="http://localhost:8080/JBossWebServices?wsdl" />
		<property name="namespaceUri" value="http://impl.webservice.spark.com/" />
		<property name="serviceName" value="TemperatureConverter" />
		<property name="portName" value="TemperatureConverterImplPort" />
	</bean>

</beans>

Where serviceInterface is the business interface that clients will use for invoking service methods. wsdlDocumentUrl is the URL for the published WSDL file. Spring will download WSDL from the given URL and generate the client side stubs when it creates the bean usually during application start up. namespaceUri corresponds to the targetNamespace in the .wsdl file. serviceName corresponds to the service name in the .wsdl file. portName corresponds to the port name in the .wsdl file.

Note Please refer to my previous port to get the wsdl file.

Now accessing web service pretty easy, just get the bean from Spring context and use that to invoke methods-

/**
 * 
 */
package com.spark.spring.webservice.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spark.webservice.TemperatureConverter;

/**
 * @author Sony
 *
 */
public class SpringWebserviceClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		TemperatureConverter converter = (TemperatureConverter)applicationContext.getBean("myWebService");
		System.out.println(converter.convertCelsiusToFahrenheit(42.0));
	}

}

inorder to use the service we need to create the service interface type as below –

/**
 * 
 */
package com.spark.webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * @author Sony
 *
 */
@SuppressWarnings("restriction")
@WebService
public interface TemperatureConverter {

	@WebMethod
	public double convertCelsiusToFahrenheit(double value);
	@WebMethod
	public double convertFahrenheitToCelsius(double value);
}

Thats it now run the main method and get the result.
Note: the service should be up and running in the server.

Happy Coding 🙂

Creating JavaWebService and JavaClient using Apache Axis2

Hi all, I have got lots of requests to demonstrate creation of webservices using java and Apache Axis2 api so here i go.

1. Setup the Development Environment

1.1. First you need to set up the development environment. Following things are needed if you want to create Web Services using Axis2 and Eclipse IDE(Juno 4.2).

Some Eclipse versions have compatibility issues with Axis2. This tutorial is tested with Apache Axis2 1.5.2, Eclipse Helios and Apache Tomcat 6.

1) Apache Axis2 Binary Distribution – Download
2) Apache Axis2 WAR Distribution – Download
3) Apache Tomcat – Download
4) Eclipse IDE(Juno 4.2) – Download
5) Java installed in your Computer – Download

Then click on the Web Service –-> Axis2 Preferences and browse the top level directory of Apache Axis2 Binary Distribution.
2. Creating the Web Service Using Bottom-Up Approach

2.1 First create a new Dynamic Web Project (File –> New –-> Other…) and choose Web –> Dynamic Web Project.
2.2 Set Apache Tomcat as the Target Runtime and click Modify to install Axis2 Web Services project facet.
2.3 Select Axis2 Web Services
2.4 Click OK and then Next. There you can choose folders and click Finish when you are done

3. Create Web Service Class

/**
 *
 */
package com.spark.webservice;

/**
 * @author Mantha Pavan Kumar
 *
 */
public class JavaWebService {

	/**
	 * @param firstnumber
	 * @param secondNumber
	 * @return
	 */
	public int addNumbers(int firstnumber,int secondNumber){

		return firstnumber+secondNumber;
	}
}

Then, select File –> New –-> Other and choose Web Service.
Click Next and make sure Generate a default service.xml file is selected.
You can go to http://localhost:8080/JavaWebService/services/listServices to see your running service which is deployed by Axis2. You can see the WSDL by clicking the link JavaWebService.

Creating a Web service client
Select File –> New –> Other… and choose Web Service Client
Set he newly created Axis2 Web service (http://localhost:8080/JavaWebService/services/listServices) as the Service definition. Then configure the Server runtime as previously and click finish.

This will generate two new classes called JavaWebServiceStub.java and JavaWebServiceCallbackHandler.java. Now we can create test class for client and use our web service. Create new class called ServiceTest.java and paste following code.

/**
 *
 */
package com.spark.webservice;

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;

import com.spark.webservice.JavaWebServiceStub.AddNumbers;
import com.spark.webservice.JavaWebServiceStub.AddNumbersResponse;

/**
 * @author Mantha Pavan Kumar
 *
 */
public class ServiceTest {

	/**
	 * @param args
	 * @throws RemoteException
	 */
	public static void main(String[] args) throws RemoteException {
		// TODO Auto-generated method stub

		JavaWebServiceStub serviceStub = new JavaWebServiceStub();
		AddNumbers addNumbers = new AddNumbers();
		addNumbers.setFirstnumber(10);
		addNumbers.setSecondNumber(20);

		AddNumbersResponse addNumbersResponse = serviceStub.addNumbers(addNumbers);
		System.out.println( addNumbersResponse.get_return());

	}

}

Now you can run the above code as java application and you will get the output
follow these screen shots for creation of webservices Creation of Java WebService
Happy Coding 🙂