Initial attempt at moving over Oscar's Jetty-based HTTP Service implementation
(FELIX-9).


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@395268 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/DummyServletHttpRequest.java b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/DummyServletHttpRequest.java
new file mode 100644
index 0000000..962a267
--- /dev/null
+++ b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/DummyServletHttpRequest.java
@@ -0,0 +1,32 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.mortbay.jetty.servlet;
+
+import org.mortbay.http.HttpRequest;
+
+public class DummyServletHttpRequest
+        extends
+                ServletHttpRequest
+{
+    public DummyServletHttpRequest(ServletHandler servletHandler,
+                       String pathInContext,
+                       HttpRequest request)
+    {
+        super(servletHandler, pathInContext, request);
+    }
+
+}
\ No newline at end of file
diff --git a/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/DummyServletHttpResponse.java b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/DummyServletHttpResponse.java
new file mode 100644
index 0000000..e2c9387
--- /dev/null
+++ b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/DummyServletHttpResponse.java
@@ -0,0 +1,32 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.mortbay.jetty.servlet;
+
+import org.mortbay.http.HttpResponse;
+
+public class DummyServletHttpResponse
+        extends
+                ServletHttpResponse
+{
+    public DummyServletHttpResponse(ServletHttpRequest request,
+            HttpResponse response)
+    {
+        super(request, response);
+    }
+
+}
+
diff --git a/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHandler.java b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHandler.java
new file mode 100644
index 0000000..a3528b4
--- /dev/null
+++ b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHandler.java
@@ -0,0 +1,98 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.mortbay.jetty.servlet;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.servlet.ServletException;
+import javax.servlet.UnavailableException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.mortbay.http.PathMap;
+import org.mortbay.util.Code;
+
+
+public class OsgiServletHandler
+        extends ServletHandler
+{
+    protected org.osgi.service.http.HttpContext     m_osgiHttpContext;
+
+
+    public OsgiServletHandler(
+            org.osgi.service.http.HttpContext osgiHttpContext)
+    {
+        m_osgiHttpContext = osgiHttpContext;
+    }
+
+
+    // allow external adding of osgi servlet holder
+    public void addOsgiServletHolder(String pathSpec, ServletHolder holder)
+    {
+        super.addServletHolder(pathSpec, holder);
+    }
+
+
+    public OsgiServletHolder removeOsgiServletHolder(String pathSpec)
+    {
+        OsgiServletHolder holder = (OsgiServletHolder)
+                super.getServletHolder(pathSpec);
+        PathMap map = super.getServletMap();
+        map.remove(pathSpec);
+
+        // Remove holder from handler name map to allow re-registration.
+        super._nameMap.remove(holder.getName());
+
+        return holder;
+    }
+
+
+    // override standard handler behaviour to return resource from OSGi
+    // HttpContext
+    public URL getResource(String uriInContext)
+                         throws MalformedURLException
+    {
+        Code.debug("OSGI ServletHandler getResource:" + uriInContext);
+        return m_osgiHttpContext.getResource(uriInContext);
+    }
+
+    // override standard behaviour to check context first
+    protected void dispatch(String pathInContext,
+                  HttpServletRequest request,
+                  HttpServletResponse response,
+                  ServletHolder servletHolder)
+        throws ServletException,
+               UnavailableException,
+               IOException
+    {
+        Code.debug("dispatch path = " + pathInContext);
+        if (m_osgiHttpContext.handleSecurity(request, response))
+        {
+            // service request
+            servletHolder.handle(request,response);
+        }
+        else
+        {
+            //TODO: any other error/auth handling we should do in here?
+            response.flushBuffer();
+        }
+    }
+}
+
+
diff --git a/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHolder.java b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHolder.java
new file mode 100644
index 0000000..e4dba28
--- /dev/null
+++ b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHolder.java
@@ -0,0 +1,91 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.mortbay.jetty.servlet;
+
+
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.UnavailableException;
+
+public class OsgiServletHolder
+        extends
+                ServletHolder
+{
+    private Servlet m_servlet;
+    private ServletConfig m_config;
+
+
+    public OsgiServletHolder(ServletHandler handler, Servlet servlet,
+            String name, Dictionary params)
+    {
+        super(handler,name,servlet.getClass().getName());
+        m_servlet = servlet;
+
+        // Seemed safer to copy params into parent holder, rather than override
+        // the getInitxxx methods.
+        if (params != null)
+        {
+            Enumeration e = params.keys();
+            while (e.hasMoreElements())
+            {
+                Object key = e.nextElement();
+                super.put(key, params.get(key));
+            }
+        }
+    }
+
+    public synchronized Servlet getServlet()
+        throws UnavailableException
+    {
+        return m_servlet;        
+    }
+
+    public Servlet getOsgiServlet()
+    {
+        return m_servlet;
+    }
+
+
+    // override "Holder" method to prevent instantiation
+    public synchronized Object newInstance()
+        throws InstantiationException,
+               IllegalAccessException
+    {
+        return getOsgiServlet();
+    }
+
+    // override "Holder" method to prevent attempt to load
+    // the servlet class.
+    public void start()
+        throws Exception
+    {
+        _class=m_servlet.getClass();
+        
+        m_config=new Config();
+        m_servlet.init(m_config);        
+    }
+
+    // override "Holder" method to prevent destroy, which is only called
+    // when a bundle manually unregisters
+    public void stop()
+    {
+    }
+}
+
diff --git a/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHttpContext.java b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHttpContext.java
new file mode 100644
index 0000000..3a95d13
--- /dev/null
+++ b/org.apache.felix.http.jetty/src/main/java/org/mortbay/jetty/servlet/OsgiServletHttpContext.java
@@ -0,0 +1,49 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.mortbay.jetty.servlet;
+
+
+import org.mortbay.util.Code;
+
+public class OsgiServletHttpContext
+        extends
+                ServletHttpContext
+{
+    protected org.osgi.service.http.HttpContext     m_osgiHttpContext;
+    
+    public OsgiServletHttpContext(
+            org.osgi.service.http.HttpContext osgiHttpContext)
+    {
+        m_osgiHttpContext = osgiHttpContext;
+    }
+    
+    // intercept to ensure OSGi context is used first for servlet calls to 
+    // getMimeType()
+    public String getMimeByExtension(String filename)
+    { 
+        Code.debug("OSGi servlet context: get mime type");
+        String encoding = m_osgiHttpContext.getMimeType(filename);
+
+        if (encoding == null)
+        {
+            encoding = super.getMimeByExtension(filename);
+        }
+        
+        return encoding;
+    }
+    
+}