FELIX-878 : Add export of configurations as json. The configs are exported by adding ".json" to the request url. The url can either contain a pid as the path info or a filter expression.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@734504 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ConfigManager.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ConfigManager.java
index b7cac28..937b512 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ConfigManager.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/compendium/ConfigManager.java
@@ -22,6 +22,7 @@
import java.util.*;
import java.util.Map.Entry;
+import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -78,7 +79,7 @@
// the filter to select part of the configurations
String pidFilter = request.getParameter( PID_FILTER );
- ConfigurationAdmin ca = this.getConfigurationAdmin();
+ final ConfigurationAdmin ca = this.getConfigurationAdmin();
// ignore this request if the pid and/or configuration admin is missing
if ( pid == null || ca == null )
@@ -117,18 +118,88 @@
}
// send the result
- response.setContentType( "text/javascript" );
+ response.setContentType( "application/json" );
response.setCharacterEncoding( "UTF-8" );
- printConfigurationJson( response.getWriter(), pid, config, pidFilter, getLocale( request ) );
+ final Locale loc = getLocale( request );
+ final String locale = ( loc != null ) ? loc.toString() : null;
+ printConfigurationJson( response.getWriter(), pid, config, pidFilter, locale );
}
+ /**
+ * @see org.apache.felix.webconsole.AbstractWebConsolePlugin#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+ */
+ protected void doGet( HttpServletRequest request, HttpServletResponse response )
+ throws ServletException, IOException
+ {
+ // let's check for a json request
+ final String info = request.getPathInfo();
+ if ( info.endsWith( ".json" ) )
+ {
+ response.setContentType( "application/json" );
+ response.setCharacterEncoding( "UTF-8" );
+
+ // after last slash and without extension
+ String pid = info.substring( info.lastIndexOf( '/' ) + 1, info.length() - 5 );
+ // check whether the pid is actually a filter for the selection
+ // of configurations to display, if the filter correctly converts
+ // into an OSGi filter, we use it to select configurations
+ // to display
+ String pidFilter = request.getParameter( PID_FILTER );
+ if ( pidFilter == null )
+ {
+ pidFilter = pid;
+ }
+ try
+ {
+ getBundleContext().createFilter( pidFilter );
+
+ // if the pidFilter was set from the pid, clear the pid
+ if ( pid == pidFilter )
+ {
+ pid = null;
+ }
+ }
+ catch ( InvalidSyntaxException ise )
+ {
+ // its ok, if the pid is just a single PID
+ pidFilter = null;
+ }
+
+ final ConfigurationAdmin ca = this.getConfigurationAdmin();
+
+ final Locale loc = getLocale( request );
+ final String locale = ( loc != null ) ? loc.toString() : null;
+
+ final PrintWriter pw = response.getWriter();
+
+ try {
+ pw.write("[");
+ final SortedMap services = this.getServices(pid, pidFilter, locale, false);
+ final Iterator i = services.keySet().iterator();
+ while ( i.hasNext() ) {
+ final String servicePid = i.next().toString();
+
+ final Configuration config = this.getConfiguration(ca, servicePid);
+ this.printConfigurationJson(pw, servicePid, config, pidFilter, locale);
+ }
+ pw.write("]");
+ } catch (InvalidSyntaxException e) {
+ // this should not happend as we checked the filter before
+ }
+ // nothing more to do
+ return;
+ }
+
+ super.doGet( request, response );
+ }
+
+ /**
+ * @see org.apache.felix.webconsole.AbstractWebConsolePlugin#renderContent(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+ */
public void renderContent( HttpServletRequest request, HttpServletResponse response ) throws IOException
{
- // true if MetaType service information is not required
- boolean optionalMetaType = false;
-
// extract the configuration pid from the request path
String pid = request.getPathInfo();
pid = pid.substring( pid.lastIndexOf( '/' ) + 1 );
@@ -158,17 +229,18 @@
pidFilter = null;
}
- Locale loc = getLocale( request );
- String locale = ( loc != null ) ? loc.toString() : null;
+ final ConfigurationAdmin ca = this.getConfigurationAdmin();
- PrintWriter pw = response.getWriter();
+ final Locale loc = getLocale( request );
+ final String locale = ( loc != null ) ? loc.toString() : null;
- String appRoot = (String) request.getAttribute( OsgiManager.ATTR_APP_ROOT );
+ final PrintWriter pw = response.getWriter();
+
+ final String appRoot = (String) request.getAttribute( OsgiManager.ATTR_APP_ROOT );
pw.println( "<script src='" + appRoot + "/res/ui/configmanager.js' language='JavaScript'></script>" );
pw.println( "<table class='content' cellpadding='0' cellspacing='0' width='100%'>" );
- ConfigurationAdmin ca = this.getConfigurationAdmin();
if ( ca == null )
{
pw.println( "<tr class='content' id='configField'>" );
@@ -183,14 +255,14 @@
pw.println( "<tr class='content' id='configField'>" );
pw.println( "<td class='content'>Configurations</th>" );
pw.println( "<td class='content'>" );
- this.listConfigurations( pw, ca, pidFilter, optionalMetaType, locale );
+ this.listConfigurations( pw, ca, pidFilter, locale );
pw.println( "</td>" );
pw.println( "</tr>" );
pw.println( "<tr class='content' id='factoryField'>" );
pw.println( "<td class='content'>Factory Configurations</th>" );
pw.println( "<td class='content'>" );
- this.listFactoryConfigurations( pw, ca, pidFilter, optionalMetaType, locale );
+ this.listFactoryConfigurations( pw, ca, pidFilter, locale );
pw.println( "</td>" );
pw.println( "</tr>" );
}
@@ -206,7 +278,7 @@
}
else
{
- config = getConfiguration( getConfigurationAdmin(), pid );
+ config = getConfiguration( ca, pid );
}
if ( config != null )
@@ -214,7 +286,7 @@
Util.startScript( pw );
pw.println( "var configuration=" );
- printConfigurationJson( pw, config.getPid(), config, pidFilter, getLocale( request ) );
+ printConfigurationJson( pw, config.getPid(), config, pidFilter, locale );
pw.println( ";" );
pw.println( "displayConfigForm(configuration);" );
@@ -255,12 +327,12 @@
}
- private void listConfigurations( PrintWriter pw, ConfigurationAdmin ca, String pidFilter, boolean optionalMetaType, String locale )
+ private void listConfigurations( PrintWriter pw, ConfigurationAdmin ca, String pidFilter, String locale )
{
try
{
// start with ManagedService instances
- SortedMap optionsPlain = getServices( ManagedService.class.getName(), pidFilter, optionalMetaType, locale );
+ SortedMap optionsPlain = getServices( ManagedService.class.getName(), pidFilter, locale, true );
// add in existing configuration (not duplicating ManagedServices)
Configuration[] cfgs = ca.listConfigurations( pidFilter );
@@ -288,7 +360,7 @@
name = pid;
}
- if ( ocd != null || optionalMetaType )
+ if ( ocd != null )
{
optionsPlain.put( pid, name );
}
@@ -303,12 +375,12 @@
}
- private void listFactoryConfigurations( PrintWriter pw, ConfigurationAdmin ca, String pidFilter, boolean optionalMetaType,
+ private void listFactoryConfigurations( PrintWriter pw, ConfigurationAdmin ca, String pidFilter,
String locale )
{
try
{
- SortedMap optionsFactory = getServices( ManagedServiceFactory.class.getName(), pidFilter, optionalMetaType, locale );
+ SortedMap optionsFactory = getServices( ManagedServiceFactory.class.getName(), pidFilter, locale, true );
printOptionsForm( pw, optionsFactory, "configSelection_factory", "create", "Create" );
}
catch ( Exception e )
@@ -318,7 +390,7 @@
}
- private SortedMap getServices( String serviceClass, String serviceFilter, boolean optionalMetaType, String locale )
+ private SortedMap getServices( String serviceClass, String serviceFilter, String locale, boolean ocdRequired )
throws InvalidSyntaxException
{
// sorted map of options
@@ -333,7 +405,7 @@
{
String pid = ( String ) pidObject;
String name;
- ObjectClassDefinition ocd = this.getObjectClassDefinition( refs[i].getBundle(), pid, locale );
+ final ObjectClassDefinition ocd = this.getObjectClassDefinition( refs[i].getBundle(), pid, locale );
if ( ocd != null )
{
name = ocd.getName() + " (";
@@ -344,8 +416,7 @@
name = pid;
}
- if ( ocd != null || optionalMetaType )
- {
+ if ( !ocdRequired || ocd != null ) {
optionsFactory.put( pid, name );
}
}
@@ -385,7 +456,7 @@
private void printConfigurationJson( PrintWriter pw, String pid, Configuration config, String pidFilter,
- Locale locale )
+ String locale )
{
JSONWriter result = new JSONWriter( pw );
@@ -407,10 +478,9 @@
}
- private void configForm( JSONWriter json, String pid, Configuration config, String pidFilter, Locale loc )
+ private void configForm( JSONWriter json, String pid, Configuration config, String pidFilter, String locale )
throws JSONException
{
- String locale = ( loc == null ) ? null : loc.toString();
json.key( ConfigManager.PID );
json.value( pid );
@@ -441,8 +511,7 @@
json.key( "title" );
json.value( pid );
json.key( "description" );
- json
- .value( "Please enter configuration properties for this configuration in the field below. This configuration has no associated description" );
+ json.value( "Please enter configuration properties for this configuration in the field below. This configuration has no associated description" );
json.key( "propertylist" );
json.value( "properties" );