« Return to Thread: mule 2 template-endpoint-router

Re: mule 2 template-endpoint-router

by rotten :: Rate this Message:

Reply to Author | View in Thread

Thanks D.

I wrote simple transformer to extract the stuff I wanted to append the end of the URL out of the message.  Then I chose to implement a simple extension of the Exception Router.  it seems to work.

I still think the Template Router is could probably do what I want, but I couldn't figure out how to use it.

-- Configured like this --

<custom-outbound-router class="org.my.custom.mule.urlAppendRouter">

    <http:outbound-endpoint host="${primary.host}"
                                       port="${primary.port}"
                                       path="/some/path/doSomething?myparameter="
                                       synchronous="true"
                                       contentType="text/plain" />
   
    <http:outbound-endpoint host="${backup.host}"
                                       port="${backup.port}"
                                       path="/some/path/doSomething?myparameter="
                                       synchronous="true"
                                       contentType="text/plain" />

</custom-outbound-router>

-- My implementation - urlAppendRouter.java --

package org.my.custom.mule;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.mule.api.MuleMessage;
import org.mule.api.endpoint.EndpointException;
import org.mule.api.endpoint.EndpointURI;
import org.mule.api.endpoint.OutboundEndpoint;
import org.mule.api.routing.CouldNotRouteOutboundMessageException;
import org.mule.config.i18n.CoreMessages;
import org.mule.endpoint.DynamicURIOutboundEndpoint;
import org.mule.endpoint.MuleEndpointURI;
import org.mule.routing.outbound.ExceptionBasedRouter;

import org.my.custom.log.*;

//
// This custom router appends whatever is left in the Message to
//    the end of the URL stub provided to router when it is invoked.
// It tries the primary, if it can't get through, it'll try the "backup".
//
// Configuration syntax is essentially the same as the Exception based Router.
//
// It still puts the message in the body of the POST, but that is ignored in our application.
//

public class urlAppendRouter extends ExceptionBasedRouter {

    //
    // Override the routine that pulls the endpoint out of the configuration.
    // (We aren't worried about supporting templates in this override...)
    //

    public OutboundEndpoint getEndpoint(int index, MuleMessage message) throws CouldNotRouteOutboundMessageException {

        // get the relevant endpoint (stub) from the config file
        OutboundEndpoint ep = (OutboundEndpoint)endpoints.get(index);

        String uri_stub = ep.getEndpointURI().toString();

        Log.debug("Found URI Stub: " + uri_stub, getClass());

        String newUriString = uri_stub + (String)message.getPayload();

        Log.debug("Appended (full) URI: " + newUriString, getClass());

        try {
            EndpointURI newUri = new MuleEndpointURI(newUriString);
            if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme())) {
                throw new CouldNotRouteOutboundMessageException(CoreMessages.schemeCannotChangeForRouter(ep
                        .getEndpointURI().getScheme(), newUri.getScheme()), message, ep);
            }

            return new DynamicURIOutboundEndpoint(ep, newUri);
        }
        catch (EndpointException e) {
            throw new CouldNotRouteOutboundMessageException(CoreMessages.templateCausedMalformedEndpoint(uri_stub,
                    newUriString), message, ep, e);
        }

    }
}


:-)  Thanks again for the pointers.  I've got something working, so I can move on to the next problem.

 « Return to Thread: mule 2 template-endpoint-router