Search in sources :

Example 1 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration 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");
    }
}
Also used : Consumer(org.apache.camel.Consumer) RestApiConsumerFactory(org.apache.camel.spi.RestApiConsumerFactory) NoSuchBeanException(org.apache.camel.NoSuchBeanException) RestConfiguration(org.apache.camel.spi.RestConfiguration) Component(org.apache.camel.Component)

Example 2 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RestComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    String restConfigurationName = getAndRemoveParameter(parameters, "componentName", String.class, componentName);
    RestEndpoint answer = new RestEndpoint(uri, this);
    answer.setComponentName(restConfigurationName);
    answer.setApiDoc(apiDoc);
    RestConfiguration config = new RestConfiguration();
    mergeConfigurations(config, findGlobalRestConfiguration());
    mergeConfigurations(config, getCamelContext().getRestConfiguration(restConfigurationName, true));
    // if no explicit host was given, then fallback and use default configured host
    String h = resolveAndRemoveReferenceParameter(parameters, "host", String.class, host);
    if (h == null && config != null) {
        h = config.getHost();
        int port = config.getPort();
        // is there a custom port number
        if (port > 0 && port != 80 && port != 443) {
            h += ":" + port;
        }
    }
    // host must start with http:// or https://
    if (h != null && !(h.startsWith("http://") || h.startsWith("https://"))) {
        h = "http://" + h;
    }
    answer.setHost(h);
    String query = ObjectHelper.after(uri, "?");
    if (query != null) {
        answer.setQueryParameters(query);
    }
    setProperties(answer, parameters);
    answer.setParameters(parameters);
    if (!remaining.contains(":")) {
        throw new IllegalArgumentException("Invalid syntax. Must be rest:method:path[:uriTemplate] where uriTemplate is optional");
    }
    String method = ObjectHelper.before(remaining, ":");
    String s = ObjectHelper.after(remaining, ":");
    String path;
    String uriTemplate;
    if (s != null && s.contains(":")) {
        path = ObjectHelper.before(s, ":");
        uriTemplate = ObjectHelper.after(s, ":");
    } else {
        path = s;
        uriTemplate = null;
    }
    // remove trailing slashes
    path = FileUtil.stripTrailingSeparator(path);
    uriTemplate = FileUtil.stripTrailingSeparator(uriTemplate);
    answer.setMethod(method);
    answer.setPath(path);
    answer.setUriTemplate(uriTemplate);
    // if no explicit component name was given, then fallback and use default configured component name
    if (answer.getComponentName() == null && config != null) {
        String name = config.getProducerComponent();
        if (name == null) {
            // fallback and use the consumer name
            name = config.getComponent();
        }
        answer.setComponentName(name);
    }
    // if no explicit producer api was given, then fallback and use default configured
    if (answer.getApiDoc() == null && config != null) {
        answer.setApiDoc(config.getProducerApiDoc());
    }
    return answer;
}
Also used : RestConfiguration(org.apache.camel.spi.RestConfiguration) Endpoint(org.apache.camel.Endpoint)

Example 3 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class RestConfigurationDefinition method asRestConfiguration.

// Implementation
//-------------------------------------------------------------------------
/**
     * Creates a {@link org.apache.camel.spi.RestConfiguration} instance based on the definition
     *
     * @param context     the camel context
     * @return the configuration
     * @throws Exception is thrown if error creating the configuration
     */
public RestConfiguration asRestConfiguration(CamelContext context) throws Exception {
    RestConfiguration answer = new RestConfiguration();
    if (component != null) {
        answer.setComponent(CamelContextHelper.parseText(context, component));
    }
    if (apiComponent != null) {
        answer.setApiComponent(CamelContextHelper.parseText(context, apiComponent));
    }
    if (producerComponent != null) {
        answer.setProducerComponent(CamelContextHelper.parseText(context, producerComponent));
    }
    if (scheme != null) {
        answer.setScheme(CamelContextHelper.parseText(context, scheme));
    }
    if (host != null) {
        answer.setHost(CamelContextHelper.parseText(context, host));
    }
    if (port != null) {
        answer.setPort(CamelContextHelper.parseInteger(context, port));
    }
    if (producerApiDoc != null) {
        answer.setProducerApiDoc(CamelContextHelper.parseText(context, producerApiDoc));
    }
    if (apiContextPath != null) {
        answer.setApiContextPath(CamelContextHelper.parseText(context, apiContextPath));
    }
    if (apiContextRouteId != null) {
        answer.setApiContextRouteId(CamelContextHelper.parseText(context, apiContextRouteId));
    }
    if (apiContextIdPattern != null) {
        // special to allow #name# to refer to itself
        if ("#name#".equals(apiComponent)) {
            answer.setApiContextIdPattern(context.getName());
        } else {
            answer.setApiContextIdPattern(CamelContextHelper.parseText(context, apiContextIdPattern));
        }
    }
    if (apiContextListing != null) {
        answer.setApiContextListing(apiContextListing);
    }
    if (contextPath != null) {
        answer.setContextPath(CamelContextHelper.parseText(context, contextPath));
    }
    if (hostNameResolver != null) {
        answer.setRestHostNameResolver(hostNameResolver.name());
    }
    if (bindingMode != null) {
        answer.setBindingMode(bindingMode.name());
    }
    if (skipBindingOnErrorCode != null) {
        answer.setSkipBindingOnErrorCode(skipBindingOnErrorCode);
    }
    if (enableCORS != null) {
        answer.setEnableCORS(enableCORS);
    }
    if (jsonDataFormat != null) {
        answer.setJsonDataFormat(jsonDataFormat);
    }
    if (xmlDataFormat != null) {
        answer.setXmlDataFormat(xmlDataFormat);
    }
    if (!componentProperties.isEmpty()) {
        Map<String, Object> props = new HashMap<String, Object>();
        for (RestPropertyDefinition prop : componentProperties) {
            String key = prop.getKey();
            String value = CamelContextHelper.parseText(context, prop.getValue());
            props.put(key, value);
        }
        answer.setComponentProperties(props);
    }
    if (!endpointProperties.isEmpty()) {
        Map<String, Object> props = new HashMap<String, Object>();
        for (RestPropertyDefinition prop : endpointProperties) {
            String key = prop.getKey();
            String value = CamelContextHelper.parseText(context, prop.getValue());
            props.put(key, value);
        }
        answer.setEndpointProperties(props);
    }
    if (!consumerProperties.isEmpty()) {
        Map<String, Object> props = new HashMap<String, Object>();
        for (RestPropertyDefinition prop : consumerProperties) {
            String key = prop.getKey();
            String value = CamelContextHelper.parseText(context, prop.getValue());
            props.put(key, value);
        }
        answer.setConsumerProperties(props);
    }
    if (!dataFormatProperties.isEmpty()) {
        Map<String, Object> props = new HashMap<String, Object>();
        for (RestPropertyDefinition prop : dataFormatProperties) {
            String key = prop.getKey();
            String value = CamelContextHelper.parseText(context, prop.getValue());
            props.put(key, value);
        }
        answer.setDataFormatProperties(props);
    }
    if (!apiProperties.isEmpty()) {
        Map<String, Object> props = new HashMap<String, Object>();
        for (RestPropertyDefinition prop : apiProperties) {
            String key = prop.getKey();
            String value = CamelContextHelper.parseText(context, prop.getValue());
            props.put(key, value);
        }
        answer.setApiProperties(props);
    }
    if (!corsHeaders.isEmpty()) {
        Map<String, String> props = new HashMap<String, String>();
        for (RestPropertyDefinition prop : corsHeaders) {
            String key = prop.getKey();
            String value = CamelContextHelper.parseText(context, prop.getValue());
            props.put(key, value);
        }
        answer.setCorsHeaders(props);
    }
    return answer;
}
Also used : HashMap(java.util.HashMap) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 4 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration in project camel by apache.

the class DefaultCamelContext method getRestConfiguration.

public RestConfiguration getRestConfiguration(String component, boolean defaultIfNotExist) {
    if (component == null) {
        component = "";
    }
    RestConfiguration config = restConfigurations.get(component);
    if (config == null && defaultIfNotExist) {
        config = getRestConfiguration();
        if (config != null && config.getComponent() != null && !config.getComponent().equals(component)) {
            config = new RestConfiguration();
            restConfigurations.put(component, config);
        }
    }
    return config;
}
Also used : RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 5 with RestConfiguration

use of org.apache.camel.spi.RestConfiguration 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

RestConfiguration (org.apache.camel.spi.RestConfiguration)36 HashMap (java.util.HashMap)10 Consumer (org.apache.camel.Consumer)10 Endpoint (org.apache.camel.Endpoint)8 CamelContext (org.apache.camel.CamelContext)6 Component (org.apache.camel.Component)3 NoSuchBeanException (org.apache.camel.NoSuchBeanException)3 RouteDefinition (org.apache.camel.model.RouteDefinition)3 Test (org.junit.Test)3 Swagger (io.swagger.models.Swagger)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)2 HttpConsumer (org.apache.camel.http.common.HttpConsumer)2 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)2 FromDefinition (org.apache.camel.model.FromDefinition)2 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)2 RestDefinition (org.apache.camel.model.rest.RestDefinition)2 FactoryFinder (org.apache.camel.spi.FactoryFinder)2