Search in sources :

Example 46 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class DemoInterceptor method printInterceptorChain.

/**
 * Prints out the interceptor chain in a format that is easy to read. It
 * also filters out instances of the DemoInterceptor so you can see what the
 * chain would look like in a normal invokation.
 *
 * @param chain
 */
public void printInterceptorChain(InterceptorChain chain) {
    Iterator<Interceptor<? extends Message>> it = chain.iterator();
    String phase = "";
    StringBuilder builder = null;
    while (it.hasNext()) {
        Interceptor<? extends Message> interceptor = it.next();
        if (interceptor instanceof DemoInterceptor) {
            continue;
        }
        if (interceptor instanceof PhaseInterceptor) {
            PhaseInterceptor pi = (PhaseInterceptor) interceptor;
            if (!phase.equals(pi.getPhase())) {
                if (builder != null) {
                    System.out.println(builder.toString());
                } else {
                    builder = new StringBuilder(100);
                }
                builder.setLength(0);
                builder.append("             ");
                builder.append(pi.getPhase());
                builder.append(": ");
                phase = pi.getPhase();
            }
            String id = pi.getId();
            int idx = id.lastIndexOf('.');
            if (idx != -1) {
                id = id.substring(idx + 1);
            }
            builder.append(id);
            builder.append(' ');
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor)

Example 47 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class FlowIdProducerIn method handleMessage.

/* (non-Javadoc)
     * @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
     */
public void handleMessage(T message) throws Fault {
    String flowId = FlowIdHelper.getFlowId(message);
    if (flowId == null) {
        flowId = FlowIdProtocolHeaderCodec.readFlowId(message);
    }
    if (flowId == null) {
        flowId = FlowIdSoapCodec.readFlowId(message);
    }
    if (flowId == null) {
        Exchange ex = message.getExchange();
        if (null != ex) {
            Message reqMsg = ex.getOutMessage();
            if (null != reqMsg) {
                flowId = FlowIdHelper.getFlowId(reqMsg);
                if (null != flowId) {
                    LOG.fine("Using FlowId '" + flowId + "' from exchange.");
                }
            }
        }
    }
    if (flowId != null) {
        LOG.fine("FlowId '" + flowId + "' found in incoming message.");
    } else {
        flowId = ContextUtils.generateUUID();
        LOG.fine("No flowId found in incoming message! Generate new flowId " + flowId);
    }
    FlowIdHelper.setFlowId(message, flowId);
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message)

Example 48 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class FlowIdProducerOut method handleResponseOut.

/**
 * Handling out responce.
 *
 * @param message
 *            the message
 * @throws Fault
 *             the fault
 */
protected void handleResponseOut(T message) throws Fault {
    Message reqMsg = message.getExchange().getInMessage();
    if (reqMsg == null) {
        LOG.warning("InMessage is null!");
        return;
    }
    // No flowId for oneway message
    Exchange ex = reqMsg.getExchange();
    if (ex.isOneWay()) {
        return;
    }
    String reqFid = FlowIdHelper.getFlowId(reqMsg);
    // if some interceptor throws fault before FlowIdProducerIn fired
    if (reqFid == null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Some interceptor throws fault.Setting FlowId in response.");
        }
        reqFid = FlowIdProtocolHeaderCodec.readFlowId(message);
    }
    // write IN message to SAM repo in case fault
    if (reqFid == null) {
        Message inMsg = ex.getInMessage();
        reqFid = FlowIdProtocolHeaderCodec.readFlowId(inMsg);
        if (null != reqFid) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("FlowId '" + reqFid + "' found in message of fault incoming exchange.");
                LOG.fine("Calling EventProducerInterceptor to log IN message");
            }
            handleINEvent(ex, reqFid);
        }
    }
    if (reqFid == null) {
        reqFid = FlowIdSoapCodec.readFlowId(message);
    }
    if (reqFid != null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("FlowId '" + reqFid + "' found in incoming message.");
        }
    } else {
        reqFid = ContextUtils.generateUUID();
        // write IN message to SAM repo in case fault
        if (null != ex.getOutFaultMessage()) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("FlowId '" + reqFid + "' generated for fault message.");
                LOG.fine("Calling EventProducerInterceptor to log IN message");
            }
            handleINEvent(ex, reqFid);
        }
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("No flowId found in incoming message! Generate new flowId " + reqFid);
        }
    }
    FlowIdHelper.setFlowId(message, reqFid);
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message)

Example 49 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class MessageToEventMapperTest method testMapEventRest.

@Test
public void testMapEventRest() throws IOException, EndpointException {
    QName portType = new QName("PORT_TYPE");
    EndpointInfo info = EasyMock.createMock(EndpointInfo.class);
    EasyMock.expect(info.getName()).andReturn(portType).anyTimes();
    EasyMock.expect(info.getAddress()).andReturn(null).anyTimes();
    EasyMock.replay(info);
    Endpoint endpoint = EasyMock.createMock(Endpoint.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(info).anyTimes();
    Map<String, String> samProperties = new HashMap<String, String>();
    EasyMock.expect(endpoint.get(EventFeature.SAM_PROPERTIES)).andReturn(samProperties).anyTimes();
    EasyMock.replay(endpoint);
    Message outMessage = EasyMock.createMock(Message.class);
    EasyMock.expect(outMessage.containsKey(Message.HTTP_REQUEST_METHOD)).andReturn(true).anyTimes();
    EasyMock.expect(outMessage.get(Message.HTTP_REQUEST_METHOD)).andReturn("POST").anyTimes();
    EasyMock.expect(outMessage.containsKey(Message.REQUEST_URI)).andReturn(true).anyTimes();
    EasyMock.expect(outMessage.get(Message.REQUEST_URI)).andReturn("REQUEST_URI").anyTimes();
    EasyMock.expect(outMessage.containsKey(Message.BASE_PATH)).andReturn(true).anyTimes();
    EasyMock.expect(outMessage.get(Message.BASE_PATH)).andReturn("REQUEST_URI").anyTimes();
    EasyMock.replay(outMessage);
    Exchange e = EasyMock.createMock(Exchange.class);
    EasyMock.expect(e.getOutMessage()).andReturn(outMessage).anyTimes();
    EasyMock.expect(e.getOutFaultMessage()).andReturn(null).anyTimes();
    EasyMock.expect(e.getInFaultMessage()).andReturn(null).anyTimes();
    EasyMock.expect(e.getBinding()).andReturn(null).anyTimes();
    EasyMock.expect(e.getEndpoint()).andReturn(endpoint).anyTimes();
    EasyMock.expect(e.get("org.apache.cxf.resource.operation.name")).andReturn("operationName").anyTimes();
    EasyMock.replay(e);
    AuthorizationPolicy authPolicy = EasyMock.createMock(AuthorizationPolicy.class);
    EasyMock.expect(authPolicy.getUserName()).andReturn("USERNAME").anyTimes();
    EasyMock.replay(authPolicy);
    CachedOutputStream cos = new CachedOutputStream();
    cos.write(1);
    cos.write(2);
    cos.write(3);
    Message message = EasyMock.createNiceMock(Message.class);
    EasyMock.expect(message.entrySet()).andReturn(null).anyTimes();
    EasyMock.expect(message.get(Message.REQUESTOR_ROLE)).andReturn(true).anyTimes();
    EasyMock.expect(message.getExchange()).andReturn(e).anyTimes();
    EasyMock.expect(message.get(Message.ENCODING)).andReturn("UTF-8").anyTimes();
    EasyMock.expect(message.getContent(CachedOutputStream.class)).andReturn(cos).anyTimes();
    EasyMock.expect(message.get("FlowId")).andReturn(FlowID).anyTimes();
    EasyMock.expect(message.get(CorrelationIdHelper.CORRELATION_ID_KEY)).andReturn("CORRELATION_ID_KEY").anyTimes();
    EasyMock.expect(message.get(Message.ACCEPT_CONTENT_TYPE)).andReturn("XML").anyTimes();
    EasyMock.expect(message.get(Message.CONTENT_TYPE)).andReturn("XML").anyTimes();
    EasyMock.expect(message.get(Message.RESPONSE_CODE)).andReturn(0).anyTimes();
    EasyMock.expect(message.get(SecurityContext.class)).andReturn(null).anyTimes();
    EasyMock.expect(message.get(AuthorizationPolicy.class)).andReturn(authPolicy).anyTimes();
    EasyMock.expect(message.get(CustomInfo.class)).andReturn(EasyMock.createMock(CustomInfo.class)).anyTimes();
    EasyMock.replay(message);
    MessageToEventMapper mapper = new MessageToEventMapper();
    mapper.setMaxContentLength(2);
    Event event = mapper.mapToEvent(message);
    Assert.assertEquals(EventTypeEnum.RESP_IN, event.getEventType());
    Assert.assertEquals("PORT_TYPE", event.getMessageInfo().getPortType());
    Assert.assertEquals("POST[/]", event.getMessageInfo().getOperationName());
    Assert.assertEquals("http://cxf.apache.org/transports/http", event.getMessageInfo().getTransportType());
    Assert.assertEquals(FlowID, event.getMessageInfo().getFlowId());
    Assert.assertNull(event.getMessageInfo().getMessageId());
}
Also used : Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) MessageToEventMapper(org.talend.esb.sam.agent.eventproducer.MessageToEventMapper) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Endpoint(org.apache.cxf.endpoint.Endpoint) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) CustomInfo(org.talend.esb.sam.agent.message.CustomInfo) Event(org.talend.esb.sam.common.event.Event) Test(org.junit.Test)

Example 50 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class MessageToEventMapperTest method testMaxContentLength.

@Test
public void testMaxContentLength() throws IOException, EndpointException {
    Message message = getTestMessage();
    MessageToEventMapper mapper = new MessageToEventMapper();
    mapper.setMaxContentLength(MAXCONTENTLENGTH);
    Event event = mapper.mapToEvent(message);
    // System.out.println(event.getContent());
    Assert.assertEquals(MAXCONTENTLENGTH, event.getContent().length());
    Assert.assertEquals("<cut><![CDATA[" + TESTCONTENT.substring(0, MAXCONTENTLENGTH - 23) + "]]></cut>", event.getContent());
    Assert.assertTrue(event.isContentCut());
}
Also used : Message(org.apache.cxf.message.Message) Event(org.talend.esb.sam.common.event.Event) MessageToEventMapper(org.talend.esb.sam.agent.eventproducer.MessageToEventMapper) Test(org.junit.Test)

Aggregations

Message (org.apache.cxf.message.Message)1002 Test (org.junit.Test)507 MessageImpl (org.apache.cxf.message.MessageImpl)291 Exchange (org.apache.cxf.message.Exchange)199 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)169 Endpoint (org.apache.cxf.endpoint.Endpoint)91 Interceptor (org.apache.cxf.interceptor.Interceptor)87 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)85 ArrayList (java.util.ArrayList)83 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)76 List (java.util.List)75 IOException (java.io.IOException)73 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)73 Method (java.lang.reflect.Method)69 Bus (org.apache.cxf.Bus)69 QName (javax.xml.namespace.QName)62 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)55 HashMap (java.util.HashMap)53 Fault (org.apache.cxf.interceptor.Fault)51 ByteArrayInputStream (java.io.ByteArrayInputStream)49