Display JSON file in browser with Mule - java

I am using JSON file to show the schema and have give this config:
<flow name="api-schema" doc:name="api-schema">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="8080" path="schema" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger"/>
<http:static-resource-handler resourceBase="${app.home}/src/main/resources/" defaultFile="schema" doc:name="HTTP Static Resource Handler"/>
</flow>
But when I am running it, its always asking to download the file. I have tried in Chrome and Safari both. How can I instruct mule to display content on browser and don't download?

The way the browser determines what to do with a resource is by looking at the Content-Type header. You can set the header by creating an outbound property with the name "Content-Type" and the value "application/json" like so:
<set-property propertyName="Content-Type" value="application/json" />
Since the static-resource-handler is now deprecated, you could switch to the parse-template processor:
<parse-template location="#[message.inboundProperties['http.listener.path']]" />

Set "Content-Type" header to "application/json" then response will be in json format.

Related

Default route for Apache Camel

I'm using Apache Camel. Using XML DSL, I mean something like
<rests id="rests" xmlns="http://camel.apache.org/schema/spring">
<rest id="rest-custom">
<get uri="my_method" method="">
<description>...</description>
<param name="..." ... />
<route>
<process ref="..." />
<to uri="..." />
</route>
</get>
<post uri="another_method" method="" >
<description>...</description>
<param name="..." .../>
<route>
<process ref="..." />
<to uri="..." />
</route>
</post>
...
So if I want new route, I will just add new <get> or <post> and it works fine.
But now I want to add some DEFAULT method. I mean, something like <get uri="*"> and <post uri="*"> in the bottom of all configuration. So if my url doesn't match any from list - it goes to default one and I can handle it with custom processor (this is the behaviour I want).
For now I don't know, how to do it. Tried to handle 404 responses, but still no success. Looks like solution should be simple, but can't find it yet.
I see only one possible use case for such a default: if you got multiple URLs for the same functionality.
If this is the case and your clients don't want or can't switch to a single URL, you could still use URL rewriting on the incoming request before it reaches your Camel application.
If you want to "catch" all unknown URLs (errors), I guess you should use standard Camel error handling (see Error Handler and Exception clause) because these REST DSL blocks are internally converted to standard Camel routes.
Finally found the solution.
<get uri="/?matchOnUriPrefix=true&bridgeEndpoint=true" method="">
<description>Default GET method</description>
<route>
...
</route>
</get>
Parameters matchOnUriPrefix=true&bridgeEndpoint=true did the trick.

Error while running mule server

I have created a flow to compress a file from the Mule end, but I got an error:
java.lang.IllegalStateException: There are at least 2 connectors matching protocol "file", so the connector to use must be specified on the endpoint using the 'connector' property/attribute. Connectors in your configuration that support "file" are: input, output,
This is the flow:
<flow name="GZipCompress" doc:name="GZipCompress">
<file:inbound-endpoint path="C:backuptest" responseTimeout="10000" doc:name="File">
<file:filename-regex-filter pattern="abc.doc" caseSensitive="false" />
</file:inbound-endpoint> <string-to-byte-array-transformer doc:name="String to Byte Array" />
<logger message="Payload size before compression : #[Integer.parseInt(payload.size())/1024] KB" level="INFO" doc:name="Logger" />
<!-- If you send gzip a String then it gets serialized and mess ends up in the gzip file. To avoid this convert to byte[] first -->
<gzip-compress-transformer /> <logger message="Payload size after compression : #[Integer.parseInt(payload.size())/1024] KB" level="INFO" doc:name="Logger" />
<file:outbound-endpoint path="C:backuptestnewfolder" responseTimeout="10000" doc:name="File" /> </flow>
There are at least 2 connectors matching protocol "file", so the connector to use must be specified on the endpoint using the 'connector' property/attribute. Connectors in your configuration that support "file" are: input, output,
This error is stating that you've defined you file global connectors and now mule doesn't know which one to attach your stated file endpoint to. One solution is to define connector-ref property in your file inbound endpoint and refer to input connector you've defined.
<file:inbound-endpoint connector-ref="input" path="C:backuptest" responseTimeout="10000" doc:name="File"/>

Maintaining Mule Message

Similar to this discussion Maintain Payload State during mule flow execution
I would like to know how to maintain an entire Mule Message throughout my flow.
I am trying to call a jersey resource component after I have received information that the user is authorized to call it. In this authorization request the payload and original http request gets altered.
My predicament is that I don't want to call the resource first as that would be inefficient and insecure however I cannot see any other plausible way to do this.
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- sub-flow here which changes the MuleMessage and loses all original inbound properties -->
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
Thanks for any help in advance
Mule 3 includes two scopes that will allow you to preserve the original message, while executing other message processors:
If you must wait for the sub-flow to complete because you need to use information produced by the sub-flow, use a message enricher like so. This example assigns the variable "authorized" to whatever the payload is once the sub-flow finishes processing the message.
<enricher target="#[variable:authorized]">
<flow-ref name="checkAuthorization" />
</enricher>
If you do not need to wait for the sub-flow to complete before allowing your Jersey Resource to run, use the async scope like so:
<async>
<flow-ref name="goForthAndDoSomething" />
</async>
There are multiple ways to save a mule message and it's properties so that it could be retrieve when required .. In your case you could save the entire Mule message in a variable and retrieve it when required for example :
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- You save your entire message in a session variable named entireMessage before calling a subflow-->
<set-session-variable variableName="entireMessage" value="#[message.payload]" />
<!-- You can now call your Sub flow -->
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
Here in the above flow you store your Mule message in a session variable named entireMessage .. You can easily retrieve the value of this session variable whenever you need in any flow anywhere like the following :-
<logger level="INFO" message="#[sessionVars['entireMessage']]"/>
This will print you Mule message
There is also an alternative way to store the http headers before it get altered ..
you can also use the following :-
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- You copy all the HTTP headers before calling a subflow-->
<copy-properties propertyName="http.*" doc:name="Copy All HTTP Headers"/>
<!-- You can now call your Sub flow -->
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
The above flow will copy all the HTTP headers before calling any other flow
UPDATED FLOW :-
If you need the original unaltered payload for your Jersy component , please overwrite the current payload with the original payload stored in session variable using set payload component
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- You save your entire message in a session variable named entireMessage before calling a subflow-->
<set-session-variable variableName="entireMessage" value="#[message.payload]" />
<!-- You can now call your Sub flow -->
<!-- overwrite current payload with original unaltered payload -->
<set-payload value="#[sessionVars['entireMessage']]" doc:name="Set Payload"/>
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
Try somthing like this to store the original message in session:
<message-properties-transformer scope="session" doc:name="Save original message">
<add-message-property key="originalMessage" value="#[message:payload]"></add-message-property>
</message-properties-transformer>
Then try something like this to get it from session:
<expression-transformer evaluator="..." expression="SESSION:originalMessage" doc:name="Restore original message"></expression-transformer>
Thanks for all your help. The final solution answer was:
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<enricher target="#[variable:unwantedPayload]" source="#[payload]">
<flow-ref name="anotherFlowCalled" />
</enricher>
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
best way to store the mule message into session , whether you flow contains subflows are private flow session vars carry till end of the execeution.

Is there any bug in showing "Number Of Pending Messages" in ACTIVE MQ?

We are using active mq with Mule ESB JMS for our webservices. It is working fine. But the problem is, even the request is forwarded to my services from queue, processed and giving the response back, but still in the Active MQ web console, Number Of Pending Messages showing the total no.of requests.
Web Console ::
Flow diagram,
Flow in Mule is ::
<jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="vbrtestmulejmsFlow1" doc:name="vbrtestmulejmsFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" doc:name="HTTP" path="AMServices">
<set-property propertyName="Content-Type" value="text/xml"/>
</http:inbound-endpoint>
<object-to-string-transformer doc:name="Object to String" mimeType="text/xml"/>
<jms:outbound-endpoint queue="servicesQueue" connector-ref="Active_MQ" doc:name="JMS" responseTimeout="1000000" mimeType="text/xml" >
</jms:outbound-endpoint>
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="5050" path="MyServices" method="POST" doc:name="HTTP" contentType="text/xml">
<set-property propertyName="Content-Type" value="text/xml"/>
</http:outbound-endpoint>
</flow>
Your flow sends the message received by the inbound HTTP endpoint to both the JMS outbound endpoint and the HTTP outbound endpoint.
So it's expected that the servicesQueue will contain 6 messages and that at the same time MyServices gets called 6 times.
Your intention is unclear but if what you wanted instead was to have servicesQueue act as an intermediary between the inbound and outbound HTTP endpoints then you need to cut your flow in two:
<flow name="AMServiceToQueue">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost"
port="8888" path="AMServices">
<set-property propertyName="Content-Type" value="text/xml"/>
</http:inbound-endpoint>
<object-to-string-transformer mimeType="text/xml"/>
<jms:outbound-endpoint queue="servicesQueue" connector-ref="Active_MQ"
exchange-pattern="request-response" responseTimeout="1000000" />
</flow>
<flow name="QueueToMyServices">
<jms:inbound-endpoint queue="servicesQueue" connector-ref="Active_MQ"
exchange-pattern="request-response" />
<http:outbound-endpoint exchange-pattern="request-response" host="localhost"
port="5050" path="MyServices" method="POST" contentType="text/xml">
<set-property propertyName="Content-Type" value="text/xml"/>
</http:outbound-endpoint>
</flow>

Why am I not getting the changed payload after consuming JMS queue in Mule?

i've made a flow with JMS using ActiveMQ, i send a message to the queue, but for any reason when im trying to consume from the queue, get the message and change it by setting the new payload, it doesn't change. What am i doing wrong?
Basically i want to send back the changed payload through HTTP response
Here's my code:
<jms:activemq-connector name="Active_MQ"
specification="1.1"
brokerURL="tcp://localhost:61616"
validateConnections="true"
doc:name="Active MQ"
persistentDelivery="true"
/>
<flow name="jmsFlow1" doc:name="jmsFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="jms" doc:name="HTTP"/>
<set-payload value="#['This is a message test']" doc:name="Set Payload"/>
<choice doc:name="Choice">
<when expression="#[true]">
<processor-chain>
<logger message="Im here!!" level="INFO" doc:name="Logger"/>
<jms:outbound-endpoint queue="StudioIN" connector-ref="Active_MQ" doc:name="JMS Queue Studio IN" exchange-pattern="request-response"/>
</processor-chain>
</when>
</choice>
<logger message="#[payload]" level="INFO" category="//// RETURNED FROM QUEUE PAYLOAD" doc:name="Logger"/>
</flow>
<flow name="fmsAdapterConsumerFlow1" doc:name="fmsAdapterConsumerFlow1">
<jms:outbound-endpoint queue="StudioIN" connector-ref="Active_MQ" doc:name="JMS StudioIN Consumer" exchange-pattern="request-response"/>
<set-payload value="#[payload + ' returned from queue']" doc:name="Set Payload"/>
</flow>
You don't specify the exchange-pattern on the jms:outbound-endpoint and jms:inbound-endpoint. Therefore Mule uses the default, which is one-way. So it's impossible that the payload change made in fmsAdapterConsumerFlow1 get replied to jmsFlow1.
Set exchange-pattern="request-response" on both JMS endpoints and also set disableTemporaryReplyToDestinations="false" on the connector otherwise you'll never receive any response.
The second flow mentioned above is to be changed.
This flow starts with a inbound-endpoint if it has to read a message and process it.
<flow name="fmsAdapterConsumerFlow1" doc:name="fmsAdapterConsumerFlow1">
<jms:inbound-endpoint queue="StudioIN" connector-ref="Active_MQ" doc:name="JMS StudioIN Consumer" exchange-pattern="request-response"/>
<set-payload value="#[payload + ' returned from queue']" doc:name="Set Payload"/>
</flow>
Hope this helps.

Categories

Resources