Search in sources :

Example 1 with Originator

use of org.talend.esb.sam.common.event.Originator in project tesb-rt-se by Talend.

the class MessageToEventMapper method mapToEvent.

/**
 * Map to event.
 *
 * @param message
 *            the message
 * @return the event
 */
public Event mapToEvent(Message message) {
    Event event = new Event();
    MessageInfo messageInfo = new MessageInfo();
    Originator originator = new Originator();
    boolean isRestMessage = isRestMessage(message);
    event.setMessageInfo(messageInfo);
    event.setOriginator(originator);
    String content = getPayload(message);
    event.setContent(content);
    handleContentLength(event);
    event.setEventType(null);
    Date date = new Date();
    event.setTimestamp(date);
    // if (isRestMessage) {
    // String queryString = (String) message.get(Message.QUERY_STRING);
    // if (queryString == null && message.getExchange().getInMessage() != null) {
    // queryString = (String) message.getExchange().getInMessage().get(Message.QUERY_STRING);
    // }
    // if (queryString != null && queryString.contains("_wadl")) {
    // return null;
    // }
    // }
    messageInfo.setFlowId(FlowIdHelper.getFlowId(message));
    if (!isRestMessage) {
        messageInfo.setMessageId(getMessageId(message));
        ServiceInfo serviceInfo = message.getExchange().getBinding().getBindingInfo().getService();
        if (null != serviceInfo) {
            String portTypeName = serviceInfo.getInterface().getName().toString();
            messageInfo.setPortType(portTypeName);
            messageInfo.setOperationName(getOperationName(message));
        }
        SoapBinding soapBinding = (SoapBinding) message.getExchange().getBinding();
        if (soapBinding.getBindingInfo() instanceof SoapBindingInfo) {
            SoapBindingInfo soapBindingInfo = (SoapBindingInfo) soapBinding.getBindingInfo();
            messageInfo.setTransportType(soapBindingInfo.getTransportURI());
        }
    } else {
        messageInfo.setTransportType("http://cxf.apache.org/transports/http");
        messageInfo.setPortType(message.getExchange().getEndpoint().getEndpointInfo().getName().toString());
        String opName = getRestOperationName(message);
        messageInfo.setOperationName(opName);
    }
    if (messageInfo.getTransportType() == null) {
        messageInfo.setTransportType("Unknown transport type");
    }
    // add custom properties from CXF properties
    if (null != message.getExchange().getEndpoint().get(EventFeature.SAM_PROPERTIES)) {
        Map<String, String> customProp = (Map<String, String>) message.getExchange().getEndpoint().get(EventFeature.SAM_PROPERTIES);
        event.getCustomInfo().putAll(customProp);
    }
    String addr = message.getExchange().getEndpoint().getEndpointInfo().getAddress();
    if (null != addr) {
        event.getCustomInfo().put("address", addr);
    }
    String correlationId = CorrelationIdHelper.getCorrelationId(message);
    if (null != correlationId) {
        event.getCustomInfo().put("CorrelationID", correlationId);
    }
    try {
        InetAddress inetAddress = InetAddress.getLocalHost();
        originator.setIp(inetAddress.getHostAddress());
        originator.setHostname(inetAddress.getHostName());
    } catch (UnknownHostException e) {
        originator.setHostname("Unknown hostname");
        originator.setIp("Unknown ip address");
    }
    originator.setProcessId(Converter.getPID());
    if (isRestMessage) {
        // String queryString = (String) message.get(Message.QUERY_STRING);
        // if (null == queryString && null != message.getExchange().getInMessage()) {
        // queryString = (String) message.getExchange().getInMessage().get(Message.QUERY_STRING);
        // }
        // if (null != queryString) {
        // event.getCustomInfo().put("Query String", queryString);
        // }
        String accept = (String) message.get(Message.ACCEPT_CONTENT_TYPE);
        if (null != accept) {
            event.getCustomInfo().put("Accept Type", accept);
        }
        // String httpMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
        // if (null != httpMethod) {
        // event.getCustomInfo().put("HTTP Method", httpMethod);
        // }
        String contentType = (String) message.get(Message.CONTENT_TYPE);
        if (null != contentType) {
            event.getCustomInfo().put("Content Type", contentType);
        }
        Integer responseCode = (Integer) message.get(Message.RESPONSE_CODE);
        if (null != responseCode) {
            event.getCustomInfo().put("Response Code", responseCode.toString());
        }
    }
    SecurityContext sc = message.get(SecurityContext.class);
    if (sc != null && sc.getUserPrincipal() != null) {
        originator.setPrincipal(sc.getUserPrincipal().getName());
    }
    if (originator.getPrincipal() == null) {
        AuthorizationPolicy authPolicy = message.get(AuthorizationPolicy.class);
        if (authPolicy != null) {
            originator.setPrincipal(authPolicy.getUserName());
        }
    }
    EventTypeEnum eventType = getEventType(message);
    event.setEventType(eventType);
    CustomInfo customInfo = CustomInfo.getOrCreateCustomInfo(message);
    // System.out.println("custom props: " + customInfo);
    event.getCustomInfo().putAll(customInfo);
    return event;
}
Also used : UnknownHostException(java.net.UnknownHostException) Date(java.util.Date) SoapBinding(org.apache.cxf.binding.soap.SoapBinding) MessageInfo(org.talend.esb.sam.common.event.MessageInfo) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Originator(org.talend.esb.sam.common.event.Originator) EventTypeEnum(org.talend.esb.sam.common.event.EventTypeEnum) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo) SecurityContext(org.apache.cxf.security.SecurityContext) Event(org.talend.esb.sam.common.event.Event) CustomInfo(org.talend.esb.sam.agent.message.CustomInfo) Map(java.util.Map) InetAddress(java.net.InetAddress)

Example 2 with Originator

use of org.talend.esb.sam.common.event.Originator in project tesb-rt-se by Talend.

the class AbstractListenerImpl method createEvent.

/**
 * Creates the event for endpoint with specific type.
 *
 * @param endpoint the endpoint
 * @param type the type
 * @return the event
 */
private Event createEvent(Endpoint endpoint, EventTypeEnum type) {
    Event event = new Event();
    MessageInfo messageInfo = new MessageInfo();
    Originator originator = new Originator();
    event.setMessageInfo(messageInfo);
    event.setOriginator(originator);
    Date date = new Date();
    event.setTimestamp(date);
    event.setEventType(type);
    messageInfo.setPortType(endpoint.getBinding().getBindingInfo().getService().getInterface().getName().toString());
    String transportType = null;
    if (endpoint.getBinding() instanceof SoapBinding) {
        SoapBinding soapBinding = (SoapBinding) endpoint.getBinding();
        if (soapBinding.getBindingInfo() instanceof SoapBindingInfo) {
            SoapBindingInfo soapBindingInfo = (SoapBindingInfo) soapBinding.getBindingInfo();
            transportType = soapBindingInfo.getTransportURI();
        }
    }
    messageInfo.setTransportType((transportType != null) ? transportType : "Unknown transport type");
    originator.setProcessId(Converter.getPID());
    try {
        InetAddress inetAddress = InetAddress.getLocalHost();
        originator.setIp(inetAddress.getHostAddress());
        originator.setHostname(inetAddress.getHostName());
    } catch (UnknownHostException e) {
        originator.setHostname("Unknown hostname");
        originator.setIp("Unknown ip address");
    }
    String address = endpoint.getEndpointInfo().getAddress();
    event.getCustomInfo().put("address", address);
    return event;
}
Also used : UnknownHostException(java.net.UnknownHostException) Originator(org.talend.esb.sam.common.event.Originator) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo) Event(org.talend.esb.sam.common.event.Event) InetAddress(java.net.InetAddress) Date(java.util.Date) SoapBinding(org.apache.cxf.binding.soap.SoapBinding) MessageInfo(org.talend.esb.sam.common.event.MessageInfo)

Example 3 with Originator

use of org.talend.esb.sam.common.event.Originator in project tesb-rt-se by Talend.

the class EventMapperTest method testEventMapper2.

@Test
public void testEventMapper2() throws IOException {
    Event event = new Event();
    event.setContent("testContent");
    MessageInfo messageInfo = EasyMock.createMock(MessageInfo.class);
    EasyMock.expect(messageInfo.getMessageId()).andReturn("MessageId").anyTimes();
    EasyMock.expect(messageInfo.getFlowId()).andReturn("FlowId").anyTimes();
    EasyMock.expect(messageInfo.getPortType()).andReturn("PortType").anyTimes();
    EasyMock.expect(messageInfo.getOperationName()).andReturn("OperationName").anyTimes();
    EasyMock.expect(messageInfo.getTransportType()).andReturn("TransportType").anyTimes();
    EasyMock.replay(messageInfo);
    event.setMessageInfo(messageInfo);
    Originator originator = EasyMock.createMock(Originator.class);
    EasyMock.expect(originator.getProcessId()).andReturn("ProcessId").anyTimes();
    EasyMock.expect(originator.getIp()).andReturn("Ip").anyTimes();
    EasyMock.expect(originator.getHostname()).andReturn("Hostname").anyTimes();
    EasyMock.expect(originator.getCustomId()).andReturn("CustomId").anyTimes();
    EasyMock.expect(originator.getPrincipal()).andReturn("Principal").anyTimes();
    EasyMock.replay(originator);
    event.setOriginator(originator);
    event.getCustomInfo().put("testKey", "testValue");
    EventType eventOut = EventMapper.map(event);
    DataHandler dh = eventOut.getContent();
    String outContent = getContent(dh);
    Assert.assertEquals(event.getContent(), outContent);
    Assert.assertEquals(eventOut.getOriginator().getProcessId(), event.getOriginator().getProcessId());
    Assert.assertEquals(eventOut.getOriginator().getIp(), event.getOriginator().getIp());
    Assert.assertEquals(eventOut.getOriginator().getHostname(), event.getOriginator().getHostname());
    Assert.assertEquals(eventOut.getOriginator().getCustomId(), event.getOriginator().getCustomId());
    Assert.assertEquals(eventOut.getOriginator().getPrincipal(), event.getOriginator().getPrincipal());
    Assert.assertEquals(eventOut.getMessageInfo().getMessageId(), event.getMessageInfo().getMessageId());
    Assert.assertEquals(eventOut.getMessageInfo().getFlowId(), event.getMessageInfo().getFlowId());
    Assert.assertSame(eventOut.getMessageInfo().getPorttype().getLocalPart(), event.getMessageInfo().getPortType());
    Assert.assertEquals(eventOut.getMessageInfo().getOperationName(), event.getMessageInfo().getOperationName());
    Assert.assertEquals(eventOut.getMessageInfo().getTransport(), event.getMessageInfo().getTransportType());
    Assert.assertEquals(eventOut.getCustomInfo().getItem().get(0).getValue(), "testValue");
    Assert.assertEquals(eventOut.getCustomInfo().getItem().get(0).getKey(), "testKey");
}
Also used : Originator(org.talend.esb.sam.common.event.Originator) EventType(org.talend.esb.sam._2011._03.common.EventType) Event(org.talend.esb.sam.common.event.Event) DataHandler(javax.activation.DataHandler) MessageInfo(org.talend.esb.sam.common.event.MessageInfo) Test(org.junit.Test)

Example 4 with Originator

use of org.talend.esb.sam.common.event.Originator in project tesb-rt-se by Talend.

the class SamRestServiceImplTest method writeEventtoDb.

private Event writeEventtoDb(String flowId, EventTypeEnum eventType) {
    Event e = new Event();
    Date ts = new Date();
    e.setTimestamp(ts);
    e.setEventType(eventType);
    Originator orig = new Originator("pid1", "127.0.0.1", "localhost", "custom_id1", "principal1");
    e.setOriginator(orig);
    MessageInfo mi = new MessageInfo("mid1", flowId, "portType_1", "seekBook", "HTTP");
    e.setMessageInfo(mi);
    e.setContentCut(false);
    e.setContent("<seekBook>Survival in the Arctic</seekBook>");
    e.getCustomInfo().put("key1", "value1");
    e.getCustomInfo().put("key2", "value2");
    eventRepository.writeEvent(e);
    return e;
}
Also used : Originator(org.talend.esb.sam.common.event.Originator) Event(org.talend.esb.sam.common.event.Event) FlowEvent(org.talend.esb.sam.server.persistence.FlowEvent) Date(java.util.Date) MessageInfo(org.talend.esb.sam.common.event.MessageInfo)

Example 5 with Originator

use of org.talend.esb.sam.common.event.Originator in project tesb-rt-se by Talend.

the class EventTypeMapper method mapOriginatorType.

/**
 * Map originator type.
 *
 * @param originatorType the originator type
 * @return the originator
 */
private static Originator mapOriginatorType(OriginatorType originatorType) {
    Originator originator = new Originator();
    if (originatorType != null) {
        originator.setCustomId(originatorType.getCustomId());
        originator.setHostname(originatorType.getHostname());
        originator.setIp(originatorType.getIp());
        originator.setProcessId(originatorType.getProcessId());
        originator.setPrincipal(originatorType.getPrincipal());
    }
    return originator;
}
Also used : Originator(org.talend.esb.sam.common.event.Originator)

Aggregations

Originator (org.talend.esb.sam.common.event.Originator)11 MessageInfo (org.talend.esb.sam.common.event.MessageInfo)10 Event (org.talend.esb.sam.common.event.Event)9 Date (java.util.Date)5 Test (org.junit.Test)3 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 SoapBinding (org.apache.cxf.binding.soap.SoapBinding)2 SoapBindingInfo (org.apache.cxf.binding.soap.model.SoapBindingInfo)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DataHandler (javax.activation.DataHandler)1 AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)1 SecurityContext (org.apache.cxf.security.SecurityContext)1 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)1 EventType (org.talend.esb.sam._2011._03.common.EventType)1 CustomInfo (org.talend.esb.sam.agent.message.CustomInfo)1 EventTypeEnum (org.talend.esb.sam.common.event.EventTypeEnum)1 FlowEvent (org.talend.esb.sam.server.persistence.FlowEvent)1