1

I have developed an Authentication Plugin for Oracle Access Manager

Briefly it contains:

  • java.class
  • the following XML

I'm trying to Dynamically get the <Value> tag of an <Attribute> from the XML file.

<Plugin type="Authentication">
    <author>Phill</author>
    <email>[email protected]</email>
    <creationDate>12:47:00, 2019-07-11</creationDate>
    <description>Phill-Plugin</description>
    <configuration>
        <AttributeValuePair>
            <Attribute type="string" length="60">GenerateUrl</Attribute>
            <mandatory>true</mandatory>
            <instanceOverride>false</instanceOverride>
            <globalUIOverride>true</globalUIOverride>
            <value>This is the value i'm trying to retrieve</value>
        </AttributeValuePair>
    </configuration>
</Plugin>

java.class

            try {

                CredentialParam tem = context.getCredential().getParam("GenerateUrl");
                String temp = (String) tem.getValue();
                System.out.println("TEST: " + temp);
                generateUrl = temp + "The User" + user;
            } catch (Exception e) {
                System.out.println("\n\n\n-------------------\n");
                System.out.println("-      Input Is:         -\n");
                System.out.println("-       "+e+"            -\n");
                System.out.println("-------------------\n");
                generateUrl = "A URL" + "The User" + user;
            }

Important Note:

The context object is an AuthenticationContext instance containing information about the Plug-in

Based on Oracle's Documentation, this is the exact way someone retrieves an Attribute but i'm always getting a NullPointerException

Is there any other way that i can retrieve the <Value>?

4
  • Hi, If you are looking for an attribute called "GenerateUrl" then I think you've misunderstood what an attribute actually is. You have two attributes for the 'Attribute' element : "type="string" and length="60" Difference between attributes and elements. If I understand your code correctly then you should be looking for the value of the 'Attribute' element (?). Apologies if it's me that is not understanding the code, but that was my first thought. Commented Jul 25, 2019 at 7:34
  • @Robert thank you for commenting, based on the Documentation tha name of it is GenerateUrl and with the method above i should get the contents of the <value> tag Commented Jul 25, 2019 at 7:36
  • @"Phill Alexakis" Ahh I see, the sentence "I'm trying to Dynamically get the Value of an Attribute from the XML file." threw me off a bit and I was only looking for where you would extract xml attributes. I might be the only one, or I might not be, to get confused by it but it might be a good Idea to clarify that it is the 'Value' element that you want the value of. Sorry I could not be of any help, I hope you'll find a solution. Commented Jul 25, 2019 at 7:43
  • Alrigt, i edited the post. I Also tried adding <Attribute type="string" length="60" name="GenerateUrl" id="GenerateUrl">GenerateUrl</Attribute> and i'm still getting a NullPointerException Commented Jul 25, 2019 at 7:44

2 Answers 2

1

I had to try another way and do proper parsing of the XML

If you can use external libs here is how:


    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        File stocks = new File("PhillPlugin.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(stocks);
            doc.getDocumentElement().normalize();

            NodeList nodes = doc.getElementsByTagName("AttributeValuePair");

            for (int i = 0; i < nodes.getLength(); i++) {
              Node node = nodes.item(i);
              if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                if(i==0)
                 {
                 tempurlGen=getValue("value",element);
                   System.out.println("GenerateUrl: " + getValue("value", element));
                 }
                 else if (i==1)
                 {
                 tempurlVal=getValue("value",element);
                 System.out.println("ValidateUrl: " + getValue("value", element));
                 }

              }
            }
          }
          static String getValue(String tag, Element element) {
            NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
            Node node = (Node) nodes.item(0);
            return node.getNodeValue();
          }

If you can't include javax libraries here is how to parse the XML using streams


    public void getXMLData() throws Exception {
        File stocks = new File("PhillPlugin.xml");
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(stocks));
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = in.read()) != -1) {
            sb.append((char) cp);
            String t = sb.toString();
            if (t.contains("</AttributeValuePair>")) {
                String test = sb.toString();
                String test1p[] = test.split("<value>|</value>");
                tempurlGen = test1p[1];
                break;
            }
        }

        sb = new StringBuilder();
        while ((cp = in.read()) != -1) {

            sb.append((char) cp);
            String t = sb.toString();
            if (t.contains("</AttributeValuePair>")) {
                String test = sb.toString();
                String test1p[] = test.split("<value>|</value>");
                tempurlVal = test1p[1];
                break;
            }

        }
    }

Make sure you define tempurlGen and tempurlVal

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "author",
    "email",
    "creationDate",
    "description",
    "configuration"
})
@XmlRootElement(name = "Plugin")
public class Plugin {

    //Add getters and setters including ones for inner classes

    @XmlElement(required = true)
    private String author;
    @XmlElement(required = true)
    private String email;
    @XmlElement(required = true)
    private String creationDate;
    @XmlElement(required = true)
    private String description;
    @XmlElement(required = true)
    private Plugin.Configuration configuration;
    @XmlAttribute(name = "type")
    private String type;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "attributeValuePair"
    })
    public static class Configuration {

        @XmlElement(name = "AttributeValuePair", required = true)
        private Plugin.Configuration.AttributeValuePair attributeValuePair;

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "attribute",
            "mandatory",
            "instanceOverride",
            "globalUIOverride",
            "value"
        })
        public static class AttributeValuePair {

            @XmlElement(name = "Attribute", required = true)
            private Plugin.Configuration.AttributeValuePair.Attribute attribute;
            @XmlElement(required = true)
            private String mandatory;
            @XmlElement(required = true)
            private String instanceOverride;
            @XmlElement(required = true)
            private String globalUIOverride;
            @XmlElement(required = true)
            private String value;

            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = {
                "value"
            })
            public static class Attribute {

                @XmlValue
                private String value;
                @XmlAttribute(name = "type")
                private String type;
                @XmlAttribute(name = "length")
                private Byte length;

            }

        }

    }

}

And for the unmarshaling part:

JAXBContext jaxbContext = JAXBContext.newInstance(Plugin.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("xml string here");
Plugin plugin = (Plugin) unmarshaller.unmarshal(reader); 

1 Comment

Thank you , this could possible work, but remember an XML needs to be constructed becuase it is being accessed OnUpload by the server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.