View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.neethi;
17  
18  import org.apache.axiom.om.OMAbstractFactory;
19  import org.apache.axiom.om.OMAttribute;
20  import org.apache.axiom.om.OMElement;
21  import org.apache.axiom.om.OMNamespace;
22  import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
23  import org.apache.neethi.builders.AssertionBuilder;
24  import javax.xml.namespace.QName;
25  import javax.xml.stream.XMLInputFactory;
26  import java.io.InputStream;
27  import java.util.Iterator;
28  
29  /**
30   * PolicyEngine provides set of methods to create a Policy object from an
31   * InputStream, OMElement, .. etc. It maintains an instance of
32   * AssertionBuilderFactory that can return AssertionBuilders that can create a
33   * Domain Assertion out of an OMElement. These AssertionBuilders are used when
34   * constructing a Policy object.
35   */
36  public class PolicyEngine {
37  
38      public static final String POLICY_NAMESPACE = "http://schemas.xmlsoap.org/ws/2004/09/policy";
39  
40      public static final String POLICY = "Policy";
41  
42      public static final String EXACTLY_ONE = "ExactlyOne";
43  
44      public static final String ALL = "All";
45  
46      public static final String POLICY_REF = "PolicyReference";
47  
48      private static AssertionBuilderFactory factory = new AssertionBuilderFactory();
49  
50      /**
51       * Registers an AssertionBuilder instances and associates it with a QName.
52       * PolicyManager or other AssertionBuilders instances can use this
53       * AssertionBuilder instance to process and build an Assertion from a
54       * OMElement with the specified QName.
55       * 
56       * @param qname
57       *            the QName of the Assertion that the Builder can build
58       * @param builder
59       *            the AssertionBuilder that can build assertions that of 'qname'
60       *            type
61       */
62      public static void registerBuilder(QName qname, AssertionBuilder builder) {
63          AssertionBuilderFactory.registerBuilder(qname, builder);
64      }
65  
66      /**
67       * Creates a Policy object from an InputStream.
68       * 
69       * @param inputStream
70       *            the InputStream of the Policy
71       * @return a Policy object of the Policy that is fed as a InputStream
72       */
73      public static Policy getPolicy(InputStream inputStream) {
74          try {
75              OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(
76                      OMAbstractFactory.getOMFactory(),
77                      XMLInputFactory.newInstance().createXMLStreamReader(
78                              inputStream)).getDocumentElement();
79              return getPolicy(element);
80  
81          } catch (Exception ex) {
82              ex.printStackTrace();
83          }
84  
85          // TODO throw an IllegalArgumentException
86          return null;
87      }
88  
89      /**
90       * Creates a PolicyReference object.
91       * 
92       * @param inputStream
93       *            the InputStream of the PolicyReference
94       * @return a PolicyReference object of the PolicyReference
95       */
96      public static PolicyReference getPolicyReferene(InputStream inputStream) {
97  
98          try {
99              OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(
100                     OMAbstractFactory.getOMFactory(),
101                     XMLInputFactory.newInstance().createXMLStreamReader(
102                             inputStream)).getDocumentElement();
103             return getPolicyReference(element);
104 
105         } catch (Exception ex) {
106             ex.printStackTrace();
107         }
108 
109         // TODO throw an IllegalArgumentException
110         return null;
111 
112     }
113 
114     /**
115      * Creates a Policy object from an OMElement.
116      * 
117      * @param element
118      *            the Policy element
119      * @retun a Policy object of the Policy element
120      */
121     public static Policy getPolicy(OMElement element) {
122         return getPolicyOperator(element);
123     }
124 
125     /**
126      * Creates a PolicyReference object from an OMElement.
127      * 
128      * @param element
129      *            the PolicyReference element
130      * @return a PolicyReference object of the PolicyReference element
131      */
132     public static PolicyReference getPolicyReference(OMElement element) {
133 
134         if (!(Constants.URI_POLICY_NS.equals(element.getNamespace()
135                 .getNamespaceURI()) && Constants.ELEM_POLICY_REF.equals(element
136                 .getLocalName()))) {
137 
138             throw new RuntimeException(
139                     "Specified element is not a <wsp:PolicyReference .. />  element");
140         }
141 
142         PolicyReference reference = new PolicyReference();
143 
144         // setting the URI value
145         reference.setURI(element.getAttributeValue(new QName("URI")));
146         return reference;
147     }
148 
149     private static Policy getPolicyOperator(OMElement element) {
150         return (Policy) processOperationElement(element, new Policy());
151     }
152 
153     private static ExactlyOne getExactlyOneOperator(OMElement element) {
154         return (ExactlyOne) processOperationElement(element, new ExactlyOne());
155     }
156 
157     private static All getAllOperator(OMElement element) {
158         return (All) processOperationElement(element, new All());
159     }
160 
161     private static PolicyOperator processOperationElement(
162             OMElement operationElement, PolicyOperator operator) {
163 
164         if (Constants.TYPE_POLICY == operator.getType()) {
165             Policy policyOperator = (Policy) operator;
166 
167             OMAttribute attribute;
168             OMNamespace namespace;
169             QName key;
170 
171             for (Iterator iterator = operationElement.getAllAttributes(); iterator
172                     .hasNext();) {
173                 attribute = (OMAttribute) iterator.next();
174                 namespace = attribute.getNamespace();
175 
176                 if (namespace == null) {
177                     key = new QName(attribute.getLocalName());
178 
179                 } else if (namespace.getPrefix() == null) {
180                     key = new QName(namespace.getNamespaceURI(), attribute
181                             .getLocalName());
182 
183                 } else {
184                     key = new QName(namespace.getNamespaceURI(), attribute
185                             .getLocalName(), namespace.getPrefix());
186                 }
187 
188                 policyOperator.addAttribute(key, attribute.getAttributeValue());
189             }
190         }
191 
192         OMElement childElement;
193 
194         for (Iterator iterator = operationElement.getChildElements(); iterator
195                 .hasNext();) {
196             childElement = (OMElement) iterator.next();
197 
198             if (Constants.URI_POLICY_NS.equals(childElement.getNamespace()
199                     .getNamespaceURI())) {
200 
201                 if (Constants.ELEM_POLICY.equals(childElement.getLocalName())) {
202                     operator
203                             .addPolicyComponent(getPolicyOperator(childElement));
204 
205                 } else if (Constants.ELEM_EXACTLYONE.equals(childElement
206                         .getLocalName())) {
207                     operator
208                             .addPolicyComponent(getExactlyOneOperator(childElement));
209 
210                 } else if (Constants.ELEM_ALL.equals(childElement
211                         .getLocalName())) {
212                     operator.addPolicyComponent(getAllOperator(childElement));
213 
214                 } else if (Constants.ELEM_POLICY_REF.equals(childElement
215                         .getLocalName())) {
216                     operator
217                             .addPolicyComponent(getPolicyReference(childElement));
218                 }
219 
220             } else {
221                 operator.addPolicyComponent(factory.build(childElement));
222             }
223         }
224         return operator;
225     }
226 }