Search in sources :

Example 1 with PolicyReference

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

the class WSPolicyFeature method initialize.

@Override
public void initialize(Bus bus) {
    initializePolicyEngine(bus);
    Collection<Policy> loadedPolicies = null;
    if (policyElements != null || policyReferenceElements != null) {
        loadedPolicies = new ArrayList<>();
        PolicyBuilder builder = bus.getExtension(PolicyBuilder.class);
        if (null != policyElements) {
            for (Element e : policyElements) {
                loadedPolicies.add(builder.getPolicy(e));
            }
        }
        if (null != policyReferenceElements) {
            for (Element e : policyReferenceElements) {
                PolicyReference pr = builder.getPolicyReference(e);
                Policy resolved = resolveReference(pr, builder, bus, null);
                if (null != resolved) {
                    loadedPolicies.add(resolved);
                }
            }
        }
    }
    Policy thePolicy = new Policy();
    if (policies != null) {
        for (Policy p : policies) {
            thePolicy = thePolicy.merge(p);
        }
    }
    if (loadedPolicies != null) {
        for (Policy p : loadedPolicies) {
            thePolicy = thePolicy.merge(p);
        }
    }
    if (!thePolicy.isEmpty()) {
        PolicyEngine pe = bus.getExtension(PolicyEngine.class);
        synchronized (pe) {
            pe.addPolicy(thePolicy);
        }
    }
}
Also used : Policy(org.apache.neethi.Policy) Element(org.w3c.dom.Element) PolicyReference(org.apache.neethi.PolicyReference)

Example 2 with PolicyReference

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

the class ExternalAttachmentProvider method readDocument.

void readDocument() {
    if (null != attachments) {
        return;
    }
    // read the document and build the attachments
    attachments = new ArrayList<>();
    final Document doc;
    try (InputStream is = location.getInputStream()) {
        if (null == is) {
            throw new PolicyException(new org.apache.cxf.common.i18n.Message("COULD_NOT_OPEN_ATTACHMENT_DOC_EXC", BUNDLE, location));
        }
        doc = StaxUtils.read(is);
    } catch (Exception ex) {
        throw new PolicyException(ex);
    }
    for (Element ae : PolicyConstants.findAllPolicyElementsOfLocalName(doc, Constants.ELEM_POLICY_ATTACHMENT)) {
        PolicyAttachment attachment = new PolicyAttachment();
        for (Node nd = ae.getFirstChild(); nd != null; nd = nd.getNextSibling()) {
            if (Node.ELEMENT_NODE != nd.getNodeType()) {
                continue;
            }
            QName qn = new QName(nd.getNamespaceURI(), nd.getLocalName());
            if (Constants.isAppliesToElem(qn)) {
                Collection<DomainExpression> des = readDomainExpressions((Element) nd);
                if (des.isEmpty()) {
                    // forget about this attachment
                    continue;
                }
                attachment.setDomainExpressions(des);
            } else if (Constants.isPolicyElement(qn)) {
                Policy p = builder.getPolicy(nd);
                if (null != attachment.getPolicy()) {
                    p = p.merge(attachment.getPolicy());
                }
                attachment.setPolicy(p);
                // cache the element so it can be used when generating the wsdl
                attachment.setElement((Element) nd);
            } else if (Constants.isPolicyRef(qn)) {
                PolicyReference ref = builder.getPolicyReference(nd);
                if (null != ref) {
                    Policy p = resolveReference(ref, doc);
                    if (null != attachment.getPolicy()) {
                        p = p.merge(attachment.getPolicy());
                    }
                    attachment.setPolicy(p);
                }
            }
        // TODO: wsse:Security child element
        }
        if (null == attachment.getPolicy() || null == attachment.getDomainExpressions()) {
            continue;
        }
        attachments.add(attachment);
    }
}
Also used : Policy(org.apache.neethi.Policy) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) PolicyReference(org.apache.neethi.PolicyReference) PolicyException(org.apache.cxf.ws.policy.PolicyException) PolicyException(org.apache.cxf.ws.policy.PolicyException)

Example 3 with PolicyReference

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

the class PolicyEngineImpl method addAssertions.

void addAssertions(PolicyComponent pc, boolean includeOptional, Collection<Assertion> assertions) {
    if (Constants.TYPE_ASSERTION == pc.getType()) {
        Assertion a = (Assertion) pc;
        if (includeOptional || !a.isOptional()) {
            assertions.add((Assertion) pc);
        }
        return;
    }
    if (Constants.TYPE_POLICY_REF == pc.getType()) {
        PolicyReference pr = (PolicyReference) pc;
        pc = pr.normalize(registry, false);
    }
    PolicyOperator po = (PolicyOperator) pc;
    List<PolicyComponent> pcs = CastUtils.cast(po.getPolicyComponents(), PolicyComponent.class);
    for (PolicyComponent child : pcs) {
        addAssertions(child, includeOptional, assertions);
    }
}
Also used : PolicyComponent(org.apache.neethi.PolicyComponent) PolicyOperator(org.apache.neethi.PolicyOperator) Assertion(org.apache.neethi.Assertion) PolicyReference(org.apache.neethi.PolicyReference)

Example 4 with PolicyReference

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

the class PolicyEngineTest method testAddAssertions.

@Test
public void testAddAssertions() {
    engine = new PolicyEngineImpl();
    Collection<Assertion> assertions = new ArrayList<>();
    Assertion a = control.createMock(Assertion.class);
    EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
    EasyMock.expect(a.isOptional()).andReturn(true);
    control.replay();
    engine.addAssertions(a, false, assertions);
    assertTrue(assertions.isEmpty());
    control.verify();
    control.reset();
    EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
    control.replay();
    engine.addAssertions(a, true, assertions);
    assertEquals(1, assertions.size());
    assertSame(a, assertions.iterator().next());
    control.verify();
    assertions.clear();
    Policy p = new Policy();
    a = new PrimitiveAssertion(new QName("http://x.y.z", "a"));
    p.addAssertion(a);
    // id has no #
    engine.getRegistry().register("ab", p);
    // local reference is an id + #
    PolicyReference pr = new PolicyReference();
    pr.setURI("#ab");
    engine.addAssertions(pr, false, assertions);
    assertEquals(1, assertions.size());
    assertSame(a, assertions.iterator().next());
}
Also used : Policy(org.apache.neethi.Policy) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) QName(javax.xml.namespace.QName) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) PolicyReference(org.apache.neethi.PolicyReference) Test(org.junit.Test)

Example 5 with PolicyReference

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

the class WSPolicyFeature method initializeEndpointPolicy.

private Policy initializeEndpointPolicy(Endpoint endpoint, Bus bus) {
    initializePolicyEngine(bus);
    DescriptionInfo i = endpoint.getEndpointInfo().getDescription();
    Collection<Policy> loadedPolicies = null;
    if (policyElements != null || policyReferenceElements != null) {
        loadedPolicies = new ArrayList<>();
        PolicyBuilder builder = bus.getExtension(PolicyBuilder.class);
        if (null != policyElements) {
            for (Element e : policyElements) {
                loadedPolicies.add(builder.getPolicy(e));
            }
        }
        if (null != policyReferenceElements) {
            for (Element e : policyReferenceElements) {
                PolicyReference pr = builder.getPolicyReference(e);
                Policy resolved = resolveReference(pr, builder, bus, i);
                if (null != resolved) {
                    loadedPolicies.add(resolved);
                }
            }
        }
    }
    Policy thePolicy = new Policy();
    if (policies != null) {
        for (Policy p : policies) {
            thePolicy = thePolicy.merge(p);
        }
    }
    if (loadedPolicies != null) {
        for (Policy p : loadedPolicies) {
            thePolicy = thePolicy.merge(p);
        }
    }
    return thePolicy;
}
Also used : Policy(org.apache.neethi.Policy) Element(org.w3c.dom.Element) DescriptionInfo(org.apache.cxf.service.model.DescriptionInfo) PolicyReference(org.apache.neethi.PolicyReference)

Aggregations

PolicyReference (org.apache.neethi.PolicyReference)7 Policy (org.apache.neethi.Policy)5 Element (org.w3c.dom.Element)4 QName (javax.xml.namespace.QName)3 InputStream (java.io.InputStream)2 Assertion (org.apache.neethi.Assertion)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 ExtensibilityElement (javax.wsdl.extensions.ExtensibilityElement)1 UnknownExtensibilityElement (javax.wsdl.extensions.UnknownExtensibilityElement)1 AbstractDescriptionElement (org.apache.cxf.service.model.AbstractDescriptionElement)1 DescriptionInfo (org.apache.cxf.service.model.DescriptionInfo)1 PolicyException (org.apache.cxf.ws.policy.PolicyException)1 PrimitiveAssertion (org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion)1 PolicyComponent (org.apache.neethi.PolicyComponent)1 PolicyOperator (org.apache.neethi.PolicyOperator)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1