The beginnings of ARP serialization. Tests for the same.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 10 Dec 2002 23:49:03 +0000 (23:49 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 10 Dec 2002 23:49:03 +0000 (23:49 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@369 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/aa/arp/Arp.java
src/edu/internet2/middleware/shibboleth/aa/arp/ArpMarshallingException.java [new file with mode: 0755]
src/edu/internet2/middleware/shibboleth/aa/arp/ArpTests.java
src/edu/internet2/middleware/shibboleth/aa/arp/Rule.java
test/arp1.xml [new file with mode: 0755]
test/arp2.xml [new file with mode: 0755]

index 133eb40..b5eea54 100755 (executable)
@@ -52,6 +52,13 @@ package edu.internet2.middleware.shibboleth.aa.arp;
 import java.security.Principal;
 import java.util.HashSet;
 
+import org.apache.xerces.parsers.DOMParser;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
 /**
  *  An Attribute Release Policy.
  *
@@ -61,7 +68,7 @@ import java.util.HashSet;
 public class Arp {
 
        private Principal principal;
-       private HashSet rules;
+       private HashSet rules = new HashSet();
        private String description;
        private boolean sitePolicy = false;
 
@@ -109,6 +116,68 @@ public class Arp {
        }
 
        /**
+        * Creates an ARP structure from an xml representation.
+        * @param the xml <code>Element</code> containing the ARP structure.
+        */
+
+       void marshall(Element xmlElement) throws ArpMarshallingException {
+
+               //Make sure we are deling with an ARP
+               if (!xmlElement.getTagName().equals("AttributeReleasePolicy")) {
+                       throw new ArpMarshallingException("Element data does not represent an ARP.");
+               }
+
+               //Grab the description
+               NodeList descriptionNodes = xmlElement.getElementsByTagName("Description");
+               if (descriptionNodes.getLength() > 0) {
+                       Element descriptionNode = (Element) descriptionNodes.item(0);
+                       if (descriptionNode.hasChildNodes()
+                               && descriptionNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
+                               description = ((CharacterData) descriptionNode.getFirstChild()).getData();
+                       }
+               }
+
+               //Grab all of the Rule Elements and marshall them
+               NodeList ruleNodes = xmlElement.getElementsByTagName("Rule");
+               if (ruleNodes.getLength() > 0) {
+                       for (int i = 0; i < ruleNodes.getLength(); i++) {
+                               Rule rule = new Rule();
+                               try {
+                                       rule.marshall((Element) ruleNodes.item(i));
+                               } catch (ArpMarshallingException me) {
+                                       throw new ArpMarshallingException("Encountered a problem marshalling ARP Rules: " + me);
+                               }
+                               rules.add(rule);
+                       }
+               }
+       }
+
+       /**
+        * Unmarshalls the <code>Arp</code> into an xml <code>Element</code>.
+        * @return the xml <code>Element</code>
+        */
+
+       Element unmarshall() {
+
+               DOMParser parser = new DOMParser();
+               Document placeHolder = parser.getDocument();
+               Element policyNode = placeHolder.createElement("AttributeReleasePolicy");
+
+               if (description != null) {
+                       Element descriptionNode = placeHolder.createElement("Description");
+                       descriptionNode.appendChild(placeHolder.createTextNode(description));
+                       policyNode.appendChild(descriptionNode);
+               }
+
+               Rule[] rules = getAllRules();
+               for (int i = 0; rules.length > i; i++) {
+                       policyNode.appendChild(rules[i].unmarshall());
+               }
+
+               return policyNode;
+       }
+
+       /**
         * Returns all of the <code>Rule</code> objects that make up this policy.
         * @return the rules 
         */
@@ -119,7 +188,7 @@ public class Arp {
        }
 
        /**
-        * Returns the description for this <code>Arp</code>.
+        * Returns the description for this <code>Arp</code> or null if no description is set.
         * @return String
         */
 
diff --git a/src/edu/internet2/middleware/shibboleth/aa/arp/ArpMarshallingException.java b/src/edu/internet2/middleware/shibboleth/aa/arp/ArpMarshallingException.java
new file mode 100755 (executable)
index 0000000..a9664ff
--- /dev/null
@@ -0,0 +1,65 @@
+/* 
+ * The Shibboleth License, Version 1. 
+ * Copyright (c) 2002 
+ * University Corporation for Advanced Internet Development, Inc. 
+ * All rights reserved
+ * 
+ * 
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ * Redistributions of source code must retain the above copyright notice, this 
+ * list of conditions and the following disclaimer.
+ * 
+ * Redistributions in binary form must reproduce the above copyright notice, 
+ * this list of conditions and the following disclaimer in the documentation 
+ * and/or other materials provided with the distribution, if any, must include 
+ * the following acknowledgment: "This product includes software developed by 
+ * the University Corporation for Advanced Internet Development 
+ * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
+ * may appear in the software itself, if and wherever such third-party 
+ * acknowledgments normally appear.
+ * 
+ * Neither the name of Shibboleth nor the names of its contributors, nor 
+ * Internet2, nor the University Corporation for Advanced Internet Development, 
+ * Inc., nor UCAID may be used to endorse or promote products derived from this 
+ * software without specific prior written permission. For written permission, 
+ * please contact shibboleth@shibboleth.org
+ * 
+ * Products derived from this software may not be called Shibboleth, Internet2, 
+ * UCAID, or the University Corporation for Advanced Internet Development, nor 
+ * may Shibboleth appear in their name, without prior written permission of the 
+ * University Corporation for Advanced Internet Development.
+ * 
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
+ * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
+ * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
+ * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
+ * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package edu.internet2.middleware.shibboleth.aa.arp;
+
+/**
+ * Indicates an error occurred while marshalling/unmarshalling an ARP construct.
+ * 
+ * @author Walter Hoehn (wassa@columbia.edu)
+ */
+
+public class ArpMarshallingException extends Exception {
+
+       public ArpMarshallingException(String message) {
+               super(message);
+       }
+
+}
+
index 5a3f606..29cde33 100755 (executable)
 
 package edu.internet2.middleware.shibboleth.aa.arp;
 
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.util.Properties;
 
-import org.apache.log4j.BasicConfigurator;
-
 import junit.framework.TestCase;
 
+import org.apache.log4j.BasicConfigurator;
+import org.apache.xerces.parsers.DOMParser;
+import org.xml.sax.InputSource;
+
 /**
  * Validation suite for <code>Arp</code> processing.
  * 
@@ -73,6 +77,48 @@ public class ArpTests extends TestCase {
                BasicConfigurator.configure();
        }
 
+       public void testArpMarshalling() {
+
+               //Test ARP description
+               try {
+                       InputStream inStream = new FileInputStream("test/arp1.xml");
+                       DOMParser parser = new DOMParser();
+                       parser.parse(new InputSource(inStream));
+                       Arp arp1 = new Arp();
+                       arp1.marshall(parser.getDocument().getDocumentElement());
+                       assertEquals(
+                               "ARP Description not marshalled properly",
+                               arp1.getDescription(),
+                               "Simplest possible ARP.");
+
+                       //Test Rule description
+                       assertEquals(
+                               "ARP Rule Description not marshalled properly",
+                               arp1.getAllRules()[0].getDescription(),
+                               "Example Rule Description.");
+               } catch (Exception e) {
+                       fail("Failed to marshall ARP.");
+               }
+
+               //Test case where ARP description does not exist
+               try {
+                       InputStream inStream = new FileInputStream("test/arp2.xml");
+                       DOMParser parser = new DOMParser();
+                       parser.parse(new InputSource(inStream));
+                       Arp arp2 = new Arp();
+                       arp2.marshall(parser.getDocument().getDocumentElement());
+                       assertNull("ARP Description not marshalled properly", arp2.getDescription());
+
+                       //Test case where ARP Rule description does not exist   
+                       assertNull(
+                               "ARP Rule Description not marshalled properly",
+                               arp2.getAllRules()[0].getDescription());
+               } catch (Exception e) {
+                       fail("Failed to marshall ARP.");
+               }
+
+       }
+
        public void testRepositories() {
 
                /*
index fc3ba48..493a259 100755 (executable)
 
 package edu.internet2.middleware.shibboleth.aa.arp;
 
+import org.apache.xerces.parsers.DOMParser;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
 /**
  *  An Attribute Release Policy Rule.
  *
@@ -77,4 +84,47 @@ public class Rule {
                this.description = description;
        }
 
+       /**
+        * Unmarshalls the <code>Rule</code> into an xml <code>Element</code>.
+        * @return the xml <code>Element</code>
+        */
+
+       public Element unmarshall() {
+
+               DOMParser parser = new DOMParser();
+               Document placeHolder = parser.getDocument();
+               Element ruleNode = placeHolder.createElement("Rule");
+
+               if (description != null) {
+                       Element descriptionNode = placeHolder.createElement("Description");
+                       descriptionNode.appendChild(placeHolder.createTextNode(description));
+                       ruleNode.appendChild(descriptionNode);
+               }
+
+               return ruleNode;
+       }
+
+       /**
+        * Creates an ARP Rule from an xml representation.
+        * @param the xml <code>Element</code> containing the ARP Rule.
+        */
+       
+       public void marshall(Element element) throws ArpMarshallingException {
+
+               //Make sure we are deling with a Rule
+               if (!element.getTagName().equals("Rule")) {
+                       throw new ArpMarshallingException("Element data does not represent an ARP Rule.");
+               }
+
+               //Grab the description
+               NodeList descriptionNodes = element.getElementsByTagName("Description");
+               if (descriptionNodes.getLength() > 0) {
+                       Element descriptionNode = (Element) descriptionNodes.item(0);
+                       if (descriptionNode.hasChildNodes()
+                               && descriptionNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
+                               description = ((CharacterData) descriptionNode.getFirstChild()).getData();
+                       }
+               }
+       }
+
 }
diff --git a/test/arp1.xml b/test/arp1.xml
new file mode 100755 (executable)
index 0000000..eec00c1
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<AttributeReleasePolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ARP.xsd">
+       <Description>Simplest possible ARP.</Description>
+       <Rule>
+               <Description>Example Rule Description.</Description>
+               <Target>
+                       <AnyTarget/>
+               </Target>
+               <Attribute name="urn:mace:eduPerson:1.0:eduPersonAffiliation">
+                       <AnyValue release="permit"/>
+               </Attribute>
+       </Rule>
+</AttributeReleasePolicy>
\ No newline at end of file
diff --git a/test/arp2.xml b/test/arp2.xml
new file mode 100755 (executable)
index 0000000..b6d7952
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<AttributeReleasePolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ARP.xsd">
+       <Rule>
+               <Target>
+                       <AnyTarget/>
+               </Target>
+               <Attribute name="urn:mace:eduPerson:1.0:eduPersonAffiliation">
+                       <AnyValue release="permit"/>
+               </Attribute>
+       </Rule>
+</AttributeReleasePolicy>
\ No newline at end of file