use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class HttpConnector method get.
public HttpURLConnection get(String endpointPath, Map<String, List<String>> headers) throws ConfigurationProviderException {
String endpointUrl = baseUrl + endpointPath;
if (logger.isDebugEnabled()) {
logger.debug("Connecting to endpoint: " + endpointUrl);
}
URL url;
try {
url = new URL(endpointUrl);
} catch (MalformedURLException e) {
throw new ConfigurationProviderException("Malformed url " + endpointUrl, e);
}
HttpURLConnection httpURLConnection;
try {
if (proxy == null) {
httpURLConnection = (HttpURLConnection) url.openConnection();
} else {
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
}
if (sslContextFactory != null) {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection;
SSLContext sslContext = sslContextFactory.getSslContext();
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
httpsURLConnection.setSSLSocketFactory(socketFactory);
}
} catch (IOException e) {
throw new ConfigurationProviderException("Unable to connect to " + url, e);
}
if (proxyAuthorization != null) {
httpURLConnection.setRequestProperty("Proxy-Authorization", proxyAuthorization);
}
headers.forEach((s, strings) -> httpURLConnection.setRequestProperty(s, strings.stream().collect(Collectors.joining(","))));
return httpURLConnection;
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class CacheConfigurationProviderTest method testGetConfiguration.
@Test
public void testGetConfiguration() throws ConfigurationProviderException {
int version = 99;
ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class);
ConfigurationCacheFileInfo configurationCacheFileInfo2 = mock(ConfigurationCacheFileInfo.class);
WriteableConfiguration configuration = mock(WriteableConfiguration.class);
WriteableConfiguration configuration2 = mock(WriteableConfiguration.class);
Map<String, List<String>> parameters = mock(Map.class);
when(configConfigurationCache.getCacheFileInfo(TEST_CONTENT_TYPE, parameters)).thenReturn(configurationCacheFileInfo);
when(configConfigurationCache.getCacheFileInfo(TEST_CONTENT_TYPE_2, parameters)).thenReturn(configurationCacheFileInfo2);
when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration);
when(configurationCacheFileInfo2.getConfiguration(version)).thenReturn(configuration2);
assertEquals(configuration, cacheConfigurationProvider.getConfiguration(TEST_CONTENT_TYPE, version, parameters));
assertEquals(configuration2, cacheConfigurationProvider.getConfiguration(TEST_CONTENT_TYPE_2, version, parameters));
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class DelegatingConfigurationProvider method getConfiguration.
@Override
public Configuration getConfiguration(String contentType, Integer version, Map<String, List<String>> parameters) throws ConfigurationProviderException {
HttpURLConnection remoteC2ServerConnection = null;
try {
if (version == null) {
remoteC2ServerConnection = getDelegateConnection(contentType, parameters);
version = Integer.parseInt(remoteC2ServerConnection.getHeaderField("X-Content-Version"));
if (logger.isDebugEnabled()) {
logger.debug("Got current version " + version + " from upstream.");
}
}
ConfigurationCacheFileInfo cacheFileInfo = configurationCache.getCacheFileInfo(contentType, parameters);
WriteableConfiguration configuration = cacheFileInfo.getConfiguration(version);
if (!configuration.exists()) {
if (remoteC2ServerConnection == null) {
remoteC2ServerConnection = getDelegateConnection(contentType, parameters);
}
try (InputStream inputStream = remoteC2ServerConnection.getInputStream();
OutputStream outputStream = configuration.getOutputStream()) {
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
throw new ConfigurationProviderException("Unable to copy remote configuration to cache.", e);
}
}
return configuration;
} finally {
if (remoteC2ServerConnection != null) {
remoteC2ServerConnection.disconnect();
}
}
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class DelegatingConfigurationProviderTest method testGetConfigurationExistsWithVersion.
@Test
public void testGetConfigurationExistsWithVersion() throws ConfigurationProviderException {
ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class);
WriteableConfiguration configuration = mock(WriteableConfiguration.class);
when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo);
when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration);
when(configuration.exists()).thenReturn(true);
assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, version, parameters));
}
use of org.apache.nifi.minifi.c2.api.ConfigurationProviderException in project nifi-minifi by apache.
the class DelegatingConfigurationProviderTest method testGetConfigurationDoesntExistWithVersion.
@Test
public void testGetConfigurationDoesntExistWithVersion() throws ConfigurationProviderException, IOException {
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(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo);
when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration);
when(configuration.exists()).thenReturn(false);
assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, version, parameters));
assertArrayEquals(payload, output.toByteArray());
}
Aggregations