Search in sources :

Example 11 with ExchangeImpl

use of org.apache.cxf.message.ExchangeImpl in project ddf by codice.

the class MetricsOutInterceptorTest method testHandleMessageWithOneWayClientMessage.

/**
     * Test method for
     * {@link ddf.metrics.interceptor.MetricsOutInterceptor#handleMessage(org.apache.cxf.message.Message)}
     * .
     *
     * @throws InterruptedException
     */
@Test
public void testHandleMessageWithOneWayClientMessage() {
    // Setup
    MetricsOutInterceptor outInterceptor = new MetricsOutInterceptor();
    Message mockMessage = mock(Message.class);
    Exchange ex = new ExchangeImpl();
    Bus mockBus = mock(Bus.class);
    InterceptorChain mockIc = mock(InterceptorChain.class);
    ex.put(Bus.class, mockBus);
    ex.setOneWay(true);
    when(mockBus.getId()).thenReturn("bus_id");
    when(mockMessage.getExchange()).thenReturn(ex);
    when(mockMessage.get(Message.PARTIAL_RESPONSE_MESSAGE)).thenReturn("false");
    when(mockMessage.get(Message.REQUESTOR_ROLE)).thenReturn(true);
    when(mockMessage.getInterceptorChain()).thenReturn(mockIc);
    // Perform test
    outInterceptor.handleMessage(mockMessage);
    // validate that LatencyTimeRecorder.beginHandling was called once
    verify(mockMessage, times(1)).getInterceptorChain();
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) Message(org.apache.cxf.message.Message) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 12 with ExchangeImpl

use of org.apache.cxf.message.ExchangeImpl in project ddf by codice.

the class MetricsOutInterceptorTest method testHandleMessageWithTwoWayClientMessageWithoutLatencyTimeRecorder.

/**
     * Test method for
     * {@link ddf.metrics.interceptor.MetricsOutInterceptor#handleMessage(org.apache.cxf.message.Message)}
     * .
     *
     * @throws InterruptedException
     */
@Test
public void testHandleMessageWithTwoWayClientMessageWithoutLatencyTimeRecorder() {
    // Setup
    MetricsOutInterceptor outInterceptor = new MetricsOutInterceptor();
    Message mockMessage = mock(Message.class);
    Exchange ex = new ExchangeImpl();
    Bus mockBus = mock(Bus.class);
    ex.put(Bus.class, mockBus);
    when(mockBus.getId()).thenReturn("bus_id");
    when(mockMessage.getExchange()).thenReturn(ex);
    when(mockMessage.get(Message.PARTIAL_RESPONSE_MESSAGE)).thenReturn("false");
    when(mockMessage.get(Message.REQUESTOR_ROLE)).thenReturn(true);
    // Perform test
    outInterceptor.handleMessage(mockMessage);
    // validate that an instance of LatencyTimeRecorder was put onto the
    // exchange
    assertThat(ex.get(LatencyTimeRecorder.class), instanceOf(LatencyTimeRecorder.class));
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) Message(org.apache.cxf.message.Message) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 13 with ExchangeImpl

use of org.apache.cxf.message.ExchangeImpl in project ddf by codice.

the class PaosInInterceptorTest method handleMessagePaosResponseBasicGood.

@Test
public void handleMessagePaosResponseBasicGood() throws IOException {
    Message message = new MessageImpl();
    message.setContent(InputStream.class, PaosInInterceptorTest.class.getClassLoader().getResource("ecprequest.xml").openStream());
    message.put(Message.CONTENT_TYPE, "application/vnd.paos+xml");
    Message outMessage = new MessageImpl();
    HashMap<String, List> protocolHeaders = new HashMap<>();
    outMessage.put(Message.PROTOCOL_HEADERS, protocolHeaders);
    outMessage.put(Message.HTTP_REQUEST_METHOD, "GET");
    protocolHeaders.put("Authorization", Collections.singletonList("BASIC dGVzdDp0ZXN0"));
    ExchangeImpl exchange = new ExchangeImpl();
    exchange.setOutMessage(outMessage);
    message.setExchange(exchange);
    PaosInInterceptor paosInInterceptor = new PaosInInterceptor(Phase.RECEIVE) {

        HttpResponseWrapper getHttpResponse(String responseConsumerURL, String soapResponse, Message message) throws IOException {
            HttpResponseWrapper httpResponseWrapper = new HttpResponseWrapper();
            if (responseConsumerURL.equals("https://sp.example.org/PAOSConsumer")) {
                httpResponseWrapper.statusCode = 200;
                httpResponseWrapper.content = new ByteArrayInputStream("actual content".getBytes());
            } else if (responseConsumerURL.equals("https://idp.example.org/saml2/sso")) {
                httpResponseWrapper.statusCode = 200;
                httpResponseWrapper.content = PaosInInterceptorTest.class.getClassLoader().getResource("idpresponse.xml").openStream();
            }
            return httpResponseWrapper;
        }
    };
    paosInInterceptor.handleMessage(message);
    assertThat(IOUtils.toString(message.getContent(InputStream.class)), is("actual content"));
}
Also used : Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 14 with ExchangeImpl

use of org.apache.cxf.message.ExchangeImpl in project camel by apache.

the class CxfProducer method process.

// As the cxf client async and sync api is implement different,
// so we don't delegate the sync process call to the async process 
public boolean process(Exchange camelExchange, AsyncCallback callback) {
    LOG.trace("Process exchange: {} in an async way.", camelExchange);
    try {
        // create CXF exchange
        ExchangeImpl cxfExchange = new ExchangeImpl();
        // set the Bus on the exchange in case the CXF interceptor need to access it from exchange
        cxfExchange.put(Bus.class, endpoint.getBus());
        // prepare binding operation info
        BindingOperationInfo boi = prepareBindingOperation(camelExchange, cxfExchange);
        Map<String, Object> invocationContext = new HashMap<String, Object>();
        Map<String, Object> responseContext = new HashMap<String, Object>();
        invocationContext.put(Client.RESPONSE_CONTEXT, responseContext);
        invocationContext.put(Client.REQUEST_CONTEXT, prepareRequest(camelExchange, cxfExchange));
        CxfClientCallback cxfClientCallback = new CxfClientCallback(callback, camelExchange, cxfExchange, boi, endpoint);
        // send the CXF async request
        client.invoke(cxfClientCallback, boi, getParams(endpoint, camelExchange), invocationContext, cxfExchange);
        if (boi.getOperationInfo().isOneWay()) {
            callback.done(false);
        }
    } catch (Throwable ex) {
        // error occurred before we had a chance to go async
        // so set exception and invoke callback true
        camelExchange.setException(ex);
        callback.done(true);
        return true;
    }
    return false;
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) HashMap(java.util.HashMap) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 15 with ExchangeImpl

use of org.apache.cxf.message.ExchangeImpl in project camel by apache.

the class CxfProducer method process.

/**
     * This processor binds Camel exchange to a CXF exchange and
     * invokes the CXF client.
     */
public void process(Exchange camelExchange) throws Exception {
    LOG.trace("Process exchange: {} in sync way.", camelExchange);
    // create CXF exchange
    ExchangeImpl cxfExchange = new ExchangeImpl();
    // set the Bus on the exchange in case the CXF interceptor need to access it from exchange
    cxfExchange.put(Bus.class, endpoint.getBus());
    // prepare binding operation info
    BindingOperationInfo boi = prepareBindingOperation(camelExchange, cxfExchange);
    Map<String, Object> invocationContext = new HashMap<String, Object>();
    Map<String, Object> responseContext = new HashMap<String, Object>();
    invocationContext.put(Client.RESPONSE_CONTEXT, responseContext);
    invocationContext.put(Client.REQUEST_CONTEXT, prepareRequest(camelExchange, cxfExchange));
    try {
        // send the CXF request
        client.invoke(boi, getParams(endpoint, camelExchange), invocationContext, cxfExchange);
    } catch (Exception exception) {
        camelExchange.setException(exception);
    } finally {
        // add cookies to the cookie store
        if (endpoint.getCookieHandler() != null) {
            try {
                Map<String, List<String>> cxfHeaders = CastUtils.cast((Map<?, ?>) cxfExchange.getInMessage().get(Message.PROTOCOL_HEADERS));
                endpoint.getCookieHandler().storeCookies(camelExchange, endpoint.getRequestUri(camelExchange), cxfHeaders);
            } catch (IOException e) {
                LOG.error("Cannot store cookies", e);
            }
        }
        // bind the CXF response to Camel exchange
        if (!boi.getOperationInfo().isOneWay()) {
            endpoint.getCxfBinding().populateExchangeFromCxfResponse(camelExchange, cxfExchange, responseContext);
        }
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) HashMap(java.util.HashMap) List(java.util.List) IOException(java.io.IOException) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException)

Aggregations

ExchangeImpl (org.apache.cxf.message.ExchangeImpl)22 Message (org.apache.cxf.message.Message)17 Test (org.junit.Test)16 Exchange (org.apache.cxf.message.Exchange)14 Bus (org.apache.cxf.Bus)9 MessageImpl (org.apache.cxf.message.MessageImpl)7 HashMap (java.util.HashMap)6 List (java.util.List)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 DefaultExchange (org.apache.camel.impl.DefaultExchange)3 MessageObserver (org.apache.cxf.transport.MessageObserver)3 WebServiceException (javax.xml.ws.WebServiceException)2 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)2 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)2 Conduit (org.apache.cxf.transport.Conduit)2 InputStream (java.io.InputStream)1