Search in sources :

Example 6 with PolicyException

use of org.apache.cxf.ws.policy.PolicyException in project cxf by apache.

the class AbstractTokenInterceptor method policyNotAsserted.

protected void policyNotAsserted(AbstractToken assertion, String reason, SoapMessage message) {
    if (assertion == null) {
        return;
    }
    AssertionInfoMap aim = message.get(AssertionInfoMap.class);
    Collection<AssertionInfo> ais = aim.get(assertion.getName());
    if (ais != null) {
        for (AssertionInfo ai : ais) {
            if (ai.getAssertion() == assertion) {
                ai.setNotAsserted(reason);
            }
        }
    }
    if (!assertion.isOptional()) {
        throw new PolicyException(new Message(reason, LOG));
    }
}
Also used : AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) PolicyException(org.apache.cxf.ws.policy.PolicyException) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap)

Example 7 with PolicyException

use of org.apache.cxf.ws.policy.PolicyException in project cxf by apache.

the class AbstractPolicySecurityTest method runOutInterceptorAndValidate.

protected Document runOutInterceptorAndValidate(SoapMessage msg, Policy policy, AssertionInfoMap aim, List<QName> assertedOutAssertions, List<QName> notAssertedOutAssertions) throws Exception {
    this.getOutInterceptor().handleMessage(msg);
    try {
        aim.checkEffectivePolicy(policy);
    } catch (PolicyException e) {
    // Expected but not relevant
    } finally {
        if (assertedOutAssertions != null) {
            for (QName assertionType : assertedOutAssertions) {
                Collection<AssertionInfo> ais = aim.get(assertionType);
                assertNotNull(ais);
                for (AssertionInfo ai : ais) {
                    checkAssertion(aim, assertionType, ai, true);
                }
            }
        }
        if (notAssertedOutAssertions != null) {
            for (QName assertionType : notAssertedOutAssertions) {
                Collection<AssertionInfo> ais = aim.get(assertionType);
                assertNotNull(ais);
                for (AssertionInfo ai : ais) {
                    checkAssertion(aim, assertionType, ai, false);
                }
            }
        }
    }
    return msg.getContent(SOAPMessage.class).getSOAPPart();
}
Also used : AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) PolicyException(org.apache.cxf.ws.policy.PolicyException) QName(javax.xml.namespace.QName) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 8 with PolicyException

use of org.apache.cxf.ws.policy.PolicyException in project cxf by apache.

the class HTTPClientPolicyTest method testUsingHTTPClientPolicies.

@Test
public void testUsingHTTPClientPolicies() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    bus = bf.createBus(POLICY_ENGINE_ENABLED_CFG);
    BusFactory.setDefaultBus(bus);
    LoggingInInterceptor in = new LoggingInInterceptor();
    bus.getInInterceptors().add(in);
    bus.getInFaultInterceptors().add(in);
    LoggingOutInterceptor out = new LoggingOutInterceptor();
    bus.getOutInterceptors().add(out);
    bus.getOutFaultInterceptors().add(out);
    // use a client wsdl with policies attached to endpoint, operation and message subjects
    URL url = HTTPClientPolicyTest.class.getResource("http_client_greeter.wsdl");
    BasicGreeterService gs = new BasicGreeterService(url, GREETER_QNAME);
    final Greeter greeter = gs.getGreeterPort();
    updateAddressPort(greeter, PORT);
    LOG.fine("Created greeter client.");
    try {
        greeter.sayHi();
        fail("Did not receive expected PolicyException.");
    } catch (WebServiceException wex) {
        PolicyException ex = (PolicyException) wex.getCause();
        assertEquals("INCOMPATIBLE_HTTPCLIENTPOLICY_ASSERTIONS", ex.getCode());
    }
    // greetMeOneWay - no message or operation policies
    greeter.greetMeOneWay("CXF");
    // greetMe - operation policy specifies receive timeout and should cause every
    // other invocation to fail
    assertEquals("CXF", greeter.greetMe("cxf"));
    try {
        greeter.greetMe("cxf");
        fail("Didn't get the exception");
    } catch (Exception ex) {
        // ex.printStackTrace();
        assertTrue(ex.getCause().getClass().getName(), ex.getCause() instanceof SocketTimeoutException);
    }
    try {
        greeter.pingMe();
        fail("Expected PingMeFault not thrown.");
    } catch (PingMeFault ex) {
        assertEquals(2, ex.getFaultInfo().getMajor());
        assertEquals(1, ex.getFaultInfo().getMinor());
    }
    ((Closeable) greeter).close();
}
Also used : PingMeFault(org.apache.cxf.greeter_control.PingMeFault) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) SocketTimeoutException(java.net.SocketTimeoutException) WebServiceException(javax.xml.ws.WebServiceException) PolicyException(org.apache.cxf.ws.policy.PolicyException) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) Greeter(org.apache.cxf.greeter_control.Greeter) Closeable(java.io.Closeable) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) BasicGreeterService(org.apache.cxf.greeter_control.BasicGreeterService) URL(java.net.URL) PolicyException(org.apache.cxf.ws.policy.PolicyException) WebServiceException(javax.xml.ws.WebServiceException) SocketTimeoutException(java.net.SocketTimeoutException) Test(org.junit.Test)

Example 9 with PolicyException

use of org.apache.cxf.ws.policy.PolicyException in project cxf by apache.

the class SecurityVerificationOutInterceptor method handleMessage.

/**
 * Checks if some security assertions are specified without binding assertion and cannot be fulfilled.
 * Throw PolicyException in this case
 *
 * @param message
 * @throws PolicyException if assertions are specified without binding
 */
public void handleMessage(SoapMessage message) throws Fault {
    if (MessageUtils.isRequestor(message)) {
        AssertionInfoMap aim = message.get(AssertionInfoMap.class);
        if (aim != null && PolicyUtils.getSecurityBinding(aim) == null) {
            AssertionInfo assertion = getSecuredPart(aim);
            if (assertion != null) {
                String error = String.format("%s assertion cannot be fulfilled without binding. " + "At least one binding assertion (%s, %s, %s) must be specified in policy.", assertion.getAssertion().getName(), SP12Constants.TRANSPORT_BINDING.getLocalPart(), SP12Constants.ASYMMETRIC_BINDING.getLocalPart(), SP12Constants.SYMMETRIC_BINDING.getLocalPart());
                assertion.setNotAsserted(error);
                LOG.severe(error);
                throw new PolicyException(assertion);
            }
        }
    }
}
Also used : AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) PolicyException(org.apache.cxf.ws.policy.PolicyException) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap)

Example 10 with PolicyException

use of org.apache.cxf.ws.policy.PolicyException in project cxf by apache.

the class ExternalAttachmentProviderTest method testReadDocumentUnknownDomainExpression.

@Test
public void testReadDocumentUnknownDomainExpression() throws MalformedURLException {
    Bus bus = control.createMock(Bus.class);
    DomainExpressionBuilderRegistry debr = control.createMock(DomainExpressionBuilderRegistry.class);
    EasyMock.expect(bus.getExtension(DomainExpressionBuilderRegistry.class)).andReturn(debr);
    EasyMock.expect(debr.build(EasyMock.isA(Element.class))).andThrow(new PolicyException(new Exception()));
    URL url = ExternalAttachmentProviderTest.class.getResource("resources/attachments3.xml");
    String uri = url.toExternalForm();
    control.replay();
    ExternalAttachmentProvider eap = new ExternalAttachmentProvider(bus);
    eap.setLocation(new UrlResource(uri));
    try {
        eap.readDocument();
        fail("Expected PolicyException not thrown.");
    } catch (PolicyException ex) {
    // expected
    }
    control.verify();
}
Also used : Bus(org.apache.cxf.Bus) PolicyException(org.apache.cxf.ws.policy.PolicyException) UrlResource(org.springframework.core.io.UrlResource) Element(org.w3c.dom.Element) PolicyException(org.apache.cxf.ws.policy.PolicyException) MalformedURLException(java.net.MalformedURLException) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) Test(org.junit.Test)

Aggregations

PolicyException (org.apache.cxf.ws.policy.PolicyException)21 AssertionInfo (org.apache.cxf.ws.policy.AssertionInfo)8 Test (org.junit.Test)8 AssertionInfoMap (org.apache.cxf.ws.policy.AssertionInfoMap)7 Message (org.apache.cxf.common.i18n.Message)6 Policy (org.apache.neethi.Policy)6 QName (javax.xml.namespace.QName)5 Element (org.w3c.dom.Element)4 URL (java.net.URL)3 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)3 Document (org.w3c.dom.Document)3 FileNotFoundException (java.io.FileNotFoundException)2 JAXBException (javax.xml.bind.JAXBException)2 UrlResource (org.springframework.core.io.UrlResource)2 Closeable (java.io.Closeable)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 JAXBElement (javax.xml.bind.JAXBElement)1 SOAPMessage (javax.xml.soap.SOAPMessage)1