use of org.apache.camel.spi.RestProducerFactory 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.RestProducerFactory in project camel by apache.
the class SwaggerRestProducerFactory method createHttpProducer.
private Producer createHttpProducer(CamelContext camelContext, Swagger swagger, Operation operation, String host, String verb, String path, String queryParameters, String consumes, String produces, String componentName, Map<String, Object> parameters) throws Exception {
LOG.debug("Using Swagger operation: {} with {} {}", operation, verb, path);
RestProducerFactory factory = (RestProducerFactory) parameters.remove("restProducerFactory");
if (factory != null) {
LOG.debug("Using RestProducerFactory: {}", factory);
if (produces == null) {
CollectionStringBuffer csb = new CollectionStringBuffer(",");
List<String> list = operation.getProduces();
if (list == null) {
list = swagger.getProduces();
}
if (list != null) {
for (String s : list) {
csb.append(s);
}
}
produces = csb.isEmpty() ? null : csb.toString();
}
if (consumes == null) {
CollectionStringBuffer csb = new CollectionStringBuffer(",");
List<String> list = operation.getConsumes();
if (list == null) {
list = swagger.getConsumes();
}
if (list != null) {
for (String s : list) {
csb.append(s);
}
}
consumes = csb.isEmpty() ? null : csb.toString();
}
String basePath;
String uriTemplate;
if (host == null) {
// if no explicit host has been configured then use host and base path from the swagger api-doc
host = swagger.getHost();
basePath = swagger.getBasePath();
uriTemplate = path;
} else {
// path includes also uri template
basePath = path;
uriTemplate = null;
}
return factory.createProducer(camelContext, host, verb, basePath, uriTemplate, queryParameters, consumes, produces, parameters);
} else {
throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
}
}
Aggregations