incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r586426 - /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
Date Fri, 19 Oct 2007 13:17:22 GMT
Author: fmeschbe
Date: Fri Oct 19 06:17:22 2007
New Revision: 586426

URL: http://svn.apache.org/viewvc?rev=586426&view=rev
Log:
SLING-64 Refactor Servlets and Script resolution
SLING-67 Provide extensible default Servlets

Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java?rev=586426&r1=586425&r2=586426&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
Fri Oct 19 06:17:22 2007
@@ -19,11 +19,15 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.servlet.Filter;
 import javax.servlet.GenericServlet;
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -31,74 +35,93 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.microsling.api.ServiceLocator;
-import org.apache.sling.microsling.api.SlingServlet;
+import org.apache.sling.microsling.api.SlingRequestContext;
 import org.apache.sling.microsling.api.exceptions.HttpStatusCodeException;
 import org.apache.sling.microsling.contenttype.ResponseContentTypeResolverFilter;
-import org.apache.sling.microsling.helpers.constants.HttpConstants;
 import org.apache.sling.microsling.request.MicroslingRequestContext;
+import org.apache.sling.microsling.scripting.SlingScriptResolver;
 import org.apache.sling.microsling.services.MicroslingServiceLocator;
 import org.apache.sling.microsling.slingservlets.DefaultSlingServlet;
-import org.apache.sling.microsling.slingservlets.RhinoJavascriptServlet;
-import org.apache.sling.microsling.slingservlets.SlingPostServlet;
-import org.apache.sling.microsling.slingservlets.VelocityTemplatesServlet;
+import org.apache.sling.microsling.slingservlets.StreamServlet;
 
 /**
  * The main microsling servlet: apply Filters to the request using our
- * MicroSlingFilterHelper, select and delegate to a SlingServlet
- * to process the request.
+ * MicroSlingFilterHelper, select and delegate to a SlingServlet to process the
+ * request.
  */
 public class MicroSlingServlet extends GenericServlet {
 
     private static final long serialVersionUID = 1L;
 
     private MicroSlingFilterHelper filterChain;
+
     private ServiceLocator serviceLocator;
 
-    private List<SlingServlet> servlets = new LinkedList<SlingServlet>();
+    private Map<String, Servlet> servlets;
+
+    private SlingScriptResolver scriptResolver;
+
+    private DefaultSlingServlet defaultSlingServlet;
 
     @Override
     public void init() throws ServletException {
         super.init();
 
         serviceLocator = new MicroslingServiceLocator();
-        
+
         // TODO use OSGi to setup this list
         filterChain = new MicroSlingFilterHelper(this);
         addFilter(new ResponseContentTypeResolverFilter());
 
+        // TODO use OSGi to setup this list
+        servlets = new HashMap<String, Servlet>();
+        addServlet("nt:file", new StreamServlet());
+
         try {
 
-            // TODO use OSGi to setup this list
-            servlets.add(new SlingPostServlet());
-            servlets.add(new VelocityTemplatesServlet());
-            servlets.add(new RhinoJavascriptServlet());
-            servlets.add(new DefaultSlingServlet());
+            scriptResolver = new SlingScriptResolver();
+            defaultSlingServlet = new DefaultSlingServlet();
 
         } catch (Exception e) {
             throw new ServletException("init() failed: " + e, e);
         }
     }
 
-    /** Execute our Filters via MicroSlingFilterHelper, which
-     *  calls our doService method after executing the filters
+    /**
+     * Execute our Filters via MicroSlingFilterHelper, which calls our doService
+     * method after executing the filters
      */
     public void service(ServletRequest req, ServletResponse resp)
             throws ServletException, IOException {
 
         // our filters might need the SlingRequestContext to store info in it
-        new MicroslingRequestContext(getServletContext(),req,serviceLocator);
+        new MicroslingRequestContext(getServletContext(), req, serviceLocator);
         filterChain.service(req, resp);
     }
 
     @Override
     public void destroy() {
-        super.destroy();
-
         // just for completeness, we have to take down our filters
         if (filterChain != null) {
             filterChain.destroy();
             filterChain = null;
         }
+
+        // destroy registered servlets
+        Servlet[] servletList = servlets.values().toArray(
+            new Servlet[servlets.size()]);
+        for (Servlet servlet : servletList) {
+            try {
+                servlet.destroy();
+            } catch (Throwable t) {
+                getServletContext().log(
+                    "Unexpected problem destroying servlet " + servlet, t);
+            }
+        }
+        servlets.clear();
+
+        // destroy base class at the end
+        super.destroy();
     }
 
     /**
@@ -112,12 +135,16 @@
         try {
             // Select a SlingServlet and delegate the actual request processing
             // to it
-            final SlingServlet selectedServlet = selectSlingServlet(req);
+            final Servlet selectedServlet = selectSlingServlet(req);
             if (selectedServlet != null) {
                 delegateToSlingServlet(selectedServlet, req, resp);
             } else {
-                throw new HttpStatusCodeException(500,
-                    "no SlingServlet found to process current request");
+                // no typed servlet, so lets try scripting
+                boolean scriptExists = scriptResolver.evaluateScript(req, resp);
+                if (!scriptExists) {
+                    // use the default servlet if there is script
+                    delegateToSlingServlet(defaultSlingServlet, req, resp);
+                }
             }
 
         } catch (HttpStatusCodeException hts) {
@@ -131,41 +158,50 @@
     }
 
     /** Select a SlingServlet to process the given request */
-    protected SlingServlet selectSlingServlet(HttpServletRequest req)
+    protected Servlet selectSlingServlet(HttpServletRequest req)
             throws Exception {
-        for (SlingServlet s : servlets) {
-            if (s.canProcess(req)) {
-                return s;
-            }
-        }
-        return null;
+
+        // use the resource type to select a servlet
+        String type = SlingRequestContext.Accessor.getSlingRequestContext(req).getResource().getResourceType();
+        return (type != null) ? servlets.get(type) : null;
     }
 
     /** Delegate to the given SlingServlet, based on the request HTTP method */
-    protected void delegateToSlingServlet(SlingServlet s,
-            HttpServletRequest req, HttpServletResponse resp) throws Exception {
-        final String method = req.getMethod();
-
-        if (HttpConstants.METHOD_GET.equals(method)) {
-            s.doGet(req, resp);
-        } else if (HttpConstants.METHOD_HEAD.equals(method)) {
-            s.doHead(req, resp);
-        } else if (HttpConstants.METHOD_POST.equals(method)) {
-            s.doPost(req, resp);
-        } else if (HttpConstants.METHOD_PUT.equals(method)) {
-            s.doPut(req, resp);
-        } else if (HttpConstants.METHOD_DELETE.equals(method)) {
-            s.doDelete(req, resp);
-        } else if (HttpConstants.METHOD_OPTIONS.equals(method)) {
-            s.doOptions(req, resp);
-        } else {
-            throw new HttpStatusCodeException(501,
-                "HTTP Method not implemented:" + method);
-        }
+    protected void delegateToSlingServlet(Servlet s, HttpServletRequest req,
+            HttpServletResponse resp) throws ServletException, IOException {
+        s.service(req, resp);
     }
 
     /** Add a filter to our MicroSlingFilterHelper */
     protected void addFilter(Filter filter) throws ServletException {
         filterChain.addFilter(filter);
+    }
+
+    /** Add servlets by resource type */
+    protected void addServlet(final String resourceType, Servlet servlet) {
+
+        try {
+            ServletConfig config = new ServletConfig() {
+                public String getInitParameter(String name) {
+                    return MicroSlingServlet.this.getInitParameter(name);
+                }
+                public Enumeration<?> getInitParameterNames() {
+                    return MicroSlingServlet.this.getInitParameterNames();
+                }
+                public ServletContext getServletContext() {
+                    return MicroSlingServlet.this.getServletContext();
+                }
+                public String getServletName() {
+                    return resourceType;
+                }
+            };
+            servlet.init(config);
+
+            // only register if initialization succeeds
+            servlets.put(resourceType, servlet);
+        } catch (Throwable t) {
+            getServletContext().log("Failed initializing servlet " + servlet + " for type
" + resourceType, t);
+        }
+
     }
 }



Mime
View raw message