org.apache.neethi.PolicyComponent Java Examples

The following examples show how to use org.apache.neethi.PolicyComponent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PolicyUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if a given policy operator has no policy components
 * or if it has only empty ExactlyOne or All components
 * containing no assertions
 *
 * @param p the policy operator
 * @return true if this policy operator is empty
 */
public static boolean isEmptyPolicyOperator(PolicyOperator p) {

    if (p.isEmpty()) {
        return true;
    }


    for (PolicyComponent component : p.getPolicyComponents()) {
        if (!(component instanceof PolicyOperator)
            || !isEmptyPolicyOperator((PolicyOperator)component)) {
            return false;
        }
    }

    return true;
}
 
Example #2
Source File: SecurityDeploymentInterceptor.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Extract carbon security config element from the Policy
 *
 * @param policy Security Policy
 * @return security config element
 */
private OMElement getSecurityConfig(Policy policy) {
    Iterator<PolicyComponent> iterator = policy.getPolicyComponents().iterator();
    while (iterator.hasNext()) {
        PolicyComponent component = iterator.next();
        if (component instanceof XmlPrimtiveAssertion) {
            OMElement value = ((XmlPrimtiveAssertion) component).getValue();
            if (value != null &&
                SecurityConfigParamBuilder.SECURITY_CONFIG_QNAME.equals(value.getQName())) {
                if (log.isDebugEnabled()) {
                    log.debug("Carbon Security config found : " + value.toString());
                }
                return value;
            }
        }
    }
    return null;
}
 
Example #3
Source File: SecurityDeploymentInterceptor.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
private Policy applyPolicyToBindings(AxisService axisService) throws ServerException {
    Parameter parameter = axisService.getParameter(APPLY_POLICY_TO_BINDINGS);
    if (parameter != null && "true".equalsIgnoreCase(parameter.getValue().toString()) &&
            axisService.getPolicySubject() != null && axisService.getPolicySubject().getAttachedPolicyComponents()
            != null) {
        Iterator iterator = axisService.getPolicySubject().
                getAttachedPolicyComponents().iterator();
        while (iterator.hasNext()) {
            PolicyComponent currentPolicyComponent = (PolicyComponent) iterator.next();
            if (currentPolicyComponent instanceof Policy) {
                Policy policy = ((Policy) currentPolicyComponent);
                String policyId = policy.getId();
                axisService.getPolicySubject().detachPolicyComponent(policyId);
                addPolicyToAllBindings(axisService, policy);
                return policy;
            }
        }
    }
    return null;
}
 
Example #4
Source File: PolicyEngineImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #5
Source File: PolicyEngineTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAssertions() throws NoSuchMethodException {
    Method m = PolicyEngineImpl.class.getDeclaredMethod("addAssertions",
        new Class[] {PolicyComponent.class, boolean.class, Collection.class});
    engine = EasyMock.createMockBuilder(PolicyEngineImpl.class)
        .addMockedMethod(m).createMock(control);
    PolicyAssertion a = control.createMock(PolicyAssertion.class);
    EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
    EasyMock.expect(a.isOptional()).andReturn(true);

    control.replay();
    assertTrue(engine.getAssertions(a, false).isEmpty());
    control.verify();

    control.reset();
    EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
    // EasyMock.expect(a.isOptional()).andReturn(false);

    control.replay();
    Collection<Assertion> ca = engine.getAssertions(a, true);
    assertEquals(1, ca.size());
    assertSame(a, ca.iterator().next());
    control.verify();

    control.reset();
    Policy p = control.createMock(Policy.class);
    EasyMock.expect(p.getType()).andReturn(Constants.TYPE_POLICY);
    engine.addAssertions(EasyMock.eq(p), EasyMock.eq(false),
                         CastUtils.cast(EasyMock.isA(Collection.class), Assertion.class));
    EasyMock.expectLastCall();

    control.replay();
    assertTrue(engine.getAssertions(p, false).isEmpty());
    control.verify();
}
 
Example #6
Source File: RM10AssertionBuilder.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equal(PolicyComponent policyComponent) {
    if (policyComponent.getType() != Constants.TYPE_ASSERTION
        || !getName().equals(((Assertion)policyComponent).getName())) {
        return false;
    }
    JaxbAssertion<RMAssertion> other =
            JaxbAssertion.cast((Assertion)policyComponent);
    return RMPolicyUtilities.equals(this.getData(), other.getData());
}
 
Example #7
Source File: HTTPServerAssertionBuilder.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equal(PolicyComponent policyComponent) {
    if (policyComponent == this) {
        return true;
    }
    if (policyComponent.getType() != Constants.TYPE_ASSERTION
        || !(policyComponent instanceof Assertion)
        || !getName().equals(((Assertion)policyComponent).getName())) {
        return false;
    }
    JaxbAssertion<HTTPServerPolicy> other = JaxbAssertion.cast((Assertion)policyComponent);
    return new ServerPolicyCalculator().equals(this.getData(), other.getData());
}
 
Example #8
Source File: JaxbAssertionTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNormalise() {
    JaxbAssertion<FooType> assertion = new JaxbAssertion<>();
    FooType data = new FooType();
    data.setName("CXF");
    data.setNumber(2);
    QName qn = new QName("http://cxf.apache.org/test/assertions/foo", "FooType");
    assertion.setName(qn);
    assertion.setData(data);

    JaxbAssertion<?> normalised = (JaxbAssertion<?>)assertion.normalize();
    assertTrue(normalised.equal(assertion));
    assertSame(assertion.getData(), normalised.getData());

    assertion.setOptional(true);
    PolicyComponent pc = assertion.normalize();
    assertEquals(Constants.TYPE_POLICY, pc.getType());
    Policy p = (Policy)pc;
    Iterator<List<Assertion>> alternatives = p.getAlternatives();

    int total = 0;
    for (int i = 0; i < 2; i++) {
        List<Assertion> pcs = alternatives.next();
        if (!pcs.isEmpty()) {
            assertTrue(assertion.equal(pcs.get(0)));
            total += pcs.size();
        }
    }
    assertFalse(alternatives.hasNext());
    assertEquals(1, total);
}
 
Example #9
Source File: TokenWrapper.java    From steady with Apache License 2.0 5 votes vote down vote up
public PolicyComponent normalize() {
    if (token != null) {
        All all = new All();
        all.addPolicyComponent(token.normalize());
        all.addPolicyComponent(this);
        return all;
    }
    return this;
}
 
Example #10
Source File: JaxbAssertionTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEqual() {
    JaxbAssertion<FooType> assertion = new JaxbAssertion<>();
    FooType data = new FooType();
    data.setName("CXF");
    data.setNumber(2);
    QName qn = new QName("http://cxf.apache.org/test/assertions/foo", "FooType");
    assertion.setName(qn);
    assertion.setData(data);

    PolicyComponent pc = new Policy();
    assertFalse(assertion.equal(pc));
    pc = new All();
    assertFalse(assertion.equal(pc));
    pc = new ExactlyOne();
    assertFalse(assertion.equal(pc));

    IMocksControl ctrl = EasyMock.createNiceControl();
    PrimitiveAssertion xpa = ctrl.createMock(PrimitiveAssertion.class);
    QName oqn = new QName("http://cxf.apache.org/test/assertions/blah", "OtherType");
    EasyMock.expect(xpa.getName()).andReturn(oqn);
    EasyMock.expect(xpa.getType()).andReturn(Constants.TYPE_ASSERTION);

    ctrl.replay();
    assertFalse(assertion.equal(xpa));
    ctrl.verify();

    FooType odata = new FooType();
    odata.setName(data.getName());
    odata.setNumber(data.getNumber());
    JaxbAssertion<FooType> oassertion = new JaxbAssertion<>();
    oassertion.setData(odata);
    oassertion.setName(qn);
    assertFalse(assertion.equal(oassertion));
    oassertion.setData(data);
    assertTrue(assertion.equal(oassertion));
    assertTrue(assertion.equal(assertion));
}
 
Example #11
Source File: TokenWrapper.java    From steady with Apache License 2.0 5 votes vote down vote up
public PolicyComponent normalize() {
    if (token != null) {
        All all = new All();
        all.addPolicyComponent(token.normalize());
        all.addPolicyComponent(this);
        return all;
    }
    return this;
}
 
Example #12
Source File: PolicyUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void logPolicy(Logger log, Level level, String msg, PolicyComponent pc) {
    if (!log.isLoggable(level)) {
        return;
    }
    if (null == pc) {
        log.log(level, msg);
        return;
    }
    StringBuilder buf = new StringBuilder();
    buf.append(msg);
    nl(buf);
    printPolicyComponent(pc, buf, 0);
    log.log(level, buf.toString());
}
 
Example #13
Source File: AssertionInfoMap.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static void getAssertions(PolicyOperator p, Collection<Assertion> assertions) {
    List<PolicyComponent> pcs = p.getPolicyComponents();
    for (PolicyComponent pc : pcs) {
        if (pc instanceof Assertion) {
            assertions.add((Assertion)pc);
        } else {
            getAssertions((PolicyOperator)pc, assertions);
        }
    }
}
 
Example #14
Source File: JaxbAssertion.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof JaxbAssertion) {
        return super.equals(o) && equal((PolicyComponent)o);
    }
    return false;
}
 
Example #15
Source File: JaxbAssertion.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public boolean equal(PolicyComponent policyComponent) {
    if (!super.equal(policyComponent)) {
        return false;
    }
    JaxbAssertion<T> a = (JaxbAssertion<T>)policyComponent;
    return data.equals(a.getData());
}
 
Example #16
Source File: AbstractSTSClient.java    From steady with Apache License 2.0 5 votes vote down vote up
protected void setPolicyInternal(Policy newPolicy) {
    this.policy = newPolicy;
    if (algorithmSuite == null) {
        Iterator<?> i = policy.getAlternatives();
        while (i.hasNext() && algorithmSuite == null) {
            List<PolicyComponent> p = CastUtils.cast((List<?>)i.next());
            for (PolicyComponent p2 : p) {
                if (p2 instanceof Binding) {
                    algorithmSuite = ((Binding)p2).getAlgorithmSuite();
                }
            }
        }
    }
}
 
Example #17
Source File: AssertionInfoMap.java    From cxf with Apache License 2.0 5 votes vote down vote up
public boolean supportsAlternative(Collection<? extends PolicyComponent> alternative,
                                   List<QName> errors) {
    boolean pass = true;
    for (PolicyComponent a : alternative) {
        pass &= supportsAlternative(a, errors);
    }
    return pass;
}
 
Example #18
Source File: Wsdl11AttachmentPolicyProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void verifyAssertionsOnly(Policy p, int expectedAssertions) {
    List<PolicyComponent> pcs;
    pcs = CastUtils.cast(p.getAssertions(), PolicyComponent.class);
    assertEquals(expectedAssertions, pcs.size());
    for (int i = 0; i < expectedAssertions; i++) {
        assertEquals(Constants.TYPE_ASSERTION, pcs.get(i).getType());
    }
}
 
Example #19
Source File: PolicyUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void printPolicyComponent(PolicyComponent pc, StringBuilder buf, int level) {
    indent(buf, level);
    buf.append("type: ");
    buf.append(typeToString(pc.getType()));
    if (Constants.TYPE_ASSERTION == pc.getType()) {
        buf.append(' ');
        buf.append(((Assertion)pc).getName());
        if (((Assertion)pc).isOptional()) {
            buf.append(" (optional)");
        }
        buf.append(" (");
        buf.append(pc);
        buf.append(')');
        nl(buf);
        if (pc instanceof PolicyContainingAssertion) {
            PolicyComponent nested = ((PolicyContainingAssertion)pc).getPolicy();
            level++;
            printPolicyComponent(nested, buf, level);
            level--;
        }
    } else {
        level++;
        List<PolicyComponent> children = CastUtils.cast(((PolicyOperator)pc).getPolicyComponents(),
            PolicyComponent.class);
        nl(buf);
        for (PolicyComponent child : children) {
            printPolicyComponent(child, buf, level);
        }
        level--;
    }
}
 
Example #20
Source File: HTTPClientAssertionBuilder.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equal(PolicyComponent policyComponent) {
    if (policyComponent == this) {
        return true;
    }
    if (policyComponent.getType() != Constants.TYPE_ASSERTION
        || !(policyComponent instanceof Assertion)
        || !getName().equals(((Assertion)policyComponent).getName())) {
        return false;
    }
    JaxbAssertion<HTTPClientPolicy> other = JaxbAssertion.cast((Assertion)policyComponent);
    return new ClientPolicyCalculator().equals(this.getData(), other.getData());
}
 
Example #21
Source File: PolicyEngineImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a given list of assertions can potentially be supported by
 * interceptors or by an already installed assertor (a conduit or transport
 * that implements the Assertor interface).
 *
 * @param alternative the policy alternative
 * @param assertor the assertor
 * @return true iff the alternative can be supported
 */
public boolean supportsAlternative(Collection<? extends PolicyComponent> alternative,
                                   Assertor assertor,
                                   Message m) {
    PolicyInterceptorProviderRegistry pipr =
        bus.getExtension(PolicyInterceptorProviderRegistry.class);
    final boolean doLog = LOG.isLoggable(Level.FINE);
    for (PolicyComponent pc : alternative) {
        if (pc instanceof Assertion) {
            Assertion a = (Assertion)pc;
            if (!a.isOptional()) {
                if (null != assertor && assertor.canAssert(a.getName())) {
                    continue;
                }
                Set<PolicyInterceptorProvider> s = pipr.get(a.getName());
                if (s.isEmpty()) {
                    if (doLog) {
                        LOG.fine("Alternative " + a.getName() + " is not supported");
                    }
                    return false;
                }
                for (PolicyInterceptorProvider p : s) {
                    if (!p.configurationPresent(m, a)) {
                        if (doLog) {
                            LOG.fine("Alternative " + a.getName() + " is not supported");
                        }
                        return false;
                    }
                }
            }
        } else {
            return false;
        }
    }
    return true;
}
 
Example #22
Source File: AbstractSTSClient.java    From steady with Apache License 2.0 5 votes vote down vote up
protected void setPolicyInternal(Policy newPolicy) {
    this.policy = newPolicy;
    if (algorithmSuite == null) {
        Iterator<?> i = policy.getAlternatives();
        while (i.hasNext() && algorithmSuite == null) {
            List<PolicyComponent> p = CastUtils.cast((List<?>)i.next());
            for (PolicyComponent p2 : p) {
                if (p2 instanceof Binding) {
                    algorithmSuite = ((Binding)p2).getAlgorithmSuite();
                }
            }
        }
    }
}
 
Example #23
Source File: PolicyEngineImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Return the vocabulary of a policy component, i.e. the set of QNames of
 * the assertions used in the componente, duplicates removed.
 * @param pc the policy component
 * @param includeOptional flag indicating if optional assertions should be included
 * @return the vocabulary
 */
Set<QName> getVocabulary(PolicyComponent pc, boolean includeOptional) {
    Collection<Assertion> assertions = getAssertions(pc, includeOptional);
    Set<QName> vocabulary = new HashSet<>();
    for (Assertion a : assertions) {
        vocabulary.add(a.getName());
    }
    return vocabulary;
}
 
Example #24
Source File: PolicyEngineImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Return a collection of all assertions used in the given policy component,
 * optionally including optional assertions.
 * The policy need not be normalised, so any policy references will have to be resolved.
 * @param pc the policy component
 * @param includeOptional flag indicating if optional assertions should be included
 * @return the assertions
 */
Collection<Assertion> getAssertions(PolicyComponent pc, boolean includeOptional) {

    Collection<Assertion> assertions = new ArrayList<>();

    if (Constants.TYPE_ASSERTION == pc.getType()) {
        Assertion a = (Assertion)pc;
        if (includeOptional || !a.isOptional()) {
            assertions.add(a);
        }
    } else {
        addAssertions(pc, includeOptional, assertions);
    }
    return assertions;
}
 
Example #25
Source File: TokenWrapper.java    From steady with Apache License 2.0 5 votes vote down vote up
public PolicyComponent normalize() {
    if (token != null) {
        All all = new All();
        all.addPolicyComponent(token.normalize());
        all.addPolicyComponent(this);
        return all;
    }
    return this;
}
 
Example #26
Source File: AbstractSTSClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void setPolicyInternal(Policy newPolicy) {
    this.policy = newPolicy;
    if (algorithmSuite == null) {
        Iterator<?> i = policy.getAlternatives();
        while (i.hasNext() && algorithmSuite == null) {
            List<PolicyComponent> p = CastUtils.cast((List<?>)i.next());
            for (PolicyComponent p2 : p) {
                if (p2 instanceof AbstractBinding) {
                    algorithmSuite = ((AbstractBinding)p2).getAlgorithmSuite();
                }
            }
        }
    }
}
 
Example #27
Source File: SimpleBatchSTSClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void setPolicyInternal(Policy newPolicy) {
    this.policy = newPolicy;
    if (algorithmSuite == null) {
        Iterator<?> i = policy.getAlternatives();
        while (i.hasNext() && algorithmSuite == null) {
            List<PolicyComponent> p = CastUtils.cast((List<?>)i.next());
            for (PolicyComponent p2 : p) {
                if (p2 instanceof AbstractBinding) {
                    algorithmSuite = ((AbstractBinding)p2).getAlgorithmSuite();
                }
            }
        }
    }
}
 
Example #28
Source File: AbstractSTSClient.java    From steady with Apache License 2.0 5 votes vote down vote up
protected void setPolicyInternal(Policy newPolicy) {
    this.policy = newPolicy;
    if (algorithmSuite == null) {
        Iterator<?> i = policy.getAlternatives();
        while (i.hasNext() && algorithmSuite == null) {
            List<PolicyComponent> p = CastUtils.cast((List<?>)i.next());
            for (PolicyComponent p2 : p) {
                if (p2 instanceof Binding) {
                    algorithmSuite = ((Binding)p2).getAlgorithmSuite();
                }
            }
        }
    }
}
 
Example #29
Source File: AbstractSTSClient.java    From steady with Apache License 2.0 5 votes vote down vote up
protected void setPolicyInternal(Policy newPolicy) {
    this.policy = newPolicy;
    if (algorithmSuite == null) {
        Iterator<?> i = policy.getAlternatives();
        while (i.hasNext() && algorithmSuite == null) {
            List<PolicyComponent> p = CastUtils.cast((List<?>)i.next());
            for (PolicyComponent p2 : p) {
                if (p2 instanceof Binding) {
                    algorithmSuite = ((Binding)p2).getAlgorithmSuite();
                }
            }
        }
    }
}
 
Example #30
Source File: TokenWrapper.java    From steady with Apache License 2.0 5 votes vote down vote up
public PolicyComponent normalize() {
    if (token != null) {
        All all = new All();
        all.addPolicyComponent(token.normalize());
        all.addPolicyComponent(this);
        return all;
    }
    return this;
}