incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r585101 - in /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling: contenttype/ requestcontext/ servlet/
Date Tue, 16 Oct 2007 10:32:18 GMT
Author: bdelacretaz
Date: Tue Oct 16 03:32:02 2007
New Revision: 585101

URL: http://svn.apache.org/viewvc?rev=585101&view=rev
Log:
SLING-54 code comments, simplification of MicroSlingServlet (hide the gory stuff elsewhere
;-) and minor fixes

Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java
    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/contenttype/ResponseContentTypeResolverFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java?rev=585101&r1=585100&r2=585101&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
Tue Oct 16 03:32:02 2007
@@ -24,9 +24,11 @@
         final SlingRequestContext ctx = SlingRequestContext.getFromRequest(request);
 
         String file = "dummy." + ctx.getExtension();
-        String type = getFilterConfig().getServletContext().getMimeType(file);
-        if (type != null) {
-            response.setContentType(type);
+        final String contentType = getFilterConfig().getServletContext().getMimeType(file);
+        if (contentType != null) {
+            // Need this info in the SlingRequestContext,
+            // for the SlingScriptResolver for example
+            ctx.setResponseContentType(contentType);
         }
 
         filterChain.doFilter(request, response);

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java?rev=585101&r1=585100&r2=585101&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/requestcontext/SlingRequestContext.java
Tue Oct 16 03:32:02 2007
@@ -17,9 +17,12 @@
 package org.apache.sling.microsling.requestcontext;
 
 import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.sling.microsling.api.Resource;
@@ -29,11 +32,12 @@
 /** Additional Request-based info used by for SlingServlets */
 public class SlingRequestContext {
 
-    private final Session session;
+    private Session session;
     private final String contentPath;
     private final String extension;
     private String responseContentType;
     private final ServletContext servletContext;
+    private final Repository repository;
     private Resource resource;
     
     public static final String REQ_ATTR_NAME = SlingRequestContext.class.getName();
@@ -48,22 +52,24 @@
     }
 
     /** TODO parse the request to extract extension, selectors, etc. */
-    public SlingRequestContext(ServletContext sctx,HttpServletRequest req) throws Exception
{
+    public SlingRequestContext(ServletContext sctx,ServletRequest servletRequest) throws
ServletException {
         this.servletContext = sctx;
         
+        if( !(servletRequest instanceof HttpServletRequest) ) {
+            throw new ServletException("SlingRequestContext requires an HttpServletRequest");
+        }
+        HttpServletRequest req = (HttpServletRequest)servletRequest;
+        
         // Store this into the request attributes
         req.setAttribute(REQ_ATTR_NAME,this);
         
         // Access our Repository and get a Session
         final String repoAttr = Repository.class.getName();
-        final Repository rep = (Repository)sctx.getAttribute(repoAttr);
-        if(rep==null) {
+        repository = (Repository)sctx.getAttribute(repoAttr);
+        if(repository==null) {
             throw new SlingException("Repository not available in ServletContext attribute
" + repoAttr);
         }
         
-        // TODO use the user's credentials
-        session = rep.login(new SimpleCredentials("admin", "admin".toCharArray()));
-        
         // TODO more precise parsing: selectors, suffix, etc.
         final String path = req.getPathInfo();
         final int lastDot = path.lastIndexOf('.');
@@ -76,7 +82,11 @@
         }
     }
     
-    public Session getSession() {
+    /** Acquire a JCR Session if not done yet, and return it */  
+    public Session getSession() throws RepositoryException {
+        if(session==null) {
+            session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
+        }
         return session;
     }
 

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java?rev=585101&r1=585100&r2=585101&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java
Tue Oct 16 03:32:02 2007
@@ -1,16 +1,18 @@
 /*
- * $Url: $
- * $Id: $
+ * 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
  *
- * Copyright 1997-2005 Day Management AG
- * Barfuesserplatz 6, 4001 Basel, Switzerland
- * All Rights Reserved.
+ *      http://www.apache.org/licenses/LICENSE-2.0
  *
- * This software is the confidential and proprietary information of
- * Day Management AG, ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Day.
+ * 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.sling.microsling.servlet;
 
@@ -27,9 +29,14 @@
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
-/**
- * The <code>MicroSlingFilterHelper</code> TODO
+/** Manages the microsling chain of servlet Filters:
+ *  stores the list, calls them when processing a request
+ *  and calls microSlingServlet.doService after that.
+ *  
+ *  This is utility code, not very interesting to study.
  */
 class MicroSlingFilterHelper {
 
@@ -58,6 +65,7 @@
 
     }
 
+    /** return our Filters as a (lazily created) array */
     private Filter[] getFilters() {
         if (requestFilters == null) {
             requestFilters = requestFilterList.toArray(new Filter[requestFilterList.size()]);
@@ -65,6 +73,7 @@
         return requestFilters;
     }
 
+    /** Add a Filter at the end of our current chain */
     void addFilter(final Filter filter) throws ServletException {
         FilterConfig config = new FilterConfig() {
             public String getFilterName() {
@@ -94,6 +103,9 @@
         requestFilters = null;
     }
 
+    /** A FilterChain that applies all Filters in an array and calls
+     *  MicroSlingServlet.doFilter when done
+     */ 
     private static class MicroSlingFilterChain implements FilterChain {
 
         private final MicroSlingServlet microSlingServlet;
@@ -114,11 +126,21 @@
 
             currentFilter++;
 
-            // call further filter if there are any
             if (currentFilter < requestFilters.length) {
+                // call the next filter
                 requestFilters[currentFilter].doFilter(request, response, this);
+                
             } else {
-                microSlingServlet.doService(request, response);
+                // done with filters, call microsling servlet
+                HttpServletRequest hReq = null;
+                HttpServletResponse hRep = null;
+                try {
+                    hReq = (HttpServletRequest)request;
+                    hRep = (HttpServletResponse)response;
+                } catch(ClassCastException cce) {
+                    throw new ServletException("Expected an HttpServletRequest and HttpServletResponse",cce);
+                }
+                microSlingServlet.doService(hReq,hRep);
             }
 
         }

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=585101&r1=585100&r2=585101&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
Tue Oct 16 03:32:02 2007
@@ -42,8 +42,9 @@
 import org.apache.sling.microsling.slingservlets.VelocityTemplatesServlet;
 
 /**
- * The main Microsling servlet: processes requests using the Sling filters to
- * select a Resource and to apply processing to it.
+ * The main microsling servlet: apply Filters to the request using our
+ * MicroSlingFilterHelper, select and delegate to a SlingServlet
+ * to process the request.  
  */
 public class MicroSlingServlet extends GenericServlet {
 
@@ -75,12 +76,14 @@
         }
     }
 
-    /**
-     * Process all requests by delegating to the first SlingServlet that can
-     * process the current request.
+    /** 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 SlingRequestContext(getServletContext(),req);
         filterChain.service(req, resp);
     }
 
@@ -89,17 +92,10 @@
      * {@link MicroSlingFilterHelper#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}
      * after all filters have been processed.
      */
-    void doService(ServletRequest request, ServletResponse response)
+    void doService(HttpServletRequest req, HttpServletResponse resp)
             throws IOException {
 
-        // assume we have those and don't care for the ClassCastException really
-        HttpServletRequest req = (HttpServletRequest) request;
-        HttpServletResponse resp = (HttpServletResponse) response;
-
         try {
-            // prepare the request context
-            new SlingRequestContext(getServletContext(), req);
-
             // Select a SlingServlet and delegate the actual request processing
             // to it
             final SlingServlet selectedServlet = selectSlingServlet(req);
@@ -154,6 +150,7 @@
         }
     }
 
+    /** Add a filter to our MicroSlingFilterHelper */
     protected void addFilter(Filter filter) throws ServletException {
         filterChain.addFilter(filter);
     }



Mime
View raw message