ws-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wer...@apache.org
Subject svn commit: r355090 - in /webservices/commons/trunk/policy/src: examples/SimplePolicyExample.java examples/policy2.xml org/apache/ws/policy/util/PolicyFactory.java org/apache/ws/policy/util/PolicyReaderDOM.java
Date Thu, 08 Dec 2005 13:42:05 GMT
Author: werner
Date: Thu Dec  8 05:41:57 2005
New Revision: 355090

URL: http://svn.apache.org/viewcvs?rev=355090&view=rev
Log:
Setup a DOM parser in addition to stream reader and
Axis2 OM.

Added:
    webservices/commons/trunk/policy/src/examples/policy2.xml
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReaderDOM.java
Modified:
    webservices/commons/trunk/policy/src/examples/SimplePolicyExample.java
    webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java

Modified: webservices/commons/trunk/policy/src/examples/SimplePolicyExample.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/examples/SimplePolicyExample.java?rev=355090&r1=355089&r2=355090&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/examples/SimplePolicyExample.java (original)
+++ webservices/commons/trunk/policy/src/examples/SimplePolicyExample.java Thu Dec  8 05:41:57
2005
@@ -16,12 +16,14 @@
 
 package examples;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
 
 import org.apache.ws.policy.model.Policy;
 import org.apache.ws.policy.util.PolicyFactory;
 import org.apache.ws.policy.util.PolicyReader;
+import org.apache.ws.policy.util.PolicyReaderDOM;
 import org.apache.ws.policy.util.PolicyWriter;
 
 /**
@@ -50,6 +52,28 @@
         pwrt.writePolicy(norm, baos);
 
 		System.out.println(baos.toString());
+
+		fis.close();
+        /*
+         * Use standard Parser, w3c DOM
+         */
+		if (args.length > 0) {
+			fis = new FileInputStream(args[0]);			
+		}
+		else {
+			fis = new FileInputStream("policy/src/examples/policy2.xml");
+		}
+        PolicyReaderDOM readerDom = PolicyFactory.getInstance().getPolicyReaderDOM();
+        Policy pDom = readerDom.readPolicy(fis);
+	    Policy normDom  = (Policy)pDom.normalize();
+	    
+        baos = new ByteArrayOutputStream();
+        pwrt.writePolicy(normDom, baos);
+
+		System.out.println(baos.toString());
+
+		fis.close();
+	
 	}
 }
 

Added: webservices/commons/trunk/policy/src/examples/policy2.xml
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/examples/policy2.xml?rev=355090&view=auto
==============================================================================
--- webservices/commons/trunk/policy/src/examples/policy2.xml (added)
+++ webservices/commons/trunk/policy/src/examples/policy2.xml Thu Dec  8 05:41:57 2005
@@ -0,0 +1,18 @@
+<wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
+            xmlns:sec="http://schemas.xmlsoap.org/ws/2002/12/secext" >
+  <!-- 2 -->
+  <sec:SecurityToken>
+    <sec:TokenType>sec:X509v3</sec:TokenType>
+  </sec:SecurityToken>
+  <sec:SecurityHeader wsp:Optional="true"
+                      MustPrepend="true"
+                      MustManifestEncryption="true" />
+  <sec:Integrity wsp:Optional="false" >
+    <sec:MessageParts
+            Dialect="http://schemas.xmlsoap.org/ws/2002/12/wsse#soap" >
+      wsp:Body()
+    </sec:MessageParts>
+  </sec:Integrity>
+</wsp:Policy>
+
+

Modified: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java?rev=355090&r1=355089&r2=355090&view=diff
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java (original)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyFactory.java Thu
Dec  8 05:41:57 2005
@@ -39,4 +39,9 @@
 	public PolicyReader getPolicyReader() {
 		return new PolicyReader();
 	}
+	
+	public PolicyReaderDOM getPolicyReaderDOM() {
+		return new PolicyReaderDOM();
+	}
+
 }

Added: webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReaderDOM.java
URL: http://svn.apache.org/viewcvs/webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReaderDOM.java?rev=355090&view=auto
==============================================================================
--- webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReaderDOM.java (added)
+++ webservices/commons/trunk/policy/src/org/apache/ws/policy/util/PolicyReaderDOM.java Thu
Dec  8 05:41:57 2005
@@ -0,0 +1,224 @@
+/*
+ * 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.ws.policy.util;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Hashtable;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import javax.xml.namespace.QName;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.NamedNodeMap;
+import org.xml.sax.SAXException;
+
+import org.apache.ws.policy.model.AndCompositeAssertion;
+import org.apache.ws.policy.model.Assertion;
+import org.apache.ws.policy.model.Policy;
+import org.apache.ws.policy.model.PolicyReference;
+import org.apache.ws.policy.model.PrimitiveAssertion;
+import org.apache.ws.policy.model.PolicyConstants;
+import org.apache.ws.policy.model.XorCompositeAssertion;
+
+/**
+ * @author Sanka Samaranayake (sanka@apache.org)
+ */
+public class PolicyReaderDOM {
+	PolicyReaderDOM() {
+	}
+
+	public Policy readPolicy(InputStream in) {
+        try {
+			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+
+			dbf.setNamespaceAware(true);
+			dbf.setValidating(false);
+
+			DocumentBuilder db = dbf.newDocumentBuilder();
+			Document doc = db.parse(in);
+			Element element = doc.getDocumentElement();
+			return readPolicy(element);
+		} catch (ParserConfigurationException e) {
+			e.printStackTrace();
+			throw new RuntimeException("error : " + e.getMessage());
+		} catch (SAXException e) {
+			e.printStackTrace();
+			throw new RuntimeException("error : " + e.getMessage());
+		} catch (IOException e) {
+			e.printStackTrace();
+			throw new RuntimeException("error : " + e.getMessage());
+		}
+
+		
+//		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(Element element) {
+		String namespace = element.getNamespaceURI();
+		String localName = element.getLocalName();
+		
+		if (!(namespace.equals(PolicyConstants.WS_POLICY_NAMESPACE_URI))) {
+			return readPrimitiveAssertion(element);
+		}
+		
+		if (localName.equals(PolicyConstants.WS_POLICY)) {
+			return readPolicy(element);
+			
+		} else if (localName.equals(PolicyConstants.AND_COMPOSITE_ASSERTION)) {
+			return readAndComposite(element);
+			
+		} else if (localName.equals(PolicyConstants.XOR_COMPOSITE_ASSERTION)) {
+			return readXorComposite(element);
+			
+		} else if (localName.equals(PolicyConstants.WS_POLICY_REFERENCE)) {
+			return readPolicyReference(element);
+			
+		} else {
+			throw new RuntimeException("unknown element ..");
+		}		
+	}
+	
+	private Policy readPolicy(Element element) {
+		Policy policy = new Policy();
+		policy.addTerms(readTerms(element));
+		return policy;
+	}
+	
+	private AndCompositeAssertion readAndComposite(Element element) {
+		AndCompositeAssertion andCompositeAssertion = new AndCompositeAssertion();
+		andCompositeAssertion.addTerms(readTerms(element));
+		return andCompositeAssertion;
+	}	
+	
+	private XorCompositeAssertion readXorComposite(Element element) {
+		XorCompositeAssertion xorCompositeAssertion = new XorCompositeAssertion();
+		xorCompositeAssertion.addTerms(readTerms(element));
+		return xorCompositeAssertion;
+	}
+	
+	private PolicyReference readPolicyReference(Element element) {
+		Attr attribute = element.getAttributeNode("URI");
+		return new PolicyReference(attribute.getValue());
+	}
+	
+	private PrimitiveAssertion readPrimitiveAssertion(Element element) {
+		QName qname = new QName(element.getNamespaceURI(), element.getLocalName(), element.getPrefix());
+        PrimitiveAssertion result = new PrimitiveAssertion(qname);
+        
+        result.setAttributes(getAttributes(element));
+        
+        // setting the text value ..
+//        String strValue = element.getTextContent();
+//     
+//        if (strValue != null && strValue.length() != 0) {
+//        	System.out.println("Seen a text content at " + element.getLocalName() +
+//        			", value: " + strValue);
+//            result.setStrValue(strValue);            
+//        }
+        
+        //CHECK ME
+		NodeList list = element.getChildNodes();
+		int length = list.getLength();
+
+		for (int i = 0; i < length; i++) {
+			Node node = list.item(i);
+			short nodeType = node.getNodeType();
+			
+			if (nodeType == Node.ELEMENT_NODE) {
+				Element childElement = (Element) node;
+				if (childElement.getNamespaceURI().equals(
+						PolicyConstants.WS_POLICY_NAMESPACE_URI)
+						&& childElement.getLocalName().equals(
+								PolicyConstants.WS_POLICY)) {
+					Policy policy = readPolicy(childElement);
+					result.addTerm(policy);
+
+				} else {
+					PrimitiveAssertion pa = readPrimitiveAssertion(childElement);
+					result.addTerm(pa);
+				}
+			}
+			else if (nodeType == Node.TEXT_NODE) {
+				String strValue = node.getNodeValue();
+		        
+		        if (strValue != null && strValue.length() != 0) {
+		        	result.setStrValue(strValue);            
+		        }
+			}
+        }        
+        return result;       
+    }
+	
+	private ArrayList readTerms(Element element) {
+		ArrayList terms = new ArrayList();
+		NodeList list = element.getChildNodes();
+		int length = list.getLength();
+
+		for (int i = 0; i < length; i++) {
+			Object obj = list.item(i);
+			
+			if (obj instanceof Element) {
+				Element e = (Element) obj;
+				terms.add(readAssertion(e));				
+			}
+		}
+		return terms;		
+	}
+	
+	private Hashtable getAttributes(Element element) {
+		Hashtable attributes = new Hashtable();
+		NamedNodeMap map = element.getAttributes();
+
+		int length = map.getLength();
+
+		for (int i = 0; i < length; i++) {
+			Attr attribute = (Attr) map.item(i);
+			String prefix = attribute.getPrefix();
+			QName qn = null;
+			if (prefix != null) {
+				qn = new QName(attribute.getNamespaceURI(), attribute.getLocalName(), prefix);
+			}
+			else {
+				qn = new QName(attribute.getNamespaceURI(), attribute.getLocalName());				
+			}
+			attributes.put(qn, attribute.getValue());
+		}
+		return attributes;
+	}
+}
\ No newline at end of file



Mime
View raw message