Search in sources :

Example 66 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class DefaultCamelContext method removeRouteDefinition.

/**
     * Removes the route definition with the given key.
     *
     * @return true if one or more routes was removed
     */
protected boolean removeRouteDefinition(String key) {
    boolean answer = false;
    Iterator<RouteDefinition> iter = routeDefinitions.iterator();
    while (iter.hasNext()) {
        RouteDefinition route = iter.next();
        if (route.idOrCreate(nodeIdFactory).equals(key)) {
            iter.remove();
            answer = true;
        }
    }
    return answer;
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition)

Example 67 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class DefaultCamelContext method explainEipJson.

public String explainEipJson(String nameOrId, boolean includeAllOptions) {
    try {
        // try to find the id within all known routes and their eips
        String eipName = nameOrId;
        NamedNode target = null;
        for (RouteDefinition route : getRouteDefinitions()) {
            if (route.getId().equals(nameOrId)) {
                target = route;
                break;
            }
            for (FromDefinition from : route.getInputs()) {
                if (nameOrId.equals(from.getId())) {
                    target = route;
                    break;
                }
            }
            Iterator<ProcessorDefinition> it = ProcessorDefinitionHelper.filterTypeInOutputs(route.getOutputs(), ProcessorDefinition.class);
            while (it.hasNext()) {
                ProcessorDefinition def = it.next();
                if (nameOrId.equals(def.getId())) {
                    target = def;
                    break;
                }
            }
            if (target != null) {
                break;
            }
        }
        if (target != null) {
            eipName = target.getShortName();
        }
        String json = getEipParameterJsonSchema(eipName);
        if (json == null) {
            return null;
        }
        // overlay with runtime parameters that id uses at runtime
        if (target != null) {
            List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("properties", json, true);
            // selected rows to use for answer
            Map<String, String[]> selected = new LinkedHashMap<String, String[]>();
            // extract options from the node
            Map<String, Object> options = new LinkedHashMap<String, Object>();
            IntrospectionSupport.getProperties(target, options, "", false);
            // remove outputs which we do not want to include
            options.remove("outputs");
            // include other rows
            for (Map<String, String> row : rows) {
                String name = row.get("name");
                String kind = row.get("kind");
                String label = row.get("label");
                String required = row.get("required");
                String value = row.get("value");
                String defaultValue = row.get("defaultValue");
                String type = row.get("type");
                String javaType = row.get("javaType");
                String deprecated = row.get("deprecated");
                String description = row.get("description");
                // find the configured option
                Object o = options.get(name);
                if (o != null) {
                    value = o.toString();
                }
                value = URISupport.sanitizePath(value);
                if (includeAllOptions || o != null) {
                    // add as selected row
                    if (!selected.containsKey(name)) {
                        selected.put(name, new String[] { name, kind, label, required, type, javaType, deprecated, value, defaultValue, description });
                    }
                }
            }
            json = ObjectHelper.before(json, "  \"properties\": {");
            StringBuilder buffer = new StringBuilder("  \"properties\": {");
            boolean first = true;
            for (String[] row : selected.values()) {
                if (first) {
                    first = false;
                } else {
                    buffer.append(",");
                }
                buffer.append("\n    ");
                String name = row[0];
                String kind = row[1];
                String label = row[2];
                String required = row[3];
                String type = row[4];
                String javaType = row[5];
                String deprecated = row[6];
                String value = row[7];
                String defaultValue = row[8];
                String description = row[9];
                // add json of the option
                buffer.append(StringQuoteHelper.doubleQuote(name)).append(": { ");
                CollectionStringBuffer csb = new CollectionStringBuffer();
                if (kind != null) {
                    csb.append("\"kind\": \"" + kind + "\"");
                }
                if (label != null) {
                    csb.append("\"label\": \"" + label + "\"");
                }
                if (required != null) {
                    csb.append("\"required\": \"" + required + "\"");
                }
                if (type != null) {
                    csb.append("\"type\": \"" + type + "\"");
                }
                if (javaType != null) {
                    csb.append("\"javaType\": \"" + javaType + "\"");
                }
                if (deprecated != null) {
                    csb.append("\"deprecated\": \"" + deprecated + "\"");
                }
                if (value != null) {
                    csb.append("\"value\": \"" + value + "\"");
                }
                if (defaultValue != null) {
                    csb.append("\"defaultValue\": \"" + defaultValue + "\"");
                }
                if (description != null) {
                    csb.append("\"description\": \"" + description + "\"");
                }
                if (!csb.isEmpty()) {
                    buffer.append(csb.toString());
                }
                buffer.append(" }");
            }
            buffer.append("\n  }\n}\n");
            // insert the original first part of the json into the start of the buffer
            buffer.insert(0, json);
            return buffer.toString();
        }
        return json;
    } catch (Exception e) {
        // ignore and return empty response
        return null;
    }
}
Also used : FromDefinition(org.apache.camel.model.FromDefinition) CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) NamedNode(org.apache.camel.NamedNode) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MalformedObjectNameException(javax.management.MalformedObjectNameException) VetoCamelContextStartException(org.apache.camel.VetoCamelContextStartException) IOException(java.io.IOException) LoadPropertiesException(org.apache.camel.util.LoadPropertiesException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) FailedToStartRouteException(org.apache.camel.FailedToStartRouteException) LinkedHashMap(java.util.LinkedHashMap) RouteDefinition(org.apache.camel.model.RouteDefinition) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 68 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class RestDefinition method asRouteDefinition.

/**
     * Transforms this REST definition into a list of {@link org.apache.camel.model.RouteDefinition} which
     * Camel routing engine can add and run. This allows us to define REST services using this
     * REST DSL and turn those into regular Camel routes.
     *
     * @param camelContext The Camel context
     */
public List<RouteDefinition> asRouteDefinition(CamelContext camelContext) {
    ObjectHelper.notNull(camelContext, "CamelContext");
    // sanity check this rest definition do not have duplicates
    validateUniquePaths();
    List<RouteDefinition> answer = new ArrayList<RouteDefinition>();
    if (camelContext.getRestConfigurations().isEmpty()) {
        camelContext.getRestConfiguration();
    }
    for (RestConfiguration config : camelContext.getRestConfigurations()) {
        addRouteDefinition(camelContext, answer, config.getComponent());
    }
    return answer;
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) ArrayList(java.util.ArrayList) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 69 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class RestDefinition method addRouteDefinition.

private void addRouteDefinition(CamelContext camelContext, List<RouteDefinition> answer, String component) {
    for (VerbDefinition verb : getVerbs()) {
        // either the verb has a singular to or a embedded route
        RouteDefinition route = verb.getRoute();
        if (route == null) {
            // it was a singular to, so add a new route and add the singular
            // to as output to this route
            route = new RouteDefinition();
            ProcessorDefinition def = verb.getTo() != null ? verb.getTo() : verb.getToD();
            route.getOutputs().add(def);
        }
        // add the binding
        RestBindingDefinition binding = new RestBindingDefinition();
        binding.setComponent(component);
        binding.setType(verb.getType());
        binding.setOutType(verb.getOutType());
        // verb takes precedence over configuration on rest
        if (verb.getConsumes() != null) {
            binding.setConsumes(verb.getConsumes());
        } else {
            binding.setConsumes(getConsumes());
        }
        if (verb.getProduces() != null) {
            binding.setProduces(verb.getProduces());
        } else {
            binding.setProduces(getProduces());
        }
        if (verb.getBindingMode() != null) {
            binding.setBindingMode(verb.getBindingMode());
        } else {
            binding.setBindingMode(getBindingMode());
        }
        if (verb.getSkipBindingOnErrorCode() != null) {
            binding.setSkipBindingOnErrorCode(verb.getSkipBindingOnErrorCode());
        } else {
            binding.setSkipBindingOnErrorCode(getSkipBindingOnErrorCode());
        }
        if (verb.getEnableCORS() != null) {
            binding.setEnableCORS(verb.getEnableCORS());
        } else {
            binding.setEnableCORS(getEnableCORS());
        }
        // register all the default values for the query parameters
        for (RestOperationParamDefinition param : verb.getParams()) {
            if (RestParamType.query == param.getType() && ObjectHelper.isNotEmpty(param.getDefaultValue())) {
                binding.addDefaultValue(param.getName(), param.getDefaultValue());
            }
        }
        route.setRestBindingDefinition(binding);
        // create the from endpoint uri which is using the rest component
        String from = "rest:" + verb.asVerb() + ":" + buildUri(verb);
        // append options
        Map<String, Object> options = new HashMap<String, Object>();
        // verb takes precedence over configuration on rest
        if (verb.getConsumes() != null) {
            options.put("consumes", verb.getConsumes());
        } else if (getConsumes() != null) {
            options.put("consumes", getConsumes());
        }
        if (verb.getProduces() != null) {
            options.put("produces", verb.getProduces());
        } else if (getProduces() != null) {
            options.put("produces", getProduces());
        }
        // append optional type binding information
        String inType = binding.getType();
        if (inType != null) {
            options.put("inType", inType);
        }
        String outType = binding.getOutType();
        if (outType != null) {
            options.put("outType", outType);
        }
        // if no route id has been set, then use the verb id as route id
        if (!route.hasCustomIdAssigned()) {
            // use id of verb as route id
            String id = verb.getId();
            if (id != null) {
                route.setId(id);
            }
        }
        String routeId = verb.idOrCreate(camelContext.getNodeIdFactory());
        if (!verb.getUsedForGeneratingNodeId()) {
            routeId = route.idOrCreate(camelContext.getNodeIdFactory());
        }
        verb.setRouteId(routeId);
        options.put("routeId", routeId);
        if (component != null && !component.isEmpty()) {
            options.put("componentName", component);
        }
        // include optional description, which we favor from 1) to/route description 2) verb description 3) rest description
        // this allows end users to define general descriptions and override then per to/route or verb
        String description = verb.getTo() != null ? verb.getTo().getDescriptionText() : route.getDescriptionText();
        if (description == null) {
            description = verb.getDescriptionText();
        }
        if (description == null) {
            description = getDescriptionText();
        }
        if (description != null) {
            options.put("description", description);
        }
        if (!options.isEmpty()) {
            String query;
            try {
                query = URISupport.createQueryString(options);
            } catch (URISyntaxException e) {
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
            from = from + "?" + query;
        }
        String path = getPath();
        String s1 = FileUtil.stripTrailingSeparator(path);
        String s2 = FileUtil.stripLeadingSeparator(verb.getUri());
        String allPath;
        if (s1 != null && s2 != null) {
            allPath = s1 + "/" + s2;
        } else if (path != null) {
            allPath = path;
        } else {
            allPath = verb.getUri();
        }
        // each {} is a parameter (url templating)
        String[] arr = allPath.split("\\/");
        for (String a : arr) {
            // need to resolve property placeholders first
            try {
                a = camelContext.resolvePropertyPlaceholders(a);
            } catch (Exception e) {
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
            if (a.startsWith("{") && a.endsWith("}")) {
                String key = a.substring(1, a.length() - 1);
                //  merge if exists
                boolean found = false;
                for (RestOperationParamDefinition param : verb.getParams()) {
                    // name is mandatory
                    String name = param.getName();
                    ObjectHelper.notEmpty(name, "parameter name");
                    // need to resolve property placeholders first
                    try {
                        name = camelContext.resolvePropertyPlaceholders(name);
                    } catch (Exception e) {
                        throw ObjectHelper.wrapRuntimeCamelException(e);
                    }
                    if (name.equalsIgnoreCase(key)) {
                        param.type(RestParamType.path);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    param(verb).name(key).type(RestParamType.path).endParam();
                }
            }
        }
        if (verb.getType() != null) {
            String bodyType = verb.getType();
            if (bodyType.endsWith("[]")) {
                bodyType = "List[" + bodyType.substring(0, bodyType.length() - 2) + "]";
            }
            RestOperationParamDefinition param = findParam(verb, RestParamType.body.name());
            if (param == null) {
                // must be body type and set the model class as data type
                param(verb).name(RestParamType.body.name()).type(RestParamType.body).dataType(bodyType).endParam();
            } else {
                // must be body type and set the model class as data type
                param.type(RestParamType.body).dataType(bodyType);
            }
        }
        // the route should be from this rest endpoint
        route.fromRest(from);
        route.id(routeId);
        route.setRestDefinition(this);
        answer.add(route);
    }
}
Also used : HashMap(java.util.HashMap) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) RouteDefinition(org.apache.camel.model.RouteDefinition)

Example 70 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class RestDefinition method route.

public RouteDefinition route() {
    // add to last verb
    if (getVerbs().isEmpty()) {
        throw new IllegalArgumentException("Must add verb first, such as get/post/delete");
    }
    // link them together so we can navigate using Java DSL
    RouteDefinition route = new RouteDefinition();
    route.setRestDefinition(this);
    VerbDefinition verb = getVerbs().get(getVerbs().size() - 1);
    verb.setRoute(route);
    return route;
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition)

Aggregations

RouteDefinition (org.apache.camel.model.RouteDefinition)102 AdviceWithRouteBuilder (org.apache.camel.builder.AdviceWithRouteBuilder)17 RouteBuilder (org.apache.camel.builder.RouteBuilder)17 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 FromDefinition (org.apache.camel.model.FromDefinition)9 HashMap (java.util.HashMap)6 Processor (org.apache.camel.Processor)6 ConnectException (java.net.ConnectException)5 Exchange (org.apache.camel.Exchange)5 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)5 IOException (java.io.IOException)4 Map (java.util.Map)4 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 ChoiceDefinition (org.apache.camel.model.ChoiceDefinition)4 HystrixConfigurationDefinition (org.apache.camel.model.HystrixConfigurationDefinition)4 HystrixDefinition (org.apache.camel.model.HystrixDefinition)4 LogDefinition (org.apache.camel.model.LogDefinition)4 ModelCamelContext (org.apache.camel.model.ModelCamelContext)4