PXEConfigTFTProvider implements TFTPProvider { protected String resolveVariables(String template, PropertySource<?> propertySource) { final StringBuffer result = new StringBuffer(); final Matcher m = TEMPLATE_REPLACEMENT_PATTERN.matcher(template); while (m.find()) { String variable = m.group(1); String encoding = ""; if (variable.contains(":")) { encoding = variable.substring(0, variable.indexOf(":")); variable = variable.substring(variable.indexOf(":") + 1); } String value = (String) propertySource.getProperty(variable); if (null != value) value = resolveVariables(value, propertySource); else { LOGGER.warn("Pattern refers to undefined variable " + variable); value = ""; } try { if (encoding.equalsIgnoreCase("base64")) value = new String(Base64.encode(value.getBytes("UTF-8"))); else if (encoding.equalsIgnoreCase("urlencoded")) value = URLEncoder.encode(value, "UTF-8").replaceAll("\\+", "%20"); else if (encoding.length() > 0) LOGGER.warn("Ignoring unsupported encoding: " + encoding); } catch (final UnsupportedEncodingException e) { LOGGER.error("That's silly: UTF8-encoding is unsupported!"); } m.appendReplacement(result, value); } m.appendTail(result); return result.toString(); } PXEConfigTFTProvider(Path tftpHome, RealmService realmService, ClientService clientService, Path fallbackTemplatePath); long getLength(SocketAddress peer, SocketAddress local, String arg0,
                        String arg1); InputStream getStream(SocketAddress peer, SocketAddress local,
                               String prefix, String fileName); static final Pattern TEMPLATE_REPLACEMENT_PATTERN; }
@Test public void testProcessTheDefaultTemplate() throws Exception { final PXEConfigTFTProvider provider = createProvider(); final MapPropertySource propertySource = createPropertySource(); final String processed = provider.resolveVariables(DEFAULT_TEMPLATE, propertySource); assertEquals(RESOLVED_TEMPLATE, processed); }