FELIX-1993 : Enhance configuration printer support
Replaced BinaryConfigurationPrinter with AtachmentProvider - this gives more flexibility and reduces the number of methods to a single call.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@903993 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/AttachmentProvider.java b/webconsole/src/main/java/org/apache/felix/webconsole/AttachmentProvider.java
new file mode 100644
index 0000000..385a9ac
--- /dev/null
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/AttachmentProvider.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.webconsole;
+
+
+import java.net.URL;
+
+
+/**
+ * This is an optional extension of the {@link ConfigurationPrinter}.
+ * If a configuration printer implements this interface, the printer
+ * can add additional attamchemtns to the output of the configuration rendering.
+ *
+ * Currently this is only supported for the ZIP mode.
+ *
+ * A service implementing this method must still register itself
+ * as a {@link ConfigurationPrinter} but not as a
+ * {@link AttachmentProvider} service.
+ * @since 3.0
+ */
+public interface AttachmentProvider
+{
+
+    /**
+     * Return an array of attachments for the given render mode.
+     * The returned list should contain URLs pointing to the
+     * attachments for this mode.
+     * @param mode The render mode.
+     * @return An array of urls or null.
+     */
+    URL[] getAttachments(String mode);
+}
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/BinaryConfigurationPrinter.java b/webconsole/src/main/java/org/apache/felix/webconsole/BinaryConfigurationPrinter.java
deleted file mode 100644
index 045db39..0000000
--- a/webconsole/src/main/java/org/apache/felix/webconsole/BinaryConfigurationPrinter.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.felix.webconsole;
-
-
-import java.io.IOException;
-import java.io.OutputStream;
-
-
-/**
- * This is an optional extension of the {@link ConfigurationPrinter}.
- * If a configuration printer implements this interface, the printer
- * can add additional files to the output of the configuration rendering.
- *
- * Currently this is only supported for the ZIP mode.
- *
- * A service implementing this method must still register itself
- * as a {@link ConfigurationPrinter} but not as a
- * {@link BinaryConfigurationPrinter} service.
- * @since 3.0
- */
-public interface BinaryConfigurationPrinter
-    extends ConfigurationPrinter
-{
-
-    /**
-     * Return a list of filename for the render mode.
-     * The returned list should just contain filenames without
-     * any path information.
-     * @return An array of filenames or null.
-     */
-    String[] getFilenames(String mode);
-
-    /**
-     * Write the contents of the file to the output stream.
-     * @param name The filename.
-     * @param out The output stream.
-     * @throws IOException If any problem occurs during writing.
-     * @throws IllegalArgumentException If the name is null or not
-     *         one from the list returned by {@link #getFilenames(String)}.
-     */
-    void writeFile(String name, String mode,  OutputStream out)
-    throws IOException;
-}
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
index 31d3de5..e544608 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
@@ -18,6 +18,7 @@
 
 
 import java.io.*;
+import java.net.URL;
 import java.text.*;
 import java.util.*;
 import java.util.zip.ZipEntry;
@@ -27,6 +28,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.felix.webconsole.*;
 import org.apache.felix.webconsole.internal.BaseWebConsolePlugin;
 import org.apache.felix.webconsole.internal.Util;
@@ -553,15 +555,37 @@
             if ( desc.match(mode) )
             {
                 // check if printer implements binary configuration printer
-                if ( desc.printer instanceof BinaryConfigurationPrinter )
+                if ( desc.printer instanceof AttachmentProvider )
                 {
-                    final String[] names = ((BinaryConfigurationPrinter)desc.printer).getFilenames(mode);
-                    if ( names != null )
+                    final URL[] attachments = ((AttachmentProvider)desc.printer).getAttachments(mode);
+                    if ( attachments != null )
                     {
-                        for(int i = 0; i < names.length; i++)
+                        for(int i = 0; i < attachments.length; i++)
                         {
-                            final OutputStream os = cf.startFile(desc.printer.getTitle(), names[i]);
-                            ((BinaryConfigurationPrinter)desc.printer).writeFile(names[i], mode, os);
+                            final URL current = attachments[i];
+                            final String path = current.getPath();
+                            final String name;
+                            if ( path == null || path.length() == 0 )
+                            {
+                                // sanity code, we should have a path, but if not let's just create
+                                // some random name
+                                name = UUID.randomUUID().toString();
+                            }
+                            else
+                            {
+                                final int pos = path.lastIndexOf('/');
+                                name = (pos == -1 ? path : path.substring(pos + 1));
+                            }
+                            final OutputStream os = cf.startFile(desc.printer.getTitle(), name);
+                            final InputStream is = current.openStream();
+                            try
+                            {
+                                IOUtils.copy(is, os);
+                            }
+                            finally
+                            {
+                                IOUtils.closeQuietly(is);
+                            }
                             cf.end();
                         }
                     }