org.apache.cxf.phase.AbstractPhaseInterceptor Java Examples

The following examples show how to use org.apache.cxf.phase.AbstractPhaseInterceptor. 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: JettyDigestAuthTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private HTTPConduit setupClient(boolean async) throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    greeter = new SOAPService(wsdl, SERVICE_NAME).getPort(Greeter.class);
    BindingProvider bp = (BindingProvider)greeter;
    ClientProxy.getClient(greeter).getInInterceptors().add(new LoggingInInterceptor());
    ClientProxy.getClient(greeter).getOutInterceptors().add(new LoggingOutInterceptor());
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                               ADDRESS);
    HTTPConduit cond = (HTTPConduit)ClientProxy.getClient(greeter).getConduit();
    HTTPClientPolicy client = new HTTPClientPolicy();
    cond.setClient(client);
    if (async) {
        if (cond instanceof AsyncHTTPConduit) {
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials("ffang", "pswd");
            bp.getRequestContext().put(Credentials.class.getName(), creds);
            bp.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);
            client.setAutoRedirect(true);
        } else {
            fail("Not an async conduit");
        }
    } else {
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "ffang");
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
        cond.setAuthSupplier(new DigestAuthSupplier());
    }

    ClientProxy.getClient(greeter).getOutInterceptors()
        .add(new AbstractPhaseInterceptor<Message>(Phase.PRE_STREAM_ENDING) {

            public void handleMessage(Message message) throws Fault {
                Map<String, ?> headers = CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
                if (headers.containsKey("Proxy-Authorization")) {
                    throw new RuntimeException("Should not have Proxy-Authorization");
                }
            }
        });
    client.setAllowChunking(false);
    return cond;
}
 
Example #2
Source File: SequenceTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnknownSequence() throws Exception {
    init("org/apache/cxf/systest/ws/rm/rminterceptors.xml");

    class SequenceIdInterceptor extends AbstractPhaseInterceptor<Message> {
        SequenceIdInterceptor() {
            super(Phase.PRE_STREAM);
        }

        public void handleMessage(Message m) {
            RMProperties rmps = RMContextUtils.retrieveRMProperties(m, true);
            if (null != rmps && null != rmps.getSequence()) {
                rmps.getSequence().getIdentifier().setValue("UNKNOWN");
            }
        }
    }
    greeterBus.getOutInterceptors().add(new SequenceIdInterceptor());
    RMManager manager = greeterBus.getExtension(RMManager.class);
    manager.getConfiguration().setBaseRetransmissionInterval(Long.valueOf(2000));

    try {
        greeter.greetMe("one");
        fail("Expected fault.");
    } catch (WebServiceException ex) {
        SoapFault sf = (SoapFault)ex.getCause();
        assertEquals("Unexpected fault code.", Soap11.getInstance().getSender(), sf.getFaultCode());
        assertNull("Unexpected sub code.", sf.getSubCode());
        assertTrue("Unexpected reason.", sf.getReason().endsWith("is not a known Sequence identifier."));
    }

    // the third inbound message has a SequenceFault header
    MessageFlow mf = new MessageFlow(outRecorder.getOutboundMessages(),
        inRecorder.getInboundMessages(), Names200408.WSA_NAMESPACE_NAME, RM10Constants.NAMESPACE_URI);
    mf.verifySequenceFault(RM10Constants.UNKNOWN_SEQUENCE_FAULT_QNAME, false, 1);
    String[] expectedActions = new String[3];
    expectedActions = new String[] {RM10Constants.CREATE_SEQUENCE_RESPONSE_ACTION,
                                    RM10_GENERIC_FAULT_ACTION};
    mf.verifyActions(expectedActions, false);
}
 
Example #3
Source File: WSSCTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecureConversation() throws Exception {
    final wssec.wssc.IPingService port =
        svc.getPort(
            new QName("http://WSSec/wssc", test.prefix),
            wssec.wssc.IPingService.class
        );

    if (PORT2.equals(test.port) || STAX_PORT2.equals(test.port)) {
        ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                                                    "https://localhost:" + test.port + "/" + test.prefix);
    } else {
        ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                "http://localhost:" + test.port + "/" + test.prefix);
    }

    if (test.prefix.charAt(0) == '_') {
        //MS would like the _ versions to send a cancel
        ((BindingProvider)port).getRequestContext()
            .put(SecurityConstants.STS_TOKEN_DO_CANCEL, Boolean.TRUE);
    }

    if (test.streaming) {
        ((BindingProvider)port).getRequestContext().put(
            SecurityConstants.ENABLE_STREAMING_SECURITY, "true"
        );
        ((BindingProvider)port).getResponseContext().put(
            SecurityConstants.ENABLE_STREAMING_SECURITY, "true"
        );
    }
    if (test.clearAction) {
        AbstractPhaseInterceptor<Message> clearActionInterceptor
            = new AbstractPhaseInterceptor<Message>(Phase.POST_LOGICAL) {
                public void handleMessage(Message message) throws Fault {
                    STSClient client = STSUtils.getClient(message, "sct");
                    client.getOutInterceptors().add(this);
                    message.put(SecurityConstants.STS_CLIENT, client);
                    String s = (String)message.get(SoapBindingConstants.SOAP_ACTION);
                    if (s == null) {
                        s = SoapActionInInterceptor.getSoapAction(message);
                    }
                    if (s != null && s.contains("RST/SCT")) {
                        message.put(SoapBindingConstants.SOAP_ACTION, "");
                    }
                }
            };
        clearActionInterceptor.addBefore(SoapPreProtocolOutInterceptor.class.getName());
        ((Client)port).getOutInterceptors().add(clearActionInterceptor);
    }

    wssec.wssc.PingRequest params = new wssec.wssc.PingRequest();
    org.xmlsoap.ping.Ping ping = new org.xmlsoap.ping.Ping();
    ping.setOrigin("CXF");
    ping.setScenario("Scenario5");
    ping.setText("ping");
    params.setPing(ping);
    try {
        wssec.wssc.PingResponse output = port.ping(params);
        assertEquals(OUT, output.getPingResponse().getText());
    } catch (Exception ex) {
        throw new Exception("Error doing " + test.prefix, ex);
    }
    ((java.io.Closeable)port).close();
}
 
Example #4
Source File: SequenceTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void doTestTwowayNoDuplicates(String cfg) throws Exception {

        init(cfg);

        class MessageNumberInterceptor extends AbstractPhaseInterceptor<Message> {
            MessageNumberInterceptor() {
                super(Phase.PRE_STREAM);
            }

            public void handleMessage(Message m) {
                RMProperties rmps = RMContextUtils.retrieveRMProperties(m, true);
                if (null != rmps && null != rmps.getSequence()) {
                    rmps.getSequence().setMessageNumber(Long.valueOf(1));
                }
            }
        }
        greeterBus.getOutInterceptors().add(new MessageNumberInterceptor());
        RMManager manager = greeterBus.getExtension(RMManager.class);
        manager.getConfiguration().setBaseRetransmissionInterval(Long.valueOf(2000));

        greeter.greetMe("one");
        try {
            ((BindingProvider)greeter).getRequestContext().put("cxf.synchronous.timeout", 5000);
            String s = greeter.greetMe("two");
            fail("Expected timeout. Received response: " + s);
        } catch (WebServiceException ex) {
            assertTrue("Unexpected exception cause", ex.getCause() instanceof IOException);
            IOException ie = (IOException)ex.getCause();
            assertTrue("Unexpected IOException message", ie.getMessage().startsWith("Timed out"));
        }

        // wait for resend to occur

        awaitMessages(4, 3, 5000);

        MessageFlow mf = new MessageFlow(outRecorder.getOutboundMessages(),
            inRecorder.getInboundMessages(), Names200408.WSA_NAMESPACE_NAME, RM10Constants.NAMESPACE_URI);

        // Expected outbound:
        // CreateSequence
        // + two requests
        // + acknowledgement

        String[] expectedActions = new String[4];
        expectedActions[0] = RM10Constants.CREATE_SEQUENCE_ACTION;
        expectedActions[1] = GREETME_ACTION;
        expectedActions[2] = GREETME_ACTION;
        expectedActions[3] = RM10Constants.SEQUENCE_ACKNOWLEDGMENT_ACTION;
        mf.verifyActions(expectedActions, true);
        mf.verifyMessageNumbers(new String[] {null, "1", "1", null}, true);
        mf.verifyLastMessage(new boolean[expectedActions.length], true);
        mf.verifyAcknowledgements(new boolean[] {false, false, false, true}, true);

        // Expected inbound:
        // createSequenceResponse
        // + 1 response without acknowledgement
        // + 1 acknowledgement/last message

        mf.verifyMessages(3, false);
        expectedActions = new String[] {RM10Constants.CREATE_SEQUENCE_RESPONSE_ACTION,
                                        GREETME_RESPONSE_ACTION,
                                        RM10Constants.SEQUENCE_ACKNOWLEDGMENT_ACTION};
        mf.verifyActions(expectedActions, false);
        mf.verifyMessageNumbers(new String[] {null, "1", null}, false);
        mf.verifyAcknowledgements(new boolean[] {false, false, true}, false);
    }
 
Example #5
Source File: UndertowDigestAuthTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private HTTPConduit setupClient(boolean async) throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    greeter = new SOAPService(wsdl, SERVICE_NAME).getPort(Greeter.class);
    BindingProvider bp = (BindingProvider)greeter;
    ClientProxy.getClient(greeter).getInInterceptors().add(new LoggingInInterceptor());
    ClientProxy.getClient(greeter).getOutInterceptors().add(new LoggingOutInterceptor());
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                               ADDRESS);
    HTTPConduit cond = (HTTPConduit)ClientProxy.getClient(greeter).getConduit();
    HTTPClientPolicy client = new HTTPClientPolicy();
    client.setConnectionTimeout(600000);
    client.setReceiveTimeout(600000);
    cond.setClient(client);
    if (async) {
        if (cond instanceof AsyncHTTPConduit) {
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials("ffang", "pswd");
            bp.getRequestContext().put(Credentials.class.getName(), creds);
            bp.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);
            client.setAutoRedirect(true);
        } else {
            fail("Not an async conduit");
        }
    } else {
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "ffang");
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
        cond.setAuthSupplier(new DigestAuthSupplier());
    }

    ClientProxy.getClient(greeter).getOutInterceptors()
        .add(new AbstractPhaseInterceptor<Message>(Phase.PRE_STREAM_ENDING) {

            public void handleMessage(Message message) throws Fault {
                Map<String, ?> headers = CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
                if (headers.containsKey("Proxy-Authorization")) {
                    throw new RuntimeException("Should not have Proxy-Authorization");
                }
            }
        });
    client.setAllowChunking(false);
    return cond;
}