Saturday, April 23, 2016


Use WSO2 ESB to test SOAP web service


In this task you should access 3rd party web service through WSO2 ESB and you are going to test the service through ESB. For that you are going to access Temperature Service hosted by w3schools. This is a SOAP web service you are going to get wsdl of it.

Step 1) Navigate to \wso2esb-4.8.0\wso2esb-4.8.0\bin location and run wso2server.bat file. If you are in Unix environment run wso2server.sh file [./wso2server.sh]

step 2) Before starting wso2esb you have to set environment variable (JAVA_HOME) as follows

JAVA_HOME = C:\Program Files\Java\jdk1.7.0

Then set JAVA_HOME for path variable as well. Edit path variable and add following entry and click ok

%JAVA_HOME%\bin;

Step 3) Set JAVA_HOME through command prompt. (try below commands)
Start => run => cmd
echo %JAVA_HOME%

(It should display JAVA_HOME = C:\Program Files\Java\jdk1.7.0 )

If java home is not set it display %JAVA_HOME% then you have to set it as follows
SET JAVA_HOME = C:\Program Files\Java\jdk1.7.0
Then you have to append that for existing Path

SET PATH = %PATH%;%JAVA_HOME%\bin

step 4) Now confirm with echo command whether JAVA_HOME and path set properly.

step 5) Run the ESB wso2server.bat located in wso2esb-4.8.1\bin (use start wso2server.bat)

step 6) Once it is started use the Admin Url display in the console. Then login as admin/admin. Then proceed with uploads the connector.

Once this is up and running in console it prompts Management console Url as below. Copy the url and proceed through your browser. [Accept the security exception prompts in browser since https] Continue up to login console.

step 7) WSO2 ESB management console login credentials are as below. Login to Management console using them
Username = admin

Password = admin

step 8Now you are going to access 3rd party web service already hosted in remotely. For that Navigate to URL = http://www.w3schools.com/xml/tempconvert.asmx?wsdl


step 9) Go to view page source and copy whole WSDL content and save it as Temperature.wsdl in wso2esb-4.8.0\wso2esb-4.8.0\repository\samples\resources\proxy location of your ESB. This wsdl file would act as a proxy service for ESB to connect remote service.

step 10In management console click Proxy service => WSDL based proxy. Then you have to fill following Proxy service settings details. Click Test URI button and check the service availability. Refer below screenshots.




step 11) Once details are given click Create Button.






step 12) Once you created proxy service you should be able to see it under list of proxies. Then click List under services tab in main menu.




step 13)It generates proxy service as follows to your remote service

  <?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="Temperature.wsdl"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
      <endpoint>
         <wsdl service="TempConvert"
               port="TempConvertSoap"
               uri="http://www.w3schools.com/xml/tempconvert.asmx?wsdl"/>
      </endpoint>
   </target>
   <publishWSDL uri="http://www.w3schools.com/xml/tempconvert.asmx?wsdl"/>
   <description/>
</proxy>
 
To fix Length required 411 error use DISABLE CHUNKING property for in Sequence.

Use SOAP UI to send request for ESB

1) Open SOAP UI version 4.5.X. Then click soapui.bat file (in Windows) or run ./soapui.sh for UNIX environment. File located = soapui-4.5.1-win32-standalone-bin\soapui-4.5.1\bin

                            Error Troubleshooting Tips

Modify generated request in below manner

 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:x="http://www.w3schools.com/xml/">
<soap:Header/>
<soap:Body>
<x:CelsiusToFahrenheit>
<!--Optional:-->
<x:Celsius>0</x:Celsius>
</x:CelsiusToFahrenheit>
</soap:Body>
</soap:Envelope>
    

Thi...


 import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.omg.Dynamic.Parameter;

import com.final2015.mtit.Employee;
import com.final2015.mtit.User;

public class MainClass {

 /**
  * @param args
  * @throws IllegalAccessException
  * @throws InstantiationException
  * @throws ClassNotFoundException
  * @throws SecurityException
  * @throws NoSuchFieldException
  * @throws NoSuchMethodException
  * @throws InvocationTargetException 
  * @throws IllegalArgumentException 
  */
 public static void main(String[] args) throws InstantiationException,
   IllegalAccessException, ClassNotFoundException,
   NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {

  // //2015 regular
  // //question 1a

  // Class<?> clazz = Class.forName("com.final2015.mtit.User");
  // User user = (User) clazz.newInstance();
  // for (Field x : clazz.getDeclaredFields()) {
  // x.setAccessible(true);

  // // set price at runtime
  // if (x.getName().equals("username")) {
  // x.set(user, "mtit_admin");
  // System.out.println(x.getName() + " = " + x.get(user));
  // continue;
  // }

  // if (x.getName().equals("password")) {
  // x.set(user, "admin123");
  // System.out.println(x.getName() + " = " + x.get(user));
  // continue;

  // }
  // }

////question 1b
//  Employee employee = new Employee("Nimal", "Colombo 10", 30001, 65000.80);
//  Class<?> clazz = Class.forName("com.final2015.mtit.Employee"); 
//  
//  
//  for(Constructor c : clazz.getConstructors()){
//   System.out.println(c.getName());
//   if(c.getParameterTypes().length >0){
//    for( Class<?> y: c.getParameterTypes()){
//     System.out.println( "parameter => " + y.getName());
//    }     
//   }
//  }


  
////question 1c
  Employee emp =new Employee("Tharindu", "Colombo 10", 40001, 85000.50);
  Class<?> clazz = Class.forName("com.final2015.mtit.Employee");  
 
   Method method1 = emp.getClass().getDeclaredMethod("displayEmployeeDetails");
   String result = (String) method1.invoke(emp);
   System.out.println(result);

 }

}
 
Modify generated request in below manner osgi

 ///<summary>
/// basic osgi implementation
///</summary>
///<author>
///TKJ
///</author>

1)OPEN ECLIPSE (in my case HELIOS JavaEE)

2)CREATE SERVICE PROJECT
 i) File => new => OSGi bundle project => next
  OR
  File => new => plug-in project =>eclipse version 3.6

 ii) Project name = com.mtit.osgi.Service
 iii) ECLIPSE VERSION 3.6 -> NEXT
 iv) set ACTIVATOR name "ServerActivator"
 v) finish

3)CREATE CLIENT PROJECT
 i) File => new => plug-in project =>eclipse version 3.6
 ii) Project name = com.mtit.osgi.Client
 iii) ECLIPSE VERSION 3.6 -> NEXT
 iv) set ACTIVATOR name as "ClientActivator"
 v) finish

4)CREATE INTERFACE IN SERVICE PROJECT
 i) right click package --> new --> interface
 ii) name = "IServiceActions"
 ii) add function skeletons to interface
  public String PrintService();
  public String Hello();

5)IMPLEMENT INTERFACE IN SERVICE PROJECT
 i) right click package --> new --> class (implements "IServiceActions")
 ii) name = "ServiceActions"
 iii) implement methods
  public String PrintService(){
   return "execute print service";
  }
  public String Hello(){
   return "Hello!!";
  }

6)IMPLEMENT Start() FUNCTION IN "ServerActivator"
 i) create global variable in the class
   org.osgi.framework.ServiceRegistration serviceReg;
 ii) comment whatever lines inside Start() method(or keep as it is)
 iii) type 
  System.out.println("service started");
  IServeceActions sactions = new ServiceActions();
  serviceReg = context.registerService(IServeceActions.class.getName(),sactions,null);

7)IMPLEMENT Stop() FUNCTION IN "ServerActivator"
 i) comment whatever lines inside Stop() method(or keep as it is)
 ii) type 
  serviceReg.unregister();
  System.out.println("service stopped");

8)ADD org.osgi.service PROJECT TO org.osgi.client BUILD PATH
 i)right click org.osgi.client --> properties --> java build path --> "project" tab
 ii)add --> select org.osgi.service --> ok

6)IMPLEMENT Start() FUNCTION IN "ClientActivator"
 i) create global variable in the class
   org.osgi.framework.ServiceReference serviceRef;
 ii) comment whatever lines inside Start() method(or keep as it is)
 iii) type 
  System.out.println("client starts!!");
  serviceRef = context.getServiceReference(IServeceActions.class.getName());
  ServiceActions sactions = (ServiceActions)context.getService(serviceRef);
  System.out.println(sactions.PrintService());

7)NEED NOT TO IMPLEMENT Stop() FUNCTION IN "ServerActivator".Keep as it is unless asked.

8)EXPORT org.osgi.service RUNTIME PACKAGES
 i) open org.osgi.service MANIFEST.MF file
 ii) open "Runtine" tab --> add --> select "org.osgi.service" --> ok
 

9)ADD DEPENDENCIES TO org.osgi.client
 i) open org.osgi.client MANIFEST.MF file
 ii) open "Dependencies" tab --> add imported package--> search and select "org.osgi.service" --> ok


10) RUN org.osgi.service (as osgi framework project)
    RUN org.osgi.client
 
Modify generated request in below manner reflect

 Book book = new Book();
 Class<?> clazz = book.getClass();
   
//Access private field you have to set accessible true
 Field privateField = clazz.getDeclaredField("bookName");
 privateField.setAccessible(true);
 System.out.println(privateField.getName() + " = " + privateField.get(book));
   
//Access public field 
 Field publicField = clazz.getField("bookCount");
 System.out.println(publicField.getName() + " = " + publicField.get(book));
   
//print array of authors
 System.out.println("Authors = " +
 Arrays.asList((String [])clazz.getField("authors").get(book)));
   
//Set value for price and display
 Field priceField = clazz.getField("price");
 priceField.set(book, 150.50);
 System.out.println(priceField.getName() + " = " + priceField.get(book));

//Modifiers
   Class<?> clazz=Class.forName("com.Employee");
   
   Field[] feilds=clazz.getDeclaredFields();
   
   for(Field field : feilds){
    System.out.println(Modifier.toString(field.getModifiers()));
   }

==========================================================================

Employee employee=new Employee();
  Class<?> clazz=employee.getClass();
  
  for (Method method : clazz.getDeclaredMethods()) {
   try {
    System.out.println("Modifier => "+Modifier.toString(
      clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()).getModifiers()));
    System.out.println("|| Return Type => "+method.getReturnType()+ " || Method Name"+
      method.getName());
   } catch (NoSuchMethodException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
============================================================================

//Method invocation
 Employee employee=new Employee();
   Class[] parameters=new Class[4];
   parameters[0]=String.class;
   parameters[1]=String.class;
   parameters[2]=String.class;
   parameters[3]=Double.TYPE;
   
   Method method=employee.getClass().getDeclaredMethod("display", parameters);
   String result=(String)method.invoke(employee, "123","Prashan","Colombo",90000.00);
   System.out.println(result);

////
   Class<?> clazz = Class.forName("com.User");
   Object object=clazz.newInstance();
   Method methodtoString=clazz.getDeclaredMethod("toString",null);
   String result=(String)methodtoString.invoke(object, null);

==========================
//Constructor parameters return types
   Constructor constructor=clazz.getConstructor(String.class,String.class,
     int.class,double.class);
   for(Class field : constructor.getParameterTypes()){
    System.out.println(field.getName());
   }





No comments:

Post a Comment