001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.ws.policy;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.ResourceBundle;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.parsers.ParserConfigurationException;
026:
027: import org.w3c.dom.Element;
028: import org.w3c.dom.NamedNodeMap;
029: import org.w3c.dom.Node;
030:
031: import org.xml.sax.SAXException;
032:
033: import org.apache.cxf.common.i18n.BundleUtils;
034: import org.apache.cxf.common.i18n.Message;
035: import org.apache.cxf.extension.BusExtension;
036: import org.apache.cxf.helpers.DOMUtils;
037: import org.apache.neethi.All;
038: import org.apache.neethi.Assertion;
039: import org.apache.neethi.Constants;
040: import org.apache.neethi.ExactlyOne;
041: import org.apache.neethi.Policy;
042: import org.apache.neethi.PolicyOperator;
043: import org.apache.neethi.PolicyReference;
044:
045: /**
046: * PolicyBuilderImpl is an implementation of the PolicyBuilder interface,
047: * provides methods to create Policy and PolicyReferenceObjects
048: * from DOM elements, but also from an input stream etc.
049: */
050: public class PolicyBuilderImpl implements PolicyBuilder, BusExtension {
051:
052: private static final ResourceBundle BUNDLE = BundleUtils
053: .getBundle(PolicyBuilderImpl.class);
054:
055: private AssertionBuilderRegistry assertionBuilderRegistry;
056:
057: public Class<?> getRegistrationType() {
058: return PolicyBuilder.class;
059: }
060:
061: public void setAssertionBuilderRegistry(AssertionBuilderRegistry abr) {
062: assertionBuilderRegistry = abr;
063: }
064:
065: public AssertionBuilderRegistry getAssertionBuilderRegistry() {
066: return assertionBuilderRegistry;
067: }
068:
069: /**
070: * Creates a PolicyReference object from an InputStream.
071: *
072: * @param inputStream the input stream
073: * @return the PolicyReference constructed from the input stream
074: */
075: public PolicyReference getPolicyReference(InputStream is)
076: throws IOException, ParserConfigurationException,
077: SAXException {
078: Element element = DOMUtils.readXml(is).getDocumentElement();
079: return getPolicyReference(element);
080: }
081:
082: /**
083: * Creates a PolicyReference object from a DOM element.
084: *
085: * @param element the element
086: * @return the PolicyReference object constructed from the element
087: */
088: public PolicyReference getPolicyReference(Element element) {
089: if (!(Constants.URI_POLICY_NS.equals(element.getNamespaceURI()) && Constants.ELEM_POLICY_REF
090: .equals(element.getLocalName()))) {
091: throw new PolicyException(new Message(
092: "NOT_A_POLICYREF_ELEMENT", BUNDLE));
093: }
094:
095: PolicyReference reference = new PolicyReference();
096: reference.setURI(element.getAttribute("URI"));
097: return reference;
098: }
099:
100: /**
101: * Creates a Policy object from an InputStream.
102: *
103: * @param inputStream the input stream
104: * @return the Policy object constructed from the input stream
105: */
106: public Policy getPolicy(InputStream is) throws IOException,
107: ParserConfigurationException, SAXException {
108: Element element = DOMUtils.readXml(is).getDocumentElement();
109: return getPolicy(element);
110: }
111:
112: /**
113: * Creates a Policy object from a DOM element.
114: *
115: * @param element the element
116: * @retun the Policy object constructed from the element
117: */
118: public Policy getPolicy(Element element) {
119: return getPolicyOperator(element);
120: }
121:
122: private Policy getPolicyOperator(Element element) {
123: return (Policy) processOperationElement(element, new Policy());
124: }
125:
126: private ExactlyOne getExactlyOneOperator(Element element) {
127: return (ExactlyOne) processOperationElement(element,
128: new ExactlyOne());
129: }
130:
131: private All getAllOperator(Element element) {
132: return (All) processOperationElement(element, new All());
133: }
134:
135: private PolicyOperator processOperationElement(
136: Element operationElement, PolicyOperator operator) {
137:
138: if (Constants.TYPE_POLICY == operator.getType()) {
139: Policy policyOperator = (Policy) operator;
140:
141: QName key;
142:
143: NamedNodeMap nnm = operationElement.getAttributes();
144: for (int i = 0; i < nnm.getLength(); i++) {
145: Node n = nnm.item(i);
146: if (Node.ATTRIBUTE_NODE == n.getNodeType()) {
147: String namespace = n.getNamespaceURI();
148: if (namespace == null) {
149: key = new QName(n.getLocalName());
150:
151: } else if (n.getPrefix() == null) {
152: key = new QName(namespace, n.getLocalName());
153:
154: } else {
155: key = new QName(namespace, n.getLocalName(), n
156: .getPrefix());
157: }
158: policyOperator.addAttribute(key, n.getNodeValue());
159: }
160: }
161: }
162:
163: Element childElement;
164: for (Node n = operationElement.getFirstChild(); n != null; n = n
165: .getNextSibling()) {
166: if (Node.ELEMENT_NODE != n.getNodeType()) {
167: continue;
168: }
169: childElement = (Element) n;
170: String namespaceURI = childElement.getNamespaceURI();
171: String localName = childElement.getLocalName();
172:
173: if (Constants.URI_POLICY_NS.equals(namespaceURI)) {
174:
175: if (Constants.ELEM_POLICY.equals(localName)) {
176: operator
177: .addPolicyComponent(getPolicyOperator(childElement));
178:
179: } else if (Constants.ELEM_EXACTLYONE.equals(localName)) {
180: operator
181: .addPolicyComponent(getExactlyOneOperator(childElement));
182:
183: } else if (Constants.ELEM_ALL.equals(localName)) {
184: operator
185: .addPolicyComponent(getAllOperator(childElement));
186:
187: } else if (Constants.ELEM_POLICY_REF.equals(localName)) {
188: operator
189: .addPolicyComponent(getPolicyReference(childElement));
190: }
191:
192: } else if (null != assertionBuilderRegistry) {
193: Assertion a = assertionBuilderRegistry
194: .build(childElement);
195: if (null != a) {
196: operator.addPolicyComponent(a);
197: }
198: }
199: }
200: return operator;
201: }
202:
203: }
|