Felix-888 : Add support for {symbolic-name}[:{version}] in the url which can be used instead of the bundle id to get information of a bundle as json, or perform an action on this bundle.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@735636 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundleAction.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundleAction.java
deleted file mode 100644
index 5cfb252..0000000
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundleAction.java
+++ /dev/null
@@ -1,48 +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.internal.core;
-
-
-import javax.servlet.http.HttpServletRequest;
-
-import org.apache.felix.webconsole.Action;
-import org.apache.felix.webconsole.internal.BaseManagementPlugin;
-
-
-abstract class BundleAction extends BaseManagementPlugin implements Action
-{
-
-    protected long getBundleId( HttpServletRequest request )
-    {
-        String bundleIdPar = request.getParameter( BundlesServlet.BUNDLE_ID );
-        if ( bundleIdPar != null )
-        {
-            try
-            {
-                return Long.parseLong( bundleIdPar );
-            }
-            catch ( NumberFormatException nfe )
-            {
-                // TODO: log
-            }
-        }
-
-        // no bundleId or wrong format
-        return -1;
-    }
-
-}
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
index cf28c0e..0778e33 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java
@@ -28,7 +28,6 @@
 import org.apache.felix.bundlerepository.*;
 import org.apache.felix.webconsole.internal.BaseWebConsolePlugin;
 import org.apache.felix.webconsole.internal.Util;
-import org.apache.felix.webconsole.internal.obr.DeployerThread;
 import org.apache.felix.webconsole.internal.servlet.OsgiManager;
 import org.json.JSONException;
 import org.json.JSONWriter;
@@ -36,7 +35,7 @@
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.component.ComponentConstants;
 import org.osgi.service.log.LogService;
-import org.osgi.service.obr.*;
+import org.osgi.service.obr.RepositoryAdmin;
 import org.osgi.service.packageadmin.ExportedPackage;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.osgi.service.startlevel.StartLevel;
@@ -122,7 +121,7 @@
                 {
                     // bundle properties
 
-                    response.setContentType( "text/javascript" );
+                    response.setContentType( "application/json" );
                     response.setCharacterEncoding( "UTF-8" );
 
                     PrintWriter pw = response.getWriter();
@@ -267,20 +266,44 @@
         pathInfo = pathInfo.substring( pathInfo.lastIndexOf( '/' ) + 1 );
 
         // assume bundle Id
-        long bundleId;
         try
         {
-            bundleId = Long.parseLong( pathInfo );
+            final long bundleId = Long.parseLong( pathInfo );
+            if ( bundleId >= 0 )
+            {
+                return getBundleContext().getBundle( bundleId );
+            }
         }
         catch ( NumberFormatException nfe )
         {
-            bundleId = -1;
+            // check if this follows the pattern {symbolic-name}[:{version}]
+            final int pos = pathInfo.indexOf(':');
+            final String symbolicName;
+            final String version;
+            if ( pos == -1 ) {
+                symbolicName = pathInfo;
+                version = null;
+            } else {
+                symbolicName = pathInfo.substring(0, pos);
+                version = pathInfo.substring(pos+1);
+            }
+
+            // search
+            final Bundle[] bundles = getBundleContext().getBundles();
+            for(int i=0; i<bundles.length; i++)
+            {
+                final Bundle bundle = bundles[i];
+                // check symbolic name first
+                if ( symbolicName.equals(bundle.getSymbolicName()) )
+                {
+                    if ( version == null || version.equals(bundle.getHeaders().get(Constants.BUNDLE_VERSION)) )
+                    {
+                        return bundle;
+                    }
+                }
+            }
         }
 
-        if ( bundleId >= 0 )
-        {
-            return getBundleContext().getBundle( bundleId );
-        }
 
         return null;
     }
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java
index d2da770..d63039f 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/InstallAction.java
@@ -17,10 +17,7 @@
 package org.apache.felix.webconsole.internal.core;
 
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.util.Map;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
@@ -30,9 +27,9 @@
 
 import org.apache.commons.fileupload.FileItem;
 import org.apache.felix.webconsole.AbstractWebConsolePlugin;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.Constants;
+import org.apache.felix.webconsole.Action;
+import org.apache.felix.webconsole.internal.BaseManagementPlugin;
+import org.osgi.framework.*;
 import org.osgi.service.log.LogService;
 import org.osgi.service.packageadmin.PackageAdmin;
 import org.osgi.service.startlevel.StartLevel;
@@ -41,7 +38,7 @@
 /**
  * The <code>InstallAction</code> TODO
  */
-public class InstallAction extends BundleAction
+public class InstallAction extends BaseManagementPlugin implements Action
 {
 
     public static final String NAME = "install";
@@ -196,7 +193,7 @@
                     }
                 }
             }
-            
+
             if ( updateBundle != null )
             {
 
@@ -273,7 +270,7 @@
                 {
                     bundle.start();
                 }
-                
+
                 return bundle;
             }
         };