Search in sources :

Example 1 with CustomInfo

use of org.talend.esb.sam.agent.message.CustomInfo 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 CustomInfo

use of org.talend.esb.sam.agent.message.CustomInfo in project tesb-rt-se by Talend.

the class MessageToEventMapperTest method getTestMessage.

private Message getTestMessage() throws IOException, EndpointException {
    Message message = new MessageImpl();
    ExchangeImpl exchange = new ExchangeImpl();
    ServiceInfo serviceInfo = new ServiceInfo();
    InterfaceInfo interfaceInfo = new InterfaceInfo(serviceInfo, new QName("interfaceNs", "interfaceName"));
    serviceInfo.setInterface(interfaceInfo);
    SoapBindingInfo bInfo = new SoapBindingInfo(serviceInfo, WSDLConstants.NS_SOAP12);
    bInfo.setTransportURI(TransportType);
    OperationInfo opInfo = new OperationInfo();
    opInfo.setName(new QName("namespace", "opName"));
    BindingOperationInfo bindingOpInfo = new BindingOperationInfo(bInfo, opInfo);
    exchange.put(BindingOperationInfo.class, bindingOpInfo);
    SoapBinding binding = new SoapBinding(bInfo);
    exchange.put(Binding.class, binding);
    String ns = "ns";
    EndpointInfo ei = new EndpointInfo(serviceInfo, ns);
    ei.setAddress(Address);
    Service service = new ServiceImpl();
    Bus bus = BusFactory.getThreadDefaultBus();
    Endpoint endpoint = new EndpointImpl(bus, service, ei);
    exchange.put(Endpoint.class, endpoint);
    message.setExchange(exchange);
    FlowIdHelper.setFlowId(message, FlowID);
    Principal principal = new X500Principal(PrincipalString);
    SecurityContext sc = new DefaultSecurityContext(principal, new Subject());
    message.put(SecurityContext.class, sc);
    CachedOutputStream cos = new CachedOutputStream();
    InputStream is = new ByteArrayInputStream(TESTCONTENT.getBytes("UTF-8"));
    IOUtils.copy(is, cos);
    message.setContent(CachedOutputStream.class, cos);
    CustomInfo customInfo = CustomInfo.getOrCreateCustomInfo(message);
    customInfo.put("key1", "value1");
    return message;
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) CustomInfo(org.talend.esb.sam.agent.message.CustomInfo) Bus(org.apache.cxf.Bus) QName(javax.xml.namespace.QName) ServiceImpl(org.apache.cxf.service.ServiceImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EndpointImpl(org.apache.cxf.endpoint.EndpointImpl) Service(org.apache.cxf.service.Service) SoapBinding(org.apache.cxf.binding.soap.SoapBinding) Subject(javax.security.auth.Subject) ByteArrayInputStream(java.io.ByteArrayInputStream) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo) DefaultSecurityContext(org.apache.cxf.interceptor.security.DefaultSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) X500Principal(javax.security.auth.x500.X500Principal) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Aggregations

SoapBinding (org.apache.cxf.binding.soap.SoapBinding)2 SoapBindingInfo (org.apache.cxf.binding.soap.model.SoapBindingInfo)2 SecurityContext (org.apache.cxf.security.SecurityContext)2 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)2 CustomInfo (org.talend.esb.sam.agent.message.CustomInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Principal (java.security.Principal)1 Date (java.util.Date)1 Map (java.util.Map)1 Subject (javax.security.auth.Subject)1 X500Principal (javax.security.auth.x500.X500Principal)1 QName (javax.xml.namespace.QName)1 Bus (org.apache.cxf.Bus)1 AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 EndpointImpl (org.apache.cxf.endpoint.EndpointImpl)1 DefaultSecurityContext (org.apache.cxf.interceptor.security.DefaultSecurityContext)1