01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.ws.policy.attachment.reference;
19:
20: import org.w3c.dom.Document;
21: import org.w3c.dom.Element;
22: import org.w3c.dom.NodeList;
23:
24: import org.xml.sax.InputSource;
25:
26: import org.apache.cxf.helpers.DOMUtils;
27: import org.apache.cxf.resource.ExtendedURIResolver;
28: import org.apache.cxf.ws.policy.PolicyBuilder;
29: import org.apache.cxf.ws.policy.PolicyConstants;
30: import org.apache.cxf.ws.policy.PolicyException;
31: import org.apache.neethi.Policy;
32:
33: /**
34: *
35: */
36: public class RemoteReferenceResolver implements ReferenceResolver {
37:
38: private String baseURI;
39: private PolicyBuilder builder;
40: private PolicyConstants constants;
41:
42: public RemoteReferenceResolver(String uri, PolicyBuilder b,
43: PolicyConstants c) {
44: baseURI = uri;
45: builder = b;
46: constants = c;
47: }
48:
49: public Policy resolveReference(String uri) {
50: int pos = uri.indexOf('#');
51: String documentURI = uri.substring(0, pos);
52:
53: InputSource is = new ExtendedURIResolver().resolve(documentURI,
54: baseURI);
55: if (null == is) {
56: return null;
57: }
58: Document doc = null;
59: try {
60: doc = DOMUtils.readXml(is.getByteStream());
61: } catch (Exception ex) {
62: throw new PolicyException(ex);
63: }
64:
65: NodeList nl = doc.getElementsByTagNameNS(constants
66: .getNamespace(), constants.getPolicyElemName());
67: String id = uri.substring(pos + 1);
68: for (int i = 0; i < nl.getLength(); i++) {
69: Element elem = (Element) nl.item(i);
70: if (id.equals(elem.getAttributeNS(constants
71: .getWSUNamespace(), constants.getIdAttrName()))) {
72: return builder.getPolicy(elem);
73: }
74: }
75:
76: return null;
77: }
78: }
|