You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ws.apache.org by sa...@apache.org on 2005/11/20 16:38:09 UTC

svn commit: r345748 - in /webservices/commons/trunk/policy/src/org/apache/policy/util: PolicyAttachmentUtil.java PolicyFactory.java PolicyReader.java PolicyWriter.java WSPolicyAttachmentUtil.java WSPolicyUtil.java

Author: sanka
Date: Sun Nov 20 07:37:48 2005
New Revision: 345748

URL: http://svn.apache.org/viewcvs?rev=345748&view=rev
Log:
Adding PolicyWriter class which creates a policy object using an inputstream. Some refactoring in the util package ..

Added:
    webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyAttachmentUtil.java
      - copied, changed from r345652, webservices/commons/trunk/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java
    webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyReader.java
Removed:
    webservices/commons/trunk/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java
    webservices/commons/trunk/policy/src/org/apache/policy/util/WSPolicyUtil.java
Modified:
    webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java
    webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java

Copied: webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyAttachmentUtil.java (from r345652, webservices/commons/trunk/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java)
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyAttachmentUtil.java?p2=webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyAttachmentUtil.java&p1=webservices/commons/trunk/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java&r1=345652&r2=345748&rev=345748&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyAttachmentUtil.java Sun Nov 20 07:37:48 2005
@@ -33,8 +33,7 @@
 import org.apache.axis2.wsdl.builder.WOMBuilderFactory;
 import org.apache.policy.model.Assertion;
 import org.apache.policy.model.Policy;
-import org.apache.policy.parser.WSPConstants;
-import org.apache.policy.parser.WSPolicyParser;
+import org.apache.policy.model.WSPConstants;
 import org.apache.wsdl.Component;
 import org.apache.wsdl.MessageReference;
 import org.apache.wsdl.WSDLBinding;
@@ -63,8 +62,6 @@
     private WSDLDescription wsdlDescription = null;
     //private HashMap loadedPolicies = new HashMap();
     private PolicyRegistry reg = new PolicyRegistry();
-    private WSPolicyParser parser = WSPolicyParser.getInstance();
-
     
     public WSPolicyAttachmentUtil() {
     }
@@ -465,7 +462,8 @@
     
     private Policy getPolicyFromElement(Element element) {
         InputStream policyInputStream = createInputStream(element);
-        return parser.buildPolicyModel(policyInputStream);
+        PolicyReader reader = PolicyFactory.getInstance().getPolicyReader();
+        return reader.readPolicy(policyInputStream);
     }
     
     private InputStream createInputStream(Element element) {
@@ -515,7 +513,8 @@
                 try {
                     URI policyURI = new URI(uriString);
                     URL policyURL = policyURI.toURL();
-                    Policy newPolicy = parser.buildPolicyModel(policyURL.openStream());
+                    PolicyReader reader = PolicyFactory.getInstance().getPolicyReader();
+                    Policy newPolicy = reader.readPolicy(policyURL.openStream());
                     reg.register(uriString, newPolicy);
                     
                 } catch (Exception e) {
@@ -644,7 +643,8 @@
     
     private void registerPolicyElement(Element element) {
         InputStream elementInputStream = createInputStream(element);
-        Policy policy = parser.buildPolicyModel(elementInputStream);
+        PolicyReader reader = PolicyFactory.getInstance().getPolicyReader();
+        Policy policy = reader.readPolicy(elementInputStream);
         reg.register(policy.getPolicyURI(), policy);
     }
 }

Modified: webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java?rev=345748&r1=345747&r2=345748&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyFactory.java Sun Nov 20 07:37:48 2005
@@ -35,4 +35,8 @@
 	public PolicyWriter getPolicyWriter() {
 		return new PolicyWriter();
 	}
+	
+	public PolicyReader getPolicyReader() {
+		return new PolicyReader();
+	}
 }

Added: webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyReader.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyReader.java?rev=345748&view=auto
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyReader.java (added)
+++ webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyReader.java Sun Nov 20 07:37:48 2005
@@ -0,0 +1,172 @@
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.policy.util;
+
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axis2.om.OMAbstractFactory;
+import org.apache.axis2.om.OMAttribute;
+import org.apache.axis2.om.OMElement;
+import org.apache.axis2.om.OMXMLParserWrapper;
+import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory;
+import org.apache.policy.model.AndCompositeAssertion;
+import org.apache.policy.model.Assertion;
+import org.apache.policy.model.Policy;
+import org.apache.policy.model.PolicyReference;
+import org.apache.policy.model.PrimitiveAssertion;
+import org.apache.policy.model.WSPConstants;
+import org.apache.policy.model.XorCompositeAssertion;
+
+/**
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public class PolicyReader {
+	PolicyReader() {
+	}
+
+	public Policy readPolicy(InputStream in) {
+		try {
+			XMLStreamReader reader = XMLInputFactory.newInstance()
+					.createXMLStreamReader(in);
+			OMXMLParserWrapper builder = OMXMLBuilderFactory
+					.createStAXOMBuilder(OMAbstractFactory.getOMFactory(),
+							reader);
+
+			OMElement element = builder.getDocumentElement();
+			return readPolicy(element);
+
+		} catch (XMLStreamException ex) {
+			throw new RuntimeException("error : " + ex.getMessage());
+		}
+	}
+	
+	private Assertion readAssertion(OMElement element) {
+		String namespace = element.getNamespace().getName();
+		String localName = element.getLocalName();
+		
+		if (!(namespace.equals(WSPConstants.WSU_NAMESPACE_URI))) {
+			return readPrimitiveAssertion(element);
+		}
+		
+		if (localName.equals(WSPConstants.WS_POLICY)) {
+			return readPolicy(element);
+			
+		} else if (localName.equals(WSPConstants.AND_COMPOSITE_ASSERTION)) {
+			return readAndComposite(element);
+			
+		} else if (localName.equals(WSPConstants.XOR_COMPOSITE_ASSERTION)) {
+			return readXorComposite(element);
+			
+		} else if (localName.equals(WSPConstants.WS_POLICY_REFERENCE)) {
+			return readPolicyReference(element);
+			
+		} else {
+			throw new RuntimeException("unknown element ..");
+		}		
+	}
+	
+	private Policy readPolicy(OMElement element) {
+		Policy policy = new Policy();
+		policy.addTerms(readTerms(element));
+		return policy;
+	}
+	
+	private AndCompositeAssertion readAndComposite(OMElement element) {
+		AndCompositeAssertion andCompositeAssertion = new AndCompositeAssertion();
+		andCompositeAssertion.addTerms(readTerms(element));
+		return andCompositeAssertion;
+	}	
+	
+	private XorCompositeAssertion readXorComposite(OMElement element) {
+		XorCompositeAssertion xorCompositeAssertion = new XorCompositeAssertion();
+		xorCompositeAssertion.addTerms(readTerms(element));
+		return xorCompositeAssertion;
+	}
+	
+	private PolicyReference readPolicyReference(OMElement element) {
+		OMAttribute attribute = element.getAttribute(new QName("URI"));
+		return new PolicyReference(attribute.getAttributeValue());
+	}
+	
+	private PrimitiveAssertion readPrimitiveAssertion(OMElement element) {
+        QName qname = element.getQName();
+        PrimitiveAssertion result = new PrimitiveAssertion(qname);
+        
+        result.setAttributes(getAttributes(element));
+        
+        // setting the text value ..
+        String strValue = element.getText();
+     
+        if (strValue != null && strValue.length() != 0) {
+            result.setStrValue(strValue);            
+        }
+        
+        //CHECK ME
+        Iterator childElements = element.getChildElements();
+        
+        while (childElements.hasNext()) {
+            OMElement childElement = (OMElement) childElements.next();
+            
+            if (childElement.getNamespace().getName().equals(WSPConstants.WS_POLICY_NAMESPACE_URI)
+                    && childElement.getLocalName().equals(WSPConstants.WS_POLICY)) {
+                Policy policy = readPolicy(childElement);
+                result.addTerm(policy);
+        
+            } else {
+                PrimitiveAssertion pa = readPrimitiveAssertion(childElement);
+                result.addTerm(pa);                
+            }
+        }        
+        return result;       
+    }
+	
+	private ArrayList readTerms(OMElement element) {
+		ArrayList terms = new ArrayList();
+		Iterator childElements = element.getChildren();
+		
+		while (childElements.hasNext()) {
+			Object obj = childElements.next();
+			
+			if (obj instanceof OMElement) {
+				OMElement e = (OMElement) obj;
+				terms.add(readAssertion(e));				
+			}
+		}
+		return terms;		
+	}
+	
+	private Hashtable getAttributes(OMElement element) {
+		Hashtable attributes = new Hashtable();
+		Iterator iterator = element.getAllAttributes();
+
+		while (iterator.hasNext()) {
+			OMAttribute attribute = (OMAttribute) iterator.next();
+			attributes.put(attribute.getQName(), attribute.getAttributeValue());
+		}
+
+		return attributes;
+	}
+}
\ No newline at end of file

Modified: webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java?rev=345748&r1=345747&r2=345748&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/policy/util/PolicyWriter.java Sun Nov 20 07:37:48 2005
@@ -16,6 +16,7 @@
 
 package org.apache.policy.util;
 
+
 import java.io.OutputStream;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -31,8 +32,8 @@
 import org.apache.policy.model.Policy;
 import org.apache.policy.model.PolicyReference;
 import org.apache.policy.model.PrimitiveAssertion;
+import org.apache.policy.model.WSPConstants;
 import org.apache.policy.model.XorCompositeAssertion;
-import org.apache.policy.parser.WSPConstants;
 
 /**
  * @author Sanka Samaranayake (sanka@apache.org)