Search in sources :

Example 6 with ConfigurationProviderException

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

the class FileSystemConfigurationCacheTest method getConfigurationTest.

@Test
public void getConfigurationTest() throws IOException, ConfigurationProviderException {
    final String pathRoot = "files";
    final String pathPattern = "config";
    FileSystemConfigurationCache cache = new FileSystemConfigurationCache(pathRoot, pathPattern);
    Map<String, List<String>> parameters = new HashMap<>();
    ConfigurationCacheFileInfo info = cache.getCacheFileInfo("text/yaml", parameters);
    WriteableConfiguration configuration = info.getConfiguration(1);
    assertEquals("config.text.yaml.v1", configuration.getName());
    assertEquals("1", configuration.getVersion());
    assertTrue(configuration.exists());
}
Also used : HashMap(java.util.HashMap) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) FileSystemConfigurationCache(org.apache.nifi.minifi.c2.cache.filesystem.FileSystemConfigurationCache) List(java.util.List) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) Test(org.junit.Test)

Example 7 with ConfigurationProviderException

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

the class FileSystemConfigurationCacheTest method getCachedConfigurationsTest.

@Test
public void getCachedConfigurationsTest() throws IOException, ConfigurationProviderException {
    final String pathRoot = "files";
    final String pathPattern = "config";
    FileSystemConfigurationCache cache = new FileSystemConfigurationCache(pathRoot, pathPattern);
    Map<String, List<String>> parameters = new HashMap<>();
    ConfigurationCacheFileInfo info = cache.getCacheFileInfo("text/yaml", parameters);
    Stream<WriteableConfiguration> configs = info.getCachedConfigurations();
    assertEquals(1, configs.count());
}
Also used : HashMap(java.util.HashMap) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) FileSystemConfigurationCache(org.apache.nifi.minifi.c2.cache.filesystem.FileSystemConfigurationCache) List(java.util.List) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) Test(org.junit.Test)

Example 8 with ConfigurationProviderException

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

the class FileSystemConfigurationCacheTest method getNonexistantConfigurationTest.

@Test
public void getNonexistantConfigurationTest() throws IOException, ConfigurationProviderException {
    final String pathRoot = "files";
    final String pathPattern = "config";
    FileSystemConfigurationCache cache = new FileSystemConfigurationCache(pathRoot, pathPattern);
    Map<String, List<String>> parameters = new HashMap<>();
    ConfigurationCacheFileInfo info = cache.getCacheFileInfo("test/contenttype", parameters);
    WriteableConfiguration configuration = info.getConfiguration(1);
    assertEquals("config.test.contenttype.v1", configuration.getName());
    assertEquals("1", configuration.getVersion());
    assertFalse(configuration.exists());
}
Also used : HashMap(java.util.HashMap) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) FileSystemConfigurationCache(org.apache.nifi.minifi.c2.cache.filesystem.FileSystemConfigurationCache) List(java.util.List) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) Test(org.junit.Test)

Example 9 with ConfigurationProviderException

use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException 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;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LoggerFactory(org.slf4j.LoggerFactory) ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) AuthorizationException(org.apache.nifi.minifi.c2.api.security.authorization.AuthorizationException) GeneralSecurityException(java.security.GeneralSecurityException) Matcher(java.util.regex.Matcher) Map(java.util.Map) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) ConfigurationProvider(org.apache.nifi.minifi.c2.api.ConfigurationProvider) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpConnector(org.apache.nifi.minifi.c2.provider.util.HttpConnector) IOException(java.io.IOException) Configuration(org.apache.nifi.minifi.c2.api.Configuration) StandardCharsets(java.nio.charset.StandardCharsets) ConfigurationCache(org.apache.nifi.minifi.c2.api.cache.ConfigurationCache) IOUtils(org.apache.commons.io.IOUtils) URLEncoder(java.net.URLEncoder) List(java.util.List) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStream(java.io.InputStream) Matcher(java.util.regex.Matcher) AuthorizationException(org.apache.nifi.minifi.c2.api.security.authorization.AuthorizationException) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) HttpURLConnection(java.net.HttpURLConnection)

Example 10 with ConfigurationProviderException

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

the class DelegatingConfigurationProviderTest method testGetConfigurationDoesntExistWithNoVersion.

@Test
public void testGetConfigurationDoesntExistWithNoVersion() throws ConfigurationProviderException, IOException {
    parameters.remove("version");
    endpointPath = "/c2/config?class=raspi3&net=edge";
    initMocks();
    ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class);
    WriteableConfiguration configuration = mock(WriteableConfiguration.class);
    byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    when(httpURLConnection.getInputStream()).thenReturn(new ByteArrayInputStream(payload));
    when(configuration.getOutputStream()).thenReturn(output);
    when(httpURLConnection.getHeaderField("X-Content-Version")).thenReturn("2");
    when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo);
    when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration);
    when(configuration.exists()).thenReturn(false);
    assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, null, parameters));
    assertArrayEquals(payload, output.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

WriteableConfiguration (org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration)11 ConfigurationCacheFileInfo (org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo)10 ConfigurationProviderException (org.apache.nifi.minifi.c2.api.ConfigurationProviderException)9 List (java.util.List)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HashMap (java.util.HashMap)4 InvalidParameterException (org.apache.nifi.minifi.c2.api.InvalidParameterException)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 MediaType (javax.ws.rs.core.MediaType)3 ConfigurationProvider (org.apache.nifi.minifi.c2.api.ConfigurationProvider)3 AuthorizationException (org.apache.nifi.minifi.c2.api.security.authorization.AuthorizationException)3 FileSystemConfigurationCache (org.apache.nifi.minifi.c2.cache.filesystem.FileSystemConfigurationCache)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Closeable (java.io.Closeable)2 OutputStream (java.io.OutputStream)2