public static Source fromDefinition()

in camel-k-core/support/src/main/java/org/apache/camel/k/support/Sources.java [74:160]


    public static Source fromDefinition(SourceDefinition definition) {
        if (definition.getLocation() == null && definition.getContent() == null) {
            throw new IllegalArgumentException("Either the source location or the source content should be set");
        }
        return new Source() {
            @Override
            public String getLocation() {
                return definition.getLocation();
            }

            @Override
            public String getId() {
                return ObjectHelper.supplyIfEmpty(definition.getId(), this::getName);
            }

            @Override
            public String getName() {
                String answer = definition.getName();
                if (ObjectHelper.isEmpty(answer) && ObjectHelper.isNotEmpty(definition.getLocation())) {
                    answer = StringSupport.substringAfter(definition.getLocation(), ":");
                    answer = StringSupport.substringBeforeLast(answer, ".");

                    if (answer.contains("/")) {
                        answer = StringSupport.substringAfterLast(answer, "/");
                    }
                }

                return answer;
            }

            @Override
            public String getLanguage() {
                String answer = definition.getLanguage();
                if (ObjectHelper.isEmpty(answer) && ObjectHelper.isNotEmpty(definition.getLocation())) {
                    answer = StringSupport.substringAfterLast(definition.getLocation(), ":");
                    answer = StringSupport.substringAfterLast(answer, ".");
                }

                return answer;
            }

            @Override
            public SourceType getType() {
                return ObjectHelper.supplyIfEmpty(definition.getType(), () -> SourceType.source);
            }

            @Override
            public Optional<String> getLoader() {
                return Optional.ofNullable(definition.getLoader());
            }

            @Override
            public List<String> getInterceptors() {
                return ObjectHelper.supplyIfEmpty(definition.getInterceptors(), Collections::emptyList);
            }

            @Override
            public List<String> getPropertyNames() {
                return ObjectHelper.supplyIfEmpty(definition.getPropertyNames(), Collections::emptyList);
            }

            /**
             * Read the content of the source as {@link InputStream}.
             *
             * @param ctx the {@link CamelContext}
             * @return the {@link InputStream} representing the source content
             */
            @Override
            public InputStream resolveAsInputStream(CamelContext ctx) {
                try {
                    InputStream is;

                    if (definition.getContent() != null) {
                        is = new ByteArrayInputStream(definition.getContent());
                    } else {
                        is = ResourceHelper.resolveMandatoryResourceAsInputStream(ctx, definition.getLocation());
                    }

                    return definition.isCompressed()
                        ? new GZIPInputStream(Base64.getDecoder().wrap(is))
                        : is;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }