Java Code Examples for org.apache.camel.catalog.CamelCatalog#componentJSonSchema()

The following examples show how to use org.apache.camel.catalog.CamelCatalog#componentJSonSchema() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
public static Set<String> componentsFromArtifact(CamelCatalog camelCatalog, String artifactId) {
    Set<String> answer = new TreeSet<String>();

    // use the camel catalog to find what components the artifact has
    for (String name : camelCatalog.findComponentNames()) {
        String json = camelCatalog.componentJSonSchema(name);
        if (json != null) {
            List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("component", json, false);
            String scheme = null;
            String artifact = null;
            for (Map<String, String> row : data) {
                if (row.get("artifactId") != null) {
                    artifact = row.get("artifactId");
                }
                if (row.get("scheme") != null) {
                    scheme = row.get("scheme");
                }
            }
            if (artifactId.equals(artifact) && scheme != null) {
                answer.add(scheme);
            }
        }
    }

    return answer;
}
 
Example 2
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Whether the component is consumer only
 */
public static boolean isComponentProducerOnly(CamelCatalog camelCatalog, String scheme) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        return false;
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("component", json, false);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String consumerOnly = propertyMap.get("producerOnly");
            if (consumerOnly != null) {
                return true;
            }
        }
    }
    return false;
}
 
Example 3
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Whether the component is consumer only
 */
public static boolean isComponentConsumerOnly(CamelCatalog camelCatalog, String scheme) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        return false;
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("component", json, false);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String consumerOnly = propertyMap.get("consumerOnly");
            if (consumerOnly != null) {
                return true;
            }
        }
    }
    return false;
}
 
Example 4
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Get's the java type for the given option if its enum based, otherwise it returns null
 *
 * @param scheme the component name
 * @param key    the option key
 * @return <tt>true</tt> if matching the default value, <tt>false</tt> otherwise
 */
public static String getEnumJavaTypeComponent(CamelCatalog camelCatalog, String scheme, String key) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        throw new IllegalArgumentException("Could not find catalog entry for component name: " + scheme);
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String javaType = propertyMap.get("javaType");
            String name = propertyMap.get("name");
            String enums = propertyMap.get("enum");
            if (key.equals(name) && enums != null) {
                return javaType;
            }
        }
    }
    return null;
}
 
Example 5
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given value corresponds to a dummy none placeholder for an enum type
 *
 * @param scheme the component name
 * @param key    the option key
 * @return <tt>true</tt> if matching the default value, <tt>false</tt> otherwise
 */
public static boolean isNonePlaceholderEnumValueComponent(CamelCatalog camelCatalog, String scheme, String key) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        throw new IllegalArgumentException("Could not find catalog entry for component name: " + scheme);
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String name = propertyMap.get("name");
            String enums = propertyMap.get("enum");
            if (key.equals(name) && enums != null) {
                if (!enums.contains("none")) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
    return false;
}
 
Example 6
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given value corresponds to a dummy none placeholder for an enum type
 *
 * @param scheme the component name
 * @param key    the option key
 * @return <tt>true</tt> if matching the default value, <tt>false</tt> otherwise
 */
public static boolean isNonePlaceholderEnumValue(CamelCatalog camelCatalog, String scheme, String key) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        throw new IllegalArgumentException("Could not find catalog entry for component name: " + scheme);
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("properties", json, true);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String name = propertyMap.get("name");
            String enums = propertyMap.get("enum");
            if (key.equals(name) && enums != null) {
                if (!enums.contains("none")) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
    return false;
}
 
Example 7
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given key is a multi valued option
 *
 * @param scheme the component name
 * @param key    the option key
 * @return <tt>true</tt> if the key is multi valued, <tt>false</tt> otherwise
 */
public static String getPrefix(CamelCatalog camelCatalog, String scheme, String key) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        throw new IllegalArgumentException("Could not find catalog entry for component name: " + scheme);
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("properties", json, true);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String name = propertyMap.get("name");
            String prefix = propertyMap.get("prefix");
            if (key.equals(name)) {
                return prefix;
            }
        }
    }
    return null;
}
 
Example 8
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given key is a multi valued option
 *
 * @param scheme the component name
 * @param key    the option key
 * @return <tt>true</tt> if the key is multi valued, <tt>false</tt> otherwise
 */
public static boolean isMultiValue(CamelCatalog camelCatalog, String scheme, String key) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        throw new IllegalArgumentException("Could not find catalog entry for component name: " + scheme);
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("properties", json, true);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String name = propertyMap.get("name");
            String multiValue = propertyMap.get("multiValue");
            if (key.equals(name)) {
                return "true".equals(multiValue);
            }
        }
    }
    return false;
}
 
Example 9
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given value is matching the default value from the given component.
 *
 * @param scheme the component name
 * @param key    the option key
 * @param value  the option value
 * @return <tt>true</tt> if matching the default value, <tt>false</tt> otherwise
 */
public static boolean isDefaultValueComponent(CamelCatalog camelCatalog, String scheme, String key, String value) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        throw new IllegalArgumentException("Could not find catalog entry for component name: " + scheme);
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String name = propertyMap.get("name");
            String defaultValue = propertyMap.get("defaultValue");
            if (key.equals(name)) {
                return value.equalsIgnoreCase(defaultValue);
            }
        }
    }
    return false;
}
 
Example 10
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given value is matching the default value from the given component.
 *
 * @param scheme the component name
 * @param key    the option key
 * @param value  the option value
 * @return <tt>true</tt> if matching the default value, <tt>false</tt> otherwise
 */
public static boolean isDefaultValue(CamelCatalog camelCatalog, String scheme, String key, String value) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        throw new IllegalArgumentException("Could not find catalog entry for component name: " + scheme);
    }

    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("properties", json, true);
    if (data != null) {
        for (Map<String, String> propertyMap : data) {
            String name = propertyMap.get("name");
            String defaultValue = propertyMap.get("defaultValue");
            if (key.equals(name)) {
                return value.equalsIgnoreCase(defaultValue);
            }
        }
    }
    return false;
}
 
Example 11
Source File: HoverFuture.java    From camel-language-server with Apache License 2.0 5 votes vote down vote up
@Override
public Hover apply(CamelCatalog camelCatalog) {
	String componentJSonSchema = camelCatalog.componentJSonSchema(uriElement.getComponentName());
	if (componentJSonSchema != null) {
		Hover hover = new Hover();
		ComponentModel componentModel = ModelHelper.generateComponentModel(componentJSonSchema, true);
		hover.setContents(Collections.singletonList((Either.forLeft(uriElement.getDescription(componentModel)))));
		return hover;
	}
	return null;
}
 
Example 12
Source File: CamelCatalogHelper.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
public static ComponentDto createComponentDto(CamelCatalog camelCatalog, String scheme) {
    // use the camel catalog
    String json = camelCatalog.componentJSonSchema(scheme);
    if (json == null) {
        return null;
    }

    ComponentDto dto = new ComponentDto();
    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("component", json, false);
    for (Map<String, String> row : data) {
        if (row.get("scheme") != null) {
            dto.setScheme(row.get("scheme"));
        } else if (row.get("syntax") != null) {
            dto.setSyntax(row.get("syntax"));
        } else if (row.get("title") != null) {
            dto.setTitle(row.get("title"));
        } else if (row.get("description") != null) {
            dto.setDescription(row.get("description"));
        } else if (row.get("label") != null) {
            String labelText = row.get("label");
            if (Strings.isNotBlank(labelText)) {
                dto.setTags(labelText.split(","));
            }
        } else if (row.get("consumerOnly") != null) {
            dto.setConsumerOnly("true".equals(row.get("consumerOnly")));
        } else if (row.get("producerOnly") != null) {
            dto.setProducerOnly("true".equals(row.get("producerOnly")));
        } else if (row.get("javaType") != null) {
            dto.setJavaType(row.get("javaType"));
        } else if (row.get("groupId") != null) {
            dto.setGroupId(row.get("groupId"));
        } else if (row.get("artifactId") != null) {
            dto.setArtifactId(row.get("artifactId"));
        } else if (row.get("version") != null) {
            dto.setVersion(row.get("version"));
        }
    }
    return dto;
}
 
Example 13
Source File: CamelCommandsHelper.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
/**
 * Populates the details for the given component, returning a Result if it fails.
 */
public static Result loadCamelComponentDetails(CamelCatalog camelCatalog, String camelComponentName, CamelComponentDetails details) {
    String json = camelCatalog.componentJSonSchema(camelComponentName);
    if (json == null) {
        return Results.fail("Could not find catalog entry for component name: " + camelComponentName);
    }
    List<Map<String, String>> data = JSonSchemaHelper.parseJsonSchema("component", json, false);

    for (Map<String, String> row : data) {
        String javaType = row.get("javaType");
        if (!Strings.isNullOrEmpty(javaType)) {
            details.setComponentClassQName(javaType);
        }
        String groupId = row.get("groupId");
        if (!Strings.isNullOrEmpty(groupId)) {
            details.setGroupId(groupId);
        }
        String artifactId = row.get("artifactId");
        if (!Strings.isNullOrEmpty(artifactId)) {
            details.setArtifactId(artifactId);
        }
        String version = row.get("version");
        if (!Strings.isNullOrEmpty(version)) {
            details.setVersion(version);
        }
    }
    if (Strings.isNullOrEmpty(details.getComponentClassQName())) {
        return Results.fail("Could not find fully qualified class name in catalog for component name: " + camelComponentName);
    }
    return null;
}
 
Example 14
Source File: CachedCamelCatalogTest.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
@Test
public void testCachedCamelCatalog() {
    CamelCatalog camelCatalog = new CamelCatalogService().createCamelCatalog();
    String json = camelCatalog.componentJSonSchema("timer");
    String json2 = camelCatalog.componentJSonSchema("timer");

    Assert.assertSame(json, json2);
}
 
Example 15
Source File: CamelDocumentationProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean handleExternal(PsiElement element, PsiElement originalElement) {
    String val = fetchLiteralForCamelDocumentation(element);
    if (val == null || !ServiceManager.getService(element.getProject(), CamelService.class).isCamelPresent()) {
        return false;
    }

    String name = asComponentName(val);
    Project project = element.getProject();
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    if (name != null && camelCatalog.findComponentNames().contains(name)) {

        String json = camelCatalog.componentJSonSchema(name);
        ComponentModel component = ModelHelper.generateComponentModel(json, false);

        // to build external links which points to github
        String artifactId = component.getArtifactId();

        String url;
        if ("camel-core".equals(artifactId)) {
            url = GITHUB_EXTERNAL_DOC_URL + "/camel-core/src/main/docs/" + name + "-component.adoc";
        } else {
            url = GITHUB_EXTERNAL_DOC_URL + "/components/" + component.getArtifactId() + "/src/main/docs/" + name + "-component.adoc";
        }

        String hash = component.getTitle().toLowerCase().replace(' ', '-') + "-component";
        BrowserUtil.browse(url + "#" + hash);
        return true;
    }

    return false;
}
 
Example 16
Source File: CamelDocumentationProvider.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Generates documentation for the endpoint option.
 * @param componentName the name of the Camel component
 * @param option the name of the Camel component option to generate documentation for
 * @param project the current project
 * @return a String representing the HTML documentation
 */
private String generateCamelEndpointOptionDocumentation(String componentName, String option, Project project) {
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    String json = camelCatalog.componentJSonSchema(componentName);
    if (json == null) {
        return null;
    }
    ComponentModel component = ModelHelper.generateComponentModel(json, true);

    EndpointOptionModel endpointOption;
    if (option.endsWith(".")) {
        // find the line with this prefix as prefix and multivalue
        endpointOption = component.getEndpointOptions().stream().filter(
            o -> "true".equals(o.getMultiValue()) && option.equals(o.getPrefix()))
            .findFirst().orElse(null);
    } else {
        endpointOption = component.getEndpointOption(option);
    }
    if (endpointOption == null) {
        return null;
    }

    StringBuilder builder = new StringBuilder();
    builder.append("<strong>").append(endpointOption.getName()).append("</strong><br/><br/>");
    builder.append("<strong>Group: </strong>").append(endpointOption.getGroup()).append("<br/>");
    builder.append("<strong>Type: </strong>").append("<tt>").append(endpointOption.getJavaType()).append("</tt>").append("<br/>");
    boolean required = false;
    if (!endpointOption.getRequired().equals("")) {
        required = true;
    }
    builder.append("<strong>Required: </strong>").append(required).append("<br/>");
    if (!endpointOption.getEnums().equals("")) {
        builder.append("<strong>Possible values: </strong>").append(endpointOption.getEnums().replace(",", ", ")).append("<br/>");
    }
    if (!endpointOption.getDefaultValue().equals("")) {
        builder.append("<strong>Default value: </strong>").append(endpointOption.getDefaultValue()).append("<br/>");
    }
    builder.append("<br/><div>").append(endpointOption.getDescription()).append("</div>");
    return builder.toString();
}
 
Example 17
Source File: CamelAddEndpointIntention.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
private static List<String> findCamelComponentNamesInArtifact(Set<String> artifactIds, boolean consumerOnly, Project project) {
    List<String> names = new ArrayList<>();

    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    for (String name : camelCatalog.findComponentNames()) {
        String json = camelCatalog.componentJSonSchema(name);
        ComponentModel model = ModelHelper.generateComponentModel(json, false);
        if (artifactIds.contains(model.getArtifactId())) {
            boolean onlyConsume = "true".equals(model.getConsumerOnly());
            boolean onlyProduce = "true".equals(model.getProducerOnly());
            boolean both = !onlyConsume && !onlyProduce;

            if (both) {
                names.add(name);
            } else if (consumerOnly && onlyConsume) {
                names.add(name);
            } else if (!consumerOnly && onlyProduce) {
                names.add(name);
            }
        }
    }

    // sort
    Collections.sort(names);

    return names;
}
 
Example 18
Source File: CamelDocumentationProvider.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement element) {
    // we only support literal - string types where Camel endpoints can be specified
    if (object == null || !(object instanceof String)) {
        return null;
    }

    String lookup = object.toString();

    // must be a Camel component
    String componentName = asComponentName(lookup);
    if (componentName == null) {
        return null;
    }

    // unescape xml &
    lookup = lookup.replaceAll("&amp;", "&");

    // get last option from lookup line
    int pos = Math.max(lookup.lastIndexOf("&"), lookup.lastIndexOf("?"));
    if (pos > 0) {
        String option = lookup.substring(pos + 1);
        // if the option has a value then drop that
        pos = option.indexOf("=");
        if (pos != -1) {
            option = option.substring(0, pos);
        }
        LOG.debug("getDocumentationElementForLookupItem: " + option);

        // if the option ends with a dot then its a prefixed/multi value option which we need special logic
        // find its real option name and documentation which we want to show in the quick doc window
        if (option.endsWith(".")) {
            CamelCatalog camelCatalog = ServiceManager.getService(psiManager.getProject(), CamelCatalogService.class).get();
            String json = camelCatalog.componentJSonSchema(componentName);
            if (json == null) {
                return null;
            }
            ComponentModel component = ModelHelper.generateComponentModel(json, true);

            final String prefixOption = option;

            // find the line with this prefix as prefix and multivalue
            EndpointOptionModel endpointOption = component.getEndpointOptions().stream().filter(
                o -> "true".equals(o.getMultiValue()) && prefixOption.equals(o.getPrefix()))
                .findFirst().orElse(null);

            // use the real option name instead of the prefix
            if (endpointOption != null) {
                option = endpointOption.getName();
            }
        }

        return new DocumentationElement(psiManager, element.getLanguage(), element, option, componentName);
    }

    return null;
}
 
Example 19
Source File: CamelEndpointSmartCompletionExtension.java    From camel-idea-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet resultSet, @NotNull String[] query) {
    boolean endsWithAmpQuestionMark = false;
    // it is a known Camel component
    String componentName = StringUtils.asComponentName(query[0]);

    // it is a known Camel component
    Project project = parameters.getOriginalFile().getManager().getProject();
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();

    String json = camelCatalog.componentJSonSchema(componentName);
    ComponentModel componentModel = ModelHelper.generateComponentModel(json, true);
    final PsiElement element = parameters.getPosition();

    // grab all existing parameters
    String concatQuery = query[0];
    String suffix = query[1];
    String queryAtPosition =  query[2];
    String prefixValue =  query[2];
    // camel catalog expects &amp; as & when it parses so replace all &amp; as &
    concatQuery = concatQuery.replaceAll("&amp;", "&");

    boolean editQueryParameters = concatQuery.contains("?");

    // strip up ending incomplete parameter
    if (queryAtPosition.endsWith("&") || queryAtPosition.endsWith("?")) {
        endsWithAmpQuestionMark = true;
        queryAtPosition = queryAtPosition.substring(0, queryAtPosition.length() - 1);
    }

    // strip up ending incomplete parameter
    if (concatQuery.endsWith("&") || concatQuery.endsWith("?")) {
        concatQuery = concatQuery.substring(0, concatQuery.length() - 1);
    }

    Map<String, String> existing = null;
    try {
        existing = camelCatalog.endpointProperties(concatQuery);
    } catch (Exception e) {
        LOG.warn("Error parsing Camel endpoint properties with url: " + queryAtPosition, e);
    }

    // are we editing an existing parameter value
    // or are we having a list of suggested parameters to choose among

    boolean caretAtEndOfLine = getIdeaUtils().isCaretAtEndOfLine(element);
    LOG.trace("Caret at end of line: " + caretAtEndOfLine);

    String[] queryParameter = getIdeaUtils().getQueryParameterAtCursorPosition(element);
    String optionValue = queryParameter[1];


    // a bit complex to figure out whether to edit the endpoint value or not
    boolean editOptionValue = false;
    if (endsWithAmpQuestionMark) {
        // should not edit value but suggest a new option instead
        editOptionValue = false;
    } else {
        if ("".equals(optionValue)) {
            // empty value so must edit
            editOptionValue = true;
        } else if (StringUtils.isNotEmpty(optionValue) && !caretAtEndOfLine) {
            // has value and cursor not at end of line so must edit
            editOptionValue = true;
        }
    }
    LOG.trace("Add new option: " + !editOptionValue);
    LOG.trace("Edit option value: " + editOptionValue);

    List<LookupElement> answer = null;
    if (editOptionValue) {
        EndpointOptionModel endpointOption = componentModel.getEndpointOption(queryParameter[0].substring(1));
        if (endpointOption != null) {
            answer = addSmartCompletionForEndpointValue(parameters.getEditor(), queryAtPosition, suffix, endpointOption, element, xmlMode);
        }
    }
    if (answer == null) {
        if (editQueryParameters) {
            // suggest a list of options for query parameters
            answer = addSmartCompletionSuggestionsQueryParameters(query, componentModel, existing, xmlMode, element, parameters.getEditor());
        } else {
            if (!resultSet.isStopped()) {
                // suggest a list of options for context-path
                answer = addSmartCompletionSuggestionsContextPath(queryAtPosition, componentModel, existing, xmlMode, element);
            }
        }
    }
    // are there any results then add them
    if (answer != null && !answer.isEmpty()) {
        resultSet.withPrefixMatcher(prefixValue).addAllElements(answer);
        resultSet.stopHere();
    }
}