ARP Rule Target serialization.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 19 Dec 2002 23:42:10 +0000 (23:42 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Thu, 19 Dec 2002 23:42:10 +0000 (23:42 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@391 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/aa/arp/ArpEngine.java
src/edu/internet2/middleware/shibboleth/aa/arp/Rule.java

index 1c5d754..a96c3de 100755 (executable)
@@ -49,7 +49,6 @@
 
 package edu.internet2.middleware.shibboleth.aa.arp;
 
-import java.io.IOException;
 import java.io.StringWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -113,7 +112,7 @@ public class ArpEngine {
         * @param functionIdentifier the identifier for the function
         * @return the <code>Matchfunction</code> or null if not registered
         */
-       
+
        public static MatchFunction lookupMatchFunction(URI functionIdentifier) throws ArpException {
                String className = null;
 
@@ -158,11 +157,11 @@ public class ArpEngine {
                                                format.setIndent(4);
                                                XMLSerializer serializer = new XMLSerializer(writer, format);
                                                serializer.serialize(userPolicies[i].unmarshall());
-                                               log.debug(
-                                                       "Dumping ARP from:" + System.getProperty("line.separator") + writer.toString());
+                                               log.debug("Dumping ARP:" + System.getProperty("line.separator") + writer.toString());
                                        }
-                               } catch (ArpMarshallingException ame) {
-                               } catch (IOException ioe) {
+                               } catch (Exception e) {
+                                       log.error(
+                                               "Encountered a strange error while writing ARP debug messages.  This should never happen.");
                                }
                        }
 
@@ -234,12 +233,12 @@ public class ArpEngine {
                throws ArpProcessingException {
 
                Set releaseSet = new HashSet();
-               
+
                log.info("Applying Attribute Release Policies.");
                if (log.isDebugEnabled()) {
                        log.debug("Processing the following attributes:");
-                       for (int i = 0;attributes.length > i; i++) {
-                               log.debug("Attribute: (" + attributes[i].getName() + ")");      
+                       for (int i = 0; attributes.length > i; i++) {
+                               log.debug("Attribute: (" + attributes[i].getName() + ")");
                        }
                }
 
@@ -305,19 +304,25 @@ public class ArpEngine {
                        if (!canonicalSpec.containsKey(attributes[i].getName().toString())) {
                                canonicalSpec.put(attributes[i].getName().toString(), attributes[i]);
                        } else {
-                               if (((Rule.Attribute) canonicalSpec.get(attributes[i].getName().toString())).denyAnyValue()) {
+                               if (((Rule.Attribute) canonicalSpec.get(attributes[i].getName().toString()))
+                                       .denyAnyValue()) {
                                        continue;
                                }
                                if (attributes[i].denyAnyValue()) {
-                                       ((Rule.Attribute) canonicalSpec.get(attributes[i].getName().toString())).setAnyValueDeny(true);
+                                       ((Rule.Attribute) canonicalSpec.get(attributes[i].getName().toString())).setAnyValueDeny(
+                                               true);
                                        continue;
                                }
                                if (attributes[i].releaseAnyValue()) {
-                                       ((Rule.Attribute) canonicalSpec.get(attributes[i].getName().toString())).setAnyValuePermit(true);
+                                       (
+                                               (Rule.Attribute) canonicalSpec.get(
+                                                       attributes[i].getName().toString())).setAnyValuePermit(
+                                               true);
                                }
                                Rule.AttributeValue[] values = attributes[i].getValues();
                                for (int j = 0; values.length > j; j++) {
-                                       ((Rule.Attribute) canonicalSpec.get(attributes[i].getName().toString())).addValue(values[j]);
+                                       ((Rule.Attribute) canonicalSpec.get(attributes[i].getName().toString())).addValue(
+                                               values[j]);
                                }
                        }
                }
index 5f91617..adfed72 100755 (executable)
@@ -66,6 +66,7 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
 
 /**
  *  An Attribute Release Policy Rule.
@@ -123,7 +124,7 @@ public class Rule {
                                descriptionNode.appendChild(placeHolder.createTextNode(description));
                                ruleNode.appendChild(descriptionNode);
                        }
-
+                       ruleNode.appendChild(placeHolder.importNode(target.unmarshall(), true));
                        return ruleNode;
                } catch (ParserConfigurationException e) {
                        log.error("Encountered a problem unmarshalling an ARP Rule: " + e);
@@ -181,7 +182,7 @@ public class Rule {
         * @param requester the SHAR making the request
         * @param resource the resource on behalf of which the request is being made
         */
-       
+
        public boolean matchesRequest(String requester, URL resource) {
                if (target.matchesAny()) {
                        return true;
@@ -212,52 +213,83 @@ public class Rule {
                private Resource resource = null;
                private boolean matchesAny = false;
 
-       /**
-        * Creates an ARP Rule Target from an xml representation.
-        * @param element the xml <code>Element</code> containing the ARP Rule.
-        */
-       void marshall(Element element) throws ArpMarshallingException {
+               /**
+                * Unmarshalls the <code>Rule.Target</code> into an xml <code>Element</code>.
+                * @return the xml <code>Element</code>
+                */
 
-               //Make sure we are dealing with a Target
-               if (!element.getTagName().equals("Target")) {
-                       log.error("Element data does not represent an ARP Rule Target.");
-                       throw new ArpMarshallingException("Element data does not represent an ARP Rule target.");
-               }
+               Element unmarshall() throws ArpMarshallingException {
 
-               //Handle <AnyTarget/> definitions
-               NodeList anyTargetNodeList = element.getElementsByTagName("AnyTarget");
-               if (anyTargetNodeList.getLength() == 1) {
-                       matchesAny = true;
-                       return;
+                       try {
+                               Document placeHolder =
+                                       DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+                               Element targetNode = placeHolder.createElement("Target");
+
+                               if (matchesAny) {
+                                       Element anyTargetNode = placeHolder.createElement("AnyTarget");
+                                       targetNode.appendChild(anyTargetNode);
+                                       return targetNode;
+                               }
+                               targetNode.appendChild(placeHolder.importNode(requester.unmarshall(), true));
+                               if (target.resource.matchesAny()) {
+                                       Element anyResourceNode = placeHolder.createElement("AnyResource");
+                                       targetNode.appendChild(anyResourceNode);
+                                       return targetNode;
+                               }
+                               targetNode.appendChild(placeHolder.importNode(resource.unmarshall(), true));
+                               return targetNode;
+                       } catch (ParserConfigurationException e) {
+                               log.error("Encountered a problem unmarshalling an ARP Rule: " + e);
+                               throw new ArpMarshallingException("Encountered a problem unmarshalling an ARP Rule.");
+                       }
                }
 
-               //Create Requester
-               NodeList requesterNodeList = element.getElementsByTagName("Requester");
-               if (requesterNodeList.getLength() == 1) {
-                       requester = new Requester();
-                       requester.marshall((Element) requesterNodeList.item(0));
-               } else {
-                       log.error("ARP Rule Target contains invalid data: incorrectly specified <Requester>.");
-                       throw new ArpMarshallingException("ARP Rule Target contains invalid data: incorrectly specified <Requester>.");
-               }
+               /**
+                * Creates an ARP Rule Target from an xml representation.
+                * @param element the xml <code>Element</code> containing the ARP Rule.
+                */
+               void marshall(Element element) throws ArpMarshallingException {
 
-               //Handle <AnyResource/>
-               NodeList anyResourceNodeList = element.getElementsByTagName("AnyResource");
-               if (anyResourceNodeList.getLength() == 1) {
-                       resource = new Resource();
-                       return;
-               }
+                       //Make sure we are dealing with a Target
+                       if (!element.getTagName().equals("Target")) {
+                               log.error("Element data does not represent an ARP Rule Target.");
+                               throw new ArpMarshallingException("Element data does not represent an ARP Rule target.");
+                       }
+
+                       //Handle <AnyTarget/> definitions
+                       NodeList anyTargetNodeList = element.getElementsByTagName("AnyTarget");
+                       if (anyTargetNodeList.getLength() == 1) {
+                               matchesAny = true;
+                               return;
+                       }
+
+                       //Create Requester
+                       NodeList requesterNodeList = element.getElementsByTagName("Requester");
+                       if (requesterNodeList.getLength() == 1) {
+                               requester = new Requester();
+                               requester.marshall((Element) requesterNodeList.item(0));
+                       } else {
+                               log.error("ARP Rule Target contains invalid data: incorrectly specified <Requester>.");
+                               throw new ArpMarshallingException("ARP Rule Target contains invalid data: incorrectly specified <Requester>.");
+                       }
 
-               //Create Resource
-               NodeList resourceNodeList = element.getElementsByTagName("Resource");
-               if (resourceNodeList.getLength() == 1) {
-                       resource = new Resource();
-                       resource.marshall((Element) resourceNodeList.item(0));
-               } else {
-                       log.error("ARP Rule Target contains invalid data: incorrectly specified <Resource>.");
-                       throw new ArpMarshallingException("ARP Rule Target contains invalid data: incorrectly specified <Resource>.");
+                       //Handle <AnyResource/>
+                       NodeList anyResourceNodeList = element.getElementsByTagName("AnyResource");
+                       if (anyResourceNodeList.getLength() == 1) {
+                               resource = new Resource();
+                               return;
+                       }
+
+                       //Create Resource
+                       NodeList resourceNodeList = element.getElementsByTagName("Resource");
+                       if (resourceNodeList.getLength() == 1) {
+                               resource = new Resource();
+                               resource.marshall((Element) resourceNodeList.item(0));
+                       } else {
+                               log.error("ARP Rule Target contains invalid data: incorrectly specified <Resource>.");
+                               throw new ArpMarshallingException("ARP Rule Target contains invalid data: incorrectly specified <Resource>.");
+                       }
                }
-       }
 
                boolean matchesAny() {
                        return matchesAny;
@@ -286,11 +318,43 @@ public class Rule {
                String getValue() {
                        return value;
                }
-                       /**
-                        * Creates an ARP Rule Target Resource from an xml representation.
-                        * @param element the xml <code>Element</code> containing the ARP Rule.
-                        */
-                       void marshall(Element element) throws ArpMarshallingException {
+
+               /**
+                * Unmarshalls the <code>Rule.Resource</code> into an xml <code>Element</code>.
+                * @return the xml <code>Element</code>
+                */
+
+               Element unmarshall() throws ArpMarshallingException {
+
+                       try {
+                               Document placeHolder =
+                                       DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+                               Element resourceNode = placeHolder.createElement("Resource");
+                               if (!matchFunctionIdentifier
+                                       .equals(new URI("urn:mace:shibboleth:arp:matchFunction:resourceTree"))) {
+                                       resourceNode.setAttribute("matchFunction", matchFunctionIdentifier.toString());
+                               }
+                               Text valueNode = placeHolder.createTextNode(value);
+                               resourceNode.appendChild(valueNode);
+                               return resourceNode;
+
+                       } catch (URISyntaxException e) {
+                               log.error("Encountered a problem unmarshalling an ARP Rule Resource: " + e);
+                               throw new ArpMarshallingException("Encountered a problem unmarshalling an ARP Rule Resource.");
+                       } catch (ParserConfigurationException e) {
+                               log.error("Encountered a problem unmarshalling an ARP Rule Resource: " + e);
+                               throw new ArpMarshallingException("Encountered a problem unmarshalling an ARP Rule Resource.");
+                       }
+               }
+
+               /**
+                * Creates an ARP Rule Target Resource from an xml representation.
+                * @param element the xml <code>Element</code> containing the ARP Rule.
+                */
+               void marshall(Element element) throws ArpMarshallingException {
+
+                       matchesAny = false;
+
                        //Make sure we are deling with a Resource
                        if (!element.getTagName().equals("Resource")) {
                                log.error("Element data does not represent an ARP Rule Target.");
@@ -330,6 +394,34 @@ public class Rule {
                }
 
                /**
+                * Unmarshalls the <code>Rule.Requester</code> into an xml <code>Element</code>.
+                * @return the xml <code>Element</code>
+                */
+
+               Element unmarshall() throws ArpMarshallingException {
+
+                       try {
+                               Document placeHolder =
+                                       DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+                               Element requesterNode = placeHolder.createElement("Requester");
+                               if (!matchFunctionIdentifier
+                                       .equals(new URI("urn:mace:shibboleth:arp:matchFunction:exactShar"))) {
+                                       requesterNode.setAttribute("matchFunction", matchFunctionIdentifier.toString());
+                               }
+                               Text valueNode = placeHolder.createTextNode(value);
+                               requesterNode.appendChild(valueNode);
+                               return requesterNode;
+
+                       } catch (URISyntaxException e) {
+                               log.error("Encountered a problem unmarshalling an ARP Rule Requester: " + e);
+                               throw new ArpMarshallingException("Encountered a problem unmarshalling an ARP Rule Requester.");
+                       } catch (ParserConfigurationException e) {
+                               log.error("Encountered a problem unmarshalling an ARP Rule Requester: " + e);
+                               throw new ArpMarshallingException("Encountered a problem unmarshalling an ARP Rule Requester.");
+                       }
+               }
+
+               /**
                 * Creates an ARP Rule Target Requester from an xml representation.
                 * @param element the xml <code>Element</code> containing the ARP Rule.
                 */