Search in sources :

Example 1 with RestEndpoint

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;
}
Also used : Exchange(org.apache.camel.Exchange) RestEndpoint(org.apache.camel.component.rest.RestEndpoint) RestApiEndpoint(org.apache.camel.component.rest.RestApiEndpoint) Endpoint(org.apache.camel.Endpoint) RestEndpoint(org.apache.camel.component.rest.RestEndpoint) RestConfiguration(org.apache.camel.spi.RestConfiguration) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 RestApiEndpoint (org.apache.camel.component.rest.RestApiEndpoint)1 RestEndpoint (org.apache.camel.component.rest.RestEndpoint)1 RestConfiguration (org.apache.camel.spi.RestConfiguration)1