12 Replies Latest reply on Sep 5, 2006 2:15 AM by markash

    GenericObjectModelProvider not setting attributes in XML

    markash

      Good Day,

      I am trying to use the DtdMarshaller to marshal the PolicyConfig to the login-config.xml, but the attributes are not being set in the XML. I have tried the ObjectModelProvider and here I am trying the GenericObjectModelProvider.

      public class LoginConfigObjectModelProvider implements GenericObjectModelProvider {
      
       public Object getRoot(Object o, MarshallingContext ctx, String namespaceURI, String localName) {
       return o;
       }
       public Object getChildren(Object o, MarshallingContext ctx, String namespaceURI, String localName) {
       if (o instanceof PolicyConfig) {
       if ("application-policy".equals(localName)) {
      
       List<AuthenticationInfo> infos = new ArrayList<AuthenticationInfo>();
       Set<String> keys = ((PolicyConfig) o).getConfigNames();
       for (String name : keys) {
       infos.add(((PolicyConfig) o).get(name));
       }
       return infos;
       }
       }
       return null;
       }
       public Object getAttributeValue(Object o, MarshallingContext ctx, String namespaceURI, String localName) {
      
       if (o instanceof AuthenticationInfo) {
       if ("name".equals(localName)) {
       return ((AuthenticationInfo) o).getName();
       }
       }
       return null;
       }
      
       public Object getElementValue(Object o, MarshallingContext ctx, String namespaceURI, String localName) {
       return null;
       }
      }
      


      13:46:37,358 WARN @main [JBossEntityResolver] Cannot load publicId from resource: security_config.dtd
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE policy PUBLIC "-//JBoss//DTD JBOSS Security Config 3.0//EN" "http://www.jboss.org/j2ee/dtd/security_config.dtd">
      
      <policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
       <application-policy></application-policy>
      </policy>
      


       StringWriter xmlOutput = new StringWriter();
      
       System.setProperty(Marshaller.PROP_MARSHALLER, DtdMarshaller.class.getName());
      
       AbstractMarshaller marshaller = (AbstractMarshaller) Marshaller.FACTORY
       .getInstance();
      
       marshaller.mapPublicIdToSystemId("-//JBoss//DTD JBOSS Security Config 3.0//EN", "http://www.jboss.org/j2ee/dtd/security_config.dtd");
       LoginConfigObjectModelProvider provider = new LoginConfigObjectModelProvider();
       marshaller.marshal(
       "http://www.jboss.org/j2ee/dtd/security_config.dtd",
       provider,
       config,
       xmlOutput);
      
       System.out.println(xmlOutput.getBuffer());
      


      Regards,
      Mark P Ashworth

        • 1. Re: GenericObjectModelProvider not setting attributes in XML
          aloubyansky

          What if you add some logging to the getAttributeValue impl? Is it ever called? What are the arguments passed into it?
          Just curious, what are you using this login-config binding for?

          Thanks.

          • 2. Re: GenericObjectModelProvider not setting attributes in XML
            markash

            Good Day,

            CHILDREN Object: class org.jboss.security.auth.login.PolicyConfig localName: application-policy
            ATTRIBUTE Object: class org.jboss.security.auth.login.AuthenticationInfo localName: name
            CHILDREN Object: class org.jboss.security.auth.login.AuthenticationInfo localName: authentication
            CHILDREN Object: class com.cibecs.brs.appserver.authentication.LoginConfigObjectModelProvider$Authentication localName: login-module
            ATTRIBUTE Object: class javax.security.auth.login.AppConfigurationEntry localName: flag
            ATTRIBUTE Object: class javax.security.auth.login.AppConfigurationEntry localName: code
            CHILDREN Object: class javax.security.auth.login.AppConfigurationEntry localName: module-option
            ATTRIBUTE Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: name
            ELEMENT Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: module-option
            ATTRIBUTE Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: name
            ELEMENT Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: module-option
            ATTRIBUTE Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: name
            ELEMENT Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: module-option
            ATTRIBUTE Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: name
            ELEMENT Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: module-option
            ATTRIBUTE Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: name
            ELEMENT Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: module-option
            ATTRIBUTE Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: name
            ELEMENT Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: module-option
            ATTRIBUTE Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: name
            ELEMENT Object: class com.cibecs.brs.appserver.authentication.ModuleOption localName: module-option
            


            I am trying to make the changes made to the login-config.xml via the XMLLoginConfig MBean to be persisted. I have implemented the logic to convert between the PolicyConfig and the Configuration and AppConfigurationEntry classes and all I need is to get the PolicyConfig to be persisted.

            I noticed that the JBossXB source that comes with the JBoss 4.0.4 GA is not the same that the 4.0.4 GA was compiled with since the debugger is stopping a line numbers that are out of sync with the actual code.

            Regards,
            Mark P Ashworth

            • 3. Re: GenericObjectModelProvider not setting attributes in XML
              aloubyansky

              You are right. I've created a jira issue and fixed it already in the SVN.
              http://jira.jboss.com/jira/browse/JBXB-80

              As to the sources, the XB and the common module have become separate projects now and are pulled in to the thirdparty as binaries. The old sources were removed from JBoss-4.0.5.XX.

              Thanks.

              • 4. Re: GenericObjectModelProvider not setting attributes in XML
                markash

                Good Day,

                Thank you for the feedback.

                If I want to change / implement the functionality, how do I go about it since this is critical for our project.

                Regards,
                Mark P Ashworth

                • 5. Re: GenericObjectModelProvider not setting attributes in XML
                  markash

                  Oops, I see it is fixed in CR6.

                  How can I get that source?

                  Regards,
                  Mark P Ashworth

                  • 6. Re: GenericObjectModelProvider not setting attributes in XML
                    aloubyansky

                    You can discuss your ideas, fixes, etc here. And use jira.jboss.com to report issues.
                    The way the source is checked out has been changing lately.
                    Check out this page http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossCommonProject
                    But jbossxb moved from common/trunk/jbossxb to the top level yesterday.

                    • 7. Re: GenericObjectModelProvider not setting attributes in XML
                      markash

                      Good Day,

                      Thank you for the heads up about the changed repository. Could you please let me know the repository path that I can use to check out the change

                      http://jira.jboss.com/jira/browse/JBXB-80

                      Regards,
                      Mark P Ashworth

                      • 9. Re: GenericObjectModelProvider not setting attributes in XML
                        markash

                        Good Day,

                        I got the source from common/jbossxb and that seems to have the change to fix the attributes.

                        The problem is that when I add a handler for an element text then that xml node is duplicated, once without the text element and once with.

                        <?xml version="1.0" encoding="UTF-8"?>
                        <!DOCTYPE policy PUBLIC "-//JBoss//DTD JBOSS Security Config 3.0//EN" "http://www.jboss.org/j2ee/dtd/security_config.dtd">
                        
                        <policy>
                         <application-policy name="other">
                         <authentication>
                         <login-module flag="required" code="org.jboss.security.auth.spi.LdapExtLoginModule">
                         <module-option name="bindCredentials"></module-option>
                         <module-option name="java.naming.provider.url"></module-option>
                         </login-module>
                         </authentication>
                         </application-policy>
                         <application-policy name="test">
                         <authentication>
                         <login-module flag="required" code="org.jboss.security.auth.spi.LdapExtLoginModule">
                         <module-option name="bindCredentials"></module-option>
                         <module-option name="java.naming.provider.url"></module-option>
                         </login-module>
                         </authentication>
                         </application-policy>
                        </policy>
                        


                        I have written a testcase to help with the fixing of the issue. The testcase has a dependancy on jbosssx.jar.

                        Here is my ObjectModelProvider

                        package org.jboss.xb.test;
                        
                        import java.util.ArrayList;
                        import java.util.Arrays;
                        import java.util.HashMap;
                        import java.util.List;
                        import java.util.Map;
                        import java.util.Set;
                        
                        import javax.security.auth.login.AppConfigurationEntry;
                        import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
                        
                        import org.jboss.security.auth.login.AuthenticationInfo;
                        import org.jboss.security.auth.login.PolicyConfig;
                        import org.jboss.xb.binding.MarshallingContext;
                        import org.jboss.xb.binding.ObjectModelProvider;
                        
                        /**
                         * @author Mark Ashworth
                         * @version 1.0.0
                         */
                        public class LoginConfigObjectModelProvider implements ObjectModelProvider {
                         /**
                         * Used to simulate the authentication node
                         */
                         static class Authentication {
                         public List<AppConfigurationEntry> entries;
                         public Authentication(List<AppConfigurationEntry> entries) {
                         this.entries = entries;
                         }
                         }
                         /**
                         * ROOT: policy
                         */
                         public Object getRoot(
                         Object o,
                         MarshallingContext ctx,
                         String namespaceURI,
                         String localName) {
                        
                         return o;
                         }
                         /**
                         * application-policy
                         */
                         public Object getChildren(
                         PolicyConfig policyConfig,
                         String namespaceUri,
                         String localName) {
                        
                         List<AuthenticationInfo> infos = new ArrayList<AuthenticationInfo>();
                         Set<String> keys = policyConfig.getConfigNames();
                         for (String name : keys) {
                         infos.add(policyConfig.get(name));
                         }
                         return infos;
                         }
                         /**
                         * application-policy name
                         */
                         public Object getAttributeValue(
                         AuthenticationInfo authInfo,
                         String namespaceUri,
                         String localName) {
                        
                         Object value = null;
                         if ("name".equals(localName)) {
                         value = authInfo.getName();
                         }
                        
                         return value;
                         }
                         /**
                         * authentication
                         */
                         public Object getChildren(
                         AuthenticationInfo authInfo,
                         String namespaceUri,
                         String localName) {
                        
                         return new Authentication(Arrays.asList((authInfo.getAppConfigurationEntry())));
                         }
                         /**
                         * login-module
                         */
                         public Object getChildren(
                         Authentication authentication,
                         String namespaceUri,
                         String localName) {
                        
                         return authentication.entries;
                         }
                         /**
                         * login-module code flag
                         */
                         public Object getAttributeValue(
                         AppConfigurationEntry entry,
                         String namespaceUri,
                         String localName) {
                        
                         Object value = null;
                         if ("code".equals(localName)) {
                         value = entry.getLoginModuleName();
                         } else if ("flag".equals(localName)) {
                        
                         if (entry.getControlFlag() == LoginModuleControlFlag.OPTIONAL) {
                         value = "optional";
                         } else if (entry.getControlFlag() == LoginModuleControlFlag.REQUIRED) {
                         value = "required";
                         } else if (entry.getControlFlag() == LoginModuleControlFlag.REQUISITE) {
                         value = "requisite";
                         } else if (entry.getControlFlag() == LoginModuleControlFlag.SUFFICIENT) {
                         value = "sufficient";
                         } else{
                         value = "";
                         }
                         }
                        
                         return value;
                         }
                         /**
                         * module-option
                         */
                         public Object getChildren(
                         AppConfigurationEntry entry,
                         String namespaceUri,
                         String localName) {
                        
                         Map<String, ModuleOption> options = new HashMap<String, ModuleOption>();
                        
                         Set<String> keys = entry.getOptions().keySet();
                         for (String key : keys) {
                         Object value = entry.getOptions().get(key);
                         options.put(key, new ModuleOption(key, value));
                         }
                        
                         return options.values();
                         }
                         /**
                         * module-option name
                         */
                         public Object getAttributeValue(
                         ModuleOption moduleOption,
                         String namespaceUri,
                         String localName) {
                        
                         Object value = null;
                         if ("name".equals(localName)) {
                         value = moduleOption.getName();
                         }
                        
                         return value;
                         }
                         /**
                         * module-option text
                         */
                        // public Object getElementValue(
                        // ModuleOption moduleOption,
                        // String namespaceUri,
                        // String localName) {
                        //
                        // return moduleOption.getValue();
                        // }
                        }
                        


                        package org.jboss.xb.test;
                        
                        import java.io.Serializable;
                        
                        /**
                         * @author Mark Ashworth
                         * @version 1.0.0
                         */
                        public class ModuleOption implements Serializable {
                         /** Serial version id*/
                         private static final long serialVersionUID = 2105836972498754806L;
                        
                         private String name;
                         private Object value = "";
                         /**
                         * Constructs an option with a null value
                         * @param name The name of the option
                         */
                         public ModuleOption(String name) {
                         this.name = name;
                         }
                         /**
                         * Constructs an option with a null value
                         * @param name The name of the option
                         * @param value The value of the option
                         */
                         public ModuleOption(String name, Object value) {
                         this.name = name;
                         this.value = value;
                         }
                         /**
                         * Returns the option name
                         * @return The name of the option
                         */
                         public String getName() {
                         return name;
                         }
                         /**
                         * Returns the option value
                         * @return The value of the option
                         */
                         public Object getValue() {
                         return value;
                         }
                         /**
                         * Sets the option value
                         * @param value The value of the option
                         */
                         public void setValue(Object value) {
                         this.value = value;
                         }
                         /**
                         * Returns whether the option is a password / credential
                         * @return boolean
                         */
                         public boolean isPassword() {
                         if (getName() == null) {
                         return false;
                         }
                         String value = getName().toUpperCase();
                         if (value.indexOf("PASSWORD") >= 0) {
                         return true;
                         }
                         if (value.indexOf("PWD") >= 0) {
                         return true;
                         }
                         if (value.indexOf("CREDENTIAL") >= 0) {
                         return true;
                         }
                         return false;
                         }
                        }
                        
                        

                        package org.jboss.xb.test;
                        
                        import java.io.StringWriter;
                        import java.util.HashMap;
                        import java.util.Map;
                        
                        import javax.security.auth.login.AppConfigurationEntry;
                        import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
                        
                        import junit.framework.TestCase;
                        
                        import org.jboss.security.auth.login.AuthenticationInfo;
                        import org.jboss.security.auth.login.PolicyConfig;
                        import org.jboss.security.auth.spi.LdapExtLoginModule;
                        import org.jboss.xb.binding.AbstractMarshaller;
                        import org.jboss.xb.binding.DtdMarshaller;
                        import org.jboss.xb.binding.Marshaller;
                        
                        public class TestPolicyConfigMarshalling extends TestCase {
                        
                         public void testMarshal() throws Exception {
                        
                         /*
                         * Create the PolicyConfig Object
                         */
                         Map<String, Object> options = new HashMap<String, Object>();
                         options.put("bindCredentials", "password");
                         options.put("java.naming.provider.url", "ldap://localhost:389");
                        
                         AuthenticationInfo[] authInfos = new AuthenticationInfo[2];
                         authInfos[0] = new AuthenticationInfo("other");
                         authInfos[0].addAppConfigurationEntry(
                         new AppConfigurationEntry(LdapExtLoginModule.class.getName(),
                         LoginModuleControlFlag.REQUIRED, options));
                        
                         authInfos[1] = new AuthenticationInfo("test");
                         authInfos[1].addAppConfigurationEntry(
                         new AppConfigurationEntry(LdapExtLoginModule.class.getName(),
                         LoginModuleControlFlag.REQUIRED, options));
                        
                        
                         PolicyConfig config = new PolicyConfig();
                         config.add(authInfos[0]);
                         config.add(authInfos[1]);
                        
                         /*
                         * Marshal the PolicyConfig to XML using the DTD
                         */
                         StringWriter xmlOutput = new StringWriter();
                        
                         System.setProperty(Marshaller.PROP_MARSHALLER, DtdMarshaller.class
                         .getName());
                        
                         AbstractMarshaller marshaller = (AbstractMarshaller) Marshaller.FACTORY
                         .getInstance();
                        
                         marshaller.mapPublicIdToSystemId(
                         "-//JBoss//DTD JBOSS Security Config 3.0//EN",
                         "http://www.jboss.org/j2ee/dtd/security_config.dtd");
                         LoginConfigObjectModelProvider provider = new LoginConfigObjectModelProvider();
                         marshaller.marshal("http://www.jboss.org/j2ee/dtd/security_config.dtd",
                         provider, config, xmlOutput);
                        
                         System.out.println(xmlOutput.getBuffer());
                         }
                        }
                        
                        


                        I could probably attach the files to the Jira task once it is created.

                        Regards,
                        Mark P Ashworth

                        • 10. Re: GenericObjectModelProvider not setting attributes in XML
                          aloubyansky

                          It's another bug.
                          http://jira.jboss.com/jira/browse/JBXB-81
                          It's fixed in the SVN.
                          Thanks!

                          • 11. Re: GenericObjectModelProvider not setting attributes in XML
                            markash

                            Good Day,

                            Thanks, I will check it out.

                            Regards,
                            Mark P Ashworth

                            • 12. Re: GenericObjectModelProvider not setting attributes in XML
                              markash

                              Good Day,

                              Thanks it works!!