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 + ")";
}
};
}
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");
}
}
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);
}
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());
}
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"));
}
Aggregations