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.wsdl11;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.StringTokenizer;
024:
025: import javax.wsdl.Definition;
026: import javax.wsdl.extensions.ExtensibilityElement;
027: import javax.wsdl.extensions.UnknownExtensibilityElement;
028: import javax.xml.namespace.QName;
029:
030: import org.apache.cxf.Bus;
031: import org.apache.cxf.helpers.CastUtils;
032: import org.apache.cxf.service.model.AbstractDescriptionElement;
033: import org.apache.cxf.service.model.BindingFaultInfo;
034: import org.apache.cxf.service.model.BindingMessageInfo;
035: import org.apache.cxf.service.model.BindingOperationInfo;
036: import org.apache.cxf.service.model.DescriptionInfo;
037: import org.apache.cxf.service.model.EndpointInfo;
038: import org.apache.cxf.service.model.Extensible;
039: import org.apache.cxf.service.model.FaultInfo;
040: import org.apache.cxf.service.model.MessageInfo;
041: import org.apache.cxf.service.model.ServiceInfo;
042: import org.apache.cxf.ws.policy.PolicyConstants;
043: import org.apache.cxf.ws.policy.PolicyProvider;
044: import org.apache.cxf.ws.policy.attachment.AbstractPolicyProvider;
045: import org.apache.cxf.ws.policy.attachment.reference.LocalServiceModelReferenceResolver;
046: import org.apache.cxf.ws.policy.attachment.reference.ReferenceResolver;
047: import org.apache.cxf.wsdl11.WSDLServiceBuilder;
048: import org.apache.neethi.Policy;
049: import org.apache.neethi.PolicyReference;
050:
051: /**
052: * PolicyAttachmentManager provides methods to retrieve element policies and
053: * calculate effective policies based on the policy subject's scope.
054: *
055: */
056: public class Wsdl11AttachmentPolicyProvider extends
057: AbstractPolicyProvider implements PolicyProvider {
058:
059: public Wsdl11AttachmentPolicyProvider() {
060: this (null);
061: }
062:
063: public Wsdl11AttachmentPolicyProvider(Bus bus) {
064: super (bus);
065: }
066:
067: public Policy getEffectivePolicy(ServiceInfo si) {
068: return getElementPolicy(si);
069: }
070:
071: /**
072: * The effective policy for a WSDL endpoint policy subject includes the element policy of the
073: * wsdl11:port element that defines the endpoint merged with the element policy of the
074: * referenced wsdl11:binding element and the element policy of the referenced wsdl11:portType
075: * element that defines the interface of the endpoint.
076: *
077: * @param ei the EndpointInfo object identifying the endpoint
078: * @return the effective policy
079: */
080: public Policy getEffectivePolicy(EndpointInfo ei) {
081: Policy p = getElementPolicy(ei);
082: p = p.merge(getElementPolicy(ei.getBinding()));
083: p = p.merge(getElementPolicy(ei.getInterface(), true));
084:
085: return p;
086: }
087:
088: /**
089: * The effective policy for a WSDL operation policy subject is calculated in relation to a
090: * specific port, and includes the element policy of the wsdl11:portType/wsdl11:operation
091: * element that defines the operation merged with that of the corresponding
092: * wsdl11:binding/wsdl11:operation element.
093: *
094: * @param bi the BindingOperationInfo identifying the operation in relation to a port
095: * @return the effective policy
096: */
097: public Policy getEffectivePolicy(BindingOperationInfo bi) {
098: DescriptionInfo di = bi.getBinding().getDescription();
099: Policy p = getElementPolicy(bi, false, di);
100: p = p.merge(getElementPolicy(bi.getOperationInfo(), false, di));
101: return p;
102: }
103:
104: /**
105: * The effective policy for a specific WSDL message (input or output) is calculated
106: * in relation to a specific port, and includes the element policy of the wsdl:message
107: * element that defines the message's type merged with the element policy of the
108: * wsdl11:binding and wsdl11:portType message definitions that describe the message.
109: * For example, the effective policy of a specific input message for a specific port
110: * would be the (element policies of the) wsdl11:message element defining the message type,
111: * the wsdl11:portType/wsdl11:operation/wsdl11:input element and the corresponding
112: * wsdl11:binding/wsdl11:operation/wsdl11:input element for that message.
113: *
114: * @param bmi the BindingMessageInfo identifiying the message
115: * @return the effective policy
116: */
117: public Policy getEffectivePolicy(BindingMessageInfo bmi) {
118: ServiceInfo si = bmi.getBindingOperation().getBinding()
119: .getService();
120: DescriptionInfo di = si.getDescription();
121: Policy p = getElementPolicy(bmi, false, di);
122: MessageInfo mi = bmi.getMessageInfo();
123: p = p.merge(getElementPolicy(mi, true, di));
124: Extensible ex = getMessageTypeInfo(mi.getName(), di);
125: p = p.merge(getElementPolicy(ex, false, di));
126:
127: return p;
128: }
129:
130: public Policy getEffectivePolicy(BindingFaultInfo bfi) {
131: ServiceInfo si = bfi.getBindingOperation().getBinding()
132: .getService();
133: DescriptionInfo di = si.getDescription();
134:
135: Policy p = getElementPolicy(bfi, false, di);
136: FaultInfo fi = bfi.getFaultInfo();
137: p = p.merge(getElementPolicy(fi, true, di));
138: Extensible ex = getMessageTypeInfo(fi.getName(), di);
139: p = p.merge(getElementPolicy(ex, false, di));
140:
141: return p;
142: }
143:
144: Policy getElementPolicy(AbstractDescriptionElement adh) {
145: return getElementPolicy(adh, false);
146: }
147:
148: Policy getElementPolicy(AbstractDescriptionElement adh,
149: boolean includeAttributes) {
150: return getElementPolicy(adh, includeAttributes, adh
151: .getDescription());
152: }
153:
154: Policy getElementPolicy(Extensible ex, boolean includeAttributes,
155: DescriptionInfo di) {
156:
157: Policy elementPolicy = new Policy();
158:
159: if (null == ex || null == di) {
160: return elementPolicy;
161: }
162:
163: List<UnknownExtensibilityElement> extensions = ex
164: .getExtensors(UnknownExtensibilityElement.class);
165: PolicyConstants constants = bus
166: .getExtension(PolicyConstants.class);
167: if (null != extensions) {
168: for (UnknownExtensibilityElement e : extensions) {
169: Policy p = null;
170: if (constants.getPolicyElemQName().equals(
171: e.getElementType())) {
172: p = builder.getPolicy(e.getElement());
173:
174: } else if (constants.getPolicyReferenceElemQName()
175: .equals(e.getElementType())) {
176: PolicyReference ref = builder.getPolicyReference(e
177: .getElement());
178: if (null != ref) {
179: p = resolveReference(ref, di);
180: }
181: }
182: if (null != p) {
183: elementPolicy = elementPolicy.merge(p);
184: }
185: }
186: }
187:
188: if (includeAttributes) {
189: Object attr = ex.getExtensionAttribute(constants
190: .getPolicyURIsAttrQName());
191: // can be of type a String, a QName, a list of Srings or a list of QNames
192: String uris = null;
193: if (attr instanceof QName) {
194: uris = ((QName) attr).getLocalPart();
195: } else if (attr instanceof String) {
196: uris = (String) attr;
197: }
198: if (null != uris) {
199: StringTokenizer st = new StringTokenizer(uris);
200: while (st.hasMoreTokens()) {
201: String uri = st.nextToken();
202: PolicyReference ref = new PolicyReference();
203: ref.setURI(uri);
204: Policy p = resolveReference(ref, di);
205: if (null != p) {
206: elementPolicy = elementPolicy.merge(p);
207: }
208: }
209: }
210: }
211:
212: return elementPolicy;
213: }
214:
215: Policy resolveReference(PolicyReference ref, DescriptionInfo di) {
216: Policy p = null;
217: if (isExternal(ref)) {
218: p = resolveExternal(ref, di.getBaseURI());
219: } else {
220: p = resolveLocal(ref, di);
221: }
222: checkResolved(ref, p);
223: return p;
224: }
225:
226: Policy resolveLocal(PolicyReference ref, DescriptionInfo di) {
227: String uri = ref.getURI().substring(1);
228: String absoluteURI = di.getBaseURI() + uri;
229: Policy resolved = registry.lookup(absoluteURI);
230: if (null != resolved) {
231: return resolved;
232: }
233: ReferenceResolver resolver = new LocalServiceModelReferenceResolver(
234: di, builder, bus.getExtension(PolicyConstants.class));
235: resolved = resolver.resolveReference(uri);
236: if (null != resolved) {
237: ref.setURI(absoluteURI);
238: registry.register(absoluteURI, resolved);
239: }
240: return resolved;
241: }
242:
243: private Extensible getMessageTypeInfo(QName name, DescriptionInfo di) {
244: if (null == di) {
245: return null;
246: }
247: Definition def = (Definition) di
248: .getProperty(WSDLServiceBuilder.WSDL_DEFINITION);
249: if (null == def) {
250: return null;
251: }
252:
253: javax.wsdl.Message m = def.getMessage(name);
254: if (null != m) {
255: List<ExtensibilityElement> extensors = CastUtils.cast(m
256: .getExtensibilityElements(),
257: ExtensibilityElement.class);
258: if (null != extensors) {
259: return new ExtensibleInfo(extensors);
260: }
261: }
262: return null;
263: }
264:
265: private class ExtensibleInfo implements Extensible {
266: private List<ExtensibilityElement> extensors;
267:
268: ExtensibleInfo(List<ExtensibilityElement> e) {
269: extensors = e;
270: }
271:
272: public <T> T getExtensor(Class<T> cls) {
273: for (ExtensibilityElement e : extensors) {
274: if (cls.isInstance(e)) {
275: return cls.cast(e);
276: }
277: }
278: return null;
279: }
280:
281: public <T> List<T> getExtensors(Class<T> cls) {
282: if (null == extensors) {
283: return null;
284: }
285:
286: List<T> list = new ArrayList<T>(extensors.size());
287: for (ExtensibilityElement e : extensors) {
288: if (cls.isInstance(e)) {
289: list.add(cls.cast(e));
290: }
291: }
292: return list;
293: }
294:
295: public void addExtensionAttribute(QName arg0, Object arg1) {
296: }
297:
298: public void addExtensor(Object arg0) {
299: }
300:
301: public Object getExtensionAttribute(QName arg0) {
302: return null;
303: }
304:
305: public Map<QName, Object> getExtensionAttributes() {
306: return null;
307: }
308:
309: public void setExtensionAttributes(Map<QName, Object> arg0) {
310: }
311:
312: }
313:
314: }
|