use of org.apache.nifi.minifi.c2.api.cache.ConfigurationCache 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;
}
Aggregations