use of org.apache.camel.component.rest.RestEndpoint in project camel by apache.
the class DefaultRestRegistry method apiDocAsJson.
@Override
public String apiDocAsJson() {
// see if there is a rest-api endpoint which would be the case if rest api-doc has been explicit enabled
if (apiProducer == null) {
Endpoint restApiEndpoint = null;
Endpoint restEndpoint = null;
for (Map.Entry<String, Endpoint> entry : camelContext.getEndpointMap().entrySet()) {
String uri = entry.getKey();
if (uri.startsWith("rest-api:")) {
restApiEndpoint = entry.getValue();
break;
} else if (restEndpoint == null && uri.startsWith("rest:")) {
restEndpoint = entry.getValue();
}
}
if (restApiEndpoint == null && restEndpoint != null) {
// no rest-api has been explicit enabled, then we need to create it first
RestEndpoint rest = (RestEndpoint) restEndpoint;
String componentName = rest.getComponentName();
if (componentName != null) {
RestConfiguration config = camelContext.getRestConfiguration(componentName, true);
String apiComponent = config.getApiComponent() != null ? config.getApiComponent() : RestApiEndpoint.DEFAULT_API_COMPONENT_NAME;
String path = config.getApiContextPath() != null ? config.getApiContextPath() : "api-doc";
restApiEndpoint = camelContext.getEndpoint(String.format("rest-api:%s/%s?componentName=%s&apiComponentName=%s&contextIdPattern=#name#", path, camelContext.getName(), componentName, apiComponent));
}
}
if (restApiEndpoint != null) {
// reuse the producer to avoid creating it
try {
apiProducer = restApiEndpoint.createProducer();
camelContext.addService(apiProducer, true);
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
}
if (apiProducer != null) {
try {
Exchange dummy = apiProducer.getEndpoint().createExchange();
apiProducer.process(dummy);
String json = dummy.hasOut() ? dummy.getOut().getBody(String.class) : dummy.getIn().getBody(String.class);
return json;
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
return null;
}
Aggregations