Come mantenere ospite in entrata con il router Mule ESB personalizzato
Domanda
Ho creato un router personalizzato con un endpoint. Il router personalizzato cerca la destinazione del punto finale in base ai parametri URL dell'URL in entrata. Ho un esempio di questo attivo e funzionante, e sto testando fuori in un browser. Sto cercando di risolvere un'ultima cosa con questo. Quando faccio la chiamata nel browser utilizzando http: // localhost: 8787 / my-site , il chiamata fa un redirect e l'URL nel browser cambia in http://server2.xyz.com: 8080 / my-sito . Non voglio all'utente di vedere sempre http://server2.xyz.com:8080/ my-sito . Voglio all'utente di vedere sempre http: // localhost: 8787 / my-sito . Come posso raggiungere questo obiettivo? Sto usando Mule edizione 2.2.1 comunità con Java 1.6.
Ecco il mio file di configurazione Mulo:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:http="http://www.mulesource.org/schema/mule/http/2.2"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd">
<model name="ProxyService">
<service name="HttpProxyService">
<inbound>
<http:inbound-endpoint address="http://localhost:8787" synchronous="true"/>
</inbound>
<outbound>
<custom-outbound-router class="com.abc.xyz.routing.LookupOutboundRouter">
<outbound-endpoint name="custom" address="http://nonexistant.server.com:8080" synchronous="true"/>
</custom-outbound-router>
</outbound>
</service>
</model>
</mule>
Ecco il mio router personalizzato:
public class LookupOutboundRouter extends AbstractOutboundRouter {
Logger logger = Logger.getLogger(LookupOutboundRouter.class);
@Override
public boolean isMatch(MuleMessage message) throws MessagingException {
return true;
}
@Override
public MuleMessage route(MuleMessage message, MuleSession session) throws MessagingException {
String[] urlValues = StringUtils.split(message.getProperty("http.request").toString(), "/");
String newUri = lookupServiceUri(urlValues[0]) + urlValues[1];
logger.info("newUri=" + newUri);
DynamicURIOutboundEndpoint ep;
try {
ep = new DynamicURIOutboundEndpoint((OutboundEndpoint) getEndpoints().get(0), new MuleEndpointURI(newUri));
MuleMessage message2 = send(session, message, ep);
return message2;
} catch (EndpointException e1) {
e1.printStackTrace();
} catch (MuleException e) {
e.printStackTrace();
}
return null;
}
/**
* This will call the service registry.
* @param id
* @return
*/
private String lookupServiceUri(String id) {
if(id.equalsIgnoreCase("12345")) {
return "http://server.xyz.com:8080/";
} else {
return "http://server2.xyz.com:8080/";
}
}
}
Soluzione
Sono stato in grado di raggiungere questo obiettivo nel browser impostando followRedirects true sul connettore HTTP. L'unico problema con questo ora è che non funziona per i reindirizzamenti POST. Sto facendo una chiamata SOAP da SoapUI ora invece di utilizzare il browser.
Entity enclosing requests cannot be redirected without user intervention
Message : Failed to route event via endpoint: org.mule.endpoint.DynamicURIOutboundEndpoint@fd285ee0. Message payload is of type: PostMethod
Type : org.mule.api.transport.DispatchException
Code : MULE_ERROR-42999
Payload : org.apache.commons.httpclient.methods.PostMethod@9fa8f
JavaDoc : http://www.mulesource.org/docs/site/current2/apidocs/org/mule/api/transport/DispatchException.html