DelegatingConfigurationProvider implements ConfigurationProvider { 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; } DelegatingConfigurationProvider(ConfigurationCache configurationCache, String delegateUrl);  DelegatingConfigurationProvider(ConfigurationCache configurationCache, HttpConnector httpConnector); @Override List<String> getContentTypes(); @Override Configuration getConfiguration(String contentType, Integer version, Map<String, List<String>> parameters); static final Pattern errorPattern; }
@Test public void testGetDelegateConnectionNoParameters() throws ConfigurationProviderException { endpointPath = "/c2/config"; initMocks(); assertEquals(httpURLConnection, delegatingConfigurationProvider.getDelegateConnection(contentType, Collections.emptyMap())); verify(httpURLConnection).setRequestProperty("Accepts", contentType); }
@Test public void testGetDelegateConnectionParameters() throws ConfigurationProviderException { assertEquals(httpURLConnection, delegatingConfigurationProvider.getDelegateConnection(contentType, parameters)); verify(httpURLConnection).setRequestProperty("Accepts", contentType); }
@Test(expected = AuthorizationException.class) public void testGetDelegateConnection403() throws ConfigurationProviderException, IOException { when(httpURLConnection.getResponseCode()).thenReturn(403); delegatingConfigurationProvider.getDelegateConnection(contentType, parameters); }
@Test(expected = InvalidParameterException.class) public void testGetDelegateConnection400() throws IOException, ConfigurationProviderException { when(httpURLConnection.getResponseCode()).thenThrow(new IOException("Server returned HTTP response code: 400 for URL: " + endpointPath)); delegatingConfigurationProvider.getDelegateConnection(contentType, parameters); }
@Test(expected = ConfigurationProviderException.class) public void testGetDelegateConnection401() throws IOException, ConfigurationProviderException { when(httpURLConnection.getResponseCode()).thenReturn(401); delegatingConfigurationProvider.getDelegateConnection(contentType, parameters); }