Developing WebServices using JAX-WS

Let us assume an enterprise is maintaining user authentication details in a centralized system. We need to create an AuthenticationService which will take credentials, validate them and return the status. The rest of the applications will use the AuthenticationService to authenticate the Users.

Create AuthenticationService interface as follows:

package com.sivalabs.caas.services;

import javax.jws.WebService;

import com.sivalabs.caas.domain.AuthenticationStatus;
import com.sivalabs.caas.domain.Credentials;
import com.sivalabs.caas.exceptions.AuthenticationServiceException;

@WebService
public interface AuthenticationService
{
public AuthenticationStatus authenticate(Credentials credentials) throws AuthenticationServiceException;
}

package com.sivalabs.caas.domain;

/**
* @author siva
*
*/
public class Credentials
{
private String userName;
private String password;
public Credentials()
{
}
public Credentials(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
//setters and getters

}
package com.sivalabs.caas.domain;

/**
* @author siva
*
*/
public class AuthenticationStatus
{
private String statusMessage;
private boolean success;
//setters and getters

}
package com.sivalabs.caas.exceptions;

/**
* @author siva
*
*/
public class AuthenticationServiceException extends RuntimeException
{

private static final long serialVersionUID = 1L;
public AuthenticationServiceException()
{
}
public AuthenticationServiceException(String msg)
{
super(msg);
}
}

Now let us implement the AuthenticationService.

package com.sivalabs.caas.services;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;

import com.sivalabs.caas.domain.AuthenticationStatus;
import com.sivalabs.caas.domain.Credentials;
import com.sivalabs.caas.exceptions.AuthenticationServiceException;

/**
* @author siva
*
*/
@WebService(endpointInterface="com.sivalabs.caas.services.AuthenticationService",
serviceName="AuthenticationService",
targetNamespace="http://sivalabs.blogspot.com/services/AuthenticationService")
public class AuthenticationServiceImpl implements AuthenticationService
{
private static final Map<string, string> CREDENTIALS = new HashMap<string, string>();
static
{
CREDENTIALS.put("admin", "admin");
CREDENTIALS.put("test", "test");
}

@Override
public AuthenticationStatus authenticate(Credentials credentials) throws AuthenticationServiceException
{
if(credentials == null)
{
throw new AuthenticationServiceException("Credentials is null");
}
AuthenticationStatus authenticationStatus = new AuthenticationStatus();
String userName = credentials.getUserName();
String password = credentials.getPassword();

if(userName==null || userName.trim().length()==0 || password==null || password.trim().length()==0)
{
authenticationStatus.setStatusMessage("UserName and Password should not be blank");
authenticationStatus.setSuccess(false);
}
else
{
if(CREDENTIALS.containsKey(userName) && password.equals(CREDENTIALS.get(userName)))
{
authenticationStatus.setStatusMessage("Valid UserName and Password");
authenticationStatus.setSuccess(true);
}
else
{
authenticationStatus.setStatusMessage("Invalid UserName and Password");
authenticationStatus.setSuccess(false);
}
}
return authenticationStatus;
}
}

Here for simplicity we are checking the credentials against the static data stored in HashMap. In real applications this check will be done against database.

Now we are going to publish the WebService.

package com.sivalabs.caas.publisher;

import javax.xml.ws.Endpoint;

import com.sivalabs.caas.services.AuthenticationServiceImpl;

public class EndpointPublisher
{
public static void main(String[] args)
{
Endpoint.publish("http://localhost:8080/CAAS/services/AuthenticationService", new AuthenticationServiceImpl());
}

}

Run this standalone class to publish the AuthenticationService.

To check whether the Service published successfully point the browser to URL http://localhost:8080/CAAS/services/AuthenticationService?wsdl. If the service published successfully you will see the WSDL content.

Now let us create a Standalone test client to test the webservice.

package com.sivalabs.caas.client;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.sivalabs.caas.domain.AuthenticationStatus;
import com.sivalabs.caas.domain.Credentials;
import com.sivalabs.caas.services.AuthenticationService;

/**
* @author siva
*
*/
public class StandaloneClient
{

public static void main(String[] args) throws Exception
{
URL wsdlUrl = new URL("http://localhost:8080/CAAS/services/AuthenticationService?wsdl");
QName qName = new QName("http://sivalabs.blogspot.com/services/AuthenticationService", "AuthenticationService");
Service service = Service.create(wsdlUrl,qName);
AuthenticationService port = service.getPort(AuthenticationService.class);
Credentials credentials=new Credentials();
credentials.setUserName("admin1");
credentials.setPassword("admin");
AuthenticationStatus authenticationStatus = port.authenticate(credentials);
System.out.println(authenticationStatus.getStatusMessage());

credentials.setUserName("admin");
credentials.setPassword("admin");
authenticationStatus = port.authenticate(credentials);
System.out.println(authenticationStatus.getStatusMessage());
}

}

Instead of writing StandaloneClient by our-self we can generate the Client using wsimport commandline tool.
wsimport tool is there in JDK/bin directory.
Go to your project src directory and execute the following command.
wsimport -keep -p com.sivalabs.caas.client http://localhost:8080/CAAS/services/AuthenticationService?wsdl

It will generate the following java and class files in com.sivalabs.caas.client package.

Authenticate.java
AuthenticateResponse.java
AuthenticationService_Service.java
AuthenticationService.java
AuthenticationServiceException_Exception.java
AuthenticationServiceException.java
AuthenticationStatus.java
Credentials.java
ObjectFactory.java
package-info.java

Now you can use the generated Java files to test the Service.

public static void main(String[] args) throws Exception
{
AuthenticationService_Service service = new AuthenticationService_Service();
com.sivalabs.caas.client.AuthenticationService authenticationServiceImplPort = service.getAuthenticationServiceImplPort();
com.sivalabs.caas.client.Credentials credentials = new com.sivalabs.caas.client.Credentials();

credentials.setUserName("admin1");
credentials.setPassword("admin");
com.sivalabs.caas.client.AuthenticationStatus authenticationStatus = authenticationServiceImplPort.authenticate(credentials);
System.out.println(authenticationStatus.getStatusMessage());

credentials.setUserName("admin");
credentials.setPassword("admin");
authenticationStatus = authenticationServiceImplPort.authenticate(credentials);
System.out.println(authenticationStatus.getStatusMessage());
}

In next article I will show how to deploy this AuthenticationService in Tomcat.

Leave a Reply

Your email address will not be published. Required fields are marked *