Wednesday, 2 October 2013

Axis2 WebService Client using Struts2 and DAO with MYSQL and Glassfish

 Axis2 WebService Client Using Struts2 and DAO with MYSQL


Hi  Reader,

      Now, lets discuss about Axis2 Webservice Client Withs Struts2 and MySql using Glassfish.

WebService can convert our Application into an webapplication which can publish it's function or message to the rest of the world.
Basic webservice is the combination of XML + HTTP. XML provides a language which can be used in different platforms and programming languages.

WebService elements are SOAP(Simple Object Access Protocol),UDDI,WSDL(Web Service Descriptator Language). 

Simple Object Access Protocol(SOAP) :

SOAP is the communication protocol for sending messages inform of XML between different platform.

Web Service Descriptor Language(WSDL) :

WSDL is a language to locate and describe the webservice. WSDL is based on the XML language.

Universal Description, Discovery and Integration(UDDI) :

UDDI is a directory for storing information about web service. UDDI communicates via SOAP.


Web service clients are created from a WSDL document which describes where the Web service is deployed and what operations this service provides. You can use either the static WSDL file generated by the Web Service wizard, or when creating JAX-WS Web services you can use the dynamic WSDL file generated by the runtime based on information it gathers from the annotations added to the Java classes.

Here we have existing Axis 2 webservice with WSDL file.We need to create client class from the WSDL file.

Axis 2 Data Binding(ADB) :

Data binding is the term used for to handle the conversion between XML and Application data structures.
Apache Axis2 data binding framework is designed to work with verity of data binding frameworks.


WebService Client using WSDL2JAVA :

Axis2 provides a WSDL2JAVA used for code generation from a WSDL service description. We can use this tool directly by running org.apache.axis2.wsdl.WSDL2JAVA class as a java application.

WSDL2JAVA provides several command line options.

1. -o  path - Sets the target directory or output folder.
2. -p package name - Sets the target package for generated classes.
3. -d name - Sets the data binding framework.
4. -uri path - Sets the path to the WSDL for service to be generated.

WSDL2JAVA -o src -p com.satya.sreemandira -uri http://localhost:8080/primeservice/prime?wsdl.

import javax.xml.ws.WebServiceRef;  

public class WebClient{

@WebServiceRef(wsdlLocation="http://localhost:8080/axisservice/axis?wsdl")
static AxisService;

public void getWsdlInfo(wsdlLocation loc,wsdlName name){

URL url=new URL(loc);
String namespace="http://Axis.com";
String servicename="AxisService";

string args=WSDL2JAVA -o src -p com.satya.sreemandira -uri<wsdllocation> -xnocompile; 

if(args != null){
QName qname=new QName(targetNameSpace,servicename);
Service service=Service.create(url, qname);
Port port=service.getPort(SampleRequest.class);
}

}

public static void main(String[] args){

WebClient client=new WebClient();
client.getwsdlInfo();
}

}

WSIMPORT

wsimport -s src http://example.com/webservice?wsdl

Step 1 :

Invoking WebService:

WebServiceClientUtility class is used to invoke webservice.

WebServiceClientUtility :

public class WebServiceClientUtility{

     public HashMap invokeWebService(String ModuleName, String ServiceName, String operationName,                       Object reqresobj, Object cobolStateObject, Object j2eeStateObject) through AppException{

                  String packageName=serviceName.toLowerCase().trim();
                  String moduleName=moduleName.toLowerCase().trim();
                      try{
                           }catch(Exception e){ }
                     return response;
            }
Step 2 :

Create PoliceService interface for getting policy information. PoliceService will define methods that PolicyAccessServiceImpl will access.

PoliceService :

public interface PoliceService{

                       public HashMap getPoliceInfo(PoliceBean pb) throws AppException;
                 }

Step 3 :

 Create PoliceServiceImpl for implementing methods from PoliceService Interface.

PoliceServiceImpl :

public class PoliceServiceImpl implements PoliceService{

     private static final String CLASSNAME=PoliceServiceImpl.class.getName();
     
      public HashMap getPoliceInfo(PoliceBean policebean) throws AppException{

                    final String METHOD_NAME="getPolicyInfo";
                    Logger.info(CLASSNAME + " "+ METHOD_NAME + " : " + "Status");

                    HashMap map = new HashMap();
                    CobolState cobolstate = policebean.getCobolState();
                    J2eeInput javastate = policebean.getJ2eeInput();
              
                   // Result map will populated if there is no error.

                    WebServiceClientUtility clientutility = new WebServiceClientUtility();
                     map = clientutility.invokeWebService("ixx", "ModuleName", "OperationName", policybean, 
                                                                              cobolstate, javastate);
                     PoliceBean bean = (PoliceBean)map.get(FEAPConstants.USE_CASE_OBJECT);

                   // Error message will populated for error conditions.
    
                   ArrayList error=(ArrayList)map.get("messageList");
                       
                        if(error.isEmpty()){
                                                          System.out.println("Map is Empty");
                                                    }
                  else{
                                                          System.out.println("Error Code");
                         }
                  LoggerMSG.info(CLASSNAME + " " + METHOD_NAME + " : " + "Ends");
                  return map;
             }

 Step 4 :

Business Delegate :

   Business Delegate hides the implementation details of the business service such as look up and access mechanisms.

       





     1. Minimizes the coupling between client and business service, by hiding the implementation details of the          service such as look up and access.
     2. Hides the details of the service creation,reconfiguration and invocation from the client.
     3.  Translates the network exceptions into application or user exceptions.
     
            PoliceDelegate is used to delegate the response to the Action class. Action class will populate data to JSP page.

PoliceDelegate :

     public class PoliceDelegate {

              private static final String CLASSNAME = PoliceDelegate.class.getName();

               public HashMap getPoliceInfo(PolicyBean pb){
                              
                               final String METHOD_NAME = "getPolicyInfo";
                               LoggerMSG.info(CLASSNAME + " " + METHOD_NAME + " : " + "Status");
                               PoliceService policyservice = new PoliceServiceImpl();

                               HashMap map = new HashMap();
                               map=policeservice.getPoliceInfo(pb);
                                   
                               LoggerMSG.info(CLASSNAME + " " + METHOD_NAME + " : " + "Ends");

                                return map;
                         }

Step 5 :

           We need to configure the webservice in the WebServiceMetaData.xml file. We need to configure the XML file using ServiceName, WSDL Location, Target Namespace, Service Endpoint.

We need to collect all the wsdl files from the server. Create a folder wsdl in our local machine drive(D:).
Put all the wsdl files in the wsdl folder. Finally point that location to the <wsdlLocation> in WebServiceMetaData.xml file.

<serviceEndPoint> is used to point to the endpoint location where the webservice resides.

 WebServiceMetaData.xml

   <wsdlproperties>
         <webservice>
               <ServiceName>ServiceName</ServiceName>
               <wsdlLocation>D:\wsdl\ipp.wsdl</wsdlLocation>
               <targetNameSpace> Target Name Space will pick up from wsdl file </targetNameSpace>
               <serviceEndPoint>http://10.0.50.145:9008</serviceEndPoint>
       </webservice>
  </wsdlproperties>


I will continue the rest in my next blog.....
                               
     
                    





                                                               




      

No comments:

Post a Comment

Spring 3 Rest Web Service Batch Update with JQuery & Ajax Integration

                      Spring 3  Rest Web Service Batch Update with JQuery & Ajax Integration Hi     In this blog I will describe the...