Search in sources :

Example 1 with CollectionStringBuffer

use of org.apache.camel.util.CollectionStringBuffer in project camel by apache.

the class RestletEndpoint method updateEndpointUri.

// Update the endpointUri with the restlet method information
protected void updateEndpointUri() {
    String endpointUri = getEndpointUri();
    CollectionStringBuffer methods = new CollectionStringBuffer(",");
    if (getRestletMethods() != null && getRestletMethods().length > 0) {
        // list the method(s) as a comma seperated list
        for (Method method : getRestletMethods()) {
            methods.append(method.getName());
        }
    } else {
        // otherwise consider the single method we own
        methods.append(getRestletMethod());
    }
    // update the uri
    endpointUri = endpointUri + "?restletMethods=" + methods;
    setEndpointUri(endpointUri);
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) Method(org.restlet.data.Method)

Example 2 with CollectionStringBuffer

use of org.apache.camel.util.CollectionStringBuffer in project camel by apache.

the class DefaultSqlPrepareStatementStrategy method prepareQuery.

@Override
public String prepareQuery(String query, boolean allowNamedParameters, final Exchange exchange) throws SQLException {
    String answer;
    if (allowNamedParameters && hasNamedParameters(query)) {
        if (exchange != null) {
            // replace all :?in:word with a number of placeholders for how many values are expected in the IN values
            Matcher matcher = REPLACE_IN_PATTERN.matcher(query);
            while (matcher.find()) {
                String found = matcher.group(1);
                Object parameter = lookupParameter(found, exchange, exchange.getIn().getBody());
                if (parameter != null) {
                    Iterator it = createInParameterIterator(parameter);
                    CollectionStringBuffer csb = new CollectionStringBuffer(",");
                    while (it.hasNext()) {
                        it.next();
                        csb.append("\\?");
                    }
                    String replace = csb.toString();
                    String foundEscaped = found.replace("$", "\\$").replace("{", "\\{").replace("}", "\\}");
                    Matcher paramMatcher = Pattern.compile("\\:\\?in\\:" + foundEscaped, Pattern.MULTILINE).matcher(query);
                    query = paramMatcher.replaceAll(replace);
                }
            }
        }
        // replace all :?word and :?${foo} with just ?
        answer = REPLACE_PATTERN.matcher(query).replaceAll("\\?");
    } else {
        answer = query;
    }
    LOG.trace("Prepared query: {}", answer);
    return answer;
}
Also used : Matcher(java.util.regex.Matcher) CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) Iterator(java.util.Iterator) CompositeIterator(org.springframework.util.CompositeIterator)

Example 3 with CollectionStringBuffer

use of org.apache.camel.util.CollectionStringBuffer in project camel by apache.

the class DefaultCamelContext method explainDataFormatJson.

public String explainDataFormatJson(String dataFormatName, DataFormat dataFormat, boolean includeAllOptions) {
    try {
        String json = getDataFormatParameterJsonSchema(dataFormatName);
        if (json == null) {
            // the model may be shared for multiple data formats such as bindy, json (xstream, jackson, gson)
            if (dataFormatName.contains("-")) {
                dataFormatName = ObjectHelper.before(dataFormatName, "-");
                json = getDataFormatParameterJsonSchema(dataFormatName);
            }
            if (json == null) {
                return 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[]>();
        Map<String, String[]> dataFormatOptions = new LinkedHashMap<String, String[]>();
        // extract options from the data format
        Map<String, Object> options = new LinkedHashMap<String, Object>();
        IntrospectionSupport.getProperties(dataFormat, options, "", false);
        for (Map.Entry<String, Object> entry : options.entrySet()) {
            String name = entry.getKey();
            String value = "";
            if (entry.getValue() != null) {
                value = entry.getValue().toString();
            }
            value = URISupport.sanitizePath(value);
            // find type and description from the json schema
            String type = null;
            String kind = null;
            String label = null;
            String required = null;
            String javaType = null;
            String deprecated = null;
            String secret = null;
            String defaultValue = null;
            String description = null;
            for (Map<String, String> row : rows) {
                if (name.equals(row.get("name"))) {
                    type = row.get("type");
                    kind = row.get("kind");
                    label = row.get("label");
                    required = row.get("required");
                    javaType = row.get("javaType");
                    deprecated = row.get("deprecated");
                    secret = row.get("secret");
                    defaultValue = row.get("defaultValue");
                    description = row.get("description");
                    break;
                }
            }
            // remember this option from the uri
            dataFormatOptions.put(name, new String[] { name, kind, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
        }
        // 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 secret = row.get("secret");
            value = URISupport.sanitizePath(value);
            String description = row.get("description");
            boolean isDataFormatOption = dataFormatOptions.containsKey(name);
            // always include from uri or path options
            if (includeAllOptions || isDataFormatOption) {
                if (!selected.containsKey(name)) {
                    // add as selected row, but take the value from uri options if it was from there
                    if (isDataFormatOption) {
                        selected.put(name, dataFormatOptions.get(name));
                    } else {
                        selected.put(name, new String[] { name, kind, label, required, type, javaType, deprecated, secret, 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 secret = row[7];
            String value = row[8];
            String defaultValue = row[9];
            String description = row[10];
            // 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 (secret != null) {
                csb.append("\"secret\": \"" + secret + "\"");
            }
            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();
    } catch (Exception e) {
        // ignore and return empty response
        return null;
    }
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) 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) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 4 with CollectionStringBuffer

use of org.apache.camel.util.CollectionStringBuffer in project camel by apache.

the class DefaultCamelContext method explainEndpointJson.

// CHECKSTYLE:OFF
public String explainEndpointJson(String uri, boolean includeAllOptions) {
    try {
        URI u = new URI(uri);
        String json = getComponentParameterJsonSchema(u.getScheme());
        if (json == null) {
            return 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[]>();
        Map<String, String[]> uriOptions = new LinkedHashMap<String, String[]>();
        // insert values from uri
        Map<String, Object> options = EndpointHelper.endpointProperties(this, uri);
        // extract consumer. prefix options
        Map<String, Object> consumerOptions = IntrospectionSupport.extractProperties(options, "consumer.");
        // and add back again without the consumer. prefix as that json schema omits that
        options.putAll(consumerOptions);
        for (Map.Entry<String, Object> entry : options.entrySet()) {
            String name = entry.getKey();
            String value = "";
            if (entry.getValue() != null) {
                value = entry.getValue().toString();
            }
            value = URISupport.sanitizePath(value);
            // find type and description from the json schema
            String type = null;
            String kind = null;
            String group = null;
            String label = null;
            String required = null;
            String javaType = null;
            String deprecated = null;
            String secret = null;
            String defaultValue = null;
            String description = null;
            for (Map<String, String> row : rows) {
                if (name.equals(row.get("name"))) {
                    type = row.get("type");
                    kind = row.get("kind");
                    group = row.get("group");
                    label = row.get("label");
                    required = row.get("required");
                    javaType = row.get("javaType");
                    deprecated = row.get("deprecated");
                    secret = row.get("secret");
                    defaultValue = row.get("defaultValue");
                    description = row.get("description");
                    break;
                }
            }
            // remember this option from the uri
            uriOptions.put(name, new String[] { name, kind, group, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
        }
        // include other rows
        for (Map<String, String> row : rows) {
            String name = row.get("name");
            String kind = row.get("kind");
            String group = row.get("group");
            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 secret = row.get("secret");
            value = URISupport.sanitizePath(value);
            String description = row.get("description");
            boolean isUriOption = uriOptions.containsKey(name);
            // always include from uri or path options
            if (includeAllOptions || isUriOption || "path".equals(kind)) {
                if (!selected.containsKey(name)) {
                    // add as selected row, but take the value from uri options if it was from there
                    if (isUriOption) {
                        selected.put(name, uriOptions.get(name));
                    } else {
                        selected.put(name, new String[] { name, kind, group, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
                    }
                }
            }
        }
        // skip component properties
        json = ObjectHelper.before(json, "  \"componentProperties\": {");
        // and rewrite 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 group = row[2];
            String label = row[3];
            String required = row[4];
            String type = row[5];
            String javaType = row[6];
            String deprecated = row[7];
            String secret = row[8];
            String value = row[9];
            String defaultValue = row[10];
            String description = row[11];
            // add json of the option
            buffer.append(StringQuoteHelper.doubleQuote(name)).append(": { ");
            CollectionStringBuffer csb = new CollectionStringBuffer();
            if (kind != null) {
                csb.append("\"kind\": \"" + kind + "\"");
            }
            if (group != null) {
                csb.append("\"group\": \"" + group + "\"");
            }
            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 (secret != null) {
                csb.append("\"secret\": \"" + secret + "\"");
            }
            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();
    } catch (Exception e) {
        // ignore and return empty response
        return null;
    }
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) URI(java.net.URI) 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) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 5 with CollectionStringBuffer

use of org.apache.camel.util.CollectionStringBuffer in project camel by apache.

the class OtherwiseDefinition method getLabel.

@Override
public String getLabel() {
    CollectionStringBuffer buffer = new CollectionStringBuffer("otherwise[");
    List<ProcessorDefinition<?>> list = getOutputs();
    for (ProcessorDefinition<?> type : list) {
        buffer.append(type.getLabel());
    }
    buffer.append("]");
    return buffer.toString();
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer)

Aggregations

CollectionStringBuffer (org.apache.camel.util.CollectionStringBuffer)21 HashMap (java.util.HashMap)5 Map (java.util.Map)5 IOException (java.io.IOException)4 LinkedHashMap (java.util.LinkedHashMap)4 TreeMap (java.util.TreeMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 MalformedObjectNameException (javax.management.MalformedObjectNameException)4 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)4 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)4 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)4 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)4 RuntimeCamelException (org.apache.camel.RuntimeCamelException)4 VetoCamelContextStartException (org.apache.camel.VetoCamelContextStartException)4 LoadPropertiesException (org.apache.camel.util.LoadPropertiesException)4 RouteDefinition (org.apache.camel.model.RouteDefinition)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1