Search in sources :

Example 6 with CollectionStringBuffer

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

the class DefaultCamelContext method explainComponentJson.

public String explainComponentJson(String componentName, boolean includeAllOptions) {
    try {
        String json = getComponentParameterJsonSchema(componentName);
        if (json == null) {
            return null;
        }
        List<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema("componentProperties", json, true);
        // selected rows to use for answer
        Map<String, String[]> selected = new LinkedHashMap<String, String[]>();
        // insert values from component
        Component component = getComponent(componentName);
        Map<String, Object> options = new HashMap<String, Object>();
        IntrospectionSupport.getProperties(component, options, null);
        for (Map.Entry<String, Object> entry : options.entrySet()) {
            String name = entry.getKey();
            // skip unwanted options which is default inherited from DefaultComponent
            if ("camelContext".equals(name) || "endpointClass".equals(name)) {
                continue;
            }
            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;
                }
            }
            // add as selected row
            selected.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");
            // always include path options
            if (includeAllOptions) {
                // add as selected row
                if (!selected.containsKey(name)) {
                    selected.put(name, new String[] { name, kind, group, label, required, type, javaType, deprecated, secret, value, defaultValue, description });
                }
            }
        }
        json = ObjectHelper.before(json, "  \"componentProperties\": {");
        StringBuilder buffer = new StringBuilder("  \"componentProperties\": {");
        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 : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) 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) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Component(org.apache.camel.Component) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 7 with CollectionStringBuffer

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

the class RestProducer method prepareExchange.

protected void prepareExchange(Exchange exchange) throws Exception {
    boolean hasPath = false;
    // uri template with path parameters resolved
    // uri template may be optional and the user have entered the uri template in the path instead
    String resolvedUriTemplate = getEndpoint().getUriTemplate() != null ? getEndpoint().getUriTemplate() : getEndpoint().getPath();
    Message inMessage = exchange.getIn();
    if (prepareUriTemplate) {
        if (resolvedUriTemplate.contains("{")) {
            // resolve template and replace {key} with the values form the exchange
            // each {} is a parameter (url templating)
            String[] arr = resolvedUriTemplate.split("\\/");
            CollectionStringBuffer csb = new CollectionStringBuffer("/");
            for (String a : arr) {
                if (a.startsWith("{") && a.endsWith("}")) {
                    String key = a.substring(1, a.length() - 1);
                    String value = inMessage.getHeader(key, String.class);
                    if (value != null) {
                        hasPath = true;
                        csb.append(value);
                    } else {
                        csb.append(a);
                    }
                } else {
                    csb.append(a);
                }
            }
            resolvedUriTemplate = csb.toString();
        }
    }
    // resolve uri parameters
    String query = getEndpoint().getQueryParameters();
    if (query != null) {
        Map<String, Object> params = URISupport.parseQuery(query);
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            Object v = entry.getValue();
            if (v != null) {
                String a = v.toString();
                // decode the key as { may be decoded to %NN
                a = URLDecoder.decode(a, "UTF-8");
                if (a.startsWith("{") && a.endsWith("}")) {
                    String key = a.substring(1, a.length() - 1);
                    String value = inMessage.getHeader(key, String.class);
                    if (value != null) {
                        params.put(key, value);
                    } else {
                        params.put(entry.getKey(), entry.getValue());
                    }
                } else {
                    params.put(entry.getKey(), entry.getValue());
                }
            }
        }
        query = URISupport.createQueryString(params);
    }
    if (query != null) {
        // the query parameters for the rest call to be used
        inMessage.setHeader(Exchange.REST_HTTP_QUERY, query);
    }
    if (hasPath) {
        String host = getEndpoint().getHost();
        String basePath = getEndpoint().getUriTemplate() != null ? getEndpoint().getPath() : null;
        basePath = FileUtil.stripLeadingSeparator(basePath);
        resolvedUriTemplate = FileUtil.stripLeadingSeparator(resolvedUriTemplate);
        // if so us a header for the dynamic uri template so we reuse same endpoint but the header overrides the actual url to use
        String overrideUri = host;
        if (!ObjectHelper.isEmpty(basePath)) {
            overrideUri += "/" + basePath;
        }
        if (!ObjectHelper.isEmpty(resolvedUriTemplate)) {
            overrideUri += "/" + resolvedUriTemplate;
        }
        // the http uri for the rest call to be used
        inMessage.setHeader(Exchange.REST_HTTP_URI, overrideUri);
    }
    final String produces = getEndpoint().getProduces();
    if (isEmpty(inMessage.getHeader(Exchange.CONTENT_TYPE)) && isNotEmpty(produces)) {
        inMessage.setHeader(Exchange.CONTENT_TYPE, produces);
    }
    final String consumes = getEndpoint().getConsumes();
    if (isEmpty(inMessage.getHeader(ACCEPT)) && isNotEmpty(consumes)) {
        inMessage.setHeader(ACCEPT, consumes);
    }
}
Also used : Message(org.apache.camel.Message) CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with CollectionStringBuffer

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

the class ManagedCircuitBreakerLoadBalancer method getExceptions.

@Override
public String getExceptions() {
    if (exceptions != null) {
        return exceptions;
    }
    List<Class<?>> classes = processor.getExceptions();
    if (classes == null || classes.isEmpty()) {
        exceptions = "";
    } else {
        CollectionStringBuffer csb = new CollectionStringBuffer(",");
        for (Class<?> clazz : classes) {
            csb.append(clazz.getCanonicalName());
        }
        exceptions = csb.toString();
    }
    return exceptions;
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer)

Example 9 with CollectionStringBuffer

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

the class CamelJMXAgentDefinition method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("CamelJMXAgent[");
    CollectionStringBuffer csb = new CollectionStringBuffer();
    if (disabled != null) {
        csb.append("disabled=" + disabled);
    }
    if (usePlatformMBeanServer != null) {
        csb.append("usePlatformMBeanServer=" + usePlatformMBeanServer);
    }
    if (createConnector != null) {
        csb.append("createConnector=" + createConnector);
    }
    if (connectorPort != null) {
        csb.append("connectorPort=" + connectorPort);
    }
    if (registryPort != null) {
        csb.append("registryPort=" + registryPort);
    }
    if (serviceUrlPath != null) {
        csb.append("serviceUrlPath=" + serviceUrlPath);
    }
    if (mbeanServerDefaultDomain != null) {
        csb.append("mbeanServerDefaultDomain=" + mbeanServerDefaultDomain);
    }
    if (mbeanObjectDomainName != null) {
        csb.append("mbeanObjectDomainName=" + mbeanObjectDomainName);
    }
    if (statisticsLevel != null) {
        csb.append("statisticsLevel=" + statisticsLevel);
    }
    if (loadStatisticsEnabled != null) {
        csb.append("loadStatisticsEnabled=" + loadStatisticsEnabled);
    }
    if (endpointRuntimeStatisticsEnabled != null) {
        csb.append("endpointRuntimeStatisticsEnabled=" + endpointRuntimeStatisticsEnabled);
    }
    if (onlyRegisterProcessorWithCustomId != null) {
        csb.append("onlyRegisterProcessorWithCustomId=" + onlyRegisterProcessorWithCustomId);
    }
    if (registerAlways != null) {
        csb.append("registerAlways=" + registerAlways);
    }
    if (registerNewRoutes != null) {
        csb.append("registerNewRoutes=" + registerNewRoutes);
    }
    if (includeHostName != null) {
        csb.append("includeHostName=" + includeHostName);
    }
    if (useHostIPAddress != null) {
        csb.append("useHostIPAddress=" + useHostIPAddress);
    }
    if (mask != null) {
        csb.append("mask=" + mask);
    }
    sb.append(csb.toString());
    sb.append("]");
    return sb.toString();
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer)

Example 10 with CollectionStringBuffer

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

the class SwaggerRestProducerFactory method createHttpProducer.

private Producer createHttpProducer(CamelContext camelContext, Swagger swagger, Operation operation, String host, String verb, String path, String queryParameters, String consumes, String produces, String componentName, Map<String, Object> parameters) throws Exception {
    LOG.debug("Using Swagger operation: {} with {} {}", operation, verb, path);
    RestProducerFactory factory = (RestProducerFactory) parameters.remove("restProducerFactory");
    if (factory != null) {
        LOG.debug("Using RestProducerFactory: {}", factory);
        if (produces == null) {
            CollectionStringBuffer csb = new CollectionStringBuffer(",");
            List<String> list = operation.getProduces();
            if (list == null) {
                list = swagger.getProduces();
            }
            if (list != null) {
                for (String s : list) {
                    csb.append(s);
                }
            }
            produces = csb.isEmpty() ? null : csb.toString();
        }
        if (consumes == null) {
            CollectionStringBuffer csb = new CollectionStringBuffer(",");
            List<String> list = operation.getConsumes();
            if (list == null) {
                list = swagger.getConsumes();
            }
            if (list != null) {
                for (String s : list) {
                    csb.append(s);
                }
            }
            consumes = csb.isEmpty() ? null : csb.toString();
        }
        String basePath;
        String uriTemplate;
        if (host == null) {
            // if no explicit host has been configured then use host and base path from the swagger api-doc
            host = swagger.getHost();
            basePath = swagger.getBasePath();
            uriTemplate = path;
        } else {
            // path includes also uri template
            basePath = path;
            uriTemplate = null;
        }
        return factory.createProducer(camelContext, host, verb, basePath, uriTemplate, queryParameters, consumes, produces, parameters);
    } else {
        throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
    }
}
Also used : CollectionStringBuffer(org.apache.camel.util.CollectionStringBuffer) RestProducerFactory(org.apache.camel.spi.RestProducerFactory)

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