node.js and node-java: What is equivalent node.js code for this java code?

微笑、不失礼 提交于 2019-12-12 19:10:38

问题


I am investigating replicating some functionality that we now have, but using node.js, but our current application interfaces to a product that exposes a Java API, so I am also looking into using node-java to do that interface, but I'm having difficult time kind of "translating" some of that Java interface code that we have to node.js.

To start, I am having trouble with trying to translate the following into the equivalent node.js code:

import javax.security.auth.Subject;
import weblogic.security.principal.WLSUserImpl;
.
.
Subject subject = new Subject();
matchUsername = "foo";
subject.getPrincipals().add(new WLSUserImpl(matchUsername));

Here's my current (non-working) attempt:

#!/apps/node-v6.6.0-linux-x64/bin

var java = require("java");

java.classpath.push("/apps/node-v6.6.0-linux-x64/code/src");
java.classpath.push("/apps/node-v6.6.0-linux-x64/code/jps-pep.jar");
java.classpath.push("/apps/node-v6.6.0-linux-x64/code/jrf-api.jar");
java.classpath.push("/apps/node-v6.6.0-linux-x64/code/org.openliberty.openaz.azapi_1.1.jar");
java.classpath.push("/apps/node-v6.6.0-linux-x64/code/wls-api.jar");

var javaLangSystem = java.import('java.lang.System');

javaLangSystem.out.printlnSync("About to do java.import()...");
var MyClass1 = java.import('javax.security.auth.Subject');
// THIS LINE CAUSES A PROBLEM ===> var MyClass = java.import("weblogic.security.principal.WLSGroupImpl");
javaLangSystem.out.printlnSync("Returned from java.import()...");

javaLangSystem.out.printlnSync("About to do java.newInstanceSync()...");
var newSubject = java.newInstanceSync('javax.security.auth.Subject');
//var newThing = java.newInstanceSync("weblogic.security.principal.WLSUserImpl");
javaLangSystem.out.printlnSync("Returned from java.newInstanceSync...");

javaLangSystem.out.printlnSync("About to do subject.getPrincipals()...");
var builtSubject = newSubject.getPrincipalsSync();
javaLangSystem.out.printlnSync("Returned from getPrincipals(), builtSubject=[" + builtSubject + "]");

//var x = builtSubject.add(newThing);

//var MyFactoryImplClass = java.import("oracle.security.jps.openaz.pep.PepRequestFactoryImpl.PepRequestFactoryImpl");

//var result = myFactoryImplClass.newPepRequest(newSubject, requestACTIONString ,requestRESOURCEString , envBuilt)

//console.log(result);

and, here's the error output when I try to run the node.js app and if that line I noted above is UNcommented:

About to do java.import()...
/apps/node-v6.6.0-linux-x64/node_modules/java/lib/nodeJavaBridge.js:227
  var clazz = java.findClassSync(name); // TODO: change to Class.forName when classloader issue is resolved.
               ^

Error: Could not create class weblogic.security.principal.WLSGroupImpl
java.lang.NoClassDefFoundError: weblogic/security/principal/WLSAbstractPrincipal$GetSignatureProperty
Caused by: java.lang.ClassNotFoundException: weblogic.security.principal.WLSAbstractPrincipal$GetSignatureProperty
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

at Error (native)
at Java.java.import (/apps/node-v6.6.0-linux-x64/node_modules/java/lib/nodeJavaBridge.js:227:20)
at Object.<anonymous> (/apps/node-v6.6.0-linux-x64/code/testOES.js:16:26)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)

There's still more after this, but I have to admit that I'm already kind of confused by what I've already shown above.

I guess that it seems like the samples I've seen tend to be the equivalent of what would be more basic, less complex code.

Thanks for any helps with this.

Jim

EDIT 1: I've updated the node.js/node-java code above to my current attempt and also the error that I'm getting if I UNcomment that one line. It seems like it cannot find that class because it's an inner class (the $GetSignature part)? I know that the WLSUserImpl class is in the wls-api.jar, which I brought in via one of the java.classpath.push() statements at the beginning of the above code.

EDIT 2: I think that I've made some progress, but I'm getting stuck at trying to replicate this part of the Java snippet in node.js+node-java:

    subject.getPrincipals().add(new WLSUserImpl(matchUsername));

Here is my current code that is supposed to do that:

var javaLangSystem = java.import('java.lang.System');
var MyFactoryImplClass = java.import('oracle.security.jps.openaz.pep.PepRequestFactoryImpl');

javaLangSystem.out.printlnSync("About to do java.newInstanceSync()...");
var newSubject = java.newInstanceSync('javax.security.auth.Subject');
var newWLSUserImplObject = java.newInstanceSync("weblogic.security.principal.WLSUserImpl","weblogic", false);
console.log("newWLSUserImplObject after newInstanceSync=[" + newWLSUserImplObject + "]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
javaLangSystem.out.printlnSync("Returned from java.newInstanceSync...");

javaLangSystem.out.printlnSync("About to do subject.getPrincipals().add()...");
var x1 = newSubject.getPrincipalsSync();
var builtSubject = x1.addSync(newWLSUserImplObject);
javaLangSystem.out.printlnSync("Returned from getPrincipals()i.add(), builtSubject=[" + builtSubject + "]");
console.log("builtSubject after add = [" + builtSubject + "]");

[Sorry, I know that there's a lot of debug stuff, plus it's all Sync, but I just want to get this to the point that it is doing something "correctly" :(...]

Anyway, here's the output:

[root@nodejs smtest8-java-PULL-NODEJS]# node testOES.js
About to do java.newInstanceSync()...
newWLSUserImplObject after newInstanceSync=[weblogic]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Returned from java.newInstanceSync...
About to do subject.getPrincipals().add()...
Returned from getPrincipals()i.add(), builtSubject=[true]
builtSubject after add = [true]

Notice that the even though the code is setting the Subject to "weblogic" what ends up in the builtSubject appears to be "true".

Also, when that builtSubject gets used later in the code (not shown) to call another method, I get:

About to do pepReqF1.newPepRequest()...
/apps/Oracle/OESCLIENT/oes_sm_instances/smtest8-java-PULL-NODEJS/testOES.js:71
var pepReq1 = pepReqF1.newPepRequestSync(builtSubject, "GET" , "test/foo2/fooresource" , null)
                       ^

Error: Error running instance method
oracle.security.jps.openaz.pep.JpsPepException: JPS-08000: **Subject object true is not supported**.
        at oracle.security.jps.openaz.pep.SubjectObjMapper.mapToJpsObject(SubjectObjMapper.java:176)
        at oracle.security.jps.openaz.pep.PepRequestImpl.setAccessSubject(PepRequestImpl.java:429)
        at oracle.security.jps.openaz.pep.PepRequestFactoryImpl.newPepRequest(PepRequestFactoryImpl.java:248)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)

    at Error (native)
    at Object.<anonymous> (/apps/Oracle/OESCLIENT/oes_sm_instances/smtest8-java-PULL-NODEJS/testOES.js:71:24)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)

In other words, the part of the code that is populating the builtSubject object is not throwing an error, but what ends up in the builtSubject appears to be wrong (a value of 'true', rather than a username "weblogic").

I cannot figure out where the 'true' value is coming from or why the builtSubject is not being populated :(.

EDIT 3: BTW, I think I am really close to getting an initial test working with that COTS API. I think that the log that I showed above shows to me that the node.js+node-java is actually invoking the API, but what I am passing into the API for the set of user (in builtSubject) is incorrect. So I think that if I/we can figure out what that builtSubject object is being incorrectly populated, I think that would at least prove that I can use the API from node.js+node-java. Then, I can go an tweak the code, e.g., maybe make it asynch, etc.

So I really hope that someone can spot what I am doing wrong :)!!

Thanks, Jim

来源:https://stackoverflow.com/questions/39629869/node-js-and-node-java-what-is-equivalent-node-js-code-for-this-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!