in src/main/java/org/apache/sling/installer/factories/configuration/impl/ConfigurationSerializerWebConsolePlugin.java [96:270]
public void service(final ServletRequest request, final ServletResponse response)
throws IOException {
final String pid = request.getParameter(PARAMETER_PID);
final String format = request.getParameter(PARAMETER_FORMAT);
// initial loading
final boolean removeMetatypeDefaultProperties;
final boolean removeComponentDefaultProperties;
final boolean removeMergedDefaultProperties;
// initial loading?
if (format == null) {
removeMetatypeDefaultProperties = true;
removeComponentDefaultProperties = true;
removeMergedDefaultProperties = true;
} else {
removeMetatypeDefaultProperties = Boolean.parseBoolean(request.getParameter(PARAMETER_REMOVE_METATYPE_DEFAULT_PROPERTIES));
removeComponentDefaultProperties = Boolean.parseBoolean(request.getParameter(PARAMETER_REMOVE_COMPONENT_DEFAULT_PROPERTIES));
removeMergedDefaultProperties = Boolean.parseBoolean(request.getParameter(PARAMETER_REMOVE_MERGED_DEFAULT_PROPERTIES));
}
Collection<ComponentDescriptionDTO> allComponentDescriptions;
if (removeComponentDefaultProperties) {
allComponentDescriptions = scr.getComponentDescriptionDTOs();
} else {
allComponentDescriptions = Collections.emptyList();
}
MetatypeHandler metatypeHandler = new MetatypeHandler(metatypeService, bundleContext);
Dictionary<String, Object> mergedProperties = ConfigTaskCreator.getDefaultProperties(infoProvider, pid);
if (mergedProperties == null) {
mergedProperties = new Hashtable<>();
}
ConfigurationSerializerFactory.Format serializationFormat = Format.JSON;
if (format != null && !format.trim().isEmpty()) {
try {
serializationFormat = ConfigurationSerializerFactory.Format.valueOf(format);
} catch (IllegalArgumentException e) {
LOGGER.warn("Illegal parameter 'format' given, falling back to default '{}'", serializationFormat, e);
}
}
final PrintWriter pw = response.getWriter();
pw.println("<script type=\"text/javascript\" src=\"" + RES_LOC + "clipboard.js\"></script>");
pw.print("<form method='get'>");
pw.println("<table class='content' cellpadding='0' cellspacing='0' width='100%'>");
titleHtml(
pw,
"OSGi Installer Configuration Printer",
"To emit the configuration properties just enter the configuration PID, select a <a href='https://sling.apache.org/documentation/bundles/configuration-installer-factory.html'>serialization format</a> and click 'Print'");
tr(pw);
tdLabel(pw, "PID");
tdContent(pw);
pw.print("<input type='text' name='");
pw.print(PARAMETER_PID);
pw.print("' value='");
if ( pid != null ) {
pw.print(escapeXml(pid));
}
pw.println("' class='input' size='120' minlength='3'>");
closeTd(pw);
closeTr(pw);
tr(pw);
tdLabel(pw, "Remove Properties");
tdContent(pw);
pw.print("<input type='checkbox' name='");
pw.print(PARAMETER_REMOVE_METATYPE_DEFAULT_PROPERTIES);
pw.print("'");
if ( removeMetatypeDefaultProperties ) {
pw.print(" checked");
}
pw.println(" id='");
pw.print(PARAMETER_REMOVE_METATYPE_DEFAULT_PROPERTIES);
pw.println("' class='input' value='true'>");
pw.println("<label for='");
pw.println(PARAMETER_REMOVE_METATYPE_DEFAULT_PROPERTIES);
pw.println("'>Metatype Default Properties</label>");
pw.print("<input type='checkbox' name='");
pw.print(PARAMETER_REMOVE_COMPONENT_DEFAULT_PROPERTIES);
pw.print("'");
if ( removeComponentDefaultProperties ) {
pw.print(" checked");
}
pw.println(" id='");
pw.print(PARAMETER_REMOVE_COMPONENT_DEFAULT_PROPERTIES);
pw.println("' class='input' value='true'>");
pw.println("<label for='");
pw.println(PARAMETER_REMOVE_COMPONENT_DEFAULT_PROPERTIES);
pw.println("'>Declarative Services Component Properties</label>");
if (Activator.MERGE_SCHEMES != null) {
pw.print("<input type='checkbox' name='");
pw.print(PARAMETER_REMOVE_MERGED_DEFAULT_PROPERTIES);
pw.print("'");
if ( removeMergedDefaultProperties ) {
pw.print(" checked");
}
pw.println(" id='");
pw.print(PARAMETER_REMOVE_MERGED_DEFAULT_PROPERTIES);
pw.println("' class='input' value='true'>");
pw.println("<label for='");
pw.println(PARAMETER_REMOVE_MERGED_DEFAULT_PROPERTIES);
pw.println("'>Merged Properties</label>");
}
pw.println("<p>Selecting any of these options strips those properties which have the same name and value as one from any of the selected sources. The removed properties are very likely being redundant and therefore do not need to be added to serialized configs.</a>");
closeTd(pw);
closeTr(pw);
tr(pw);
tdLabel(pw, "Serialization Format");
tdContent(pw);
pw.print("<select name='");
pw.print(PARAMETER_FORMAT);
pw.println("'>");
option(pw, "JSON", "OSGi Configurator JSON", format);
option(pw, "CONFIG", "Apache Felix Config", format);
option(pw, "PROPERTIES", "Java Properties", format);
option(pw, "PROPERTIES_XML", "Java Properties (XML)", format);
pw.println("</select>");
pw.println(" <input type='submit' value='Print' class='submit'>");
closeTd(pw);
closeTr(pw);
if (pid != null && !pid.trim().isEmpty()) {
tr(pw);
tdLabel(pw, "Serialized Configuration Properties");
tdContent(pw);
Configuration configuration = configurationAdmin.getConfiguration(pid, null);
Dictionary<String, Object> properties = configuration.getProperties();
if (properties == null) {
pw.print("<p class='ui-state-error-text'>");
pw.print("No configuration properties for pid '" + escapeXml(pid) + "' found!");
pw.println("</p>");
} else {
properties = ConfigUtil.cleanConfiguration(properties);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
if (removeMetatypeDefaultProperties) {
metatypeHandler.updateConfiguration(configuration.getFactoryPid(), configuration.getPid(), properties, mergedProperties);
}
if (removeComponentDefaultProperties) {
removeComponentDefaultProperties(allComponentDescriptions, configuration.getPid(), configuration.getFactoryPid(), properties, mergedProperties);
}
if (removeMergedDefaultProperties) {
ConfigUtil.removeRedundantProperties(properties, mergedProperties);
}
ConfigurationSerializerFactory.create(serializationFormat).serialize(properties, baos);
pw.println("<textarea rows=\"20\" cols=\"120\" id=\"output\" readonly>");
pw.print(new String(baos.toByteArray(), StandardCharsets.UTF_8));
pw.println("</textarea>");
pw.println("<button type='button' id='copy'>Copy to Clipboard</a>");
} catch (Exception e) {
pw.print("<p class='ui-state-error-text'>");
pw.print("Error serializing pid '" + escapeXml(pid) + "': " + e.getMessage());
pw.println("</p>");
LOGGER.warn("Error serializing pid '{}'", pid, e);
}
}
closeTd(pw);
closeTr(pw);
}
pw.println("</table>");
pw.print("</form>");
}