/** * Checks if any of the Camel routes is using an EIP with the given name * * @param camelContext the Camel context * @param name the name of the EIP * @return <tt>true</tt> if in use, <tt>false</tt> if not */ public static boolean isEipInUse(CamelContext camelContext, String name) { for (RouteDefinition route : camelContext.getRouteDefinitions()) { for (FromDefinition from : route.getInputs()) { if (name.equals(from.getShortName())) { return true; } } Iterator<ProcessorDefinition> it = ProcessorDefinitionHelper.filterTypeInOutputs(route.getOutputs(), ProcessorDefinition.class); while (it.hasNext()) { ProcessorDefinition def = it.next(); if (name.equals(def.getShortName())) { return true; } } } return false; }
@Test public void testCustomId() { RouteDefinition route = context.getRouteDefinition("myRoute"); assertNotNull(route); assertTrue(route.hasCustomIdAssigned()); FromDefinition from = route.getInputs().get(0); assertEquals("fromFile", from.getId()); assertTrue(from.hasCustomIdAssigned()); ChoiceDefinition choice = (ChoiceDefinition) route.getOutputs().get(0); assertEquals("myChoice", choice.getId()); assertTrue(choice.hasCustomIdAssigned()); WhenDefinition when = choice.getWhenClauses().get(0); assertTrue(when.hasCustomIdAssigned()); assertEquals("UK", when.getId()); LogDefinition log = (LogDefinition) choice.getOtherwise().getOutputs().get(0); assertFalse(log.hasCustomIdAssigned()); }
/** * Used for auto registering endpoints from the <tt>from</tt> or <tt>to</tt> DSL if they have an id attribute set */ protected void registerEndpointsWithIdsDefinedInFromOrToTypes(Element element, ParserContext parserContext, String contextId, Binder<Node> binder) { NodeList list = element.getChildNodes(); int size = list.getLength(); for (int i = 0; i < size; i++) { Node child = list.item(i); if (child instanceof Element) { Element childElement = (Element) child; Object object = binder.getJAXBNode(child); // we only want from/to types to be registered as endpoints if (object instanceof FromDefinition || object instanceof SendDefinition) { registerEndpoint(childElement, parserContext, contextId); } // recursive registerEndpointsWithIdsDefinedInFromOrToTypes(childElement, parserContext, contextId, binder); } } }
public void testCustomId() { RouteDefinition route = context.getRouteDefinition("myRoute"); assertNotNull(route); assertTrue(route.hasCustomIdAssigned()); FromDefinition from = route.getInputs().get(0); assertEquals("fromFile", from.getId()); assertTrue(from.hasCustomIdAssigned()); ChoiceDefinition choice = (ChoiceDefinition) route.getOutputs().get(0); assertEquals("myChoice", choice.getId()); assertTrue(choice.hasCustomIdAssigned()); WhenDefinition when = choice.getWhenClauses().get(0); assertTrue(when.hasCustomIdAssigned()); assertEquals("UK", when.getId()); LogDefinition log = (LogDefinition) choice.getOtherwise().getOutputs().get(0); assertFalse(log.hasCustomIdAssigned()); }
protected void assertValidContext(CamelContext context) { assertNotNull("No context found!", context); List<RouteDefinition> routes = ((ModelCamelContext)context).getRouteDefinitions(); LOG.debug("Found routes: " + routes); assertEquals("One Route should be found", 1, routes.size()); for (RouteDefinition route : routes) { List<FromDefinition> inputs = route.getInputs(); assertEquals("Number of inputs", 1, inputs.size()); FromDefinition fromType = inputs.get(0); assertEquals("from URI", "seda:test.a", fromType.getUri()); List<?> outputs = route.getOutputs(); assertEquals("Number of outputs", 1, outputs.size()); } }
public static String getUri(FromDefinition input) { String key = input.getUri(); if (Strings2.isEmpty(key)) { String ref = input.getRef(); if (!Strings2.isEmpty(ref)) { return "ref:" + ref; } } return key; }
private static void replaceInputs(Iterable<RouteDefinition> definitions, String oldFrom, String newFrom) { for (RouteDefinition definition : definitions) { List<FromDefinition> inputs = definition.getInputs(); for (int i = 0; i < inputs.size(); i++) { if (oldFrom.equals(inputs.get(i).getEndpointUri())) { inputs.set(i, new FromDefinition(newFrom)); } } } }
public static AdviceWithTask replaceFromWith(final RouteDefinition route, final String uri) { return new AdviceWithTask() { public void task() throws Exception { FromDefinition from = route.getInputs().get(0); LOG.info("AdviceWith replace input from [{}] --> [{}]", from.getUriOrRef(), uri); from.setEndpoint(null); from.setRef(null); from.setUri(uri); } }; }
public static AdviceWithTask replaceFrom(final RouteDefinition route, final Endpoint endpoint) { return new AdviceWithTask() { public void task() throws Exception { FromDefinition from = route.getInputs().get(0); LOG.info("AdviceWith replace input from [{}] --> [{}]", from.getUriOrRef(), endpoint.getEndpointUri()); from.setRef(null); from.setUri(null); from.setEndpoint(endpoint); } }; }
public void testStartRouteThenStopMutateAndStartRouteAgain() throws Exception { List<RouteDefinition> routes = context.getRouteDefinitions(); assertCollectionSize("Route", routes, 1); RouteDefinition route = routes.get(0); endpointA = getMandatoryEndpoint("seda:test.a", SedaEndpoint.class); endpointB = getMandatoryEndpoint("seda:test.b", SedaEndpoint.class); endpointC = getMandatoryEndpoint("seda:test.C", SedaEndpoint.class); // send from A over B to results MockEndpoint results = getMockEndpoint("mock:results"); results.expectedBodiesReceived(expectedBody); template.sendBody(endpointA, expectedBody); assertMockEndpointsSatisfied(); // stop the route context.stopRoute(route); // lets mutate the route... FromDefinition fromType = assertOneElement(route.getInputs()); fromType.setUri("seda:test.C"); context.startRoute(route); // now lets check it works // send from C over B to results results.reset(); results = getMockEndpoint("mock:results"); results.expectedBodiesReceived(expectedBody); template.sendBody(endpointC, expectedBody); assertMockEndpointsSatisfied(); }
private void findInputComponents(List<FromDefinition> defs, Set<String> components, Set<String> languages, Set<String> dataformats) { if (defs != null) { for (FromDefinition def : defs) { findUriComponent(def.getUri(), components); findSchedulerUriComponent(def.getUri(), components); } } }
@SuppressWarnings("deprecation") protected List<URI> getInnerContextConsumerList(CamelContext context) throws URISyntaxException { List<URI> consumerList = new ArrayList<URI>(); List<RouteDefinition> routeDefinitions = context.getRouteDefinitions(); for (RouteDefinition routeDefinition : routeDefinitions) { List<FromDefinition> inputs = routeDefinition.getInputs(); for (FromDefinition input : inputs) { consumerList.add(new URI(input.getUri())); } } return consumerList; }
/** * Get a map with the route id (key) and the priority (value) for 'from' endpoint * * @return */ protected Map<String, String> getRouteToEndpointPriority() { Map<String, String> answer = new HashMap<String, String>(); List<RouteDefinition> routes = context.getRouteDefinitions(); Assert.assertNotNull(routes); for (RouteDefinition current : routes) { Assert.assertEquals(new Integer(1), new Integer(current.getInputs().size())); Assert.assertNotNull(current.getInputs().get(0)); Assert.assertNotNull(current.getId()); Assert.assertTrue(current.getId().trim().length() > 0); FromDefinition from = current.getInputs().get(0); Endpoint endpoint = getMandatoryEndpoint(from.getUri()); Assert.assertNotNull(endpoint); Assert.assertTrue(endpoint instanceof LoadBalancedFileEndpoint); LoadBalancedFileEndpoint lbEndpoint = (LoadBalancedFileEndpoint)endpoint; GenericFileFilter filter = lbEndpoint.getFilter(); Assert.assertNotNull(filter); Assert.assertTrue(filter instanceof PriorityFileFilter); PriorityFileFilter priorityFileFilter = (PriorityFileFilter)filter; Assert.assertNotNull(priorityFileFilter.getPriorityName()); answer.put(current.getId(), priorityFileFilter.getPriorityName()); } return answer; }
public static String formatInputFromDefinition(FromDefinition from) { URI uri = URI.create(from.getEndpointUri()); if (uri.getScheme().startsWith("twitter")) { return formatInputFromURI(uri); } return uri.toString(); }
@Test public void testNotification() throws InterruptedException, IOException { String body = readFile(NOTIFICATION_JSON,Charset.defaultCharset()); out.setExpectedMessageCount(1); CamelContext context = context(); RouteDefinition routeDefinition = context.getRouteDefinition("WEBHOOK-TEST"); assertNotNull("RouteDefinition is null",routeDefinition); List<FromDefinition> inputs = routeDefinition.getInputs(); FromDefinition from = inputs.get(0); String uri = from.getEndpointUri(); uri = uri.replaceFirst("jetty:",""); LOG.debug("uri: {}", uri); // Send HTTP notification HttpClient httpclient = new HttpClient(); PostMethod httppost = new PostMethod(uri); Header contentHeader = new Header("Content-Type", "application/json"); httppost.setRequestHeader(contentHeader); StringRequestEntity reqEntity = new StringRequestEntity(body,null,null); httppost.setRequestEntity(reqEntity); int status = httpclient.executeMethod(httppost); assertEquals("Received wrong response status", 200, status); out.assertIsSatisfied(); List<Exchange> exchanges = out.getExchanges(); LOG.debug("EXCHANGE COUNT: {}",exchanges.size()); for (Exchange exchange : exchanges) { Message message = exchange.getIn(); String messageBody = message.getBody(String.class); Object o = message.getBody(); LOG.debug("class: " + o.getClass().toString()); LOG.debug("messageBody: " + messageBody); LOG.debug("id: " + exchange.getExchangeId()); //assertEquals("Body not equal",body,messageBody); } }
protected static void addInputs(CamelCatalog camelCatalog, NodeDto owner, List<FromDefinition> inputs) { Map<String, Integer> nodeCounts = new HashMap<>(); for (FromDefinition input : inputs) { addChild(camelCatalog, owner, input, nodeCounts); } }
public DefaultRouteContext(CamelContext camelContext, RouteDefinition route, FromDefinition from, Collection<Route> routes) { this.camelContext = camelContext; this.route = route; this.from = from; this.routes = routes; }
public FromDefinition getFrom() { return from; }
private void verifyRouteDefinitions(List<RouteDefinition> routeDefinitions, CamelComponentImplementationModel ccim) throws Exception { // service name & namespace // TODO what happens when we have multiple services? String serviceName = ccim.getComponent().getServices().get(0).getName(); String compositeNs = ccim.getComponent().getComposite().getTargetNamespace(); // number of switchyard:// consumers/from statements int serviceConsumer = 0; for (RouteDefinition routeDefinition : routeDefinitions) { if (routeDefinition.getInputs().isEmpty()) { throw CamelComponentMessages.MESSAGES.mustHaveAtLeastOneInput(); } for (FromDefinition fromDefinition : routeDefinition.getInputs()) { URI from = URI.create(this.getCamelContext().resolvePropertyPlaceholders(fromDefinition.getUri())); if (from.getScheme().equals(CamelConstants.SWITCHYARD_COMPONENT_NAME)) { if (serviceConsumer > 0) { throw CamelComponentMessages.MESSAGES.onlyOneSwitchYardInputPerImpl(); } String authority = from.getAuthority(); if (!serviceName.equals(authority)) { throw CamelComponentMessages.MESSAGES.implementationConsumerDoesNotMatchService(serviceName); } serviceConsumer++; } } List<ProcessorDefinition<?>> outputs = routeDefinition.getOutputs(); for (ProcessorDefinition<?> processorDefinition : outputs) { if (processorDefinition instanceof ToDefinition) { ToDefinition to = (ToDefinition) processorDefinition; URI componentUri = null; if (to.getRef() != null) { componentUri = URI.create(getCamelContext().getRegistry().lookupByNameAndType(to.getRef(), Endpoint.class).getEndpointUri()); } else if (to.getUri() != null) { componentUri = URI.create(this.getCamelContext().resolvePropertyPlaceholders(to.getUri())); } else { throw CamelComponentMessages.MESSAGES.couldNotResolveToEndpointUri(to.toString()); } if (componentUri.getScheme().equals(CamelConstants.SWITCHYARD_COMPONENT_NAME)) { final String referenceName = componentUri.getAuthority(); final QName refServiceName = new QName(compositeNs, referenceName); if (!containsServiceRef(ccim.getComponent().getReferences(), referenceName)) { throw CamelComponentMessages.MESSAGES.couldNotFindServiceReference(referenceName, to.toString()); } QName qualifiedRefName = ComponentNames.qualify( ccim.getComponent().getQName(), refServiceName); final ServiceReference service = getServiceDomain().getServiceReference(qualifiedRefName); if (service == null) { throw CamelComponentMessages.MESSAGES.couldNotFindServiceName(qualifiedRefName.toString(), to.toString()); } } } } } if (serviceConsumer != 1) { throw CamelComponentMessages.MESSAGES.cannotCreateComponentImpl(); } }
/** * Gets the from type * * @return the from type */ FromDefinition getFrom();