Saturday, September 15, 2012

Create a proxy in WSO2 ESB using admin services.

Do you want to know how to create a proxy in WSO2 ESB without using UI. Here is a sample code
that will help you to understand how to use an admin service to achieve the task.
package org.wso2.test;

import org.apache.axis2.AxisFault;
import org.wso2.carbon.proxyadmin.stub.ProxyServiceAdminProxyAdminException;
import org.wso2.carbon.proxyadmin.stub.ProxyServiceAdminStub;
import org.wso2.carbon.proxyadmin.stub.types.carbon.ProxyData;
import org.wso2.carbon.utils.CarbonUtils;

import java.rmi.RemoteException;

public class TestProxy {

    public static void main(String[] args)
            throws RemoteException, ProxyServiceAdminProxyAdminException {

        String userName = "admin";
        String password = "admin";
        // Add the service URL
        String serviceEndPoint="http://localhost:8281/services/echo" ;
        // Proxy Admin service's endpoing URL
        String endPoint = "https://localhost:9444/services/" +"ProxyServiceAdmin";

        // Set client trust store
        System.setProperty("javax.net.ssl.trustStore", "/media/data/wso2/products/wso2/esb/" +
                                                       "wso2esb-4.5.0/repository/resources/security" +
                                                       "/client-truststore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
        System.setProperty("javax.net.ssl.trustStoreType","JKS");

        ProxyServiceAdminStub proxyServiceAdminStub = new ProxyServiceAdminStub(endPoint);

        CarbonUtils.setBasicAccessSecurityHeaders(userName, password,
                                                  proxyServiceAdminStub._getServiceClient());

        //Set proxy configuration data
        String[] transport = {"http", "https"};
        ProxyData data = new ProxyData();
        data.setName("TestProxy2");
        data.setWsdlURI("http://localhost:8281/services/echo?wsdl");
        data.setTransports(transport);
        data.setStartOnLoad(true);
        data.setEndpointXML("<endpoint http:="http:" ns="ns" synapse="synapse" ws.apache.org="ws.apache.org" xmlns="\"><address serviceendpoint="serviceendpoint" uri="\">
</address>
</endpoint>");
        data.setEnableSecurity(true);

        proxyServiceAdminStub.addProxy(data);
    }
}


Download complete maven project here

6 comments:

  1. I upgraded my php to 5.3.X.
    I installed file downloaded from https://github.com/wso2/wsf/archive/master.zip.
    I installed wso2.
    I am getting the error 'Fatal error: Cannot use object of type WSData as array in wsf_wsdl.php on line 211'...
    Do you know what is the problem?
    Can you help me? This is veru critical to me.
    Thanks in advance

    ReplyDelete
  2. I tried this but got this error:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axiom/om/OMFactory
    at org.wso2.test.TestProxy.main(TestProxy.java:30)
    Caused by: java.lang.ClassNotFoundException: org.apache.axiom.om.OMFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 1 more
    Java Result: 1

    Do you know why?

    ReplyDelete
    Replies
    1. Did you build the maven project attached in the blog post and try to run? You need to have required dependencies in the classpath to run? If you are running the project outside the IDE which you open the maven project, make sure required jars available in the classpath. You can run ant command in ESB_HOME/bin folder and then point ESB_HOME/repository/lib location to classpath.

      Delete
    2. Yes, If you please could have a look: https://dl.dropboxusercontent.com/u/18038213/pac.zip
      I tried it with Eclipse Juno, ESB 4.6.0 and Mac OS-X.

      Delete
  3. I think you have not changed the ports of the ESB 4.6.0. Then you should change the proxy service admins URL.
    https://localhost:9444/services/" +"ProxyServiceAdmin. Change the port to default one which is 9443. And also make sure you change the WSDL uri to 8280 also.

    ReplyDelete
  4. I have best idea to open any blocked site very easily and no need to change any proxy just one click and open any site very easily and you can access anywhere you are
    DownloadForAll UK proxy

    ReplyDelete