通过 Javascript 使用 WSO2 管理服务

Consume WSO2 admin services via Javascript

我正在尝试使用 WSO2 管理服务,特别是在身份服务器中。我想创建一个新租户。我使用 TenantMgtAdminService (https://127.0.0.1:9443/services/TenantMgtAdminService?wsdl),函数 addTenant。为了使用这项服务,我正在使用 jaggery。这是我的代码:

<%
function tenant() {
 var log = new Log();
 var ws = require('ws');
 
 var version = new ws.WSRequest();
 var options = new Array();
 options.useSOAP = 1.2;
 options.useWSA = 1.0;
 options.action = "urn:addTenant";
 var payload = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.mgt.tenant.carbon.wso2.org" xmlns:xsd="http://beans.common.stratos.carbon.wso2.org/xsd">
<soapenv:Header/>
  <soapenv:Body>
   <ser:addTenant>
     <ser:tenantInfoBean>
        <xsd:active>true</xsd:active>
        <xsd:admin>admin</xsd:admin>
        <xsd:adminPassword>admin</xsd:adminPassword>
        <xsd:createdDate>2016-01-11T11:15:04-04:00</xsd:createdDate>
        <xsd:email>maikelf@mymail.com</xsd:email>
        <xsd:firstname>Manuel</xsd:firstname>
        <xsd:lastname>Fdez</xsd:lastname>
        <xsd:originatedService>mymail.com</xsd:originatedService>
        <xsd:successKey>test</xsd:successKey>
        <xsd:tenantDomain>mymail.com</xsd:tenantDomain>
        <xsd:tenantId>4</xsd:tenantId>
        <xsd:usagePlan>demo</xsd:usagePlan>
     </ser:tenantInfoBean>
  </ser:addTenant>
</soapenv:Body>
</soapenv:Envelope>';

 var result;
 
 try {
  version.open(options, "https://127.0.0.1:9443/services/TenantMgtAdminService", false, "admin", "admin");
  version.send(payload);
 } catch (e) {
  log.error(e.toString());
  return e.toString();
 }
 return result;
}
 
print(tenant()); 
%>

payload的值取自SOAPUI

在soapui

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.mgt.tenant.carbon.wso2.org" xmlns:xsd="http://beans.common.stratos.carbon.wso2.org/xsd">
<soapenv:Header/>
  <soapenv:Body>
   <ser:addTenant>
     <!--Optional:-->
     <ser:tenantInfoBean>
        <!--Optional:-->
        <xsd:active>true</xsd:active>
        <!--Optional:-->
        <xsd:admin>admin</xsd:admin>
        <!--Optional:-->
        <xsd:adminPassword>admin</xsd:adminPassword>
        <!--Optional:-->
        <xsd:createdDate>2016-01-11T11:15:04-04:00</xsd:createdDate>
        <!--Optional:-->
        <xsd:email>maikelf@mymail.com</xsd:email>
        <!--Optional:-->
        <xsd:firstname>Manuel</xsd:firstname>
        <!--Optional:-->
        <xsd:lastname>Fdez</xsd:lastname>
        <!--Optional:-->
        <xsd:originatedService>mymail.com</xsd:originatedService>
        <!--Optional:-->
        <xsd:successKey>test</xsd:successKey>
        <!--Optional:-->
        <xsd:tenantDomain>mymail.com</xsd:tenantDomain>
        <!--Optional:-->
        <xsd:tenantId>4</xsd:tenantId>
        <!--Optional:-->
        <xsd:usagePlan>demo</xsd:usagePlan>
     </ser:tenantInfoBean>
  </ser:addTenant>
</soapenv:Body>
</soapenv:Envelope>

我得到的结果是这个错误:

JavaException: org.jaggeryjs.scriptengine.exceptions.ScriptException: 调用服务时出错

在 Identity Server 控制台中

[2016-01-13 15:16:10,848]  INFO {org.wso2.carbon.core.services.util.CarbonAuthenticationUtil} -  'admin@carbon.super [-1234]' logged in at [2016-01-13 15:16:10, 847-0500]
[2016-01-13 15:16:17,275]  INFO {org.wso2.carbon.core.services.util.CarbonAuthenticationUtil} -  'admin@carbon.super [-1234]' logged in at [2016-01-13 15:16:17, 275-0500]

在 Jaggery 控制台中

[2016-01-13 15:28:05,052] ERROR {org.jaggeryjs.hostobjects.ws.WSRequestHostObject} -  Error occured while invoking the serviceorg.apache.axis2.AxisFault: namespace mismatch require http://services.mgt.tenant.carbon.wso2.org found http://schemas.xmlsoap.org/soap/envelope/
    at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
    at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:367)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:413)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:224)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:554)
    at org.jaggeryjs.hostobjects.ws.WSRequestHostObject.jsFunction_send(WSRequestHostObject.java:362)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
    at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:386)
    at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)

    at org.jaggeryjs.rhino.jtest.c32._c_tenant_1(/jtest//index.jag:16)
    at org.jaggeryjs.rhino.jtest.c32.call(/jtest//index.jag)
    at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:74)
    at org.jaggeryjs.rhino.jtest.c32._c_script_0(/jtest//index.jag:24)
    at org.jaggeryjs.rhino.jtest.c32.call(/jtest//index.jag)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
    at org.jaggeryjs.rhino.jtest.c32.call(/jtest//index.jag)
    at org.jaggeryjs.rhino.jtest.c32.exec(/jtest//index.jag)
    at org.jaggeryjs.scriptengine.engine.RhinoEngine.execScript(RhinoEngine.java:577)
    at org.jaggeryjs.scriptengine.engine.RhinoEngine.exec(RhinoEngine.java:280)
    at org.jaggeryjs.jaggery.core.manager.WebAppManager.execute(WebAppManager.java:298)
    at org.jaggeryjs.jaggery.core.JaggeryServlet.doGet(JaggeryServlet.java:24)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:690)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:477)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:369)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.jaggeryjs.jaggery.core.JaggeryFilter.doFilter(JaggeryFilter.java:21)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.wso2.carbon.tomcat.ext.valves.CompositeValve.invoke(CompositeValve.java:172)
    at org.wso2.carbon.tomcat.ext.valves.CarbonStuckThreadDetectionValve.invoke(CarbonStuckThreadDetectionValve.java:156)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.wso2.carbon.tomcat.ext.valves.CarbonContextCreatorValve.invoke(CarbonContextCreatorValve.java:52)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1653)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
[2016-01-13 15:28:05,058] ERROR {JAGGERY.index:jag} -  JavaException: org.jaggeryjs.scriptengine.exceptions.ScriptException: Error occured while invoking the service

使用管理服务的推荐方法是使用 Jaggery WSStub [1]。

示例 WSStub 代码

<%
   var ws = require('ws');
   var stub = new ws.WSStub('https://localhost:9443/services/OAuth2TokenValidationService?wsdl');
   var tokenValidator = stub.services['OAuth2TokenValidationService'].operations['findOAuthConsumerIfTokenIsValid'];
   var validationResult = tokenValidatorrequest(accsessToken);
%>

Since the above service is an admin service, a valid user name and password needs to be provided in order to consume the service.

请按照推荐的方法访问管理服务。

[1].http://jaggeryjs.org/documentation.jag?api=wsstub