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);
}
}
}
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);
}
}
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);
}
}
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());
}
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;
}
Aggregations