use of org.apache.camel.spi.RestConfiguration in project camel by apache.
the class RestEndpoint method createConsumer.
@Override
public Consumer createConsumer(Processor processor) throws Exception {
RestConsumerFactory factory = null;
String cname = null;
if (getComponentName() != null) {
Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
if (comp != null && comp instanceof RestConsumerFactory) {
factory = (RestConsumerFactory) comp;
} else {
comp = getCamelContext().getComponent(getComponentName());
if (comp != null && comp instanceof RestConsumerFactory) {
factory = (RestConsumerFactory) comp;
}
}
if (factory == null) {
if (comp != null) {
throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestConsumerFactory");
} else {
throw new NoSuchBeanException(getComponentName(), RestConsumerFactory.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 RestConsumerFactory) {
factory = (RestConsumerFactory) comp;
cname = name;
break;
}
}
}
// lookup in registry
if (factory == null) {
Set<RestConsumerFactory> factories = getCamelContext().getRegistry().findByType(RestConsumerFactory.class);
if (factories != null && factories.size() == 1) {
factory = factories.iterator().next();
}
}
// and there must only be exactly one so we safely can pick this one
if (factory == null) {
RestConsumerFactory found = null;
String foundName = null;
for (String name : DEFAULT_REST_CONSUMER_COMPONENTS) {
Object comp = getCamelContext().getComponent(name, true);
if (comp != null && comp instanceof RestConsumerFactory) {
if (found == null) {
found = (RestConsumerFactory) comp;
foundName = name;
} else {
throw new IllegalArgumentException("Multiple RestConsumerFactory found on classpath. Configure explicit which component to use");
}
}
}
if (found != null) {
LOG.debug("Auto discovered {} as RestConsumerFactory", foundName);
factory = found;
}
}
if (factory != null) {
// if no explicit port/host configured, then use port from rest configuration
String scheme = "http";
String host = "";
int port = 80;
RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
if (config.getScheme() != null) {
scheme = config.getScheme();
}
if (config.getHost() != null) {
host = config.getHost();
}
int num = config.getPort();
if (num > 0) {
port = num;
}
// if no explicit hostname set then resolve the hostname
if (ObjectHelper.isEmpty(host)) {
if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
host = "0.0.0.0";
} else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
host = HostUtils.getLocalHostName();
} else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
host = HostUtils.getLocalIp();
}
}
// calculate the url to the rest service
String path = getPath();
if (!path.startsWith("/")) {
path = "/" + path;
}
// there may be an optional context path configured to help Camel calculate the correct urls for the REST services
// this may be needed when using camel-servlet where we cannot get the actual context-path or port number of the servlet engine
// during init of the servlet
String contextPath = config.getContextPath();
if (contextPath != null) {
if (!contextPath.startsWith("/")) {
path = "/" + contextPath + path;
} else {
path = contextPath + path;
}
}
String baseUrl = scheme + "://" + host + (port != 80 ? ":" + port : "") + path;
String url = baseUrl;
if (uriTemplate != null) {
// make sure to avoid double slashes
if (uriTemplate.startsWith("/")) {
url = url + uriTemplate;
} else {
url = url + "/" + uriTemplate;
}
}
Consumer consumer = factory.createConsumer(getCamelContext(), processor, getMethod(), getPath(), getUriTemplate(), getConsumes(), getProduces(), config, getParameters());
configureConsumer(consumer);
// add to rest registry so we can keep track of them, we will remove from the registry when the consumer is removed
// the rest registry will automatic keep track when the consumer is removed,
// and un-register the REST service from the registry
getCamelContext().getRestRegistry().addRestService(consumer, url, baseUrl, getPath(), getUriTemplate(), getMethod(), getConsumes(), getProduces(), getInType(), getOutType(), getRouteId(), getDescription());
return consumer;
} else {
throw new IllegalStateException("Cannot find RestConsumerFactory in Registry or as a Component to use");
}
}
use of org.apache.camel.spi.RestConfiguration in project camel by apache.
the class RestEndpoint method createProducer.
@Override
public Producer createProducer() throws Exception {
RestProducerFactory apiDocFactory = null;
RestProducerFactory factory = null;
if (apiDoc != null) {
LOG.debug("Discovering camel-swagger-java on classpath for using api-doc: {}", apiDoc);
// lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to classpath etc)
try {
FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
Object instance = finder.newInstance(DEFAULT_API_COMPONENT_NAME);
if (instance instanceof RestProducerFactory) {
// this factory from camel-swagger-java will facade the http component in use
apiDocFactory = (RestProducerFactory) instance;
}
parameters.put("apiDoc", apiDoc);
} catch (NoFactoryAvailableException e) {
throw new IllegalStateException("Cannot find camel-swagger-java on classpath to use with api-doc: " + apiDoc);
}
}
String cname = getComponentName();
if (cname != null) {
Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
if (comp != null && comp instanceof RestProducerFactory) {
factory = (RestProducerFactory) comp;
} else {
comp = getCamelContext().getComponent(getComponentName());
if (comp != null && comp instanceof RestProducerFactory) {
factory = (RestProducerFactory) comp;
}
}
if (factory == null) {
if (comp != null) {
throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestProducerFactory");
} else {
throw new NoSuchBeanException(getComponentName(), RestProducerFactory.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 RestProducerFactory) {
factory = (RestProducerFactory) comp;
cname = name;
break;
}
}
}
parameters.put("componentName", cname);
// lookup in registry
if (factory == null) {
Set<RestProducerFactory> factories = getCamelContext().getRegistry().findByType(RestProducerFactory.class);
if (factories != null && factories.size() == 1) {
factory = factories.iterator().next();
}
}
// and there must only be exactly one so we safely can pick this one
if (factory == null) {
RestProducerFactory found = null;
String foundName = null;
for (String name : DEFAULT_REST_PRODUCER_COMPONENTS) {
Object comp = getCamelContext().getComponent(name, true);
if (comp != null && comp instanceof RestProducerFactory) {
if (found == null) {
found = (RestProducerFactory) comp;
foundName = name;
} else {
throw new IllegalArgumentException("Multiple RestProducerFactory found on classpath. Configure explicit which component to use");
}
}
}
if (found != null) {
LOG.debug("Auto discovered {} as RestProducerFactory", foundName);
factory = found;
}
}
if (factory != null) {
LOG.debug("Using RestProducerFactory: {}", factory);
Producer producer;
if (apiDocFactory != null) {
// wrap the factory using the api doc factory which will use the factory
parameters.put("restProducerFactory", factory);
producer = apiDocFactory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
} else {
producer = factory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
}
RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
RestProducer answer = new RestProducer(this, producer, config);
answer.setOutType(outType);
answer.setType(inType);
return answer;
} else {
throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
}
}
use of org.apache.camel.spi.RestConfiguration in project camel by apache.
the class CamelRestAutoConfiguration method restConfiguration.
@Bean(name = RestConstants.DEFAULT_REST_CONFIGURATION_ID)
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean
public RestConfiguration restConfiguration() throws Exception {
final RestConfiguration configuration = new RestConfiguration();
// Copy properties
IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), configuration, IntrospectionSupport.getNonNullProperties(config));
return configuration;
}
use of org.apache.camel.spi.RestConfiguration in project camel by apache.
the class SparkComponent method doCreateConsumer.
Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception {
String path = basePath;
if (uriTemplate != null) {
// make sure to avoid double slashes
if (uriTemplate.startsWith("/")) {
path = path + uriTemplate;
} else {
path = path + "/" + uriTemplate;
}
}
path = FileUtil.stripLeadingSeparator(path);
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("spark-rest", true);
}
Map<String, Object> map = new HashMap<String, Object>();
if (consumes != null) {
map.put("accept", consumes);
}
// setup endpoint options
if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
map.putAll(config.getEndpointProperties());
}
if (ObjectHelper.isNotEmpty(path)) {
// spark-rest uses :name syntax instead of {name} so we need to replace those
Matcher matcher = PATTERN.matcher(path);
path = matcher.replaceAll(":$1");
}
// prefix path with context-path if configured in rest-dsl configuration
String contextPath = config.getContextPath();
if (ObjectHelper.isNotEmpty(contextPath)) {
contextPath = FileUtil.stripTrailingSeparator(contextPath);
contextPath = FileUtil.stripLeadingSeparator(contextPath);
if (ObjectHelper.isNotEmpty(contextPath)) {
path = contextPath + "/" + path;
}
}
String url;
if (api) {
url = "spark-rest:%s:%s?matchOnUriPrefix=true";
} else {
url = "spark-rest:%s:%s";
}
url = String.format(url, verb, path);
String query = URISupport.createQueryString(map);
if (!query.isEmpty()) {
url = url + "?" + query;
}
// get the endpoint
SparkEndpoint endpoint = camelContext.getEndpoint(url, SparkEndpoint.class);
setProperties(camelContext, endpoint, parameters);
// configure consumer properties
Consumer consumer = endpoint.createConsumer(processor);
if (config.isEnableCORS()) {
// if CORS is enabled then configure that on the spark consumer
if (config.getConsumerProperties() == null) {
config.setConsumerProperties(new HashMap<String, Object>());
}
config.getConsumerProperties().put("enableCors", true);
}
if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
setProperties(camelContext, consumer, config.getConsumerProperties());
}
return consumer;
}
use of org.apache.camel.spi.RestConfiguration in project camel by apache.
the class UndertowComponent method doCreateConsumer.
Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception {
String path = basePath;
if (uriTemplate != null) {
// make sure to avoid double slashes
if (uriTemplate.startsWith("/")) {
path = path + uriTemplate;
} else {
path = path + "/" + uriTemplate;
}
}
path = FileUtil.stripLeadingSeparator(path);
String scheme = "http";
String host = "";
int port = 0;
RestConfiguration config = configuration;
if (config == null) {
config = camelContext.getRestConfiguration("undertow", true);
}
if (config.getScheme() != null) {
scheme = config.getScheme();
}
if (config.getHost() != null) {
host = config.getHost();
}
int num = config.getPort();
if (num > 0) {
port = num;
}
// prefix path with context-path if configured in rest-dsl configuration
String contextPath = config.getContextPath();
if (ObjectHelper.isNotEmpty(contextPath)) {
contextPath = FileUtil.stripTrailingSeparator(contextPath);
contextPath = FileUtil.stripLeadingSeparator(contextPath);
if (ObjectHelper.isNotEmpty(contextPath)) {
path = contextPath + "/" + path;
}
}
// if no explicit hostname set then resolve the hostname
if (ObjectHelper.isEmpty(host)) {
if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
host = "0.0.0.0";
} else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
host = HostUtils.getLocalHostName();
} else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
host = HostUtils.getLocalIp();
}
}
Map<String, Object> map = new HashMap<String, Object>();
// build query string, and append any endpoint configuration properties
if (config.getComponent() == null || config.getComponent().equals("undertow")) {
// setup endpoint options
if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
map.putAll(config.getEndpointProperties());
}
}
boolean cors = config.isEnableCORS();
if (cors) {
// allow HTTP Options as we want to handle CORS in rest-dsl
map.put("optionsEnabled", "true");
}
String query = URISupport.createQueryString(map);
String url;
if (api) {
url = "undertow:%s://%s:%s/%s?matchOnUriPrefix=true&httpMethodRestrict=%s";
} else {
url = "undertow:%s://%s:%s/%s?httpMethodRestrict=%s";
}
// must use upper case for restrict
String restrict = verb.toUpperCase(Locale.US);
if (cors) {
restrict += ",OPTIONS";
}
// get the endpoint
url = String.format(url, scheme, host, port, path, restrict);
if (!query.isEmpty()) {
url = url + "&" + query;
}
UndertowEndpoint endpoint = camelContext.getEndpoint(url, UndertowEndpoint.class);
setProperties(camelContext, endpoint, parameters);
if (!map.containsKey("undertowHttpBinding")) {
// use the rest binding, if not using a custom http binding
endpoint.setUndertowHttpBinding(new RestUndertowHttpBinding());
}
// configure consumer properties
Consumer consumer = endpoint.createConsumer(processor);
if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
setProperties(camelContext, consumer, config.getConsumerProperties());
}
return consumer;
}
Aggregations