how can we call a http servlet from mule? - servlets

I have a requirement in which i need to call a servlet from mule flow.I got one answer from the below site
http://mule.1045714.n5.nabble.com/Creating-query-string-for-calling-a-servlet-td2665836.html
But when i copy that code in my flow it is showing some errors in flow. Is there any other method or solution to call servlet from mule. I have servlet URL and username and password. I am using mule 3.5 anypoint studio. Please help me in completing this task
EDIT: (Pasted from a comment from the OP)
<flow name="CallServletServiceFlow1" doc:name="CallServletServiceFlow1">
<http:inbound-endpoint exchange-pattern="request-response" doc:name="HTTP" address="localhost:8080/servlet"/>
<object-to-string-transformer doc:name="Object to String"/>
<http:outbound-endpoint exchange-pattern="request-response" user="javauser" password="javauser1" method="GET" doc:name="HTTP" address="servlet/customTaskServlet?" connector-ref="HTTP_HTTPS"/>
</flow>

You seem to be copying old Mule 2 syntax in Mule 3, so this can not work.
Just use a simple HTTP outbound endpoint in your flow.
Reference: http://www.mulesoft.org/documentation/display/current/HTTP+Transport+Reference
EDIT: If you use the address attribute in HTTP endpoints (instead of the host port and path), you need to specify the scheme. So for example, you should replace address="localhost:8080/servlet" with address="http://localhost:8080/servlet".

Related

Mulesoft Rest Web service

I creating Rest Web service for fetching attachment file through http and storing file to local drive so i need filename dynamically as it is come in http request
My flow as follows
<foreach doc:name="For Each">
<set-payload value="#[payload.getInputStream() ]" doc:name="Get Inputstream from Payload"/>
<logger message="}}}#[message.inboundAttachments['payload']]" level="INFO" doc:name="Logger"/>
<file:outbound-endpoint path="F:\mule_data\e_files" responseTimeout="10000" doc:name="File" outputPattern="abcd.jpg"/>
</foreach>
<set-payload value="{"status":"success"}" doc:name="Generate JSON Response" />
</flow>
Here outputPattern="abcd.jpg" is harcoded so i need filename and format which coming through request
Please help
i refer from https://blogs.mulesoft.com/dev/mule-dev/soap-rest-attachments/
Please go through my answer in the question linked below.
How send a file in an HTTP request and upload it to file server via FTP in Mule
Once you are able to read/get the attachment file,you can get all the metadata associated with file.For reading name you can do following-
use #[payload.dataSource.part.fileName] to get name of attachment.

How to set full url dynamically in http request connector in Mule

As shown in this question, it is possible to have parts of the url in variables, when requesting an http endpoint with mule.
But I have a full url, returned from an endpoint supporting hateoas. Is it possible to use this full url for requests without splitting it up into several parts (host, port, path)?
With full url I mean sth. like "http://example.com/a/specific/path" instead of host="example.com", port="80", path="/a/specific/path"
One simple way to do is to split the url into multiple parts and set it into a flow variables. Then you can use this flow variables anywhere you want.
An simple example to do it is as follows:-
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="csv-to-smtpFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/aa" doc:name="HTTP"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="http://example.com:80/a/specific/path" doc:name="Set Payload"/>
<set-variable variableName="url" value="#[new URL(payload);]" doc:name="Variable"/>
<set-variable variableName="protocol" value="#[url.getProtocol();]" doc:name="Variable"/>
<set-variable variableName="host" value="#[url.getHost();]" doc:name="Variable"/>
<set-variable variableName="port" value="#[url.getPort();]" doc:name="Variable"/>
<set-variable variableName="path" value="#[url.getPath();]" doc:name="Variable"/>
<logger message="Done" level="INFO"/>
</flow>
Here you can see I am setting the url http://example.com:80/a/specific/path in a payload and then splitting it into host, port, path etc and storing it in variables.
Pls note if your url is in the form http://example.com:80/a/specific/path, you will get the port using expression #[url.getProtocol();]
But if your url is in the form http://example.com/a/specific/path, you will not get the port number.
Ref:- https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

Mule: How to dynamically set a url on an http request endpoint

Using the deprecated http implementation it was possible to dynamically set the url on an outbound http endpoint from the payload or properties:
<http:outbound-endpoint address="http://#[payload]" method="GET" />
Is it possible to do this using the new http request connector?
Yes, it is. Here is a simple example:
<http:request-config
name="HTTP_Request_Configuration"
host="#[flowVars.address]"
port="80"
basePath="/"
doc:name="HTTP Request Configuration"/>
<flow name="httpFlow">
...
<set-variable
variableName="address"
value="#[message.inboundProperties.'http.query.params'.site]"
doc:name="Set site address variable"/>
<http:request
config-ref="HTTP_Request_Configuration"
path="/"
method="GET"
doc:name="Get dynamic HTTP"/>
</flow>
Just define the host attribute using the MEL expression you require.
host="#[flowVars.someVariable]"

Mule HTTP parameter

I created a service where you call the mule service via http:
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8086" path="idnum" doc:name="HTTP"/>
so if you want to call this service you type:
http://localhost:8086/idnum
but what i want is for the http to accept an id number as a parameter and then store the id number into a variable so that i can use the id number. So the http would look like:
http://localhost:8086/idnum/4583948364094 for example.
So my question is how do you get the parameter from the url
From Mule-3.6 on wards, we have HTTP Listener Connector, using which you can pass URI parameters.
You can access the URI parameters using the below MEL
#[message.inboundProperties.'http.uri.params'.id] provided your URI should be like this: http://localhost:8086/idnum/{id}
You need to put the id as a message inbound parameter as following .. In Mule flow you need to do the following :-
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8086"
path="idnum" doc:name="HTTP"/>
<!-- storing id in variable myId -->
<set-variable variableName="myId"
value="#[message.inboundProperties['id']]"
doc:name="Variable"/>
<!-- Print the variable in console -->
<logger level="INFO" message="My id :- #[flowVars['myId']]"/>
</flow>
Now in browser access
http://localhost:8086/idnum/?id=4583948364094
Now you can store it in a variable in Mule and you can see it in a logger like:
My id :- 4583948364094

403 Forbidden error Mule using http outbound endpoint

I am trying to poll an RSS feed using Mule Studio but have been unsuccessful polling the information using the http outbound-endpoint in the Mule application. I have previously encountered the same issue using the http inbound-endpoint with the same site but was able to resolve the issue by assigning the User-Agent to MuleESB by setting the address to be
http://www.theaggie.org/feed/?User-Agent=MuleESB
However, now with the http outbound-endpoint, I am unable to poll the feed and get a 403 Forbidden error. My XML for the flow is
<flow name="aggregatorFlow1" doc:name="aggregatorFlow1">
<poll>
<processor-chain>
<set-variable variableName="httpMessages" value="#[[]]" />
<http:outbound-endpoint exchange-pattern="one-way" address="http://www.theaggie.org/feed/?User-Agent=MuleESB" method="GET" />
<expression-component>httpMessages.add(message.payloadAs(java.lang.String))</expression-component>
</processor-chain>
</poll>
<logger level="INFO" message="#[httpMessages]" />
</flow>
If I change the exchange-pattern to request-response, the logger would just output [ ]. I do not have issues with other sites using the same xml code.
Clearly the exchange-pattern should be request-response, as you seem to care about the payload of the response.
Try adding the following before the http:outbound-endpoint:
<set-property propertyName="User-Agent" value="MuleESB" />

Resources