use of org.talend.esb.sam.common.event.MessageInfo 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;
}
use of org.talend.esb.sam.common.event.MessageInfo 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;
}
use of org.talend.esb.sam.common.event.MessageInfo 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");
}
use of org.talend.esb.sam.common.event.MessageInfo 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;
}
use of org.talend.esb.sam.common.event.MessageInfo in project tesb-rt-se by Talend.
the class EventTypeMapper method mapMessageInfo.
/**
* Map message info.
*
* @param messageInfoType the message info type
* @return the message info
*/
private static MessageInfo mapMessageInfo(MessageInfoType messageInfoType) {
MessageInfo messageInfo = new MessageInfo();
if (messageInfoType != null) {
messageInfo.setFlowId(messageInfoType.getFlowId());
messageInfo.setMessageId(messageInfoType.getMessageId());
messageInfo.setOperationName(messageInfoType.getOperationName());
messageInfo.setPortType(messageInfoType.getPorttype() == null ? "" : messageInfoType.getPorttype().toString());
messageInfo.setTransportType(messageInfoType.getTransport());
}
return messageInfo;
}
Aggregations