1 10 11 package org.mule.routing.outbound; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.mule.config.i18n.Message; 18 import org.mule.config.i18n.Messages; 19 import org.mule.impl.MuleMessage; 20 import org.mule.impl.endpoint.MuleEndpointURI; 21 import org.mule.umo.UMOException; 22 import org.mule.umo.UMOFilter; 23 import org.mule.umo.UMOMessage; 24 import org.mule.umo.UMOSession; 25 import org.mule.umo.endpoint.EndpointException; 26 import org.mule.umo.endpoint.UMOEndpoint; 27 import org.mule.umo.endpoint.UMOEndpointURI; 28 import org.mule.umo.routing.CouldNotRouteOutboundMessageException; 29 import org.mule.umo.routing.RoutePathNotFoundException; 30 import org.mule.umo.routing.RoutingException; 31 import org.mule.umo.transformer.TransformerException; 32 import org.mule.umo.transformer.UMOTransformer; 33 import org.mule.util.TemplateParser; 34 35 39 40 public class FilteringOutboundRouter extends AbstractOutboundRouter 41 { 42 private UMOTransformer transformer; 43 44 private UMOFilter filter; 45 46 private boolean useTemplates = false; 47 48 private TemplateParser parser = TemplateParser.createSquareBracesStyleParser(); 50 51 public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) 52 throws RoutingException 53 { 54 UMOMessage result = null; 55 56 if (endpoints == null || endpoints.size() == 0) 57 { 58 throw new RoutePathNotFoundException(new Message(Messages.NO_ENDPOINTS_FOR_ROUTER), message, null); 59 } 60 61 UMOEndpoint ep = getEndpoint(0, message); 62 63 try 64 { 65 if (synchronous) 66 { 67 result = send(session, message, ep); 68 } 69 else 70 { 71 dispatch(session, message, ep); 72 } 73 } 74 catch (UMOException e) 75 { 76 throw new CouldNotRouteOutboundMessageException(message, ep, e); 77 } 78 return result; 79 } 80 81 public UMOFilter getFilter() 82 { 83 return filter; 84 } 85 86 public void setFilter(UMOFilter filter) 87 { 88 this.filter = filter; 89 } 90 91 public boolean isMatch(UMOMessage message) throws RoutingException 92 { 93 if (getFilter() == null) 94 { 95 return true; 96 } 97 if (transformer != null) 98 { 99 try 100 { 101 Object payload = transformer.transform(message.getPayload()); 102 message = new MuleMessage(payload, message); 103 } 104 catch (TransformerException e) 105 { 106 throw new RoutingException(new Message(Messages.TRANSFORM_FAILED_BEFORE_FILTER), message, 107 (UMOEndpoint)endpoints.get(0), e); 108 } 109 } 110 return getFilter().accept(message); 111 } 112 113 public UMOTransformer getTransformer() 114 { 115 return transformer; 116 } 117 118 public void setTransformer(UMOTransformer transformer) 119 { 120 this.transformer = transformer; 121 } 122 123 public void addEndpoint(UMOEndpoint endpoint) 124 { 125 if (!useTemplates && parser.isContainsTemplate(endpoint.getEndpointURI().toString())) 126 { 127 useTemplates = true; 128 } 129 super.addEndpoint(endpoint); 130 } 131 132 143 public UMOEndpoint getEndpoint(int index, UMOMessage message) 144 throws CouldNotRouteOutboundMessageException 145 { 146 if (!useTemplates) 147 { 148 return (UMOEndpoint)endpoints.get(index); 149 } 150 else 151 { 152 UMOEndpoint ep = (UMOEndpoint)endpoints.get(index); 153 String uri = ep.getEndpointURI().toString(); 154 if (logger.isDebugEnabled()) 155 { 156 logger.debug("Uri before parsing is: " + uri); 157 } 158 Map props = new HashMap (); 159 props.putAll(ep.getProperties()); 162 for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();) 163 { 164 String propertyKey = (String )iterator.next(); 165 props.put(propertyKey, message.getProperty(propertyKey)); 166 } 167 String newUriString = parser.parse(props, uri); 168 if (logger.isDebugEnabled()) 169 { 170 logger.debug("Uri after parsing is: " + uri); 171 } 172 try 173 { 174 UMOEndpointURI newUri = new MuleEndpointURI(newUriString); 175 if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme())) 176 { 177 throw new CouldNotRouteOutboundMessageException(new Message( 178 Messages.SCHEME_CANT_CHANGE_FOR_ROUTER_X_X, ep.getEndpointURI().getScheme(), 179 newUri.getScheme()), message, ep); 180 } 181 ep.setEndpointURI(newUri); 182 } 183 catch (EndpointException e) 184 { 185 throw new CouldNotRouteOutboundMessageException(new Message( 186 Messages.TEMPLATE_X_CAUSED_MALFORMED_ENDPOINT_X, uri, newUriString), message, ep, e); 187 } 188 189 return ep; 190 } 191 } 192 193 public boolean isUseTemplates() 194 { 195 return useTemplates; 196 } 197 198 public void setUseTemplates(boolean useTemplates) 199 { 200 this.useTemplates = useTemplates; 201 } 202 } 203 | Popular Tags |