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.attachment.external;
019:
020: import javax.xml.xpath.XPathConstants;
021:
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.NodeList;
026:
027: import org.xml.sax.InputSource;
028:
029: import org.apache.cxf.helpers.DOMUtils;
030: import org.apache.cxf.helpers.XPathUtils;
031: import org.apache.cxf.resource.ExtendedURIResolver;
032: import org.apache.cxf.service.model.BindingFaultInfo;
033: import org.apache.cxf.service.model.BindingInfo;
034: import org.apache.cxf.service.model.BindingMessageInfo;
035: import org.apache.cxf.service.model.BindingOperationInfo;
036: import org.apache.cxf.service.model.EndpointInfo;
037: import org.apache.cxf.service.model.InterfaceInfo;
038: import org.apache.cxf.service.model.OperationInfo;
039: import org.apache.cxf.service.model.ServiceInfo;
040: import org.apache.cxf.ws.policy.PolicyException;
041:
042: /**
043: *
044: */
045: public class Wsdl11XPointerDomainExpression implements DomainExpression {
046:
047: private static final String NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
048: private static final String SERVICE_ELEM_NAME = "service";
049: private static final String PORT_ELEM_NAME = "port";
050: private static final String PORTTYPE_ELEM_NAME = "portType";
051: private static final String BINDING_ELEM_NAME = "binding";
052: private static final String OPERATION_ELEM_NAME = "operation";
053: private static final String NAME_ATTR_NAME = "name";
054:
055: private String baseURI;
056: private NodeList nodes;
057:
058: Wsdl11XPointerDomainExpression(String u) {
059: baseURI = u;
060: }
061:
062: public boolean appliesTo(BindingFaultInfo bfi) {
063: throw new UnsupportedOperationException();
064: }
065:
066: public boolean appliesTo(BindingMessageInfo bmi) {
067: throw new UnsupportedOperationException();
068: }
069:
070: public boolean appliesTo(BindingOperationInfo boi) {
071: if (baseURI.equals(boi.getBinding().getDescription()
072: .getBaseURI())) {
073: for (int i = 0; i < nodes.getLength(); i++) {
074: Node n = nodes.item(i);
075: if (Node.ELEMENT_NODE != n.getNodeType()) {
076: continue;
077: }
078: Element e = (Element) n;
079: if (matchesBindingOperation(e, boi)) {
080: Element p = (Element) e.getParentNode();
081: return matchesBinding(p, boi.getBinding());
082: } else if (matchesOperation(e, boi.getOperationInfo())) {
083: return true;
084: }
085: }
086: }
087: return false;
088: }
089:
090: public boolean appliesTo(EndpointInfo ei) {
091: if (baseURI.equals(ei.getDescription().getBaseURI())) {
092: for (int i = 0; i < nodes.getLength(); i++) {
093: Node n = nodes.item(i);
094: if (Node.ELEMENT_NODE != n.getNodeType()) {
095: continue;
096: }
097: Element e = (Element) n;
098: if (matchesPort(e, ei)) {
099: Element p = (Element) e.getParentNode();
100: return matchesService(p, ei.getService());
101: } else if (matchesPortType(e, ei.getInterface())) {
102: return true;
103: } else if (matchesBinding(e, ei.getBinding())) {
104: return true;
105: }
106: }
107: }
108: return false;
109: }
110:
111: public boolean appliesTo(ServiceInfo si) {
112: if (baseURI.equals(si.getDescription().getBaseURI())) {
113: for (int i = 0; i < nodes.getLength(); i++) {
114: Node n = nodes.item(i);
115: if (Node.ELEMENT_NODE != n.getNodeType()) {
116: continue;
117: }
118: Element e = (Element) n;
119: if (matchesService(e, si)) {
120: return true;
121: }
122: }
123: }
124: return false;
125: }
126:
127: void evaluate(String uri) {
128: int pos = uri.indexOf('#');
129:
130: String documentURI = uri.substring(0, pos);
131: String path = uri.substring(pos + 1);
132:
133: InputSource is = new ExtendedURIResolver().resolve(documentURI,
134: baseURI);
135: if (null == is) {
136: System.out.println("Failed to resolve: " + documentURI
137: + " w.r.t baseURI: " + baseURI);
138: return;
139: }
140: Document doc = null;
141: try {
142: doc = DOMUtils.readXml(is.getByteStream());
143: } catch (Exception ex) {
144: throw new PolicyException(ex);
145: }
146:
147: XPathUtils xu = new XPathUtils();
148: nodes = (NodeList) xu.getValue(path, doc,
149: XPathConstants.NODESET);
150: }
151:
152: boolean matchesService(Element e, ServiceInfo si) {
153: return NAMESPACE.equals(e.getNamespaceURI())
154: && SERVICE_ELEM_NAME.equals(e.getLocalName())
155: && si.getName().getLocalPart().equals(
156: e.getAttribute(NAME_ATTR_NAME));
157: }
158:
159: boolean matchesPortType(Element e, InterfaceInfo ii) {
160: return NAMESPACE.equals(e.getNamespaceURI())
161: && PORTTYPE_ELEM_NAME.equals(e.getLocalName())
162: && ii.getName().getLocalPart().equals(
163: e.getAttribute(NAME_ATTR_NAME));
164: }
165:
166: boolean matchesPort(Element e, EndpointInfo ei) {
167: return NAMESPACE.equals(e.getNamespaceURI())
168: && PORT_ELEM_NAME.equals(e.getLocalName())
169: && ei.getName().getLocalPart().equals(
170: e.getAttribute(NAME_ATTR_NAME));
171: }
172:
173: boolean matchesBinding(Element e, BindingInfo ei) {
174: return NAMESPACE.equals(e.getNamespaceURI())
175: && BINDING_ELEM_NAME.equals(e.getLocalName())
176: && ei.getName().getLocalPart().equals(
177: e.getAttribute(NAME_ATTR_NAME));
178: }
179:
180: boolean matchesBindingOperation(Element e, BindingOperationInfo boi) {
181: return NAMESPACE.equals(e.getNamespaceURI())
182: && OPERATION_ELEM_NAME.equals(e.getLocalName())
183: && boi.getName().getLocalPart().equals(
184: e.getAttribute(NAME_ATTR_NAME));
185: }
186:
187: boolean matchesOperation(Element e, OperationInfo boi) {
188: return NAMESPACE.equals(e.getNamespaceURI())
189: && OPERATION_ELEM_NAME.equals(e.getLocalName())
190: && boi.getName().getLocalPart().equals(
191: e.getAttribute(NAME_ATTR_NAME));
192: }
193:
194: }
|