Search in sources :

Example 1 with Pair

use of org.apache.nifi.minifi.c2.api.util.Pair in project nifi-minifi by apache.

the class FileSystemConfigurationCache method getCacheFileInfo.

@Override
public ConfigurationCacheFileInfo getCacheFileInfo(String contentType, Map<String, List<String>> parameters) throws InvalidParameterException {
    String pathString = pathPattern;
    for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
        if (entry.getValue().size() != 1) {
            throw new InvalidParameterException("Multiple values for same parameter not supported in this provider.");
        }
        pathString = pathString.replaceAll(Pattern.quote("${" + entry.getKey() + "}"), entry.getValue().get(0));
    }
    pathString = pathString + "." + contentType.replace('/', '.');
    String[] split = pathString.split("/");
    for (String s1 : split) {
        int openBrace = s1.indexOf("${");
        if (openBrace >= 0 && openBrace < s1.length() + 2) {
            int closeBrace = s1.indexOf("}", openBrace + 2);
            if (closeBrace >= 0) {
                throw new InvalidParameterException("Found unsubstituted variable " + s1.substring(openBrace + 2, closeBrace));
            }
        }
    }
    String[] splitPath = split;
    Path path = pathRoot.toAbsolutePath();
    for (int i = 0; i < splitPath.length - 1; i++) {
        String s = splitPath[i];
        path = resolveChildAndVerifyParent(path, s);
    }
    Pair<Path, String> dirPathAndFilename = new Pair<>(path, splitPath[splitPath.length - 1]);
    if (logger.isDebugEnabled()) {
        StringBuilder message = new StringBuilder("Parameters {");
        message.append(parameters.entrySet().stream().map(e -> e.getKey() + ": [" + String.join(", ", e.getValue()) + "]").collect(Collectors.joining(", ")));
        message.append("} -> ");
        message.append(dirPathAndFilename.getFirst().resolve(dirPathAndFilename.getSecond()).toAbsolutePath());
        logger.debug(message.toString());
    }
    return new FileSystemCacheFileInfoImpl(this, dirPathAndFilename.getFirst(), dirPathAndFilename.getSecond() + ".v");
}
Also used : Path(java.nio.file.Path) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) List(java.util.List) Map(java.util.Map) Pair(org.apache.nifi.minifi.c2.api.util.Pair)

Example 2 with Pair

use of org.apache.nifi.minifi.c2.api.util.Pair in project nifi-minifi by apache.

the class NiFiRestConfigurationProvider method getIdAndVersionStream.

private Pair<Stream<Pair<String, Integer>>, Closeable> getIdAndVersionStream(String filenamePattern) throws ConfigurationProviderException, IOException {
    Pattern filename = Pattern.compile(filenamePattern);
    Pair<Stream<Pair<String, String>>, Closeable> streamCloseablePair = getIdAndFilenameStream();
    return new Pair<>(streamCloseablePair.getFirst().map(p -> {
        Matcher matcher = filename.matcher(p.getSecond());
        if (!matcher.matches()) {
            return null;
        }
        return new Pair<>(p.getFirst(), Integer.parseInt(matcher.group(1)));
    }).filter(Objects::nonNull), streamCloseablePair.getSecond());
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Closeable(java.io.Closeable) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) Pair(org.apache.nifi.minifi.c2.api.util.Pair)

Example 3 with Pair

use of org.apache.nifi.minifi.c2.api.util.Pair in project nifi-minifi by apache.

the class NiFiRestConfigurationProvider method getConfiguration.

@Override
public Configuration getConfiguration(String contentType, Integer version, Map<String, List<String>> parameters) throws ConfigurationProviderException {
    if (!CONTENT_TYPE.equals(contentType)) {
        throw new ConfigurationProviderException("Unsupported content type: " + contentType + " supported value is " + CONTENT_TYPE);
    }
    String filename = templateNamePattern;
    for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
        if (entry.getValue().size() != 1) {
            throw new InvalidParameterException("Multiple values for same parameter not supported in this provider.");
        }
        filename = filename.replaceAll(Pattern.quote("${" + entry.getKey() + "}"), entry.getValue().get(0));
    }
    int index = filename.indexOf("${");
    while (index != -1) {
        int endIndex = filename.indexOf("}", index);
        if (endIndex == -1) {
            break;
        }
        String variable = filename.substring(index + 2, endIndex);
        if (!"version".equals(variable)) {
            throw new InvalidParameterException("Found unsubstituted parameter " + variable);
        }
        index = endIndex + 1;
    }
    String id = null;
    if (version == null) {
        String filenamePattern = Arrays.stream(filename.split(Pattern.quote("${version}"), -1)).map(Pattern::quote).collect(Collectors.joining("([0-9+])"));
        Pair<String, Integer> maxIdAndVersion = getMaxIdAndVersion(filenamePattern);
        id = maxIdAndVersion.getFirst();
        version = maxIdAndVersion.getSecond();
    }
    filename = filename.replaceAll(Pattern.quote("${version}"), Integer.toString(version));
    WriteableConfiguration configuration = configurationCache.getCacheFileInfo(contentType, parameters).getConfiguration(version);
    if (configuration.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Configuration " + configuration + " exists and can be served from configurationCache.");
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Configuration " + configuration + " doesn't exist, will need to download and convert template.");
        }
        if (id == null) {
            try {
                String tmpFilename = templateNamePattern;
                for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
                    if (entry.getValue().size() != 1) {
                        throw new InvalidParameterException("Multiple values for same parameter not supported in this provider.");
                    }
                    tmpFilename = tmpFilename.replaceAll(Pattern.quote("${" + entry.getKey() + "}"), entry.getValue().get(0));
                }
                Pair<Stream<Pair<String, String>>, Closeable> streamCloseablePair = getIdAndFilenameStream();
                try {
                    String finalFilename = filename;
                    id = streamCloseablePair.getFirst().filter(p -> finalFilename.equals(p.getSecond())).map(Pair::getFirst).findFirst().orElseThrow(() -> new InvalidParameterException("Unable to find template named " + finalFilename));
                } finally {
                    streamCloseablePair.getSecond().close();
                }
            } catch (IOException | TemplatesIteratorException e) {
                throw new ConfigurationProviderException("Unable to retrieve template list", e);
            }
        }
        HttpURLConnection urlConnection = httpConnector.get("/templates/" + id + "/download");
        try (InputStream inputStream = urlConnection.getInputStream()) {
            ConfigSchema configSchema = ConfigMain.transformTemplateToSchema(inputStream);
            SchemaSaver.saveConfigSchema(configSchema, configuration.getOutputStream());
        } catch (IOException e) {
            throw new ConfigurationProviderException("Unable to download template from url " + urlConnection.getURL(), e);
        } catch (JAXBException e) {
            throw new ConfigurationProviderException("Unable to convert template to yaml", e);
        } finally {
            urlConnection.disconnect();
        }
    }
    return configuration;
}
Also used : InputStream(java.io.InputStream) Closeable(java.io.Closeable) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) HttpURLConnection(java.net.HttpURLConnection) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) List(java.util.List) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) Map(java.util.Map) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Example 4 with Pair

use of org.apache.nifi.minifi.c2.api.util.Pair in project nifi-minifi by apache.

the class ConfigService method initContentTypeInfo.

protected ConfigurationProviderInfo initContentTypeInfo(List<ConfigurationProvider> configurationProviders) {
    List<Pair<MediaType, ConfigurationProvider>> mediaTypeList = new ArrayList<>();
    List<String> contentTypes = new ArrayList<>();
    Set<MediaType> seenMediaTypes = new LinkedHashSet<>();
    for (ConfigurationProvider configurationProvider : configurationProviders) {
        try {
            for (String contentTypeString : configurationProvider.getContentTypes()) {
                MediaType mediaType = MediaType.valueOf(contentTypeString);
                if (seenMediaTypes.add(mediaType)) {
                    contentTypes.add(contentTypeString);
                    mediaTypeList.add(new Pair<>(mediaType, configurationProvider));
                }
            }
        } catch (ConfigurationProviderException e) {
            return new ConfigurationProviderInfo(null, null, e);
        }
    }
    return new ConfigurationProviderInfo(mediaTypeList, contentTypes, null);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) ConfigurationProvider(org.apache.nifi.minifi.c2.api.ConfigurationProvider) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) Pair(org.apache.nifi.minifi.c2.api.util.Pair)

Example 5 with Pair

use of org.apache.nifi.minifi.c2.api.util.Pair in project nifi-minifi by apache.

the class ConfigService method initConfigurationProviderValue.

public ConfigurationProviderValue initConfigurationProviderValue(ConfigurationProviderKey key) {
    if (logger.isDebugEnabled()) {
        logger.debug("Attempting to load and cache configuration with key " + key);
    }
    try {
        List<MediaType> acceptValues = key.getAcceptValues();
        Pair<MediaType, ConfigurationProvider> providerPair = getProvider(acceptValues);
        Map<String, List<String>> parameters = key.getParameters();
        Integer version = null;
        List<String> versionList = parameters.get("version");
        if (versionList != null && versionList.size() > 0) {
            try {
                version = Integer.parseInt(versionList.get(0));
            } catch (NumberFormatException e) {
                throw new InvalidParameterException("Unable to parse " + version + " as integer.", e);
            }
        }
        return new ConfigurationProviderValue(providerPair.getSecond().getConfiguration(providerPair.getFirst().toString(), version, parameters), providerPair.getFirst(), null);
    } catch (ConfigurationProviderException e) {
        return new ConfigurationProviderValue(null, null, e);
    }
}
Also used : ConfigurationProvider(org.apache.nifi.minifi.c2.api.ConfigurationProvider) ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) MediaType(javax.ws.rs.core.MediaType) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

List (java.util.List)3 ConfigurationProviderException (org.apache.nifi.minifi.c2.api.ConfigurationProviderException)3 InvalidParameterException (org.apache.nifi.minifi.c2.api.InvalidParameterException)3 Pair (org.apache.nifi.minifi.c2.api.util.Pair)3 Closeable (java.io.Closeable)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Stream (java.util.stream.Stream)2 MediaType (javax.ws.rs.core.MediaType)2 ConfigurationProvider (org.apache.nifi.minifi.c2.api.ConfigurationProvider)2 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 Path (java.nio.file.Path)1 LinkedHashSet (java.util.LinkedHashSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JAXBException (javax.xml.bind.JAXBException)1 WriteableConfiguration (org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration)1 ConfigSchema (org.apache.nifi.minifi.commons.schema.ConfigSchema)1