Developing web services -part 2

Developing a CreditCardservice and Deploying in Tomcat

  1. Create a directory structure Axis2Example for our Credit Card Validation Web Service as shown below in Figure 1.

Figure 1. Axis2Example Project Directory Structure

Create a CheckSEI.java interface with within the package com.ibm.axis2.ccheck as shown below.

Listing 1. CheckSEI.java

package com.ibm.axis2.ccheck;
import javax.jws.WebService;
@WebService (name="CheckCard",
targetNamespace="http://ccheck.axis2.ibm.com")
public interface CheckSEI {
public String doCheck(String targetName);
}
  1. Create a CheckCard.java class inside com.ibm.axis2.ccheck package as shown in Listing 2.

Listing 2. CheckCard.java

package com.ibm.axis2.ccheck;
importjavax.jws.WebService;
@WebService(serviceName="CheckCardService",
portName="cport",
endpointInterface="com.ibm.axis2.ccheck.CheckSEI",
targetNamespace="http://ccheck.axis2.ibm.com")
publicclassCheckCardimplementsCheckSEI{
publicstaticfinalintINVALID=-1;
publicstaticfinalintVISA=0;
publicstaticfinalintMASTERCARD=1;
publicstaticfinalintAMERICAN_EXPRESS=2;
publicstaticfinalintEN_ROUTE=3;
publicstaticfinalintDINERS_CLUB=4;
privatestaticfinalString[]cardNames=
{"Visa","Mastercard","AmericanExpress","EnRoute",
"Diner'sCLub/CarteBlanche",
};
publicstaticbooleanvalidCC(Stringnumber)
throwsException{
intCardID;
if((CardID=getCardID(number))!=-1)
returnvalidCCNumber(number);
returnfalse;
}
publicstaticbooleanvalidCCNumber(Stringn){
try{
intj=n.length();
String[]s1=newString[j];
for(inti=0;i<n.length();i++)s1[i]=""+n.charAt(i);
intchecksum=0;
for(inti=s1.length-1;i>=0;i-=2){
intk=0;
if(i>0){
k=Integer.valueOf(s1[i-1]).intValue()*2;
if(k>9){
Strings=""+k;
k=Integer.valueOf(s.substring(0,1)).intValue()+
Integer.valueOf(s.substring(1)).intValue();
}
checksum+=Integer.valueOf(s1[i]).intValue()+k;
}
else
checksum+=Integer.valueOf(s1[0]).intValue();
}
return((checksum%10)==0);
}
catch(Exceptione){
e.printStackTrace();
returnfalse;
}
}
publicstaticintgetCardID(Stringnumber){
intvalid=INVALID;
Stringdigit1=number.substring(0,1);
Stringdigit2=number.substring(0,2);
Stringdigit3=number.substring(0,3);
Stringdigit4=number.substring(0,4);
if(isNumber(number)){
if(digit1.equals("4")){
if(number.length()==13||number.length()==16)
valid=VISA;
}
elseif(digit2.compareTo("51")>=0&&digit2.compareTo("55")<=0){
if(number.length()==16)
valid=MASTERCARD;
}
elseif(digit2.equals("34")||digit2.equals("37")){
if(number.length()==15)
valid=AMERICAN_EXPRESS;
}
elseif(digit4.equals("2014")||digit4.equals("2149")){
if(number.length()==15)
valid=EN_ROUTE;
}
elseif(digit2.equals("36")||digit2.equals("38")||
(digit3.compareTo("300")>=0&&digit3.compareTo("305")<=0)){
if(number.length()==14)
valid=DINERS_CLUB;
}
}
returnvalid;
}
publicstaticbooleanisNumber(Stringn){
try{
doubled=Double.valueOf(n).doubleValue();
returntrue;
}
catch(NumberFormatExceptione){
e.printStackTrace();
returnfalse;
}
}
publicstaticStringgetCardName(intid){
return(id<-1&&id<cardNames.length?cardNames[id]:"");
}
publicStringdoCheck(StringaCard){
Strings="";
booleanb;
try{
if(getCardID(aCard)>-1)
{
s="Thisisa"+getCardName(getCardID(aCard))+"Card.";
if(b=validCC(aCard))
{
s=s+"Thecardnumber"+aCard+"isVALID";
}
else{s=s+"Thecardnumber"+aCard+"isIN-VALID";}
}
else{return("INVALIDINPUT");}
}
catch(Exceptione){}
return(s);
}
}

Now create a services.xml file inside META-INF directory as shown in Listing 3.Listing 3. Services.xml

<service>
<description>
This Web Service Validates CreditCard Number.
</description>
<parameter >com.ibm.axis2.ccheck.CheckCard</parameter>
<operation>
<messageReceiver/>
</operation>
</service>

Listing 3. Services.xml

Now open the command prompt by typing cmd in START > Run.

See figure

Figure 2. Opening Command Prompt

Move to the directory ccheck and type javac *.java as shown below (in order to run javac command from the project directory (Axis2Example, you need to set <JAVA_HOME>bin| to the Path of the user variable).

Figure 3. Compile the CreditCard interface and class

Now move to Code First Service directory and type jar cvf CreditCardService.aar * as shown below to generate the aar file. (See Figure 4)

Figure 4. Generate CreditCardService.aar file

Start tomcat and open browser by typing http://localhost:8080/axis2 and click on Administration

Figure 5. Running Axis2 on Tomcat

You need to provide the default username: admin and password: axis2 to login as shown below in Figure 6.

After sucessfully logging, click on Upload web service and the browse the CreditCardService.aar file in theAxis2Example|Code First Service directory and finally click on Upload (to sucessfully deploy the web service) as shown below in Figure 7

Now click on Available Service or paste this link http://localhost:8080/axis2/axis2-admin/listService in the browser to find out the deployed CreditCard web service as shown below in Figure 8

Clink on the service to check the WSDL of the CreditCardService at http://localhost:8080/axis2/services/CheckCardService?wsdl

Stub Generation

Open command prompt and move to the <AXIS2_HOME>bin| directory and type the following command as shown in Figure 9.

wsdl2java.bat -uri http://localhost:8080/axis2/services/CheckCardService?wsdl -o client

Figure 9. Stub generation for credit Card Service

(You will find the stubs name : CheckCardServiceCallbackHandler.java, CheckCardServiceStub.java and ExceptionException.java are generated inside <AXIS2_HOME>|bin|src|com|ibm|axis2|ccheck| directory).

Developing POJO Client for the CreditCardService

  1. Create File > New > Java Project name Axis2Client in Eclipse as shown in Figure 10.

Create a package com.ibm.axis2.ccheck inside Axis2Client|src folder as shown in Figure 11

Now Select the Axis2Client Project and Configure the build path as shown below in Figure 12

Click on Add External JARs and select all the jars from <AXIS2_HOME>lib| and then click OK

Now copy the Generated Stubs and paste it in the com.ibm.axis2.ccheck package of the Axis2Client project as shown in Figure 13.

Now create a TestClient.java class inside the com.ibm.axis2.ccheck package as shown below.

Listing 4. Services.xml

package com.ibm.axis2.ccheck;
import java.io.*;
import com.ibm.axis2.ccheck.*;
public class TestClient {
public static void main(String[] args) throws Exception {
CheckCardServiceStub stub = new CheckCardServiceStub();
//Create the request
com.ibm.axis2.ccheck.CheckCardServiceStub.DoCheck  request = new
com.ibm.axis2.ccheck.CheckCardServiceStub.DoCheck();
System.out.println("Enter your Credit Card Number:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
request.setArgs0(s);
//Invoke the service
com.ibm.axis2.ccheck.CheckCardServiceStub.DoCheckResponse response
= stub.doCheck(request);
System.out.println(response.get_return());
} }