0
votes

I need to send multiple response from a mule flow one to a http endpoint which would be JSON and another to an SMTP endpoint where the recipient receives a custom email. If I use a transformer to transform the json respone which is default both endpoints receive the same custom email format. So how can I send the default JSON response to the http endpoint and the Custom Transformer generated email to the smtp endpoint below is my flow and Custom Transformer

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.xsd">
    <data-mapper:config name="json2_to_json" transformationGraphPath="json2_to_json.grf" doc:name="json_to_json"/>

    <flow name="jirarestFlow3" doc:name="jirarestFlow3">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="6767" doc:name="HTTP" contentType="application/json"/>
        <logger message="This is from hari #[message.payload]" level="DEBUG" doc:name="Logger"/>
        <data-mapper:transform config-ref="json2_to_json" doc:name="JSON To JSON"/>
        <logger level="DEBUG" doc:name="Logger" message="This is from Data Mapper #[json:fields/priority/id]"/>
        <set-variable variableName="myPayload" value="#[json:fields/reporter/emailAddress]" doc:name="tmpPayload"/>
        <logger message="MyPayload is #[myPayload]" level="DEBUG" doc:name="Logger"/>

            <http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="9999" path="rest/api/2/issue/" method="POST"  user="" password="" contentType="application/json" doc:name="HTTP"/>
        <response>
            <!--set-variable variableName="responsePayload" value="#[message.payload]" doc:name="respPayload"/-->
            <!--http:response-builder doc:name="HTTP Response Builder" contentType="text/plain" status="200"/--> 
            <!--expression-transformer doc:name="Expression" /-->
            <custom-transformer class="org.hhmi.transformer.EmailBodyTransformer" doc:name="Java"/>
            <smtps:outbound-endpoint host="smtp.gmail.com" port="465" user="pandalai" password="soapui67" to="#[myPayload]" from="[email protected]" subject="Jira Ticket" responseTimeout="10000" mimeType="text/plain" doc:name="SMTP">
                <email:string-to-email-transformer doc:name="Email to String"/>                     
            </smtps:outbound-endpoint>
            </response>

    </flow>
</mule>

Custom Transformer

package org.xxx.transformer;

import org.apache.log4j.Logger;

import org.xxx.dto.JsonBean;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;

import org.mule.transformer.AbstractMessageTransformer;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class EmailBodyTransformer extends AbstractMessageTransformer {
    public static Logger logger = Logger.getLogger(EmailBodyTransformer.class);

    @Override
    public Object transformMessage(MuleMessage message, String outputEncoding)
            throws TransformerException {
        // TODO Auto-generated method stub
        StringBuffer html = new StringBuffer();
        Map<String,String> map = new HashMap<String,String>();
        ObjectMapper mapper = new ObjectMapper();
        JsonBean jBean = new JsonBean();
        try {
            logger.debug("EmailBodyTransformer payload "
                    + message.getPayloadAsString());
            logger.debug("EmailBodyTransformer class " + message.getClass());

            map = mapper.readValue(message.getPayloadAsString(), 
                    new TypeReference<HashMap<String,String>>(){});
                jBean.setId(map.get("id"));
                jBean.setKey(map.get("key"));
                jBean.setSelf(map.get("self"));
                System.out.println("EmailBodyTransformer id from map: "+ jBean.getId());
                System.out.println("EmailBodyTransformer map: "+ map);

/*              html.append("");
                html.append("");
                html.append("");
                html.append("Dear ").append(jBean.getId()).append("  ");
                html.append("Thank you for your order. Your transaction reference is: <strong>");
                html.append(jBean.getKey()).append("</strong>");
                html.append("");*/

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return message;
        //return html;
    }

}
2

2 Answers

1
votes

The <response></response> tags mean, that whatever you put there will be put into your http:inbound-endpoint response. Since smtp is asynchronous, it will be ignored in the response phase, and the block will return whatever it gets from your custom transformer.

The solution is to move the contents inside the <response></response> block into an <async></async> block, so your custom transformer will not interfere with the http response. Inside the <response></response> block you will need just an <object-to-string-transformer> to transform your http:outbound-endpoint into a string.

EDIT:

Move the <object-to-string-transformer> after the http:outbound-endpoint and change your transformer to take String, not input stream. Otherwise the input stream gets closed once either of the two readers reads it. You will not need the <response></response> after this.

0
votes

Your requirement is exactly what mule-requester-module is based on:

http://java.dzone.com/articles/introducing-mule-requester

example project: https://github.com/mulesoft/mule-module-requester

This module allows you to asynchronously do two things.

Hope this helps