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/07 17:49:54 UTC
svn commit: r331547 - in /webservices/commons/policy: ./ src/ src/org/
src/org/apache/ src/org/apache/policy/ src/org/apache/policy/model/
src/org/apache/policy/parser/ src/org/apache/policy/util/
Author: sanjiva
Date: Mon Nov 7 08:49:40 2005
New Revision: 331547
URL: http://svn.apache.org/viewcvs?rev=331547&view=rev
Log:
importing policy model code from scratch area
Added:
webservices/commons/policy/
webservices/commons/policy/build.xml
webservices/commons/policy/src/
webservices/commons/policy/src/org/
webservices/commons/policy/src/org/apache/
webservices/commons/policy/src/org/apache/policy/
webservices/commons/policy/src/org/apache/policy/model/
webservices/commons/policy/src/org/apache/policy/model/AndCompositeAssertion.java
webservices/commons/policy/src/org/apache/policy/model/Assertion.java
webservices/commons/policy/src/org/apache/policy/model/CompositeAssertion.java
webservices/commons/policy/src/org/apache/policy/model/Policy.java
webservices/commons/policy/src/org/apache/policy/model/PolicyReference.java
webservices/commons/policy/src/org/apache/policy/model/PrimitiveAssertion.java
webservices/commons/policy/src/org/apache/policy/model/XorCompositeAssertion.java
webservices/commons/policy/src/org/apache/policy/parser/
webservices/commons/policy/src/org/apache/policy/parser/WSPConstants.java
webservices/commons/policy/src/org/apache/policy/parser/WSPolicyParser.java
webservices/commons/policy/src/org/apache/policy/util/
webservices/commons/policy/src/org/apache/policy/util/PolicyRegistry.java
webservices/commons/policy/src/org/apache/policy/util/StringUtils.java
webservices/commons/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java
Added: webservices/commons/policy/build.xml
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/build.xml?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/build.xml (added)
+++ webservices/commons/policy/build.xml Mon Nov 7 08:49:40 2005
@@ -0,0 +1,23 @@
+<project basedir="." default="jar">
+ <property name="src" value="src"></property>
+ <property name="classes" value="classes"></property>
+ <property name="bin" value="bin"></property>
+
+ <target name="init">
+ <mkdir dir="${classes}"></mkdir>
+ <mkdir dir="${bin}"></mkdir>
+ </target>
+
+ <target name="compile" depends="init">
+ <javac srcdir="${src}" destdir="${classes}">
+ <classpath>
+ <fileset dir="..\lib" includes="*.jar"></fileset>
+ </classpath>
+ </javac>
+ </target>
+
+ <target name="jar" depends="compile">
+ <jar basedir="${classes}" destfile="${bin}/ws-policy.jar"></jar>
+ <copy file="${bin}/ws-policy.jar" todir="../lib" overwrite="true"></copy>
+ </target>
+</project>
\ No newline at end of file
Added: webservices/commons/policy/src/org/apache/policy/model/AndCompositeAssertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/model/AndCompositeAssertion.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/model/AndCompositeAssertion.java (added)
+++ webservices/commons/policy/src/org/apache/policy/model/AndCompositeAssertion.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,342 @@
+/*
+ * 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.model;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.policy.util.PolicyRegistry;
+
+/**
+ * AndCompositeAssertion represents either policy or a single policy
+ * alternative. It requires that all its terms are satisfied.
+ */
+public class AndCompositeAssertion extends CompositeAssertion implements Assertion {
+
+ public AndCompositeAssertion() {
+ }
+
+ /**
+ * Adds an Assertion to its terms list
+ * @param assertion Assertion to be added
+ */
+ public void addTerm(Assertion assertion) {
+ if (!(isNormalized() && (assertion instanceof PrimitiveAssertion))) {
+ setNormalize(false);
+ }
+ super.addTerm(assertion);
+ }
+
+ /**
+ * Returns the intersection of self and argument against a
+ * specified Policy Registry.
+ *
+ * @param assertion the assertion to intersect with self
+ * @param reg a sepcified policy registry
+ * @return assertion the assertion which is equivalent to
+ * intersection between self and the argument
+ */
+ public Assertion intersect(Assertion assertion, PolicyRegistry reg) {
+
+ CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this : normalize(reg));
+
+ if (!(normalizedMe instanceof AndCompositeAssertion)) {
+ return normalizedMe.intersect(assertion, reg);
+ }
+
+ if (assertion instanceof PrimitiveAssertion) {
+ QName qname = ((PrimitiveAssertion) assertion).getName();
+ Iterator iterator = getTerms().iterator();
+ boolean isMatch = false;
+
+ while (iterator.hasNext()) {
+ PrimitiveAssertion primTerm = (PrimitiveAssertion) iterator.next();
+ if (primTerm.getName().equals(qname)) {
+ isMatch = true;
+ break;
+ }
+ }
+ return (isMatch) ? normalizedMe : new XorCompositeAssertion();
+ }
+
+ CompositeAssertion target = (CompositeAssertion) assertion;
+ target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
+
+ if (target instanceof Policy) {
+ XorCompositeAssertion alters = (XorCompositeAssertion) target.getTerms().get(0);
+ return normalizedMe.intersect(alters);
+
+ } else if (target instanceof XorCompositeAssertion) {
+ XorCompositeAssertion result = new XorCompositeAssertion();
+ Iterator iterator = target.getTerms().iterator();
+
+ while (iterator.hasNext()) {
+ AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator.next();
+ Assertion value = normalizedMe.intersect(andTerm);
+ if (value instanceof AndCompositeAssertion) {
+ result.addTerm(value);
+ }
+ }
+ return result;
+ }
+
+ if (normalizedMe.isEmpty()) {
+ return target;
+ }
+ if (target.isEmpty()) {
+ return normalizedMe;
+ }
+
+ List primTermsA = ((size() > target.size()) ? normalizedMe.getTerms() : target.getTerms());
+ List primTermsB = ((size() > target.size()) ? target.getTerms() : normalizedMe.getTerms());
+
+ boolean isMatch = true;
+ PrimitiveAssertion primTermA, primTermB;
+ QName qnameA, qnameB;
+
+ for (int i = 0; i < primTermsA.size(); i++) {
+ primTermA = (PrimitiveAssertion) primTermsA.get(i);
+ qnameA = primTermA.getName();
+ boolean flag = false;
+
+ for (int j = 0; j < primTermsB.size(); j++) {
+ primTermB = (PrimitiveAssertion) primTermsB.get(j);
+ qnameB = primTermB.getName();
+ if (qnameA.equals(qnameB)) {
+ flag = true;
+ break;
+ }
+ }
+ if (!flag) {
+ isMatch = false;
+ break;
+ }
+ }
+
+ if (isMatch) { // vocabulary matches
+ AndCompositeAssertion result = new AndCompositeAssertion();
+ result.addTerms(primTermsA);
+ result.addTerms(primTermsB);
+ return result;
+ }
+
+ return new XorCompositeAssertion(); // no behaviour is admisible
+ }
+
+
+
+ /**
+ * Returns an assertion which is equivalent to merge of self and the
+ * argument.
+ *
+ * @param assertion the assertion to be merged with
+ * @param reg the policy registry which the is used resolve external policy
+ * references
+ * @return assertion the resultant assertion which is equivalent to merge
+ * of self and argument
+ */
+ public Assertion merge(Assertion assertion, PolicyRegistry reg) {
+
+ CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this : normalize(reg));
+
+ if (!(normalizedMe instanceof AndCompositeAssertion)) {
+ return normalizedMe.merge(assertion, reg);
+ }
+
+ if (assertion instanceof PrimitiveAssertion) {
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerm(assertion);
+ andTerm.addTerms(normalizedMe.getTerms());
+ andTerm.setNormalize(true);
+ return andTerm;
+ }
+
+ CompositeAssertion target = (CompositeAssertion) assertion;
+ target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
+
+ if (target instanceof Policy) {
+ XorCompositeAssertion xorTerm = (XorCompositeAssertion) target.getTerms().get(0);
+ return normalizedMe.merge(xorTerm);
+
+ } else if (target instanceof XorCompositeAssertion) {
+ XorCompositeAssertion xorTerm = new XorCompositeAssertion();
+
+ Iterator hisAndTerms = target.getTerms().iterator();
+ while (hisAndTerms.hasNext()) {
+ AndCompositeAssertion hisAndTerm = (AndCompositeAssertion) hisAndTerms.next();
+ xorTerm.addTerm(normalizedMe.merge(hisAndTerm));
+ }
+ xorTerm.setNormalize(true);
+ return xorTerm;
+
+ } else if (target instanceof AndCompositeAssertion) {
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerms(normalizedMe.getTerms());
+ andTerm.addTerms(target.getTerms());
+ andTerm.setNormalize(true);
+ return andTerm;
+ }
+
+ throw new IllegalArgumentException("error : merge is not defined for"
+ + assertion.getClass().getName());
+ }
+
+ /**
+ * Returns an Assertion which is normalized using a specified
+ * policy registry.
+ *
+ * @param reg the policy registry used to resolve policy
+ * references
+ * @return an Assertion which is the normalized form of
+ * self
+ */
+ public Assertion normalize(PolicyRegistry reg) {
+ AndCompositeAssertion resultantAndTerm = new AndCompositeAssertion();
+ XorCompositeAssertion resultantXorTerm = new XorCompositeAssertion();
+
+ ArrayList childAndTermList = new ArrayList();
+ ArrayList childXorTermList = new ArrayList();
+
+ Iterator myTerms = getTerms().iterator();
+
+ while (myTerms.hasNext()) {
+ Object term = myTerms.next();
+
+ if (term instanceof PolicyReference) {
+ if (reg == null) {
+ throw new RuntimeException("PolicyCache is not defined");
+ }
+ PolicyReference policyRef = (PolicyReference) term;
+ Policy policy = reg.lookup(policyRef.getPolicyURIString());
+ if (policy == null) {
+ throw new RuntimeException("PolicyReference<" + policyRef.getPolicyURIString() + "can not be resolved");
+ }
+
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerms(policy.getTerms());
+ Assertion normalizedPolicyRef = andTerm.normalize(reg);
+
+ if (normalizedPolicyRef instanceof AndCompositeAssertion) {
+ childAndTermList.add(normalizedPolicyRef);
+ } else {
+ childXorTermList.add(normalizedPolicyRef);
+ }
+
+
+ } else if (term instanceof PrimitiveAssertion) {
+ resultantAndTerm.addTerm((Assertion) term);
+
+ } else if (term instanceof CompositeAssertion) {
+ CompositeAssertion cterm = (CompositeAssertion) term;
+
+ cterm =((cterm.isNormalized()) ? cterm :(CompositeAssertion) cterm.normalize(reg));
+
+ if (cterm instanceof AndCompositeAssertion) {
+ childAndTermList.add(cterm);
+ } else {
+ childXorTermList.add(cterm);
+ }
+ }
+ }
+
+ // processing child-AndCompositeAssertions
+
+ if (! childAndTermList.isEmpty()) {
+ Iterator ANDterms = childAndTermList.iterator();
+
+ while (ANDterms.hasNext()) {
+ CompositeAssertion ANDterm = (CompositeAssertion) ANDterms.next();
+ resultantAndTerm.addTerms(ANDterm.getTerms());
+ }
+ }
+
+ // processing child-XORCompositeAssertions
+ if (childXorTermList.size() > 1) {
+
+ outer : for (int i = 0; i < childXorTermList.size(); i++) {
+ inner : for (int j = i; j < childXorTermList.size(); j++) {
+ if (i != j) {
+ XorCompositeAssertion xorTermA = (XorCompositeAssertion) childXorTermList.get(i);
+ XorCompositeAssertion xorTermB = (XorCompositeAssertion) childXorTermList.get(j);
+
+ /*
+ * if xorTermA or xorTermB is empty then the result should be an
+ * a policy with zero alternatives
+ */
+
+ if (xorTermA.isEmpty() || xorTermB.isEmpty()) {
+ resultantXorTerm = new XorCompositeAssertion();
+ break outer;
+ }
+ Iterator interatorA = xorTermA.getTerms().iterator();
+
+ while (interatorA.hasNext()) {
+ CompositeAssertion andTermA = (CompositeAssertion) interatorA.next();
+ Iterator iteratorB = xorTermB.getTerms().iterator();
+
+ while (iteratorB.hasNext()) {
+
+ CompositeAssertion andTermB = (CompositeAssertion) iteratorB.next();
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerms(andTermA.getTerms());
+ andTerm.addTerms(andTermB.getTerms());
+ resultantXorTerm.addTerm(andTerm);
+ }
+ }
+
+ }
+ }
+ }
+
+ } else if (childXorTermList.size() == 1) {
+ CompositeAssertion XORterm = (CompositeAssertion) childXorTermList.get(0);
+ resultantXorTerm.addTerms(XORterm.getTerms());
+ }
+
+ if (childXorTermList.isEmpty()) {
+
+ resultantAndTerm.setNormalize(true);
+ return resultantAndTerm;
+ }
+
+ if (resultantXorTerm.isEmpty()) {
+ if (resultantAndTerm.isEmpty()) {
+ resultantAndTerm.setNormalize(true);
+ return resultantAndTerm;
+ }
+ resultantXorTerm.setNormalize(true);
+ return resultantXorTerm;
+ }
+
+ // get list of primitive assertions form result (AndCompositeAssertion)
+ List primTerms = resultantAndTerm.getTerms();
+
+ // these terms should be AndCompositeAssertions
+ Iterator andTerms = resultantXorTerm.getTerms().iterator();
+
+ while (andTerms.hasNext()) {
+ CompositeAssertion andTerm = (CompositeAssertion) andTerms.next();
+ andTerm.addTerms(primTerms);
+ }
+
+ resultantXorTerm.setNormalize(true);
+ return resultantXorTerm;
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/model/Assertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/model/Assertion.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/model/Assertion.java (added)
+++ webservices/commons/policy/src/org/apache/policy/model/Assertion.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,110 @@
+/*
+ * 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.model;
+
+import org.apache.policy.util.PolicyRegistry;
+
+/**
+ * Assertion is an interface which all constructs of policy must implements. It
+ * defines few policy operations that all policy constructs must support.
+ */
+
+public interface Assertion {
+ /** Defines the short value for Primitive Assertions */
+ public static final short PRIMITIVE_TYPE = 0x1;
+
+ /** Defines the short value for ANDCompositeAssertion */
+ public static final short COMPOSITE_AND_TYPE = 0x2;
+
+ /** Defines the short value for XORCompositeAssertion*/
+ public static final short COMPOSITE_XOR_TYPE = 0x3;
+
+// /**
+// *
+// * @return
+// */
+// public Assertion normalize() throws UnsupportedOperationException;
+//
+ /**
+ * Returns an assertion which is the equivalent of intersect of
+ * self and argument. The rules to construct the equivalent assertion are
+ * specified in WS Policy 1.0 specification.
+ *
+ * @param assertion the assertion to intersect with
+ * @return the equivalent of intersect of self and the argument
+ */
+ public Assertion intersect(Assertion assertion)
+ throws UnsupportedOperationException;
+
+ /**
+ * Returns an assertion which is equivalent of intersect of self and
+ * argument. Here the external policy are resolved via a policy registry
+ * that is supplied as an argument.
+ *
+ * @param assertion the assertion to intersect with
+ * @param cache the policy registry which is used to resolve external
+ * policy references
+ * @return the equivalent of intersection of self and argument
+ * @throws UnsupportedOperationException if the operation is not meaningful
+ */
+ public Assertion intersect(Assertion assertion, PolicyRegistry reg)
+ throws UnsupportedOperationException;
+
+ /**
+ * Returns the equivalent of merge of self and argument. The rules to
+ * construct the equivalent of merge are defined in WS Policy specification
+ * 1.0
+ *
+ * @param assertion the argument to merge with
+ * @return the equivalent of the merge of self and argument
+ */
+ public Assertion merge(Assertion assertion)
+ throws UnsupportedOperationException;
+
+ /**
+ * Returns the equivalent of merge of self and argument. The rules to
+ * construct argument are specified in WS Policy specification 1.0 Here the
+ * external policy references are resolved via a policy registry that is
+ * supplied as an argument
+ *
+ * @param assertion the assertion to merge with
+ * @param reg the policy registry that should be used to resolve external
+ * policy references
+ * @return the equivalent of merge of self and argument
+ * @throws UnsupportedOperationException if the merge is not meaningful
+ */
+ public Assertion merge(Assertion assertion, PolicyRegistry reg) throws UnsupportedOperationException;
+
+ /**
+ * Returns ture if the assertion has a parent
+ *
+ * @return true if a parent exists , false otherwise
+ */
+ public boolean hasParent();
+
+ /**
+ * Returns the parent of self or null if a parent non-exists
+ *
+ * @return the parent of self
+ */
+ public Assertion getParent();
+
+ /**
+ * Set the parent to argument
+ * @param parent the parent that should be parent of self
+ */
+ public void setParent(Assertion parent);
+}
Added: webservices/commons/policy/src/org/apache/policy/model/CompositeAssertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/model/CompositeAssertion.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/model/CompositeAssertion.java (added)
+++ webservices/commons/policy/src/org/apache/policy/model/CompositeAssertion.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,133 @@
+/*
+ * 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.model;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.policy.util.PolicyRegistry;
+
+/**
+ * CompositeAssertion abstract class implements few method which are common to
+ * composite assertions. A composite assertion as some terms (if any) and
+ * implicit logic that whether all (or any) of its terms should be statisfied.
+ */
+public abstract class CompositeAssertion implements Assertion {
+
+ /** */
+ protected boolean flag = true;
+
+ /** */
+ private List list = new ArrayList();
+
+ /** */
+ private Assertion parent = null;
+
+ /**
+ * Adds an assertion as one of its terms
+ *
+ * @param assertion the assertion that should be added as its term
+ */
+ public void addTerm(Assertion assertion){
+ assertion.setParent(this);
+ list.add(assertion);
+ }
+
+ /**
+ * Adds set of assertions as its terms
+ *
+ * @param assertions the set of assertions that should be added as its
+ * terms
+ */
+ public void addTerms(List assertions) {
+ Iterator items = assertions.iterator();
+
+ while (items.hasNext()) {
+ Object value = items.next();
+
+ if (!(value instanceof Assertion)) {
+ throw new IllegalArgumentException("argument contains a " +
+ "non-assertion");
+ }
+ addTerm((Assertion) value);
+ }
+ }
+
+ public List getTerms() {
+ return list;
+ }
+
+ /**
+ * Returns true if no terms exist or false otherwise
+ * @return true if no terms exist or false otherwise
+ */
+ public boolean isEmpty() {
+ return list.size() == 0;
+ }
+
+ public boolean remove(Assertion assertion) {
+ return list.remove(assertion);
+ }
+
+ public int size() {
+ return list.size();
+ }
+
+ public boolean hasParent() {
+ return parent != null;
+ }
+
+ public Assertion getParent() {
+ return parent;
+ }
+
+ public void setParent(Assertion parent) {
+ this.parent = parent;
+ }
+
+ protected Assertion normalize() {
+ return normalize(null);
+ }
+
+ abstract protected Assertion normalize(PolicyRegistry reg);
+
+ public Assertion intersect(Assertion assertion)
+ throws UnsupportedOperationException {
+ return intersect(assertion, null);
+ }
+
+ public Assertion merge(Assertion assertion)
+ throws UnsupportedOperationException {
+ return merge(assertion, null);
+ }
+
+ protected boolean isNormalized() {
+ return flag;
+ }
+
+ protected void setNormalize(boolean value) {
+ Iterator children = getTerms().iterator();
+
+ while (children.hasNext()) {
+ Object child = children.next();
+ if (child instanceof CompositeAssertion) {
+ ((CompositeAssertion) child).setNormalize(true);
+ }
+ }
+ flag = value;
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/model/Policy.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/model/Policy.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/model/Policy.java (added)
+++ webservices/commons/policy/src/org/apache/policy/model/Policy.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,249 @@
+/*
+ * 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.model;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.policy.util.PolicyRegistry;
+
+/**
+ * Policy is the access point for policy framework. It the object model that
+ * represents a policy at runtime.
+ *
+ */
+public class Policy extends AndCompositeAssertion implements Assertion {
+ private String policyURI = null;
+ private String xmlBase = null;
+ private String id = null;
+
+ public Policy() {
+ setNormalize(false);
+ }
+
+ public Policy(String id) {
+ this(null, id);
+ setNormalize(false);
+ }
+
+ public Policy(String xmlBase, String id) {
+ this.xmlBase = xmlBase;
+ this.id = id;
+ setNormalize(false);
+ }
+
+ public void setBase(String xmlBase) {
+ this.xmlBase = xmlBase;
+ }
+
+ public String getBase() {
+ return xmlBase;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getPolicyURI() {
+ return (xmlBase != null) ? xmlBase + "#" + id : "#" + id;
+ }
+
+ public Assertion normalize() {
+ return normalize(null);
+ }
+
+ public Assertion normalize(PolicyRegistry reg) {
+ if (getParent() == null) {
+
+ String xmlBase = getBase();
+ String id = getId();
+ Policy result = new Policy(xmlBase, id);
+
+ AndCompositeAssertion resultantAndTerm = new AndCompositeAssertion();
+ XorCompositeAssertion resultantXorTerm = new XorCompositeAssertion();
+
+ ArrayList childAndTermList = new ArrayList();
+ ArrayList childXorTermList = new ArrayList();
+
+ Iterator myTerms = getTerms().iterator();
+
+ while (myTerms.hasNext()) {
+ Object term = myTerms.next();
+
+ if (term instanceof PrimitiveAssertion) {
+ resultantAndTerm.addTerm((Assertion) term);
+
+ } else if (term instanceof PolicyReference) {
+
+ if (reg == null) {
+ throw new RuntimeException("PolicyCache is not defined");
+ }
+
+ PolicyReference policyRef = (PolicyReference) term;
+ Policy policy = reg.lookup(policyRef.getPolicyURIString());
+
+ if (policy == null) {
+ throw new RuntimeException("PolicyReference<" + policyRef.getPolicyURIString() + "can not be resolved");
+ }
+
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerms(policy.getTerms());
+ Assertion normalizedPolicyRef = andTerm.normalize(reg);
+
+ if (normalizedPolicyRef instanceof AndCompositeAssertion) {
+ childAndTermList.add(normalizedPolicyRef);
+ } else {
+ childXorTermList.add(normalizedPolicyRef);
+ }
+
+ } else if (term instanceof CompositeAssertion) {
+ CompositeAssertion cterm = (CompositeAssertion) term;
+
+ cterm =((cterm.isNormalized()) ? cterm :(CompositeAssertion) cterm.normalize(reg));
+
+ if (cterm instanceof AndCompositeAssertion) {
+ childAndTermList.add(cterm);
+ } else {
+ childXorTermList.add(cterm);
+ }
+ }
+ }
+
+ // processing child-AndCompositeAssertion
+ if (! childAndTermList.isEmpty()) {
+ Iterator andTerms = childAndTermList.iterator();
+
+ while (andTerms.hasNext()) {
+ CompositeAssertion andTerm = (CompositeAssertion) andTerms.next();
+ resultantAndTerm.addTerms(andTerm.getTerms());
+ }
+ }
+
+ // processing child-XORCompositeAssertions
+ if (childXorTermList.size() > 1) {
+
+ outer : for (int i = 0; i < childXorTermList.size(); i++) {
+ inner : for (int j = i; j < childXorTermList.size(); j++) {
+ if (i != j) {
+ XorCompositeAssertion xorTermA = (XorCompositeAssertion) childXorTermList.get(i);
+ XorCompositeAssertion xorTermB = (XorCompositeAssertion) childXorTermList.get(j);
+
+ // what if XORtermA or XORtermB is empty?
+ if (xorTermA.isEmpty() || xorTermB.isEmpty()) {
+ resultantXorTerm = new XorCompositeAssertion();
+ break outer;
+ }
+ Iterator iterA = xorTermA.getTerms().iterator();
+
+ while (iterA.hasNext()) {
+ // must be an ANDterm
+ CompositeAssertion andTermA = (CompositeAssertion) iterA.next();
+ Iterator iterB = xorTermB.getTerms().iterator();
+ while (iterB.hasNext()) {
+ // must be an ANDterm
+ CompositeAssertion andTermB = (CompositeAssertion) iterB.next();
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerms(andTermA.getTerms());
+ andTerm.addTerms(andTermB.getTerms());
+ resultantXorTerm.addTerm(andTerm);
+ }
+ }
+
+ }
+ }
+ }
+
+ } else if (childXorTermList.size() == 1) {
+ CompositeAssertion xorTerm = (CompositeAssertion) childXorTermList.get(0);
+ resultantXorTerm.addTerms(xorTerm.getTerms());
+ }
+
+ if (childXorTermList.isEmpty()) {
+ XorCompositeAssertion alters = new XorCompositeAssertion();
+ alters.addTerm(resultantAndTerm);
+ result.addTerm(alters);
+ result.setNormalize(true);
+ return result;
+ }
+
+ if (resultantXorTerm.isEmpty()) {
+ result.addTerm(resultantXorTerm);
+ result.setNormalize(true);
+ return result;
+ }
+
+ // get list of primitive assertions form result (AndCompositeAssertion)
+ List primTerms = resultantAndTerm.getTerms();
+
+ // these terms should be AndCompositeAssertions
+ Iterator andTerms = resultantXorTerm.getTerms().iterator();
+
+ while (andTerms.hasNext()) {
+ CompositeAssertion andTerm = (CompositeAssertion) andTerms.next();
+ andTerm.addTerms(primTerms);
+ }
+ result.addTerm(resultantXorTerm);
+ result.setNormalize(true);
+ return result;
+
+ } else {
+ return super.normalize();
+ }
+ }
+
+ public Assertion intersect(Assertion assertion , PolicyRegistry reg) {
+
+ Policy result = new Policy(getBase(), getId());
+ Policy normalizedMe = (Policy) ((isNormalized()) ? this : normalize(reg));
+
+ XorCompositeAssertion alters = (XorCompositeAssertion) normalizedMe.getTerms().get(0);
+
+ if (assertion instanceof PrimitiveAssertion) {
+ result.addTerm(alters.intersect(assertion, reg));
+ return result;
+
+ } else {
+ CompositeAssertion target = (CompositeAssertion) assertion;
+ target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
+
+ if (target instanceof Policy) {
+ XorCompositeAssertion alters2 = (XorCompositeAssertion) target.getTerms().get(0);
+ result.addTerm(alters.intersect(alters2));
+ return result;
+ } else {
+ result.addTerm(alters.intersect(target));
+ return result;
+ }
+ }
+ }
+
+ public Assertion merge(Assertion assertion, PolicyRegistry reg) {
+ Policy result = new Policy(getBase(), getId());
+ Policy normalizedMe = (Policy) ((isNormalized()) ? this : normalize(reg));
+ XorCompositeAssertion alters = (XorCompositeAssertion) normalizedMe.getTerms().get(0);
+ Assertion test = alters.merge(assertion, reg);
+
+ result.addTerm(test);
+ result.setNormalize(true);
+ return result;
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/model/PolicyReference.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/model/PolicyReference.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/model/PolicyReference.java (added)
+++ webservices/commons/policy/src/org/apache/policy/model/PolicyReference.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,70 @@
+/*
+ * 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.model;
+
+import org.apache.policy.util.PolicyRegistry;
+
+/**
+ * PolicyReference class has implicit reference to a external policy. It acts
+ * as wrapper to external policies in the standard policy framework.
+ *
+ */
+public class PolicyReference implements Assertion {
+
+ private String PolicyURIString = null;
+ private Assertion parent = null;
+
+ public PolicyReference(String policyURIString) {
+ this.PolicyURIString = policyURIString;
+ }
+
+ public String getPolicyURIString() {
+ return PolicyURIString;
+ }
+
+ public Assertion intersect(Assertion assertion) {
+ throw new UnsupportedOperationException("intersect is not defined for "
+ + "PolicyReference");
+ }
+
+ public Assertion intersect(Assertion assertion, PolicyRegistry reg)
+ throws UnsupportedOperationException {
+ throw new UnsupportedOperationException();
+ }
+
+ public Assertion merge(Assertion assertion, PolicyRegistry reg)
+ throws UnsupportedOperationException {
+ throw new UnsupportedOperationException();
+ }
+
+ public Assertion merge(Assertion assertion) {
+ throw new UnsupportedOperationException("merge is not supported for " +
+ "PolicyReference");
+ }
+
+ public boolean hasParent() {
+ return parent != null;
+ }
+
+ public Assertion getParent() {
+ return parent;
+ }
+
+ public void setParent(Assertion parent) {
+ this.parent = parent;
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/model/PrimitiveAssertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/model/PrimitiveAssertion.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/model/PrimitiveAssertion.java (added)
+++ webservices/commons/policy/src/org/apache/policy/model/PrimitiveAssertion.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,96 @@
+/*
+ * 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.model;
+
+import javax.xml.namespace.QName;
+
+import org.apache.policy.util.PolicyRegistry;
+
+/**
+ * PrimitiveAssertion wraps an assertion which is indivisible. Such assertion
+ * require domain specific knowledge for further processing. Hence this class
+ * seperates that domain specific knowledge from generic framework.
+ *
+ */
+public class PrimitiveAssertion implements Assertion {
+ private Assertion owner = null;
+ private QName qname;
+ private Object value;
+
+ public PrimitiveAssertion(QName qname, Object value) {
+ this.qname = qname;
+ this.value = value;
+ }
+
+ public QName getName() {
+ return qname;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ public Assertion intersect(Assertion assertion, PolicyRegistry reg) {
+ if (assertion instanceof CompositeAssertion) {
+ return assertion.intersect(this, reg);
+
+ } else { // both are primitives
+ PrimitiveAssertion target = (PrimitiveAssertion) assertion;
+
+ if(this.getName().equals(target.getName())) {
+ AndCompositeAssertion resultAnd = new AndCompositeAssertion();
+ resultAnd.addTerm(this);
+ resultAnd.addTerm(target);
+ return resultAnd;
+ }
+ return new XorCompositeAssertion();
+ }
+ }
+
+ public Assertion intersect(Assertion assertion)
+ throws UnsupportedOperationException {
+ return intersect(assertion, null);
+ }
+
+ public Assertion merge(Assertion assertion, PolicyRegistry reg) {
+
+ AndCompositeAssertion resultAnd = new AndCompositeAssertion();
+ resultAnd.addTerm(this);
+ resultAnd.addTerm(assertion);
+ return resultAnd;
+ }
+
+ public Assertion merge(Assertion assertion) {
+ return merge(assertion, null);
+ }
+
+// public Assertion normalize() {
+// throw new UnsupportedOperationException("normalize is not supported " +
+// "in primitive assertions");
+// }
+
+ public boolean hasParent() {
+ return owner != null;
+ }
+
+ public Assertion getParent() {
+ return owner;
+ }
+
+ public void setParent(Assertion parent) {
+ this.owner = parent;
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/model/XorCompositeAssertion.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/model/XorCompositeAssertion.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/model/XorCompositeAssertion.java (added)
+++ webservices/commons/policy/src/org/apache/policy/model/XorCompositeAssertion.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,214 @@
+/*
+ * 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.model;
+
+import java.util.Iterator;
+
+import org.apache.policy.util.PolicyRegistry;
+
+/**
+ * XORCompositeAssertion represents a bunch of policy alternatives. It requires
+ * that exactly one of its terms (policy alternative) is statisfied.
+ *
+ */
+public class XorCompositeAssertion extends CompositeAssertion implements Assertion {
+
+ public XorCompositeAssertion() {
+ }
+
+ public void addTerm(Assertion assertion) {
+ if(!(isNormalized() && (assertion instanceof AndCompositeAssertion)
+ && ((AndCompositeAssertion) assertion).isNormalized())) {
+ setNormalize(false);
+ }
+ super.addTerm(assertion);
+ }
+
+ public Assertion normalize(PolicyRegistry reg) {
+ XorCompositeAssertion xorLogic = new XorCompositeAssertion();
+ Iterator terms = getTerms().iterator();
+
+ while (terms.hasNext()) {
+ Assertion term = (Assertion) terms.next();
+
+ if (term instanceof PrimitiveAssertion) { // just wrap it in an AND
+ // logic and add
+ AndCompositeAssertion wrapper = new AndCompositeAssertion();
+ wrapper.addTerm(term);
+ xorLogic.addTerm(wrapper);
+ } else if (term instanceof PolicyReference) {
+ if (reg == null) {
+ throw new RuntimeException("PolicyCache is not defined");
+ }
+
+ PolicyReference policyRef = (PolicyReference) term;
+ Policy policy = reg.lookup(policyRef.getPolicyURIString());
+ if (policy == null) {
+ throw new RuntimeException("PolicyReference<" + policyRef.getPolicyURIString() +"> cannot be resolved");
+ }
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerms(policy.getTerms());
+ Assertion normalizedPolicy = andTerm.normalize(reg);
+ if (normalizedPolicy instanceof XorCompositeAssertion) {
+ xorLogic.addTerms(((XorCompositeAssertion) normalizedPolicy).getTerms());
+ } else {
+ xorLogic.addTerm(normalizedPolicy);
+ }
+
+ } else {
+ // must be a composite assertion
+ CompositeAssertion cterm = (CompositeAssertion) term;
+ cterm =((cterm.isNormalized()) ? cterm :(CompositeAssertion) cterm.normalize(reg));
+
+ if (cterm instanceof XorCompositeAssertion) {
+ // just adds the child-terms to super
+ xorLogic.addTerms(cterm.getTerms());
+ } else {
+ // must be an AndCompositeAssertion with primitives
+ xorLogic.addTerm(cterm);
+ }
+ }
+ }
+ xorLogic.setNormalize(true);
+ return xorLogic;
+ }
+
+ public Assertion intersect(Assertion assertion, PolicyRegistry reg) {
+ CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this : normalize(reg));
+
+ if (!(normalizedMe instanceof XorCompositeAssertion)) {
+ return normalizedMe.intersect(assertion, reg);
+ }
+
+ XorCompositeAssertion result = new XorCompositeAssertion();
+
+ if (assertion instanceof PrimitiveAssertion) {
+
+ Iterator iterator = normalizedMe.getTerms().iterator();
+
+ while (iterator.hasNext()) {
+ AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator.next();
+ Assertion value = andTerm.intersect(assertion);
+ if (value instanceof AndCompositeAssertion) {
+ result.addTerm(value);
+ }
+ }
+
+ } else {
+ CompositeAssertion target = (CompositeAssertion) assertion;
+ target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
+
+ Iterator iterator = normalizedMe.getTerms().iterator();
+ while (iterator.hasNext()) {
+ AndCompositeAssertion andTerm = (AndCompositeAssertion) iterator.next();
+
+ if (target instanceof AndCompositeAssertion) {
+ Assertion value = andTerm.intersect(target);
+
+ if (value instanceof AndCompositeAssertion) {
+ result.addTerm(value);
+ }
+
+ } else if (target instanceof XorCompositeAssertion) {
+
+ Iterator andTerms = target.getTerms().iterator();
+
+ while (andTerms.hasNext()) {
+ AndCompositeAssertion tAndTerm = (AndCompositeAssertion) andTerms.next();
+ Assertion value = andTerm.intersect(tAndTerm);
+
+ if (value instanceof AndCompositeAssertion) {
+ result.addTerm(value);
+ }
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ public Assertion merge(Assertion assertion, PolicyRegistry reg) {
+ CompositeAssertion normalizedMe = (CompositeAssertion) ((isNormalized()) ? this : normalize(reg));
+
+ if (!(normalizedMe instanceof XorCompositeAssertion)) {
+ return normalizedMe.merge(assertion, reg);
+ }
+
+ if (assertion instanceof PrimitiveAssertion) {
+ XorCompositeAssertion xorTerm = new XorCompositeAssertion();
+
+ Iterator iterator = normalizedMe.getTerms().iterator();
+ if (iterator.hasNext()) {
+ do {
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerm(assertion);
+ AndCompositeAssertion anAndTerm = (AndCompositeAssertion) iterator.next();
+ andTerm.addTerms(anAndTerm.getTerms());
+ xorTerm.addTerm(andTerm);
+ } while (iterator.hasNext());
+ } else {
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerm(assertion);
+ xorTerm.addTerm(andTerm);
+ }
+ xorTerm.setNormalize(true);
+ return xorTerm;
+ }
+
+ CompositeAssertion target = (CompositeAssertion) assertion;
+ target = (CompositeAssertion) ((target.isNormalized()) ? target : target.normalize(reg));
+
+ if (target instanceof Policy) {
+ XorCompositeAssertion xorTerm = (XorCompositeAssertion) target.getTerms().get(0);
+ return normalizedMe.merge(xorTerm);
+
+ } else if (target instanceof XorCompositeAssertion) {
+ XorCompositeAssertion xorTerm = new XorCompositeAssertion();
+ Iterator hisAndTerms = target.getTerms().iterator();
+ Iterator myAndTerms = normalizedMe.getTerms().iterator();
+
+ while (myAndTerms.hasNext()) {
+ AndCompositeAssertion myAndTerm = (AndCompositeAssertion) myAndTerms.next();
+ while (hisAndTerms.hasNext()) {
+ AndCompositeAssertion hisAndTerm = (AndCompositeAssertion) hisAndTerms.next();
+ xorTerm.addTerm(myAndTerm.merge(hisAndTerm));
+ }
+ }
+
+ xorTerm.setNormalize(true);
+ return xorTerm;
+
+ } else if (target instanceof AndCompositeAssertion) {
+ XorCompositeAssertion xorTerm = new XorCompositeAssertion();
+ Iterator myAndTerms = normalizedMe.getTerms().iterator();
+
+ while (myAndTerms.hasNext()) {
+ AndCompositeAssertion andTerm = new AndCompositeAssertion();
+ andTerm.addTerms(target.getTerms());
+ AndCompositeAssertion myAndTerm = (AndCompositeAssertion) myAndTerms.next();
+ andTerm.addTerms(myAndTerm.getTerms());
+ xorTerm.addTerm(andTerm);
+ }
+
+ xorTerm.setNormalize(true);
+ return xorTerm;
+ }
+
+ throw new IllegalArgumentException("error : merge is not defined for" + target.getClass().getName());
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/parser/WSPConstants.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/parser/WSPConstants.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/parser/WSPConstants.java (added)
+++ webservices/commons/policy/src/org/apache/policy/parser/WSPConstants.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,44 @@
+/*
+ * 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.parser;
+
+/**
+ * WSPConstants interfaces defines some CONST VALUES that are used in the
+ * entier framework.
+ *
+ */
+public interface WSPConstants {
+
+ /** */
+ public static final String AND_COMPOSITE_ASSERTION = "All";
+
+ /** */
+ public static final String XOR_COMPOSITE_ASSERTION = "ExactlyOne";
+
+ /** */
+ public static final String WS_POLICY = "Policy";
+
+ /** */
+ public static final String WS_POLICY_REFERENCE = "PolicyReference";
+
+ /** */
+ public static final String WS_POLICY_NAMESPACE_URI = "http://schemas.xmlsoap.org/ws/2004/09/policy";
+
+ /** */
+ public static final String WSU_NAMESPACE_URI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
+
+}
Added: webservices/commons/policy/src/org/apache/policy/parser/WSPolicyParser.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/parser/WSPolicyParser.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/parser/WSPolicyParser.java (added)
+++ webservices/commons/policy/src/org/apache/policy/parser/WSPolicyParser.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,343 @@
+/*
+ * 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.parser;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.StringReader;
+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.OMNamespace;
+import org.apache.axis2.om.OMNode;
+import org.apache.axis2.om.OMText;
+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.CompositeAssertion;
+import org.apache.policy.model.Policy;
+import org.apache.policy.model.PolicyReference;
+import org.apache.policy.model.PrimitiveAssertion;
+import org.apache.policy.model.XorCompositeAssertion;
+import org.apache.policy.util.StringUtils;
+
+/**
+ * WSPolicyParser provides methods to build a Policy Model form an InputStream
+ * and to write a Policy Model to an OutputStream.
+ *
+ */
+public class WSPolicyParser {
+ public static WSPolicyParser self = null;
+
+ private WSPolicyParser() {
+ }
+
+ public static WSPolicyParser getInstance() {
+ if (self == null) {
+ self = new WSPolicyParser();
+ }
+ return self;
+ }
+
+ public Policy buildPolicyModel(InputStream in) {
+
+ Policy model = null;
+
+ try {
+ XMLStreamReader xmlr =
+ XMLInputFactory.newInstance().createXMLStreamReader(in);
+ OMXMLParserWrapper builder =
+ OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), xmlr);
+
+ OMElement root = builder.getDocumentElement();
+
+ model = getPolicy(root);
+
+ } catch (XMLStreamException ex) {
+ throw new RuntimeException("error : " + ex.getMessage());
+ }
+
+ return model;
+ }
+
+ public Policy getPolicy(OMElement value) {
+ if (value.getNamespace().getName().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) ||
+ value.getLocalName().equals(WSPConstants.WS_POLICY)) {
+
+ Policy policy = new Policy();
+ OMAttribute xmlBase = getAttribute(value, new QName("", "base"));
+
+ if (xmlBase != null) {
+ policy.setBase(xmlBase.getAttributeValue());
+ }
+
+ OMAttribute id = getAttribute(value, new QName(WSPConstants.WSU_NAMESPACE_URI, "Id"));
+
+ if (id != null) {
+ policy.setId(id.getAttributeValue());
+ }
+
+ Iterator children = value.getChildren();
+
+ while (children.hasNext()){
+
+ OMNode node = (OMNode) children.next();
+
+ if (node instanceof OMElement){
+ OMElement ome = (OMElement) node;
+
+ if (isCompositeAssertion(ome)) {
+ policy.addTerm(getCompositeAssertion(ome));
+ } else if (isPolicyReference(ome)){
+ policy.addTerm(getPolicyReference(ome));
+ } else {
+ policy.addTerm(getPrimitiveAssertion(ome));
+ }
+
+ }
+ }
+ return policy;
+
+
+ }
+ throw new IllegalArgumentException("Error : input is not a vaild policy element");
+ }
+
+ public Assertion getCompositeAssertion(OMElement value) {
+ CompositeAssertion compositeAssertion = null;
+
+ if (value.getLocalName().equals(WSPConstants.WS_POLICY)) {
+ String policyURI = "{" + value.getNamespace().getName() + "}" + value.getLocalName();
+ compositeAssertion = new Policy(policyURI);
+
+ } else if (value.getLocalName().equals(WSPConstants.AND_COMPOSITE_ASSERTION)) {
+ compositeAssertion = new AndCompositeAssertion();
+
+ } else if (value.getLocalName().equals(WSPConstants.XOR_COMPOSITE_ASSERTION)) {
+ compositeAssertion = new XorCompositeAssertion();
+
+ } else if (value.getLocalName().equals(WSPConstants.WS_POLICY_REFERENCE)) {
+
+ OMAttribute uriAttr = value.getAttribute(new QName("URI"));
+
+ return new PolicyReference(uriAttr.getAttributeValue());
+
+// try {
+// URI policyURI = new URI(uriAttr.getValue());
+// URL policyURL = policyURI.toURL();
+// return buildPolicyModel(policyURL.openStream());
+//
+//
+// } catch (Exception ex) {
+// throw new RuntimeException("error : " + ex.getMessage());
+// }
+
+ } else {
+ throw new IllegalArgumentException("cannot resolve the argument to" +
+ "a composite assertion");
+ }
+
+ Iterator children = value.getChildren();
+
+ while (children.hasNext()){
+ OMNode node = (OMNode) children.next();
+
+ if (node instanceof OMElement){
+ OMElement ome = (OMElement) node;
+
+ if (isCompositeAssertion(ome)) {
+ compositeAssertion.addTerm(getCompositeAssertion(ome));
+ } else if (isPolicyReference(ome)){
+ compositeAssertion.addTerm(getPolicyReference(ome));
+ } else {
+ compositeAssertion.addTerm(getPrimitiveAssertion(ome));
+ }
+
+ }
+ }
+ return compositeAssertion;
+ }
+
+ public PrimitiveAssertion getPrimitiveAssertion(OMElement value) {
+ QName qname = new QName(value.getNamespace().getName(), value.getLocalName());
+ return new PrimitiveAssertion(qname, value);
+ }
+
+ public boolean isCompositeAssertion(OMElement value) {
+
+ return (value.getNamespace().getName().equals(WSPConstants.WS_POLICY_NAMESPACE_URI))
+ && (value.getLocalName().equals(WSPConstants.WS_POLICY)
+ || value.getLocalName().equals(WSPConstants.AND_COMPOSITE_ASSERTION)
+ || value.getLocalName().equals(WSPConstants.XOR_COMPOSITE_ASSERTION));
+ }
+
+ public boolean isPolicyReference(OMElement value) {
+ return ((value.getNamespace().getName().equals(WSPConstants.WS_POLICY_NAMESPACE_URI)))
+ && (value.getLocalName().equals(WSPConstants.WS_POLICY_REFERENCE));
+ }
+
+ public Assertion getPolicyReference(OMElement ome) {
+ OMAttribute attri = getAttribute(ome, new QName("", "URI"));
+ String uriString = attri.getAttributeValue();
+ return new PolicyReference(uriString);
+ }
+
+ public void printModel(Policy model, OutputStream out) {
+ PrintWriter pw = new PrintWriter(out, true);
+ printAssertion(0, model, pw);
+ }
+
+ private void printAssertion(int tab, Assertion assertion, PrintWriter pw) {
+ if (assertion instanceof PrimitiveAssertion) {
+ printPrimitiveAssertion(tab, (PrimitiveAssertion) assertion, pw);
+ } else if (assertion instanceof CompositeAssertion) {
+ printCompositeAssertion(tab, (CompositeAssertion) assertion, pw);
+ }
+ }
+
+ private void printPrimitiveAssertion(int tab, PrimitiveAssertion primitive, PrintWriter pw) {
+ printOMElement(tab, (OMElement) primitive.getValue(), pw);
+ }
+
+ private void printCompositeAssertion(int tab, CompositeAssertion composite, PrintWriter pw) {
+ if (composite instanceof Policy) {
+ pw.print(StringUtils.getChars(tab, ' ') + "<wsp:Policy xmlns:wsp=\""
+ + WSPConstants.WS_POLICY_NAMESPACE_URI + "\"");
+
+ Iterator iterator = composite.getTerms().iterator();
+ if (iterator.hasNext()) {
+ pw.println(">");
+ do {
+ Assertion child = (Assertion) iterator.next();
+ printAssertion(tab + 4, child, pw);
+
+ } while (iterator.hasNext());
+
+ pw.println(StringUtils.getChars(tab, ' ') + "</wsp:Policy>");
+
+ } else {
+ pw.println("/>");
+ }
+
+ } else if (composite instanceof AndCompositeAssertion) {
+ pw.print(StringUtils.getChars(tab, ' ') + "<wsp:All");
+
+ Iterator iterator = composite.getTerms().iterator();
+ if (iterator.hasNext()) {
+ pw.println(">");
+ do {
+ Assertion child = (Assertion) iterator.next();
+ printAssertion(tab + 4, child, pw);
+
+ } while (iterator.hasNext());
+
+ pw.println(StringUtils.getChars(tab, ' ') + "</wsp:All>");
+
+ } else {
+ pw.println("/>");
+ }
+
+ } else if (composite instanceof XorCompositeAssertion) {
+ pw.print(StringUtils.getChars(tab, ' ') + "<wsp:ExactlyOne");
+
+ Iterator iterator = composite.getTerms().iterator();
+ if (iterator.hasNext()) {
+ pw.println(">");
+ do {
+ Assertion child = (Assertion) iterator.next();
+ printAssertion(tab + 4, child, pw);
+
+ } while (iterator.hasNext());
+
+ pw.println(StringUtils.getChars(tab, ' ') + "</wsp:ExactlyOne>");
+
+ } else {
+ pw.println("/>");
+ }
+
+ }
+ }
+
+ private void printOMElement(int tab, OMElement element, PrintWriter pw) {
+ OMNamespace ns = element.getNamespace();
+
+ pw.print(StringUtils.getChars(tab, ' '));
+ pw.print("<" + ns.getPrefix() + ":" + element.getLocalName() + " xmlns:"
+ + ns.getPrefix() + "=\""+ ns.getName() + "\"");
+
+ Iterator attributes = element.getAllAttributes();
+ if (attributes != null) {
+
+ while (attributes.hasNext()) {
+ OMAttribute attribute = (OMAttribute) attributes.next();
+ pw.print(" "+ attribute.getLocalName() + "=\""
+ + attribute.getAttributeValue() + "\"");
+ }
+ }
+
+ Iterator children = element.getChildren();
+
+ if (children.hasNext()) {
+ pw.println(">");
+
+ do {
+ Object child = children.next();
+
+ if (child instanceof OMElement) {
+ printOMElement(tab + 4, (OMElement) child, pw);
+
+ } else if (child instanceof OMText) {
+ String strValue = ((OMText) child).getText().trim();
+
+ if (strValue.length() != 0) {
+ pw.println(StringUtils.getChars(tab + 4, ' ')
+ + strValue);
+ }
+
+ }
+
+ } while (children.hasNext());
+
+ pw.print(StringUtils.getChars(tab, ' '));
+ pw.println("<" + ns.getPrefix() + ":" + element.getLocalName() + "/>");
+
+ } else {
+ pw.println("/>");
+ }
+ }
+
+ private OMAttribute getAttribute(OMElement target, QName qname) {
+ Iterator iterator = target.getAllAttributes();
+ while (iterator.hasNext()) {
+ OMAttribute attr = (OMAttribute) iterator.next();
+ if (qname.equals(attr.getQName())) {
+ return attr;
+ }
+ }
+ return null;
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/util/PolicyRegistry.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/util/PolicyRegistry.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/util/PolicyRegistry.java (added)
+++ webservices/commons/policy/src/org/apache/policy/util/PolicyRegistry.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,71 @@
+/*
+ * 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.util.HashMap;
+import java.util.Iterator;
+
+import org.apache.policy.model.Policy;
+
+/**
+ * PolicyRegistry is contains reference to external policy that are used by the
+ * policy model to resolve policy reference objects to their actual policies.
+ *
+ */
+public class PolicyRegistry {
+ HashMap reg = new HashMap ();
+ PolicyRegistry parent = null;
+
+ public PolicyRegistry () {
+ }
+
+ public PolicyRegistry (PolicyRegistry parent) {
+ this.parent = parent;
+ }
+
+ public Policy lookup (String policyURI) throws IllegalArgumentException {
+
+ Policy policy = (Policy) reg.get (policyURI);
+
+ if (policy == null && parent != null) {
+ policy = parent.lookup (policyURI);
+ }
+
+// if (policy == null) {
+// throw new IllegalArgumentException ("policy '" + policyURI + "' not in registry");
+// }
+
+ return policy;
+ }
+ // register a policy
+ public void register (String policyURI, Policy policy) {
+ reg.put (policyURI, policy);
+ }
+
+ // unregister a policy
+ public void unregister (String policyURI) {
+ reg.remove (policyURI);
+ }
+
+ public Iterator keys() {
+ return reg.keySet().iterator();
+ }
+
+ public Iterator values() {
+ return reg.values().iterator();
+ }
+}
Added: webservices/commons/policy/src/org/apache/policy/util/StringUtils.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/util/StringUtils.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/util/StringUtils.java (added)
+++ webservices/commons/policy/src/org/apache/policy/util/StringUtils.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+/**
+ * StringUtils provides few utility functions for other classes in the
+ * framework.
+ */
+public class StringUtils {
+
+ public static String getChars(int noOfChars, char theChar) {
+ if (noOfChars <= 0) {
+ return "";
+ }
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < noOfChars; i++) {
+ buf.append(theChar);
+ }
+ return buf.toString();
+ }
+
+}
Added: webservices/commons/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java
URL: http://svn.apache.org/viewcvs/webservices/commons/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java?rev=331547&view=auto
==============================================================================
--- webservices/commons/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java (added)
+++ webservices/commons/policy/src/org/apache/policy/util/WSPolicyAttachmentUtil.java Mon Nov 7 08:49:40 2005
@@ -0,0 +1,588 @@
+/*
+ * 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.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.wsdl.WSDLException;
+import javax.xml.namespace.QName;
+
+import org.apache.axis2.wsdl.WSDLConstants;
+import org.apache.axis2.wsdl.WSDLVersionWrapper;
+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.wsdl.Component;
+import org.apache.wsdl.MessageReference;
+import org.apache.wsdl.WSDLBinding;
+import org.apache.wsdl.WSDLBindingMessageReference;
+import org.apache.wsdl.WSDLBindingOperation;
+import org.apache.wsdl.WSDLDescription;
+import org.apache.wsdl.WSDLEndpoint;
+import org.apache.wsdl.WSDLExtensibilityAttribute;
+import org.apache.wsdl.WSDLInterface;
+import org.apache.wsdl.WSDLOperation;
+import org.apache.wsdl.WSDLService;
+import org.apache.wsdl.extensions.DefaultExtensibilityElement;
+import org.w3c.dom.Element;
+
+import com.ibm.wsdl.util.xml.DOM2Writer;
+
+/**
+ * This util class which implements WSPolicyAttachment sepcification (September
+ * 2004).
+ *
+ * @author Sanka Samaranayake <ss...@gmail.com>
+ *
+ */
+public class WSPolicyAttachmentUtil {
+
+ private WSDLDescription wsdlDescription = null;
+ //private HashMap loadedPolicies = new HashMap();
+ private PolicyRegistry reg = new PolicyRegistry();
+ private WSPolicyParser parser = WSPolicyParser.getInstance();
+
+
+ public WSPolicyAttachmentUtil() {
+ }
+
+ public WSPolicyAttachmentUtil(WSDLDescription wsdlDescription) {
+ this.wsdlDescription = wsdlDescription;
+ populatePolicyRegistry();
+ }
+
+ public WSPolicyAttachmentUtil(InputStream wsdlInputStream) {
+ try {
+ WSDLVersionWrapper build = WOMBuilderFactory.
+ getBuilder(WSDLConstants.WSDL_1_1).build(wsdlInputStream);
+ wsdlDescription = build.getDescription();
+ populatePolicyRegistry();
+
+ } catch (WSDLException e) {
+ throw new IllegalArgumentException("error : "+ e.getMessage());
+ }
+ }
+
+ public void setWSDLDescription(WSDLDescription wsdlDescription) {
+ this.wsdlDescription = wsdlDescription;
+ reg = new PolicyRegistry();
+ populatePolicyRegistry();
+ }
+
+ public WSDLDescription getWSDLDescription() {
+ if (wsdlDescription != null) {
+ return wsdlDescription;
+ }
+ throw new IllegalStateException("ERROR: A WSDLDescription is not set");
+
+ }
+
+ public Policy getPolicyForService(QName serviceName) {
+ return getServicePolicy(serviceName);
+ }
+
+ public Policy getPolicyForEndPoint(QName epName) {
+ Policy servicePolicy = null;
+ Policy endPointPolicy = null;
+
+ Iterator iterator = wsdlDescription.getServices().values().iterator();
+
+ while (iterator.hasNext()) {
+ WSDLService service = (WSDLService) iterator.next();
+ if (service.getEndpoints().containsKey(epName)) {
+ servicePolicy = getPolicyForService(service.getName());
+ break;
+ }
+ }
+
+ endPointPolicy = getEndPointPolicy(epName);
+
+ return (servicePolicy != null)
+ ? (Policy) servicePolicy.merge(endPointPolicy)
+ : endPointPolicy;
+ }
+
+ public Policy getPolicyForOperation(QName endPoint, QName operation) {
+ Policy endPointPolicy = getPolicyForEndPoint(endPoint),
+ operationPolicy = getOperationPolicy(endPoint, operation);
+ return (endPointPolicy != null)
+ ? (Policy) endPointPolicy.merge(operationPolicy)
+ : operationPolicy;
+ }
+
+ public Policy getPolicyForInputMessage(QName endPoint, QName operation) {
+ Policy operationPolicy = getPolicyForOperation(endPoint, operation),
+ inputMsgPolicy = getInputMeassagePolicy(endPoint, operation);
+ return (operationPolicy != null)
+ ? (Policy) operationPolicy.merge(inputMsgPolicy)
+ : inputMsgPolicy;
+ }
+
+ public Policy getPolicyForOutputMessage(QName endPoint, QName operation) {
+ Policy operationPolicy = getPolicyForOperation(endPoint, operation),
+ outputMsgPolicy = getOutputMeassagePolicy(endPoint, operation);
+ return (operationPolicy != null)
+ ? (Policy) operationPolicy.merge(outputMsgPolicy)
+ : outputMsgPolicy;
+
+ }
+
+ public Policy getServicePolicy(QName serName) {
+ WSDLService service = getWSDLDescription().getService(serName);
+ return (service == null) ? null :(Policy) getComponentPolicy(service).normalize(reg);
+ }
+
+ public Policy getEndPointPolicy(QName epName) {
+ WSDLEndpoint endpoint = getEndpoint(epName);
+ if (endpoint == null) {
+ return null;
+ }
+
+ ArrayList policies = new ArrayList();
+
+ // wsdl:port
+ Assertion epPolicy = getComponentPolicy(endpoint);
+ if (epPolicy != null) {
+ policies.add(getComponentPolicy(endpoint));
+ }
+
+ //wsdl:binding
+ WSDLBinding wsdlBinding = endpoint.getBinding();
+ Assertion wsdlBindingPolicy = getComponentPolicy(wsdlBinding);
+ if (wsdlBindingPolicy != null) {
+ policies.add(getComponentPolicy(wsdlBinding));
+ }
+
+ //wsdl:portType
+ WSDLInterface wsdlInterface = wsdlBinding.getBoundInterface();
+ Assertion portTypePolicy = getComponentPolicy(wsdlInterface);
+ if (portTypePolicy != null) {
+ policies.add(getComponentPolicy(wsdlInterface));
+ }
+
+ return getEffectivePolicy(policies);
+ }
+
+ public Policy getOperationPolicy(QName endPointName, QName opName) {
+ WSDLEndpoint endPoint = getEndpoint(endPointName);
+
+ ArrayList list = new ArrayList();
+
+ //wsdl:binding/wsdl:operation
+ WSDLBinding binding = endPoint.getBinding();
+ WSDLBindingOperation bindingOperation = (WSDLBindingOperation) binding.getBindingOperations().get(opName);
+
+ Assertion bindingPolicy = getComponentPolicy(bindingOperation);
+ if (bindingPolicy != null) {
+ list.add(bindingPolicy);
+ }
+
+ // wsdl:portType/wsdl:operation
+ WSDLOperation wsdlOperation = bindingOperation.getOperation();
+ Assertion interfacePolicy = getComponentPolicy(wsdlOperation);
+
+ if (interfacePolicy != null) {
+ list.add(interfacePolicy);
+
+ }
+
+ return getEffectivePolicy(list);
+ }
+
+ public Policy getInputMeassagePolicy(QName endPointName, QName opName) {
+ List policies = new ArrayList();
+ WSDLEndpoint endPoint = getEndpoint(endPointName);
+
+ // wsdl:binding/wsdl:operation/wsdl:input
+ WSDLBindingOperation wsdlBindingOperation = endPoint.getBinding().getBindingOperation(opName);
+ WSDLBindingMessageReference bindingInput = wsdlBindingOperation.getInput();
+
+ //List extensibilityAttributes = bindingInput.getExtensibilityAttributes();
+ Policy bindingInputPolicy = getEffectivePolicy(getPoliciesAsExtensibleElements(bindingInput));
+ if (bindingInputPolicy != null) {
+ policies.add(bindingInputPolicy);
+ }
+
+ // wsdl:portType/wsdl:operation/wsdl:input
+ WSDLOperation wsdlOperation = wsdlBindingOperation.getOperation();
+ MessageReference operationInput = wsdlOperation.getInputMessage();
+ Policy operationInputPolicy = getEffectivePolicy(getPoliciesAsExtensibilityAttribute(operationInput));
+ if (operationInputPolicy != null) {
+ policies.add(operationInputPolicy);
+ }
+
+ // wsdl:Message
+ // TODO
+ Policy messageInputPolicy = getEffectivePolicy(getPoliciesAsExtensibleElements(operationInput));
+ if (messageInputPolicy != null) {
+ policies.add(messageInputPolicy);
+ }
+
+ return getEffectivePolicy(policies);
+ }
+
+ public Policy getOutputMeassagePolicy(QName endPointName, QName opName) {
+ List policies = new ArrayList();
+ WSDLEndpoint endPoint = getEndpoint(endPointName);
+
+
+ // wsdl:binding/wsdl:operation/wsdl:output
+ WSDLBindingOperation wsdlBindingOperation = endPoint.getBinding().getBindingOperation(opName);
+ WSDLBindingMessageReference bindingOutput = wsdlBindingOperation.getOutput();
+
+ Policy bindingOutputPolicy = getEffectivePolicy(getPoliciesAsExtensibleElements(bindingOutput));
+ if (bindingOutputPolicy != null) {
+ policies.add(getComponentPolicy(bindingOutput));
+ }
+
+ // wsdl:portType/wsdl:operation/wsdl:output
+ WSDLOperation wsdlOperation = wsdlBindingOperation.getOperation();
+ MessageReference operationOutput = wsdlOperation.getOutputMessage();
+ Policy operationOutputPolicy = getEffectivePolicy(getPoliciesAsExtensibilityAttribute(operationOutput));
+ if (operationOutputPolicy != null) {
+ policies.add(operationOutputPolicy);
+ }
+
+ // wsdl:Message
+ // TODO
+ Policy messageOutputPolicy = getEffectivePolicy(getPoliciesAsExtensibleElements(operationOutput));
+ if (messageOutputPolicy != null) {
+ policies.add(messageOutputPolicy);
+ }
+
+ return getEffectivePolicy(policies);
+ }
+
+ public Policy getFaultMeassagePolicy(QName endPointName, QName opName, QName fault) {
+ throw new UnsupportedOperationException();
+ }
+
+ public List getServiceElementPolicy(WSDLService wsdlService) {
+ return getPoliciesAsExtensibleElements(wsdlService);
+ }
+
+ private Policy getEffectivePolicy(List policies) {
+ Policy result = null;
+
+ if (!policies.isEmpty()) {
+ Iterator iter = policies.iterator();
+ result = (Policy) iter.next();
+ while (iter.hasNext()) {
+ Policy next = (Policy) iter.next();
+ result = (Policy) result.merge(next, reg);
+ }
+ }
+ return result;
+ }
+
+ private WSDLEndpoint getEndpoint(QName epName) {
+ Iterator iterator = wsdlDescription.getServices().values().iterator();
+ while (iterator.hasNext()) {
+ WSDLService service = (WSDLService) iterator.next();
+ if (service.getEndpoints().containsKey(epName)) {
+ return service.getEndpoint(epName);
+ }
+ }
+ return null;
+ }
+
+ private Policy getComponentPolicy(Component component) {
+ List myPolicyList = new ArrayList();
+ List attrPolicyList = getPoliciesAsExtensibilityAttribute(component),
+ elePolicyList = getPoliciesAsExtensibleElements(component);
+
+ myPolicyList.addAll(attrPolicyList);
+ myPolicyList.addAll(elePolicyList);
+
+ return getEffectivePolicy(myPolicyList);
+ }
+
+ private List getPoliciesAsExtensibilityAttribute(Component component) {
+ Iterator iterator;
+ List policyURIStrings = new ArrayList();
+ List policies = new ArrayList();
+ iterator = component.getExtensibilityAttributes().iterator();
+
+ while (iterator.hasNext()) {
+ WSDLExtensibilityAttribute exAttribute = (WSDLExtensibilityAttribute) iterator.next();
+ QName qname = exAttribute.getKey();
+
+ if (qname.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) &&
+ qname.getLocalPart().equals("PolicyURIs")) {
+ String value = exAttribute.getValue().toString();
+ String[] uriStrings = value.split(" ");
+
+ for (int i = 0; i < uriStrings.length; i++) {
+ policyURIStrings.add(uriStrings[i].trim());
+ }
+ }
+ }
+ if (!policyURIStrings.isEmpty()) {
+ iterator = policyURIStrings.iterator();
+
+ do {
+ String policyURIString = (String) iterator.next();
+ Policy policy = getPolicyFromURI(policyURIString);
+ policies.add(policy);
+ } while (iterator.hasNext());
+ }
+ return policies;
+ }
+
+ private List getPoliciesAsExtensibleElements(Component component) {
+
+ ArrayList policies = new ArrayList();
+ Iterator iterator = component.getExtensibilityElements().iterator();
+
+ while (iterator.hasNext()) {
+ Object extensibilityElement = iterator.next();
+ if (extensibilityElement instanceof DefaultExtensibilityElement) {
+ DefaultExtensibilityElement defaultExtensibilityElement = (DefaultExtensibilityElement) extensibilityElement;
+ Element element = defaultExtensibilityElement.getElement();
+
+ if (element.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) && element.getLocalName().equals("PolicyReference")) {
+ policies.add(getPolicyAsPolicyRef(element));
+
+ } else if (element.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) && element.getLocalName().equals("Policy")) {
+ policies.add(getPolicyAsElement(element));
+ }
+ }
+// WSDLExtensibilityElement exElement = (WSDLExtensibilityElement) iterator.next();
+// Element element = (Element) exElement.getElement();
+// if (element.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) && element.getLocalName().equals("PolicyReference")) {
+// policyList.add(getPolicyAsPolicyRef(element));
+//
+// } else if (element.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) && element.getLocalName().equals("Policy")) {
+// policyList.add(getPolicyAsElement(element));
+// }
+
+ }
+ return policies;
+ }
+
+ private Policy getPolicyAsPolicyRef(Element element) {
+ String policyURIString = element.getAttribute("URI");
+ if (policyURIString != null && policyURIString.length() != 0) {
+ return getPolicyFromURI(policyURIString);
+ }
+ return null;
+ }
+
+ private Policy getPolicyAsElement(Element element) {
+ InputStream policyInputStream = getInputStream(element);
+ return parser.buildPolicyModel(policyInputStream);
+ }
+
+// private OMElement getElementAsOM(Element element, OMElement parent) {
+// OMFactory factory = OMFactory.newInstance();
+//
+// String namespaceURI = element.getNamespaceURI();
+// String localName = element.getLocalName();
+// QName qname = new QName(namespaceURI, localName);
+//
+// OMElement omElement = factory.createOMElement(qname, parent);
+// element.getChildNodes();
+// return null;
+// }
+
+ private InputStream getInputStream(Element element) {
+
+ StringWriter sw = new StringWriter();
+ DOM2Writer.serializeAsXML(element, sw);
+
+ return new ByteArrayInputStream(sw.toString().getBytes());
+ }
+
+ private Policy getPolicyFromURI(String policyURIString) {
+ return reg.lookup(policyURIString);
+ }
+
+ public String getTargetURI() {
+ return getWSDLDescription().getTargetNameSpace();
+ }
+
+ private void populatePolicyRegistry() {
+ Iterator iterator;
+ WSDLDescription des = getWSDLDescription();
+ List extElements = des.getExtensibilityElements();
+ registerPoliciesAsElements(extElements);
+
+ iterator = des.getWsdlInterfaces().values().iterator();
+ while (iterator.hasNext()) {
+ WSDLInterface interfaze = (WSDLInterface) iterator.next();
+ registerPoliciesInWSDLInterface(interfaze);
+ }
+
+ iterator = des.getBindings().values().iterator();
+ while (iterator.hasNext()) {
+ WSDLBinding wsdlBinding = (WSDLBinding) iterator.next();
+ registerPoliciesInWSDLBinding(wsdlBinding);
+ }
+
+ iterator = des.getServices().values().iterator();
+ while (iterator.hasNext()) {
+ WSDLService service = (WSDLService) iterator.next();
+ registerPoliciesInService(service);
+ }
+
+ iterator = reg.keys();
+ while (iterator.hasNext()) {
+ String uriString = (String) iterator.next();
+ Policy policy = reg.lookup(uriString);
+ if (policy == null) {
+ try {
+ URI policyURI = new URI(uriString);
+ URL policyURL = policyURI.toURL();
+ Policy newPolicy = parser.buildPolicyModel(policyURL.openStream());
+ reg.register(uriString, newPolicy);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ reg.unregister(uriString);
+ iterator = reg.keys();
+ }
+ }
+ }
+ }
+
+ private void registerPoliciesInService(WSDLService service) {
+ List extensibilityElements = service.getExtensibilityElements();
+ registerPoliciesAsElements(extensibilityElements);
+
+ Iterator iterator = service.getEndpoints().values().iterator();
+ while (iterator.hasNext()) {
+ WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) iterator.next();
+ extensibilityElements = wsdlEndpoint.getExtensibilityElements();
+ registerPoliciesAsElements(extensibilityElements);
+ }
+ }
+
+ private void registerPoliciesInWSDLBinding(WSDLBinding wsdlBinding) {
+ List extensibilityElements = wsdlBinding.getExtensibilityElements();
+ registerPoliciesAsElements(extensibilityElements);
+
+ Iterator iterator = wsdlBinding.getBindingOperations().values().iterator();
+ while (iterator.hasNext()) {
+ WSDLBindingOperation wsdlBindingOperation = (WSDLBindingOperation) iterator.next();
+ registerPoliciesInBindOperation(wsdlBindingOperation);
+ }
+ }
+
+ private void registerPoliciesInBindOperation(WSDLBindingOperation wsdlBindingOperation) {
+ List extensibilityElements = wsdlBindingOperation.getExtensibilityElements();
+ registerPoliciesAsElements(extensibilityElements);
+
+ if (wsdlBindingOperation.getInput() != null) {
+ extensibilityElements = wsdlBindingOperation.getInput().getExtensibilityElements();
+ registerPoliciesAsElements(extensibilityElements);
+ }
+ if (wsdlBindingOperation.getOutput() != null) {
+ extensibilityElements = wsdlBindingOperation.getOutput().getExtensibilityElements();
+ registerPoliciesAsElements(extensibilityElements);
+ }
+ }
+
+ private void registerPoliciesInWSDLInterface(WSDLInterface wsdlInterface) {
+ registerPoliciesInElement(wsdlInterface);
+ Iterator iterator = wsdlInterface.getOperations().values().iterator();
+ while (iterator.hasNext()) {
+ WSDLOperation wsdlOperation = (WSDLOperation) iterator.next();
+ registerPoliciesInWSDLOperation(wsdlOperation);
+ }
+ }
+
+ private void registerPoliciesInWSDLOperation(WSDLOperation wsdlOperation){
+ List extensibilityElements = wsdlOperation.getExtensibilityElements();
+ registerPoliciesAsElements(extensibilityElements);
+
+ if (wsdlOperation.getInputMessage() != null) {
+ registerPoliciesInElement(wsdlOperation.getInputMessage());
+ }
+ if (wsdlOperation.getOutputMessage() != null) {
+ registerPoliciesInElement(wsdlOperation.getOutputMessage());
+ }
+ }
+
+ private void registerPoliciesInElement(Component component) {
+ registerPoliciesAsAttribute(component.getExtensibilityAttributes());
+ registerPoliciesAsElements(component.getExtensibilityElements());
+ }
+
+ private void registerPoliciesAsElements(List elements) {
+ Iterator iterator = elements.iterator();
+ while (iterator.hasNext()) {
+ Object extensibilityElement = iterator.next();
+
+ if (extensibilityElement instanceof DefaultExtensibilityElement) {
+ DefaultExtensibilityElement defaultExtensibilityElement = (DefaultExtensibilityElement) extensibilityElement;
+ Element element = defaultExtensibilityElement.getElement();
+
+ if (element.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) && element.getLocalName().equals("PolicyReference")) {
+ String uriString = element.getAttribute("URI");
+
+ if (reg.lookup(uriString) == null) {
+ reg.register(uriString, null);
+ }
+ }
+
+ String policyID = element.getAttributeNS(WSPConstants.WSU_NAMESPACE_URI, "Id");
+
+ if (policyID.length() != 0) {
+ registerPolicyElement(element);
+ }
+ }
+ }
+ }
+
+ private void registerPoliciesAsAttribute(List elements) {
+ Iterator iterator = elements.iterator();
+
+ while (iterator.hasNext()) {
+ WSDLExtensibilityAttribute wsdlExtensibilityAttribute = (WSDLExtensibilityAttribute) iterator.next();
+ QName qname = wsdlExtensibilityAttribute.getKey();
+
+ if (qname.getNamespaceURI().equals(WSPConstants.WS_POLICY_NAMESPACE_URI) &&
+ qname.getLocalPart().equals("PolicyURIs")) {
+ String value = wsdlExtensibilityAttribute.getValue().toString();
+ String[] policyURIs = value.split(" ");
+ for (int i = 0; i < policyURIs.length; i++) {
+ String policyURI = policyURIs[i].trim();
+
+ if (reg.lookup(policyURI) == null) {
+ reg.register(policyURI, null);
+ }
+ }
+ }
+ }
+ }
+
+ private void registerPolicyElement(Element element) {
+ InputStream elementInputStream = getInputStream(element);
+ Policy policy = parser.buildPolicyModel(elementInputStream);
+
+ reg.register(policy.getPolicyURI(), policy);
+ }
+}