Como criar um http proxy dinamico com mule parte 2
1. Como criar um HTTP proxy dinamico com Mule –
Parte 2
As razões para armazenar essa informação de uma classe de feijão
dedicado é para tornar mais fácil estender a classe com a
informação adicional, para facilitar a migração de armazenamento
numa base de dados e para manter os diferentes tipos de dados
armazenados no contexto de mula a um mínimo.
Configuração Mule HTTP proxy dinamico
A configuração Mule dinâmica proxy HTTP é implementado como
segue:
<?xml version="1.0" encoding="UTF-8"?>
<!--
The dynamic HTTP proxy Mule configuration file.
Author: Ivan Krizsan
-->
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripti
ng"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:test="http://www.mulesoft.org/schema/mule/test"
version="CE-3.4.0"
xsi:schemaLocation="http://www.springframework.org/schema/be
ans http://www.springframework.org/schema/beans/spring-beans-cu
rrent.xsd
http://www.springframework.org/schema/util http://www.springframe
work.org/schema/util/spring-util-current.xsd
2. 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/s
chema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/s
chema/mule/test/current/mule-test.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.
org/schema/mule/scripting/current/mule-scripting.xsd">
<spring:beans>
<!--
Mappings from path to server represented by a hash map.
A map has been choosen to limit the scope of this example.
Storing data about mappings between path to server in a dat
abase
will enable runtime modifications to the mapping data without
having to stop and restart the general proxy Mule application
.
-->
<util:map id="pathToServerAndPortMapping" map-class="java.
util.HashMap">
<!-- Entry for MyServer. -->
<spring:entry key="services/GreetingService">
<spring:bean class="com.ivan.mule.dynamichttpproxy.Ser
verInformationBean">
<spring:constructor-arg value="localhost"/>
<spring:constructor-arg value="8182"/>
<spring:constructor-arg value="MyServer"/>
</spring:bean>
</spring:entry>
<!-- Entry for SomeOtherServer. -->
<spring:entry key="services/GreetingService?wsdl">
3. <spring:bean class="com.ivan.mule.dynamichttpproxy.Ser
verInformationBean">
<spring:constructor-arg value="127.0.0.1"/>
<spring:constructor-arg value="8182"/>
<spring:constructor-arg value="SomeOtherServer"/>
</spring:bean>
</spring:entry>
</util:map>
</spring:beans>
<flow name="HTTPGeneralProxyFlow">
<!--
Note that if you increase the length of the path to, for instanc
e
generalProxy/additionalPath, then the expression determinin
g
the outgoing path need to be modified accordingly.
Changing the path, without changing its length, require no
modification to outgoing path.
-->
<http:inbound-endpoint
exchange-pattern="request-response"
host="localhost"
port="8981"
path="dynamicHttpProxy" doc:name="HTTP Receiver"/>
<!-- Extract outgoing path from received HTTP request. -->
<set-property
value="#[org.mule.util.StringUtils.substringAfter(org.mule.util.
StringUtils.substringAfter(message.inboundProperties['http.request'],
'/'), '/')]"
4. propertyName="outboundPath"
doc:name="Extract Outbound Path From Request" />
<logger message="#[string:Outbound path = #[message.outbo
undProperties['outboundPath']]]" level="DEBUG"/>
<!--
Using the HTTP request path, select which server to forward
the request to.
Note that there should be some kind of error handling in cas
e there is no server for the current path.
Error handling has been omitted in this example.
-->
<enricher target="#[variable:outboundServer]">
<scripting:component doc:name="Groovy">
<!--
If storing mapping data in a database, this Groovy script
should be replaced with a database query.
-->
<scripting:script engine="Groovy">
<![CDATA[
def theMap = muleContext.getRegistry().lookupObjec
t("pathToServerAndPortMapping")
def String theOutboundPath = message.getOutbound
Property("outboundPath")
def theServerBean = theMap[theOutboundPath]
theServerBean
]]>
</scripting:script>
</scripting:component>
</enricher>
5. <logger
message="#[string:Server address = #[groovy:message.getI
nvocationProperty('outboundServer').serverAddress]]"
level="DEBUG"/>
<logger
message="#[string:Server port = #[groovy:message.getInvoc
ationProperty('outboundServer').serverPort]]"
level="DEBUG"/>
<logger
message="#[string:Server name = #[groovy:message.getInv
ocationProperty('outboundServer').serverName]]"
level="DEBUG"/>
<!-- Log the request and its metadata for development purpose
s, -->
<test:component logMessageDetails="true"/>
<!--
Cannot use a MEL expression in the value of the method attr
ibute
on the HTTP outbound endpoints so have to revert to this wa
y of
selecting HTTP method in the outgoing request.
In this example, only support for GET and POST has been i
mplemented.
This can of course easily be extended to support additional
HTTP
verbs as desired.
-->
<choice doc:name="Choice">
<!-- Forward HTTP GET requests. -->