1

I'm developing a custom authentication plugin for OAM(Oracle Access Manager) in Java language using JDeveloper IDE.


i'm parsing a URL and i get the variables i want from a JSONObject correctly on a Main.class file without triggering any kind of Exception

This leads me to the assumption that the whole parsing code is correct which means the readJsonFromUrl function does it job.


Let me mention what my PhillPlugin.class includes

  1. public ExecutionStatus process(AuthenticationContext context) , triggered when the Plug-In is to run.
  2. public void getDataGenerate(String Url), called inside process function to created the JSONObject from URL
  3. public static JSONObject readJsonFromUrl(String url) called inside getDataGenerate function
  4. private static String readAll(Reader rd) used for parsing inside readJsonFromUrl

Now i upload the Plug-In to the server, i run it and get the following in it's Logs

java.lang.NoClassDefFoundError: org/json/JSONObject
    at phillplugin.PhillPlugin.readJsonFromUrl(PhillPlugin.java:184)
    at phillplugin.PhillPlugin.getDataGenerate(PhillPlugin.java:132)
    at phillplugin.PhillPlugin.process(PhillPlugin.java:63)

What is needed in order to create the Plug-In:

  • PhillPlugin.class
  • PhillPlugin.xml
  • MANIFEST.MF

I'm mentioning the above because i have to include somewhere in these files the org.json path. ( it already exists as an import in PhillPlugin.class and Main.class )

The org.json.jar is included in Project's Libraries as well as all the .jars in order to build the Plug-In


MANIFEST.MF

Manifest-Version: 1.0
Bundle-Version: 10
Bundle-Name: PhillPlugin
Bundle-Activator: phillplugin.PhillPlugin
Bundle-ManifestVersion: 2
Import-Package: org.osgi.framework;version="1.3.0",oracle.security.am.plugin,oracle.security.am.plugin.authn,oracle.security.am.plugin.impl,oracle.security.am.plugin.api,oracle.security.am.common.utilities.principal,oracle.security.idm,javax.security.auth
Bundle-SymbolicName: PhillPlugin
CLASSPATH: felix.jar, identitystore.jar, oam-plugin.jar, utilities.jar, org.json.jar

Sample of the PhillPlugin.Class

I'm not supposed to include the URL for security purposes. (Trust me it's Valid)

    public void getDataGenerate(String Url) {
        System.out.println("-----   Reading Json Object  -----");
                       JSONObject json;
        try {
            json = readJsonFromUrl(Url);
            System.out.println("The Json Object: "+json.toString());
            otp=Integer.parseInt((String) json.get("otp"));
            System.out.println("The User is:"+user+"\n"+"His OTP is: "+otp);
        } catch (Exception e) {
            System.out.println("Exception : "+e.toString());
        }          

    public static JSONObject readJsonFromUrl(String url) throws IOException,JSONException {
        System.out.println("Opening Stream");
        InputStream is = new URL(url).openStream();
        System.out.println("Stream opened");
        try {
         System.out.println("----------\n\n\nUrl to Parse: "+url+"\n\n\n");
          BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
          System.out.println("\n\n\n"+"BufferedReader opened\n\n\n\n");
          String jsonText =(String) readAll(rd);
          System.out.println("\n\n\nJsonTEXT:"+jsonText+"\n\n\n");
            JSONObject json=null;
            System.out.println("\n\n Created Json Instance\n\n\n");
            try{
                System.out.println("inside try statement - initializing JSONObject with the text above \n\n\n");
             //-------ERROR TRIGGERED HERE---------
             json = new JSONObject(jsonText);
                System.out.println("--------------------Object created-------------------");
            }catch (Exception e) {
                System.out.println("\n\n\n\n\nJSONOBJECT failed to be created: \n"+e);
            }
          System.out.println("\n\n\nJSON OBJECT"+json+"\n\n\n\n");
          return json;
        } finally {
          is.close();
        }

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
          sb.append((char) cp);
        }
        return sb.toString();
      }

PhillPlugin.xml

<Plugin type="Authentication">
    <author>uid=Phill</author>
    <email>phill@oracle.com</email>
    <creationDate>12:47:00, 2019-07-11</creationDate>
    <description>Phill-Plugin Prints Hello</description>
    <configuration>
    </configuration>
</Plugin>


This is the output on server Logs before crashing:


Stream opened
----------
Url to Parse: https://something

BufferedReader opened

JsonTEXT: it's correct

Created Json Instance

inside try statement - initializing JSONObject with the text above 

I'm worrying too much about the MANIFEST.MF file because probably i'm doing something wrong in there

Sorry for the long post, i will provide any extra information if needed, Thank you

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31
  • 1
    in manifest: it's "Class-Path:" not CLASSPATH but it should be generated using maven for example, since there are other restrictions (line size, spaces on carriage return ...) – pdem Jul 16 '19 at 14:09

2 Answers2

1

When writing an Authenticaiton Plugin for OAM Server , all extra libraries should be mentioned in MANIFEST.MF as well as the external .jar files.

Furthermore , all .jar files should be deployed in the final plugin .jar as well as the external libraries

In my case i had to include org.json.jar at the exported PhillPlugin.jar as follows:

  • PhillPlugin.jar

enter image description here

as you can see both org.json.jar and it's libraries org are required

  • Manifest.MF

The last step is to mention in MANIFEST.MF all the extra classes that you are using in your plugin

In my case i had to include this in my Import-Package Attribute in order to be able to create a JSONObject instance

org.json;resolution:=optional,
org.json.JSONObject;resolution:=optional

If you would like to use a JSONArray you had to add this:

org.json.JSONArray;resolution:=optional

and so on.

Edit: The class-path should be mentioned as follows:

Bundle-ClassPath: org.json.jar, felix.jar, identity-provider.jar, oam-plugin.jar, utilities.jar
Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31
0

The CLASSPATH entry in the MANIFEST.MF is obviously wrong. Correct name is Class-Path see: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Whole MANIFEST.MF documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

maxxyme
  • 2,164
  • 5
  • 31
  • 48
  • it might be, but `CLASSPATH` worked for me, if it was wrong i couldn't use anything from the other 4 .jars , surprisingly i can use the content of the 4 .jars except the org.json.jar – Phill Alexakis Jul 16 '19 at 14:16
  • Can you please provide the imports along with your Java class excerpt because maybe what you're using might not be the same `JSONObject`. By the way, how do you obtain the `org.json.jar`? – maxxyme Jul 16 '19 at 14:27
  • @PhillAlexakis It is the line limit of 72 char then. Do you use any building tool for generation? Or elese you wil have to manage it manually: \n\b every 72 chars. – pdem Jul 16 '19 at 14:29
  • what exactly do you mean? i'm building the PhillPlugin.jar with Jdeveloper, not manually with commands – Phill Alexakis Jul 16 '19 at 18:09
  • @maxxyme i corrected the `Class-Path` but the error is still present – Phill Alexakis Jul 17 '19 at 06:51
  • @maxxyme i'm importing the `org.json.jar` from Jdeveloper's IDE Libraries these are my java imports `import org.json.JSONObject;` `import org.json.JSONException;` and i'm trying to include these two imports on `Import-Package` at `MANIFEST.MF` like this `Import-Package: org.osgi.framework;version="1.3.0",oracle.security.am.plugin,oracle.security.am.plugin.authn,oracle.security.am.plugin.impl,oracle.security.am.plugin.api,oracle.security.am.common.utilities.principal,oracle.security.idm,javax.security.auth,org.json.JSONObject,org.json.JSONException` but it doesn't seem to work – Phill Alexakis Jul 17 '19 at 07:14
  • ah! the keyword is `Import-Package` thus you should only lists packages there! Neither `org.json.JSONObject` nor `org.json.JSONException` are packages actually, it's fully qualified class names (i.e. package + Class name). You should resort to just put `org.json` instead here. – maxxyme Jul 17 '19 at 08:59
  • yes indeed you are right, but I'm still getting a new exception on the `manifest` file exactly on the line i added `org.json` ----> `Caused By: org.osgi.framework.BundleException: Unresolved constraint in bundle PhillPlugin [2]: Unable to resolve 2.0: missing requirement [2.0] osgi.wiring.package; (osgi.wiring.package=org.json)` – Phill Alexakis Jul 17 '19 at 13:08
  • Unfortunately I'm not an expert - even no XP at all - in OSGi, but given questions & answers here on SO it seems like you are trying to reference an extra "bundle" or "lib" in your plugin, while it can't be done that way. Check this answer https://stackoverflow.com/a/30532447/666414 you should read it like Karaf = OAM and bundle = plugin, but the main problem seems in the maven file with generation of the MANIFEST.MF – maxxyme Jul 17 '19 at 14:21
  • i'll give it a try but seems the logic is the same but the illustration is different . i'll give it a shot – Phill Alexakis Jul 18 '19 at 07:01