Search in sources :

Example 1 with Component

use of org.apache.camel.Component in project camel by apache.

the class ExpressionBuilder method propertiesComponentExpression.

public static Expression propertiesComponentExpression(final String key, final String locations, final String defaultValue) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = simpleExpression(key).evaluate(exchange, String.class);
            String text2 = simpleExpression(locations).evaluate(exchange, String.class);
            try {
                if (text2 != null) {
                    // the properties component is optional as we got locations
                    // getComponent will create a new component if none already exists
                    Component component = exchange.getContext().getComponent("properties");
                    PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
                    // enclose key with {{ }} to force parsing
                    String[] paths = text2.split(",");
                    return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken(), paths);
                } else {
                    // the properties component is mandatory if no locations provided
                    Component component = exchange.getContext().hasComponent("properties");
                    if (component == null) {
                        throw new IllegalArgumentException("PropertiesComponent with name properties must be defined" + " in CamelContext to support property placeholders in expressions");
                    }
                    PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
                    // enclose key with {{ }} to force parsing
                    return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken());
                }
            } catch (Exception e) {
                // property with key not found, use default value if provided
                if (defaultValue != null) {
                    return defaultValue;
                }
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }

        @Override
        public String toString() {
            return "properties(" + key + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Component(org.apache.camel.Component) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 2 with Component

use of org.apache.camel.Component in project camel by apache.

the class RestApiEndpoint method createConsumer.

@Override
public Consumer createConsumer(Processor processor) throws Exception {
    RestApiConsumerFactory factory = null;
    String cname = null;
    // the API then uses the api component (eg usually camel-swagger-java) to build the API
    if (getComponentName() != null) {
        Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
        if (comp != null && comp instanceof RestApiConsumerFactory) {
            factory = (RestApiConsumerFactory) comp;
        } else {
            comp = getCamelContext().getComponent(getComponentName());
            if (comp != null && comp instanceof RestApiConsumerFactory) {
                factory = (RestApiConsumerFactory) comp;
            }
        }
        if (factory == null) {
            if (comp != null) {
                throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestApiConsumerFactory");
            } else {
                throw new NoSuchBeanException(getComponentName(), RestApiConsumerFactory.class.getName());
            }
        }
        cname = getComponentName();
    }
    // try all components
    if (factory == null) {
        for (String name : getCamelContext().getComponentNames()) {
            Component comp = getCamelContext().getComponent(name);
            if (comp != null && comp instanceof RestApiConsumerFactory) {
                factory = (RestApiConsumerFactory) comp;
                cname = name;
                break;
            }
        }
    }
    // lookup in registry
    if (factory == null) {
        Set<RestApiConsumerFactory> factories = getCamelContext().getRegistry().findByType(RestApiConsumerFactory.class);
        if (factories != null && factories.size() == 1) {
            factory = factories.iterator().next();
        }
    }
    if (factory != null) {
        // calculate the url to the rest API service
        RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
        // calculate the url to the rest API service
        String path = getPath();
        if (path != null && !path.startsWith("/")) {
            path = "/" + path;
        }
        Consumer consumer = factory.createApiConsumer(getCamelContext(), processor, path, config, getParameters());
        configureConsumer(consumer);
        return consumer;
    } else {
        throw new IllegalStateException("Cannot find RestApiConsumerFactory in Registry or as a Component to use");
    }
}
Also used : Consumer(org.apache.camel.Consumer) RestApiConsumerFactory(org.apache.camel.spi.RestApiConsumerFactory) NoSuchBeanException(org.apache.camel.NoSuchBeanException) RestConfiguration(org.apache.camel.spi.RestConfiguration) Component(org.apache.camel.Component)

Example 3 with Component

use of org.apache.camel.Component in project camel by apache.

the class RefComponentTest method bindToRegistry.

private void bindToRegistry(JndiRegistry jndi) throws Exception {
    Component comp = new DirectComponent();
    comp.setCamelContext(context);
    Endpoint slow = comp.createEndpoint("direct:somename");
    Consumer consumer = slow.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
            template.send("mock:result", exchange);
        }
    });
    consumer.start();
    // bind our endpoint to the registry for ref to lookup
    jndi.bind("foo", slow);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Consumer(org.apache.camel.Consumer) DirectComponent(org.apache.camel.component.direct.DirectComponent) Component(org.apache.camel.Component) DirectComponent(org.apache.camel.component.direct.DirectComponent)

Example 4 with Component

use of org.apache.camel.Component in project camel by apache.

the class ComponentConfigurationTest method testIntrospectDefaultComponentParameters.

/**
     * Shows we can introspect the parameters of a DefaultComponent (i.e. a non {@link UriEndpointComponent})
     * though we only get to introspect the parameter values from teh current configuration
     */
@Test
public void testIntrospectDefaultComponentParameters() throws Exception {
    Component component = context.getComponent("cheese");
    ComponentConfiguration configuration = component.createComponentConfiguration();
    assertNotNull("Should have created a ComponentConfiguration for component " + component, configuration);
    SortedMap<String, ParameterConfiguration> parameterMap = configuration.getParameterConfigurationMap();
    assertTrue("getParameterConfigurationMap() should be empty as we have no parameters yet", parameterMap.isEmpty());
    // configure the uri and query parameters
    configuration.setUriString("somePath?foo=something&bar=123");
    parameterMap = configuration.getParameterConfigurationMap();
    assertEquals("getParameterConfigurationMap() size", 2, parameterMap.size());
    ParameterConfiguration barConfiguration = configuration.getParameterConfiguration("bar");
    assertNotNull("should hav a configuration for 'bar'", barConfiguration);
    assertEquals("barConfiguration.getName()", "bar", barConfiguration.getName());
    assertEquals("barConfiguration.getParameterType()", String.class, barConfiguration.getParameterType());
}
Also used : ComponentConfiguration(org.apache.camel.ComponentConfiguration) Component(org.apache.camel.Component) SedaComponent(org.apache.camel.component.seda.SedaComponent) Test(org.junit.Test)

Example 5 with Component

use of org.apache.camel.Component in project camel by apache.

the class ComponentConfigurationTest method testSetParametersFromUriString.

/**
     * Test that parameters are strictly typed on {@link UriEndpointComponent}s
     */
@Test
public void testSetParametersFromUriString() throws Exception {
    Component component = context.getComponent("seda");
    ComponentConfiguration configuration = component.createComponentConfiguration();
    assertNotNull("Should have created a ComponentConfiguration for component " + component, configuration);
    // configure the uri and query parameters
    configuration.setUriString("foo?concurrentConsumers=5&size=1000");
    // notice the parameters are all correctly typed due to the use of a UriEndpointComponent
    // and the associated @UriEndpoint / @UriParam annotations
    assertEquals("concurrentConsumers", 5, configuration.getParameter("concurrentConsumers"));
    assertEquals("size", 1000, configuration.getParameter("size"));
    configuration.setUriString("foo?concurrentConsumers=9&size=2000");
    assertEquals("concurrentConsumers", 9, configuration.getParameter("concurrentConsumers"));
    assertEquals("size", 2000, configuration.getParameter("size"));
}
Also used : ComponentConfiguration(org.apache.camel.ComponentConfiguration) Component(org.apache.camel.Component) SedaComponent(org.apache.camel.component.seda.SedaComponent) Test(org.junit.Test)

Aggregations

Component (org.apache.camel.Component)52 Test (org.junit.Test)24 ComponentConfiguration (org.apache.camel.ComponentConfiguration)14 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)9 SedaComponent (org.apache.camel.component.seda.SedaComponent)9 Map (java.util.Map)7 CamelContext (org.apache.camel.CamelContext)7 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)7 IOException (java.io.IOException)6 Endpoint (org.apache.camel.Endpoint)6 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)6 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)6 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)5 LinkedHashMap (java.util.LinkedHashMap)4 MalformedObjectNameException (javax.management.MalformedObjectNameException)4 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)4 NoSuchBeanException (org.apache.camel.NoSuchBeanException)4 RuntimeCamelException (org.apache.camel.RuntimeCamelException)4 VetoCamelContextStartException (org.apache.camel.VetoCamelContextStartException)4 DirectComponent (org.apache.camel.component.direct.DirectComponent)4