How to replace .drl file dynamically while using drools in mule esb? - mule

what i need is to replace the .drl file dynamically. through some nasty code i am able to load the .drl file and change the content but it seems that drools holds on to its initial file.
these are my config files (just in case):
mule-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns:bpm="http://www.mulesoft.org/schema/mule/bpm"
xmlns:client="http://www.mulesoft.org/schema/mule/client"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:management="http://www.mulesoft.org/schema/mule/management"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd
http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/3.2/mule-quartz.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.2/mule-vm.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/3.2/mule-jms.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/3.2/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/bpm http://www.mulesoft.org/schema/mule/bpm/3.2/mule-bpm.xsd
http://www.mulesoft.org/schema/mule/client http://www.mulesoft.org/schema/mule/client/3.2/mule-client.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.mulesoft.org/schema/mule/management http://www.mulesoft.org/schema/mule/management/current/mule-management.xsd
">
<bpm:jbpm />
<bpm:drools />
<!-- Define EMC Routing facts bean -->
<spring:bean name="NoFactsBean" class="java.util.ArrayList"/>
<jms:activemq-connector
name="jms-connector"
brokerURL="tcp://localhost:61616"
disableTemporaryReplyToDestinations="true"
specification="1.1"/>
<jms:endpoint name="UnsortedOrders" queue="UnsortedOrders"/>
<jms:endpoint name="DestinationEMC" queue="DestinationEMC"/>
<jms:endpoint name="DestinationOriginal" queue="DestinationOriginal"/>
<vm:endpoint name="DroolsEndpoint" path="drools"/>
<!-- GOLI -->
<spring:beans>
<spring:bean id="dto-convertor" class="com.hamgam.hit.esb.XMLToDTO" scope="singleton" />
<spring:bean id="rule-convertor" class="com.hamgam.hit.esb.RuleXMLToDRL" scope="singleton" />
</spring:beans>
<http:listener-config name="HTTP_Listener" host="localhost" port="8084" doc:name="HTTP Listener Configuration" />
<!-- /GOLI -->
<flow name="RestEtryPoint">
<http:listener config-ref="HTTP_Listener" path="/{name}" doc:name="HTTP" />
<set-variable variableName="name" value="#[message.inboundProperties['http.uri.params']['name']]" />
<set-variable variableName="msg" value="#[payload]"/>
<expression-component>
app.registry['dto'] = app.registry['dto-convertor'].convert(flowVars['name']);
</expression-component>
<set-payload value="#[app.registry['dto']]"/>
<jms:outbound-endpoint ref="UnsortedOrders"/>
<logger level="INFO" doc:name="Logger" message="***************Entry***************" />
</flow>
<flow name="jBPMProcess">
<composite-source>
<vm:inbound-endpoint path="in" exchange-pattern="request-response"/>
<jms:inbound-endpoint ref="UnsortedOrders"/>
</composite-source>
<!-- TODO streaming="false"Transform XML to object -->
<bpm:process processName="DecisionRouting" processDefinition="routing-process.jpdl.xml"/>
<logger level="INFO" doc:name="Logger" message="***************JBPM Process***************" />
</flow>
<flow name="DroolsFlow">
<vm:inbound-endpoint ref="DroolsEndpoint" exchange-pattern="request-response"/>
<bpm:rules rulesDefinition="emcRoutingRules.drl" initialFacts-ref="NoFactsBean" />
<expression-transformer evaluator="groovy" expression="message.getPayload().getObject()"/>
<logger level="INFO" doc:name="Logger" message="***************Drools Flow***************" />
</flow>
<flow name="ConsumeEmcQ">
<jms:inbound-endpoint ref="DestinationEMC"/>
<logger level="INFO" doc:name="Logger" message="*************Consume Q4 DestinationEMC*****************" />
<logger level="INFO" doc:name="Logger" message="#[payload]" />
</flow>
<flow name="ConsumeOrigQ">
<jms:inbound-endpoint ref="DestinationOriginal"/>
<logger level="INFO" doc:name="Logger" message="*************Consume Q4 DestinationOriginal*****************" />
</flow>
</mule>
this is routing-process.jpdl.xml:
<?xml version="1.0" encoding="UTF-8"?>
<process description="routing example" name="DecisionRouting" xmlns="http://jbpm.org/4.3/jpdl">
<mule-receive name="unsortedOrder" endpoint="UnsortedOrders" type="com.hamgam.hit.esb.XMLDTO" var="order">
<transition to="sendToDrools" />
</mule-receive>
<mule-send name="sendToDrools" expr="#{order}" endpoint="DroolsEndpoint" exchange-pattern="request-response" var="order">
<transition to="defineDestination" />
</mule-send>
<decision name="defineDestination">
<transition to="sendToEMC">
<condition expr="#{order.destination == 'EMC'}" />
</transition>
<transition to="sendToOriginalDest">
<condition expr="#{order.destination == 'ORIG'}" />
</transition>
<transition to="routingFailed" />
</decision>
<mule-send name="sendToEMC"
expr="#{order}" endpoint="DestinationEMC" exchange-pattern="one-way">
<transition to="routingSuccessful" />
</mule-send>
<mule-send name="sendToOriginalDest"
expr="#{order}" endpoint="DestinationOriginal" exchange-pattern="one-way">
<transition to="routingSuccessful" />
</mule-send>
<end name="routingSuccessful" />
<end name="routingFailed" />
</process>
and this is emcRoutingRules.drl that originally placed in resource folder :
package com.hamgam.hit.esb
import com.hamgam.hit.esb.*;
import org.mule.MessageExchangePattern
global org.mule.module.bpm.MessageService mule;
# default dialect for the semantic code will be MVEL
dialect "mvel"
declare XMLDTO
#role( event )
end
rule "Select Emc"
lock-on-active
when
$x:XMLDTO(inputXML == "moien")
then
modify( $x ) { setDestination("EMC") }
end
rule "Select Original Destination"
lock-on-active
when
$x:XMLDTO( inputXML == "goli" )
then
modify( $x ) { setDestination("ORIG") }
end
and this is how i am replacing the .drl file :
private void generateFile(String rule) throws IOException{
PrintWriter writer;
try {
ClassLoader classLoader = RuleXMLToDRL.class.getClassLoader();
File file =
new File(classLoader.getResource("emcRoutingRules.drl").getFile());
writer = new PrintWriter(file, "UTF-8");
writer.println(""
+ "package com.hamgam.hit.esb "
+ "import com.hamgam.hit.esb.*; "
+ "import org.mule.MessageExchangePattern "
+ "global org.mule.module.bpm.MessageService mule; "
+ "dialect \"mvel\" declare XMLDTO #role( event ) end");
writer.println(rule);
writer.close();
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

I am not proud, but i came up with a hacker solution:
as i mentioned before any change on mule-config.xml file will lead to mules rebuild, so after changing .drl file i am making a dummy change to mule-config.xml file too.
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("mule-config.xml", true)));
out.println("<!-- -->");
out.flush();
note that the solution wont work if you lunch from Anypoint studio. but deploy it in mule standalone and it will work.
Till i find a better solution, this works just fine ;)

Related

Mule Basic Authentication

I am trying to use Basic authentication and get the credentials in Mule flows.
Here is my Mule flows:
<http:listener config-ref="httpConfig" path="/*" doc:name="HTTP" allowedMethods="get, post, delete" />
<apikit:router config-ref="api-config" doc:name="APIkit Router" />
<flow name="get:/sourcing/helloworld-secure:api-config">
<flow-ref name="authenticate-ldap" doc:name="authenticate-ldap"/>
</flow>
<flow name="authenticate-ldap">
<logger message="Name: #[message.inboundProperties.get('username')] Password:#[message.inboundProperties['password']] level="INFO"/>
<component class="com.test.AuthTest" doc:name="Java"/>
<logger message="After java: #[flowVars.username] #[flowVars.password]" level="INFO" doc:name="Logger"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<data-mapper:transform config-ref="Map_auth_to_ldap_request" doc:name="Map auth to ldap request"/>
</flow>
Java code:
public class AuthTest implements Callable {
#Override
public Map<String, String> onCall(MuleEventContext eventContext) throws Exception {
Map<String, String> authMap = new HashMap<String, String>();
final String authorization = eventContext.getMessage().getInboundProperty("authorization");
if (authorization != null && authorization.startsWith("Basic")) {
String base64Credentials = authorization.substring("Basic".length()).trim();
String credentials = new String(Base64.decode(base64Credentials), Charset.forName(Base64.PREFERRED_ENCODING));
if (credentials != null) {
final String[] values = credentials.split(":",2);
eventContext.getMessage().setInvocationProperty("username", values[0]);
eventContext.getMessage().setInvocationProperty("password", values[1]);
authMap.put("username", values[0]);
authMap.put("password", values[1]);
}
}
return authMap;
}
}
To run this application, I am using rest client in Chrome and selecting Basic Auth, then giving username and password.
Above code is working fine. What I need is instead of getting credentials in Java then put into Map, I need to get the credentials in Mule flows without using Java code. Is this possible in Mule flows directly?
I have implemented with Spring security which is working fine, but in this case I need user to enter credentials and proceed further.
For implementing the basic-authentication you can use the "mule-ss:security-manager" element to provide the source of the authentication and the "http:basic-security-filter" element to establish the restriction. Finally you can get the the credentials in the flow by using the "expresion-transformer" element. Here you have an example:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml"
xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:ws="http://www.mulesoft.org/schema/mule/ws"
xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps"
xmlns:email="http://www.mulesoft.org/schema/mule/email"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:mule-ss="http://www.mulesoft.org/schema/mule/spring-security"
xmlns:ss="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.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/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd
http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.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/spring-security http://www.mulesoft.org/schema/mule/spring-security/3.1/mule-spring-security.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
">
<spring:beans>
<ss:authentication-manager alias="authenticationManager">
<ss:authentication-provider>
<ss:user-service id="userService">
<ss:user name="user" password="password" authorities="ROLE_ADMIN" />
<ss:user name="anon" password="anon" authorities="ROLE_ANON" />
</ss:user-service>
</ss:authentication-provider>
</ss:authentication-manager>
</spring:beans>
<mule-ss:security-manager>
<mule-ss:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" />
</mule-ss:security-manager>
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="9091" doc:name="HTTP Listener Configuration"/>
<flow name="testingFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/*" doc:name="HTTP"/>
<logger message="Before Authentication" level="INFO" doc:name="Log Failure"/>
<http:basic-security-filter realm="mule-realm"/>
<set-payload value="#[message.inboundProperties.'Authorization']"/>
<set-payload value="#[message.payloadAs(java.lang.String).substring('Basic'.length()).trim()]"/>
<expression-transformer expression="#[new String(org.mule.util.Base64.decode(payload),java.nio.charset.Charset.forName('UTF-8')).split(':');]" />
<set-payload value="#[['user':payload[0],'password':payload[1]]]"/>
<logger message="#[payload]" level="INFO" doc:name="User - Password"/>
</flow>
</mule>
Documentation reference:
https://docs.mulesoft.com/mule-user-guide/v/3.7/http-listener-connector#authentication

Unable to post a file to http endpoint inside a choice in mule

Here's the my mule app xml config
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:test="http://www.mulesoft.org/schema/mule/test"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd
http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/3.6/mule-test.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.6/mule.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.6/mule-vm.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.6/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.6/mule-file.xsd">
<object-to-string-transformer name="httptoobj" />
<file:connector name="input"/>
<http:http-response-to-object-transformer
name="httptostring" />
<configuration>
<expression-language autoResolveVariables="false">
<import class="com.xyz.alertcampaign.appworkflow.CampaignStatus" />
</expression-language>
</configuration>
<flow name="polling" doc:name="polling" processingStrategy="synchronous">
<quartz:inbound-endpoint repeatInterval="3000"
startDelay="3000" jobName="couchbasePoller" doc:name="Quartz">
<quartz:event-generator-job stateful="true" />
</quartz:inbound-endpoint>
<component doc:name="Java">
<singleton-object
class="com.xyz.alertcampaign.appworkflow.CouchbasePoller" />
</component>
<vm:outbound-endpoint exchange-pattern="one-way"
path="oozieQueue" doc:name="Trigger workflow" />
</flow>
<flow name="oozie-workflow-manager" doc:name="oozie-workflow-manager">
<vm:inbound-endpoint exchange-pattern="one-way"
path="oozieQueue" doc:name="VM" />
<foreach collection="#[payload.items()]" counterVariableName="counter">
<choice doc:name="Choice">
<when expression="#[payload.status == CampaignStatus.NOT_STARTED]">
<file:inbound-endpoint address="file://#[payload.fileName]" connector-ref="input"/>
<http:outbound-endpoint exchange-pattern="request-response"
address="http://localhost:8080/oozie/v1/jobs"
responseTransformer-refs="httptoobj" method="POST" doc:name="HTTP"
contentType="application/xml;charset=UTF-8" />
<choice doc:name="Choice">
<when expression="#[message.inboundProperties['http.status'] == 201]">
<component doc:name="Java">
<singleton-object
class="com.xyz.alertcampaign.appworkflow.JobUpdater" />
</component>
</when>
<otherwise>
<logger level="ERROR" message="Error occurred on creating Job in OOzie " />
</otherwise>
</choice>
</when>
<when
expression="#[payload.status == CampaignStatus.UPDATED || payload.status == CampaignStatus.EXPIRED]">
<logger level="ERROR" message="#[payload.status]" />
<http:outbound-endpoint exchange-pattern="request-response"
method="PUT"
address="http://localhost:8080/oozie/v1/job/#[payload.jobId]?action=kill"
responseTransformer-refs="httptoobj" doc:name="HTTP" />
<choice doc:name="Choice">
<when expression="#[message.inboundProperties['http.status'] == 200]">
<component doc:name="Java">
<singleton-object
class="com.xyz.alertcampaign.appworkflow.JobUpdater" />
</component>
</when>
<otherwise>
<logger level="ERROR" message="Error occurred on killing Job in OOzie " />
</otherwise>
</choice>
</when>
</choice>
</foreach>
</flow>
</mule>
Getting following exception (stack):
Exception when loading mule' xml:
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'file:inbound-endpoint'. One of '{"http://www.mulesoft.org/schema/mule/core":abstract-message-processor, "http://www.mulesoft.org/schema/mule/core":abstract-outbound-endpoint, "http://www.mulesoft.org/schema/mule/core":abstract-mixed-content-message-processor}' is expected.
Why am I not allowed to post a file to http inside choice router?
You can only use a file:inbound-endpoint at the beginning of a flow. If you're trying to retrieve a file mid-flow, then consider using the Mule Requestor module. This allows you to request a resource (i.e. a file) at any point in a flow.
The problem is that it's an inbound endpoint so you are adding a message source in the middle of a flow. What you should do is define another flow that starts with that file inbound endpoint (with all that logic) and use a flow reference element in the choice to it.
For more details check out the doc.
Additionaly I think there is another problem where the inbound endpoint does not allow dynamic inputs. I solved it via MuleRequester

MuleRequester deleting file after reading

Following is my mule configuration.
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:test="http://www.mulesoft.org/schema/mule/test"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:mulerequester="http://www.mulesoft.org/schema/mule/mulerequester"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd
http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/3.6/mule-test.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.6/mule.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.6/mule-vm.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.6/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/mulerequester http://www.mulesoft.org/schema/mule/mulerequester/1.0-SNAPSHOT/mule-mulerequester.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.6/mule-file.xsd">
<vm:outbound-endpoint exchange-pattern="one-way"
path="oozieQueue" doc:name="Trigger workflow" />
</flow>
<flow name="oozie-workflow-manager" doc:name="oozie-workflow-manager">
<vm:inbound-endpoint exchange-pattern="one-way"
path="oozieQueue" doc:name="VM" />
<foreach collection="#[payload.items()]" counterVariableName="counter">
<choice doc:name="Choice">
<when expression="#[payload.status == CampaignStatus.NOT_STARTED]">
<mulerequester:request config-ref="Mule_Requester"
resource="file://#[payload.fileName]" returnClass="java.lang.String" doc:name="Request a message from a queue" />
<logger level="ERROR" message="Payload is : #[message.payload]" />
<enricher target="#[variable:jsonResponse]">
<http:outbound-endpoint exchange-pattern="request-response"
address="http://abc.xyz.com:11000/oozie/v2/jobs?action=start"
method="POST" doc:name="HTTP"
contentType="application/xml;charset=UTF-8" />
</enricher>
<choice doc:name="Choice">
<when expression="#[message.inboundProperties['http.status'] == 201]">
<component doc:name="Java">
<singleton-object
class="com.xyz.alertcampaign.appworkflow.JobUpdater" />
</component>
</when>
<otherwise>
<logger level="ERROR" message="Error occurred on creating Job in OOzie " />
</otherwise>
</choice>
</when>
<when
expression="#[payload.status == CampaignStatus.UPDATED || payload.status == CampaignStatus.EXPIRED]">
<logger level="ERROR" message="#[payload.status]" />
<http:outbound-endpoint exchange-pattern="request-response"
method="PUT"
address="http://localhost:8080/oozie/v1/job/#[payload.jobId]?action=kill"
responseTransformer-refs="httptoobj" doc:name="HTTP" />
<choice doc:name="Choice">
<when expression="#[message.inboundProperties['http.status'] == 200]">
<component doc:name="Java">
<singleton-object
class="com.xyx.alertcampaign.appworkflow.JobUpdater" />
</component>
</when>
<otherwise>
<logger level="ERROR" message="Error occurred on killing Job in OOzie " />
</otherwise>
</choice>
</when>
</choice>
</foreach>
</flow>
I have a symbolic link for the file path . But both the symbolic link and the file is getting deleted after MuleRequester is done.
Try setting the resource url to file://#[payload.fileName]?autoDelete=false

Mule ESB Until-Successful Not Working Properly

I have spent the past few hours trying to get until-successful to work properly, but to no avail even after following the model here: "http://blogs.mulesoft.org/meet-until-successful-store-and-forward-for-mule/". The main flow that uses until-successful is SEND_TO_POST_SERVICE. I want until-successful to continue running the referenced flow until the max retries. Below are my flows and relevant information. The internal Mule server version is 3.4, and the Mule Studio version is 3.4.0
################## Flow
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:core="http://www.mulesoft.org/schema/mule/core"
version="EE-3.4.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<choice-exception-strategy name="Post_Exception_Strategy">
<catch-exception-strategy when="exception.causedBy(java.net.SocketTimeoutException) or exception.causedBy(java.net.ConnectException)" doc:name="Catch Exception Strategy">
<logger message="Network exception occurred" level="INFO" doc:name="Logger"/>
</catch-exception-strategy>
<catch-exception-strategy when="exception.causedBy(java.lang.Throwable)" doc:name="Catch Exception Strategy">
<logger message="General exception occurred" level="INFO" doc:name="Logger"/>
</catch-exception-strategy>
</choice-exception-strategy>
<flow name="compositeFlow1"
doc:name="compositeFlow1">
<composite-source doc:name="Comp Source">
<inbound-endpoint doc:name="DTCOM"
ref="SUBMISSION_REQUEST_EP"/>
</composite-source>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<flow-ref name="POST_TO_SERVICES"
doc:name="POST_TO_SERVICES_ref"/>
<exception-strategy ref="Post_Exception_Strategy"
doc:name="Reference Exception Strategy"/>
</flow>
<flow name="POST_TO_SERVICES" doc:name="POST_TO_SERVICES" processingStrategy="synchronous">
<!-- Send to the POST Service -->
<async>
<flow-ref name="SEND_TO_POST_SERVICE" doc:name="SEND_TO_POST_SERVICE_ref"/>
</async>
<outbound-endpoint ref="HTTP_EP_1" doc:name="HTTP_EP_1"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<set-variable variableName="statusCode" value="#[message.inboundProperties['http.status']]" doc:name="statusCode"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<logger message="Data submitted to Endpoint HTTP_STATUS_CODE: #[statusCode]" level="INFO" doc:name="Logger"/>
<exception-strategy ref="Post_Exception_Strategy" doc:name="Reference Exception Strategy"/>
</flow>
<spring:beans>
<spring:bean id="myListableObjectStore" class="org.mule.util.store.SimpleMemoryObjectStore"/>
</spring:beans>
<flow name="SEND_TO_POST_SERVICE" doc:name="Send to POST Service">
<choice doc:name="Choice">
<when expression="${send_to_post} == 'true'">
<until-successful objectStore-ref="myListableObjectStore" maxRetries="3" secondsBetweenRetries="2" doc:name="Until_Success">
<flow-ref name="CONNECT_TO_POST_SERVICE" doc:name="CONNECT_TO_POST_SERVICE_ref"/>
</until-successful>
</when>
<otherwise>
<logger message="NOT Posting to SERVICE" level="DEBUG" doc:name="Send_Logger"/>
</otherwise>
</choice>
</flow>
<flow name="CONNECT_TO_POST_SERVICE" doc:name="CONNECT_TO_POST_SERVICE">
<logger message="$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$In Until-Successful for POST" level="INFO" doc:name="USLogger"/>
<processor-chain doc:name="Processor Chain">
<logger message="Sending JSON to POST Service" level="INFO" doc:name="JSON_POST_Logger"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="Content-Type" value="application/json"/>
</message-properties-transformer>
<outbound-endpoint ref="POST_SERVICE_EP" doc:name="POST_SERVICE" exchange-pattern="request-response"></outbound-endpoint>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="Status Code response from Sending POST JSON = #[message.inboundProperties['http.status']]" level="INFO" doc:name="StatusCodeLog"/>
<logger message="Message from POST Service for #[payload]" level="INFO" doc:name="LoggerMessageOutput"/>
</processor-chain>
</flow>
</mule>
################## Endpoints
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xmlns:https="http://www.mulesoft.org/schema/mule/https"
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.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
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:endpoint name="POST_SERVICE_EP" exchange-pattern="request-response" address="http://127.0.0.1:8900/post/service/" connector-ref="http_connector" contentType="application/json" doc:name="HTTP"/>
<http:endpoint name="HTTP_EP_1" method="PUT" contentType="application/json" connector-ref="http_connector_2" address="http://127.0.0.1:8900/put/service/" doc:name="HTTP"/>
</mule>
################## Connectors
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:https="http://www.mulesoft.org/schema/mule/https"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" version="EE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
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:connector name="http_connector" clientSoTimeout="1000000" serverSoTimeout="1000000" doc:name="http_connector" keepAlive="true">
<receiver-threading-profile maxThreadsActive="10" doThreading="true" threadWaitTimeout="-1" maxBufferSize="10000" maxThreadsIdle="5" poolExhaustedAction="WAIT"/>
<dispatcher-threading-profile maxThreadsActive="10" doThreading="true" threadWaitTimeout="-1" maxBufferSize="10000" maxThreadsIdle="5" poolExhaustedAction="WAIT"/>
</http:connector>
<http:connector name="http_connector_2" doc:name="http_connector" keepAlive="true">
<receiver-threading-profile maxThreadsActive="10" doThreading="true" threadWaitTimeout="-1" maxBufferSize="10000" maxThreadsIdle="5" poolExhaustedAction="WAIT"/>
<dispatcher-threading-profile maxThreadsActive="10" doThreading="true" threadWaitTimeout="-1" maxBufferSize="10000" maxThreadsIdle="5" poolExhaustedAction="WAIT"/>
</http:connector>
</mule>
The flow you are flow-refing (ie CONNECT_TO_POST_SERVICE) is a private flow not a sub-flow.
This means that exceptions thrown in it will not be propagated to the caller. Change it to a sub-flow and it should work.
PS. Also remove the processor-chain in CONNECT_TO_POST_SERVICE: it's completely useless.

Mule file download with HTTP Endpoint

My HTTP Endpoint responsible for downloading the file at the end of the flow is erroring out. It keeps trying to communicate with http://:80/ instead of the URL passed in. What am I doing wrong here?
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:https="http://www.mulesoft.org/schema/mule/https" 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="CE-3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
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/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd ">
<flow name="BingFlow1" doc:name="BingFlow1">
<http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8081" doc:name="HTTP"/>
<https:outbound-endpoint exchange-pattern="request-response" host="api.datamarket.azure.com" port="443" path="Data.ashx/Bing/Search/v1/Web?Query=%27contract%20california%27&WebFileType=%27PDF%27&$top=50&$format=Json" user="*****" password="*****" doc:name="Bing"/>
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<expression-transformer expression="#[message.payload.d.results]" doc:name="Expression"/>
<collection-splitter doc:name="Collection Splitter"/>
<expression-transformer expression="#[org.mule.util.StringUtils.substringAfter(message.payload.Url, 'http://')]" doc:name="Expression"/>
<logger message="Payload is: #[message.payload]" level="INFO" doc:name="Logger"/>
<http:outbound-endpoint exchange-pattern="request-response" host="#[org.mule.util.StringUtils.substringBefore(message.payload, '/')]" port="80" method="GET" doc:name="HTTP" contentType="application/pdf" mimeType="application/pdf" path="#[org.mule.util.StringUtils.substringAfter(message.payload, '/')]"/>
<file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[org.mule.util.StringUtils.replace(message.payload, '/','.')]" path="/home/user/Documents/output" mimeType="application/pdf"/>
</flow>
</mule>
When changed to match the first answer, I get this exception:
********************************************************************************
Message : Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Message payload is of type: LinkedHas
hMap
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Message payload is of type: LinkedHashMap (org.mule.transp
ort.http.components.RestServiceException)
org.mule.transport.http.components.RestServiceWrapper:219 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/transport/http/components/RestServiceException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.transport.http.components.RestServiceException: Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Mess
age payload is of type: LinkedHashMap
at org.mule.transport.http.components.RestServiceWrapper.doInvoke(RestServiceWrapper.java:219)
at org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:126)
at org.mule.component.AbstractComponent.access$000(AbstractComponent.java:61)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
--- SOLVED ---
Big thanks to David. This is my final solution:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:https="http://www.mulesoft.org/schema/mule/https" 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="CE-3.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.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/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
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 ">
<configuration doc:name="Configuration">
<expression-language>
<import class="org.mule.util.StringUtils" />
</expression-language>
</configuration>
<flow name="BingQuery" doc:name="BingQuery">
<http:inbound-endpoint exchange-pattern="one-way"
host="localhost" port="8082" doc:name="HTTP" />
<https:outbound-endpoint exchange-pattern="request-response"
host="api.datamarket.azure.com" port="443"
path="Data.ashx/Bing/Search/v1/Web?Query=%27california%20school%20district%20contract%27&WebFileType=%27PDF%27&$top=10&$format=Json"
user="*****" password="*****"
doc:name="Bing" />
<json:json-to-object-transformer
returnClass="java.util.Map" doc:name="JSON to Object" />
<expression-transformer expression="#[message.payload.d.results]"
doc:name="Expression" />
<collection-splitter doc:name="Collection Splitter" />
<vm:outbound-endpoint exchange-pattern="one-way"
doc:name="VM" path="fileWriter" />
</flow>
<flow name="RestProcessor" doc:name="RestProcessor">
<vm:inbound-endpoint exchange-pattern="one-way"
doc:name="VM" path="fileWriter" />
<set-variable variableName="fileName" value="#[message.payload.ID].pdf"
doc:name="Variable" />
<http:rest-service-component
serviceUrl="#[joinChar=message.payload.Url.contains('?')?'&':'?' ; StringUtils.join(new String[]{message.payload.Url,(String)joinChar,'followRedirects=true'})]"
httpMethod="GET">
<http:error-filter>
<expression-filter
expression="#[Integer.valueOf(message.inboundProperties['http.status']) >= 400]"></expression-filter>
</http:error-filter>
</http:rest-service-component>
<file:outbound-endpoint responseTimeout="10000"
doc:name="File" outputPattern="#[flowVars.fileName]" path="/home/ken/Documents/output"
mimeType="application/pdf" />
</flow>
</mule>
I can't spot the error: your flow looks good (except a problem I'll detail below) so, alternatively to using an http:outbound-endpoint, which can be finicky at times, you could use the http:rest-service-component.
Your next problem will be in the outputPattern of the file endpoint: remember at this point message.payload will be the response from the HTTP endpoint not the (partial) URL anymore. You need to pre-compute the file name and store it in a flow variable, then use it.
This would give:
<configuration>
<expression-language>
<import class="org.mule.util.StringUtils" />
</expression-language>
</configuration>
<flow name="BingFlow1" doc:name="BingFlow1">
<http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8081" doc:name="HTTP"/>
<https:outbound-endpoint exchange-pattern="request-response" host="api.datamarket.azure.com" port="443" path="Data.ashx/Bing/Search/v1/Web?Query=%27contract%20california%27&WebFileType=%27PDF%27&$top=50&$format=Json" user="*****" password="*****" doc:name="Bing"/>
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<expression-transformer expression="#[message.payload.d.results]" doc:name="Expression"/>
<collection-splitter doc:name="Collection Splitter"/>
<set-variable variableName="fileName" value="#[org.mule.util.StringUtils.substringAfter(message.payload.Url, 'http://').replace('/','.')]" />
<http:rest-service-component serviceUrl="#[joinChar=message.payload.Url.contains('?')?'&':'?' ; StringUtils.join(new String[]{message.payload.Url,(String)joinChar,'followRedirects=true'})]" httpMethod="GET">
<http:error-filter>
<expression-filter expression="#[Integer.valueOf(message.inboundProperties['http.status']) >= 400]" />
</http:error-filter>
</http:rest-service-component>
<file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[flowVars.fileName]" path="/home/user/Documents/output" mimeType="application/pdf"/>
</flow>