Search in sources :

Example 76 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class LocalDocumentReferenceResolver method resolveReference.

public Policy resolveReference(String uri, Element el) {
    if (el == null) {
        return null;
    }
    if (uri.equals(el.getAttributeNS(PolicyConstants.WSU_NAMESPACE_URI, PolicyConstants.WSU_ID_ATTR_NAME))) {
        return builder.getPolicy(el);
    }
    Element el2 = DOMUtils.getFirstElement(el);
    while (el2 != null) {
        Policy p = resolveReference(uri, el2);
        if (p != null) {
            return p;
        }
        el2 = DOMUtils.getNextElement(el2);
    }
    return null;
}
Also used : Policy(org.apache.neethi.Policy) Element(org.w3c.dom.Element)

Example 77 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class Wsdl11AttachmentPolicyProvider method resolveLocal.

Policy resolveLocal(PolicyReference ref, DescriptionInfo di) {
    String uri = ref.getURI().substring(1);
    String absoluteURI = di.getBaseURI();
    if (absoluteURI == null) {
        absoluteURI = Integer.toString(di.hashCode()) + ref.getURI();
    } else {
        absoluteURI = absoluteURI + ref.getURI();
    }
    Policy resolved = registry.lookup(absoluteURI);
    if (null != resolved) {
        return resolved;
    }
    ReferenceResolver resolver = new LocalServiceModelReferenceResolver(di, builder);
    resolved = resolver.resolveReference(uri);
    if (null != resolved) {
        ref.setURI(absoluteURI);
        registry.register(absoluteURI, resolved);
    }
    return resolved;
}
Also used : Policy(org.apache.neethi.Policy) LocalServiceModelReferenceResolver(org.apache.cxf.ws.policy.attachment.reference.LocalServiceModelReferenceResolver) ReferenceResolver(org.apache.cxf.ws.policy.attachment.reference.ReferenceResolver) LocalServiceModelReferenceResolver(org.apache.cxf.ws.policy.attachment.reference.LocalServiceModelReferenceResolver)

Example 78 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class Wsdl11AttachmentPolicyProvider method getElementPolicy.

Policy getElementPolicy(Extensible ex, boolean includeAttributes, DescriptionInfo di) {
    if (null == ex || null == di) {
        return null;
    }
    if (di.getProperty("registeredPolicy") == null) {
        List<UnknownExtensibilityElement> diext = di.getExtensors(UnknownExtensibilityElement.class);
        if (diext != null) {
            for (UnknownExtensibilityElement e : diext) {
                String uri = e.getElement().getAttributeNS(PolicyConstants.WSU_NAMESPACE_URI, PolicyConstants.WSU_ID_ATTR_NAME);
                if (Constants.isPolicyElement(e.getElementType()) && !StringUtils.isEmpty(uri)) {
                    String id = (di.getBaseURI() == null ? Integer.toString(di.hashCode()) : di.getBaseURI()) + "#" + uri;
                    Policy policy = registry.lookup(id);
                    if (policy == null) {
                        try {
                            policy = builder.getPolicy(e.getElement());
                            String fragement = "#" + uri;
                            registry.register(fragement, policy);
                            registry.register(id, policy);
                        } catch (Exception policyEx) {
                            // ignore the policy can not be built
                            LOG.warning("Failed to build the policy '" + uri + "':" + policyEx.getMessage());
                        }
                    }
                }
            }
        }
        di.setProperty("registeredPolicy", true);
    }
    Policy elementPolicy = null;
    List<UnknownExtensibilityElement> extensions = ex.getExtensors(UnknownExtensibilityElement.class);
    if (null != extensions) {
        for (UnknownExtensibilityElement e : extensions) {
            Policy p = null;
            if (Constants.isPolicyElement(e.getElementType())) {
                p = builder.getPolicy(e.getElement());
            } else if (Constants.isPolicyRef(e.getElementType())) {
                PolicyReference ref = builder.getPolicyReference(e.getElement());
                if (null != ref) {
                    p = resolveReference(ref, di);
                }
            }
            if (null != p) {
                if (elementPolicy == null) {
                    elementPolicy = new Policy();
                }
                elementPolicy = elementPolicy.merge(p);
            }
        }
    }
    if (includeAttributes && ex.getExtensionAttributes() != null) {
        for (Map.Entry<QName, Object> ent : ex.getExtensionAttributes().entrySet()) {
            if (Constants.isPolicyURIsAttr(ent.getKey())) {
                Object attr = ent.getValue();
                // can be of type a String, a QName, a list of Srings or a list of QNames
                String uris = null;
                if (attr instanceof QName) {
                    uris = ((QName) attr).getLocalPart();
                } else if (attr instanceof String) {
                    uris = (String) attr;
                }
                if (null != uris) {
                    StringTokenizer st = new StringTokenizer(uris);
                    while (st.hasMoreTokens()) {
                        String uri = st.nextToken();
                        PolicyReference ref = new PolicyReference();
                        ref.setURI(uri);
                        Policy p = resolveReference(ref, di);
                        if (null != p) {
                            elementPolicy = elementPolicy == null ? new Policy().merge(p) : elementPolicy.merge(p);
                        }
                    }
                }
            }
        }
    }
    return elementPolicy;
}
Also used : Policy(org.apache.neethi.Policy) StringTokenizer(java.util.StringTokenizer) QName(javax.xml.namespace.QName) UnknownExtensibilityElement(javax.wsdl.extensions.UnknownExtensibilityElement) PolicyReference(org.apache.neethi.PolicyReference) Map(java.util.Map)

Example 79 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class EffectivePolicyImplTest method testInitialiseServerFaultPolicy.

@Test
public void testInitialiseServerFaultPolicy() {
    Message m = new MessageImpl();
    EndpointInfo ei = control.createMock(EndpointInfo.class);
    BindingFaultInfo bfi = control.createMock(BindingFaultInfo.class);
    PolicyEngineImpl engine = control.createMock(PolicyEngineImpl.class);
    BindingOperationInfo boi = control.createMock(BindingOperationInfo.class);
    EasyMock.expect(bfi.getBindingOperation()).andReturn(boi).anyTimes();
    EndpointPolicy endpointPolicy = control.createMock(EndpointPolicy.class);
    EasyMock.expect(engine.getServerEndpointPolicy(ei, (Destination) null, m)).andReturn(endpointPolicy);
    Policy ep = control.createMock(Policy.class);
    EasyMock.expect(endpointPolicy.getPolicy()).andReturn(ep);
    Policy op = control.createMock(Policy.class);
    EasyMock.expect(engine.getAggregatedOperationPolicy(boi, m)).andReturn(op);
    Policy merged = control.createMock(Policy.class);
    EasyMock.expect(ep.merge(op)).andReturn(merged);
    Policy fp = control.createMock(Policy.class);
    EasyMock.expect(engine.getAggregatedFaultPolicy(bfi, m)).andReturn(fp);
    EasyMock.expect(merged.merge(fp)).andReturn(merged);
    EasyMock.expect(merged.normalize(null, true)).andReturn(merged);
    control.replay();
    EffectivePolicyImpl epi = new EffectivePolicyImpl();
    epi.initialisePolicy(ei, boi, bfi, engine, m);
    assertSame(merged, epi.getPolicy());
    control.verify();
}
Also used : Policy(org.apache.neethi.Policy) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) MessageImpl(org.apache.cxf.message.MessageImpl) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo) Test(org.junit.Test)

Example 80 with Policy

use of org.apache.neethi.Policy in project cxf by apache.

the class EffectivePolicyImplTest method testChooseAlternative.

@Test
public void testChooseAlternative() {
    Message m = new MessageImpl();
    EffectivePolicyImpl epi = new EffectivePolicyImpl();
    Policy policy = new Policy();
    epi.setPolicy(policy);
    PolicyEngineImpl engine = control.createMock(PolicyEngineImpl.class);
    Assertor assertor = control.createMock(Assertor.class);
    AlternativeSelector selector = control.createMock(AlternativeSelector.class);
    EasyMock.expect(engine.getAlternativeSelector()).andReturn(selector);
    EasyMock.expect(selector.selectAlternative(policy, engine, assertor, null, m)).andReturn(null);
    control.replay();
    try {
        epi.chooseAlternative(engine, assertor, m);
        fail("Expected PolicyException not thrown.");
    } catch (PolicyException ex) {
    // expected
    }
    control.verify();
    control.reset();
    EasyMock.expect(engine.getAlternativeSelector()).andReturn(selector);
    Collection<Assertion> alternative = new ArrayList<>();
    EasyMock.expect(selector.selectAlternative(policy, engine, assertor, null, m)).andReturn(alternative);
    control.replay();
    epi.chooseAlternative(engine, assertor, m);
    Collection<Assertion> choice = epi.getChosenAlternative();
    assertSame(choice, alternative);
    control.verify();
}
Also used : Policy(org.apache.neethi.Policy) Message(org.apache.cxf.message.Message) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Aggregations

Policy (org.apache.neethi.Policy)122 Test (org.junit.Test)47 Assertion (org.apache.neethi.Assertion)27 QName (javax.xml.namespace.QName)23 ArrayList (java.util.ArrayList)21 All (org.apache.neethi.All)18 ExactlyOne (org.apache.neethi.ExactlyOne)18 Message (org.apache.cxf.message.Message)15 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)14 Element (org.w3c.dom.Element)13 Bus (org.apache.cxf.Bus)12 PrimitiveAssertion (org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion)12 List (java.util.List)9 Interceptor (org.apache.cxf.interceptor.Interceptor)9 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)9 HashMap (java.util.HashMap)7 OMElement (org.apache.axiom.om.OMElement)7 MessageImpl (org.apache.cxf.message.MessageImpl)7 ReferenceResolver (org.apache.cxf.ws.policy.attachment.reference.ReferenceResolver)6 InputStream (java.io.InputStream)5