use of org.apache.nifi.minifi.c2.api.InvalidParameterException 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");
}
use of org.apache.nifi.minifi.c2.api.InvalidParameterException in project nifi-minifi by apache.
the class FileSystemConfigurationCacheTest method getConfigurationInvalidParametersTest.
@Test(expected = InvalidParameterException.class)
public void getConfigurationInvalidParametersTest() throws IOException, InvalidParameterException {
final String pathRoot = "files";
final String pathPattern = "${test}/config";
FileSystemConfigurationCache cache = new FileSystemConfigurationCache(pathRoot, pathPattern);
Map<String, List<String>> parameters = new HashMap<>();
cache.getCacheFileInfo("test/contenttype", parameters);
}
use of org.apache.nifi.minifi.c2.api.InvalidParameterException in project nifi-minifi by apache.
the class DelegatingConfigurationProvider method getDelegateConnection.
protected HttpURLConnection getDelegateConnection(String contentType, Map<String, List<String>> parameters) throws ConfigurationProviderException {
StringBuilder queryStringBuilder = new StringBuilder();
try {
parameters.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEachOrdered(e -> e.getValue().stream().sorted().forEachOrdered(v -> {
try {
queryStringBuilder.append(URLEncoder.encode(e.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(v, "UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw new ConfigurationProviderException("Unsupported encoding.", ex).wrap();
}
queryStringBuilder.append("&");
}));
} catch (ConfigurationProviderException.Wrapper e) {
throw e.unwrap();
}
String url = "/c2/config";
if (queryStringBuilder.length() > 0) {
queryStringBuilder.setLength(queryStringBuilder.length() - 1);
url = url + "?" + queryStringBuilder.toString();
}
HttpURLConnection httpURLConnection = httpConnector.get(url);
httpURLConnection.setRequestProperty("Accepts", contentType);
try {
int responseCode;
try {
responseCode = httpURLConnection.getResponseCode();
} catch (IOException e) {
Matcher matcher = errorPattern.matcher(e.getMessage());
if (matcher.matches()) {
responseCode = Integer.parseInt(matcher.group(1));
} else {
throw e;
}
}
if (responseCode >= 400) {
String message = "";
InputStream inputStream = httpURLConnection.getErrorStream();
if (inputStream != null) {
try {
message = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} finally {
inputStream.close();
}
}
if (responseCode == 400) {
throw new InvalidParameterException(message);
} else if (responseCode == 403) {
throw new AuthorizationException("Got authorization exception from upstream server " + message);
} else {
throw new ConfigurationProviderException(message);
}
}
} catch (IOException e) {
throw new ConfigurationProviderException("Unable to get response code from upstream server.", e);
}
return httpURLConnection;
}
use of org.apache.nifi.minifi.c2.api.InvalidParameterException 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;
}
use of org.apache.nifi.minifi.c2.api.InvalidParameterException in project nifi-minifi by apache.
the class ConfigService method getConfig.
@GET
public Response getConfig(@Context HttpServletRequest request, @Context HttpHeaders httpHeaders, @Context UriInfo uriInfo) {
try {
authorizer.authorize(SecurityContextHolder.getContext().getAuthentication(), uriInfo);
} catch (AuthorizationException e) {
logger.warn(HttpRequestUtil.getClientString(request) + " not authorized to access " + uriInfo, e);
return Response.status(403).build();
}
Map<String, List<String>> parameters = new HashMap<>();
for (Map.Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
parameters.put(entry.getKey(), entry.getValue());
}
List<MediaType> acceptValues = httpHeaders.getAcceptableMediaTypes();
boolean defaultAccept = false;
if (acceptValues.size() == 0) {
acceptValues = Arrays.asList(MediaType.WILDCARD_TYPE);
defaultAccept = true;
}
if (logger.isDebugEnabled()) {
StringBuilder builder = new StringBuilder("Handling request from ").append(HttpRequestUtil.getClientString(request)).append(" with parameters ").append(parameters).append(" and Accept");
if (defaultAccept) {
builder = builder.append(" default value");
}
builder = builder.append(": ").append(acceptValues.stream().map(Object::toString).collect(Collectors.joining(", ")));
logger.debug(builder.toString());
}
try {
ConfigurationProviderValue configurationProviderValue = configurationCache.get(new ConfigurationProviderKey(acceptValues, parameters));
Configuration configuration = configurationProviderValue.getConfiguration();
Response.ResponseBuilder ok = Response.ok();
ok = ok.header("X-Content-Version", configuration.getVersion());
ok = ok.type(configurationProviderValue.getMediaType());
byte[] buffer = new byte[1024];
int read;
try (InputStream inputStream = configuration.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
MessageDigest md5 = MessageDigest.getInstance("MD5");
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
while ((read = inputStream.read(buffer)) >= 0) {
outputStream.write(buffer, 0, read);
md5.update(buffer, 0, read);
sha256.update(buffer, 0, read);
}
ok = ok.header("Content-MD5", bytesToHex(md5.digest()));
ok = ok.header("X-Content-SHA-256", bytesToHex(sha256.digest()));
ok = ok.entity(outputStream.toByteArray());
} catch (ConfigurationProviderException | IOException | NoSuchAlgorithmException e) {
logger.error("Error reading or checksumming configuration file", e);
throw new WebApplicationException(500);
}
return ok.build();
} catch (AuthorizationException e) {
logger.warn(HttpRequestUtil.getClientString(request) + " not authorized to access " + uriInfo, e);
return Response.status(403).build();
} catch (InvalidParameterException e) {
logger.info(HttpRequestUtil.getClientString(request) + " made invalid request with " + HttpRequestUtil.getQueryString(request), e);
return Response.status(400).entity("Invalid request.").build();
} catch (ConfigurationProviderException e) {
logger.warn("Unable to get configuration.", e);
return Response.status(500).build();
} catch (ExecutionException | UncheckedExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof WebApplicationException) {
throw (WebApplicationException) cause;
}
logger.error(HttpRequestUtil.getClientString(request) + " made request with " + HttpRequestUtil.getQueryString(request) + " that caused error.", cause);
return Response.status(500).entity("Internal error").build();
}
}
Aggregations