incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r585088 - in /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling: api/ contenttype/ resource/ servlet/
Date Tue, 16 Oct 2007 09:24:05 GMT
Author: fmeschbe
Date: Tue Oct 16 02:24:04 2007
New Revision: 585088

URL: http://svn.apache.org/viewvc?rev=585088&view=rev
Log:
SLING-54 Drop RequestFilter interface for the real servlet Filter interface

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/AbstractFilter.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java
Removed:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestFilter.java
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/resource/ResourceResolverFilter.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/AbstractFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/AbstractFilter.java?rev=585088&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/AbstractFilter.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/AbstractFilter.java
Tue Oct 16 02:24:04 2007
@@ -0,0 +1,129 @@
+/*
+ * 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.sling.microsling.api;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * The <code>AbstractFilter</code> is a basic abstract implementation of the
+ * <code>javax.servlet.Filter</code> interface.
+ */
+public abstract class AbstractFilter implements Filter {
+
+    private FilterConfig filterConfig;
+
+    /**
+     * Default implementation of filter initialization keeping the filter
+     * configuration made available through the {@link #getFilterConfig()}
+     * method and calls the {@link #init()} method.
+     * <p>
+     * Implementations should not overwrite this method but rather implement
+     * their initialization in the {@link #init()} method.
+     *
+     * @param filterConfig The filter configuration
+     * @throws ServletException forwarded from calling the {@link #init()}
+     *             method.
+     */
+    public void init(FilterConfig filterConfig) throws ServletException {
+        this.filterConfig = filterConfig;
+
+        init();
+    }
+
+    /**
+     * Further initialization by the implementation of this class.
+     *
+     * @throws ServletException may be thrown if an error occurrs initializing
+     *             the filter.
+     */
+    protected abstract void init() throws ServletException;
+
+    /**
+     * Calls the
+     * {@link #doFilter(HttpServletRequest, HttpServletResponse, FilterChain)}
+     * method casting the <code>request</code> and <code>response</code>
+     * objects to the respective HTTP request and response objects. In case of a
+     * non-http request and/or response a <code>ServletException</code> is
+     * thrown.
+     *
+     * @param request The servlet request
+     * @param response The servlet response
+     * @param filterChain The filter chain to which processing is to be handed.
+     * @throws IOException forwarded from calling the
+     *             {@link #doFilter(HttpServletRequest, HttpServletResponse, FilterChain)}
+     *             method
+     * @throws ServletException if <code>request</code> and/or
+     *             <code>response</code> is not a HTTP request or response or
+     *             forwarded from calling the
+     *             {@link #doFilter(HttpServletRequest, HttpServletResponse, FilterChain)}
+     *             method.
+     */
+    public void doFilter(ServletRequest request, ServletResponse response,
+            FilterChain filterChain) throws IOException, ServletException {
+
+        try {
+            HttpServletRequest req = (HttpServletRequest) request;
+            HttpServletResponse res = (HttpServletResponse) response;
+            doFilter(req, res, filterChain);
+        } catch (ClassCastException cce) {
+            throw new ServletException("HTTP Requests supported only");
+        }
+    }
+
+    /**
+     * Called by the
+     * {@link #doFilter(ServletRequest, ServletResponse, FilterChain)} method
+     * for HTTP requests.
+     *
+     * @param request The HTTP servlet request
+     * @param response The HTTP servlet response
+     * @param filterChain The filter chain to which processing is to be handed.
+     * @throws IOException may be thrown in case of an Input/Output problem
+     * @throws ServletException may be thrown in case of any other servlet
+     *             processing issue.
+     */
+    protected abstract void doFilter(HttpServletRequest request,
+            HttpServletResponse response, FilterChain filterChain)
+            throws IOException, ServletException;
+
+    /**
+     * This default implementation does nothing. Implementations of this class
+     * may overwrite but should for stability reasons call this base class
+     * implementation.
+     */
+    public void destroy() {
+    }
+
+    /**
+     * Returns the <code>FilterConfig</code> with which this filter has been
+     * initialized.
+     */
+    protected FilterConfig getFilterConfig() {
+        return filterConfig;
+    }
+}

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=585088&r1=585087&r2=585088&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 02:24:04 2007
@@ -1,18 +1,36 @@
 package org.apache.sling.microsling.contenttype;
 
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
-import org.apache.sling.microsling.api.RequestFilter;
+import org.apache.sling.microsling.api.AbstractFilter;
 import org.apache.sling.microsling.requestcontext.SlingRequestContext;
 
-public class ResponseContentTypeResolverFilter implements RequestFilter {
+public class ResponseContentTypeResolverFilter extends AbstractFilter {
+
+    @Override
+    protected void init() {
+        // no further initialization
+    }
+
+    public void doFilter(HttpServletRequest request,
+            HttpServletResponse response, FilterChain filterChain)
+            throws IOException, ServletException {
+
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(request);
+
+        String file = "dummy." + ctx.getExtension();
+        String type = getFilterConfig().getServletContext().getMimeType(file);
+        if (type != null) {
+            response.setContentType(type);
+        }
+
+        filterChain.doFilter(request, response);
 
-    /** Set the desired Content-Type for the response in the SlingRequestContext */
-    public void doFilter(HttpServletRequest req) throws Exception {
-        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
-        
-        // TODO: use the Accept HTTP header instead?
-        ctx.setResponseContentType(ctx.getMimeType("fakefile." + ctx.getExtension()));
     }
 
 }

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java?rev=585088&r1=585087&r2=585088&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/ResourceResolverFilter.java
Tue Oct 16 02:24:04 2007
@@ -16,28 +16,45 @@
  */
 package org.apache.sling.microsling.resource;
 
+import java.io.IOException;
+
+import javax.jcr.RepositoryException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
-import org.apache.sling.microsling.api.RequestFilter;
+import org.apache.sling.microsling.api.AbstractFilter;
 import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.requestcontext.SlingRequestContext;
 
 /** A RequestFilter that selects the Resource to process */
-public class ResourceResolverFilter implements RequestFilter {
+public class ResourceResolverFilter extends AbstractFilter {
+
+    @Override
+    protected void init() {
+        // no further initialization
+    }
+
+    @Override
+    protected void doFilter(HttpServletRequest request,
+            HttpServletResponse response, FilterChain filterChain)
+            throws IOException, ServletException {
+
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(request);
 
-    public void doFilter(HttpServletRequest req) throws Exception {
-        
-        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
-        
         // TODO support OCM-mapped objects as well
-        Resource r = null;
-        if(ctx.getSession().itemExists(ctx.getContentPath())) {
-            r = new JcrNodeResource(ctx.getSession(),ctx.getContentPath());
-        }
-        
-        if(r != null) {
-            ctx.setResource(r);
+        try {
+            if (ctx.getSession().itemExists(ctx.getContentPath())) {
+                Resource r = new JcrNodeResource(ctx.getSession(),
+                    ctx.getContentPath());
+                ctx.setResource(r);
+            }
+        } catch (RepositoryException re) {
+            throw new ServletException(re.getMessage(), re);
         }
+
+        filterChain.doFilter(request, response);
     }
-    
+
 }

Added: 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=585088&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingFilterHelper.java
Tue Oct 16 02:24:04 2007
@@ -0,0 +1,126 @@
+/*
+ * $Url: $
+ * $Id: $
+ *
+ * Copyright 1997-2005 Day Management AG
+ * Barfuesserplatz 6, 4001 Basel, Switzerland
+ * All Rights Reserved.
+ *
+ * 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.
+ */
+package org.apache.sling.microsling.servlet;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+/**
+ * The <code>MicroSlingFilterHelper</code> TODO
+ */
+class MicroSlingFilterHelper {
+
+    private MicroSlingServlet microSling;
+
+    private List<Filter> requestFilterList = new LinkedList<Filter>();
+
+    private Filter[] requestFilters;
+
+    MicroSlingFilterHelper(MicroSlingServlet microSling) {
+        this.microSling = microSling;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see javax.servlet.FilterChain#doFilter(javax.servlet.ServletRequest,
+     *      javax.servlet.ServletResponse)
+     */
+    public void service(ServletRequest request, ServletResponse response)
+            throws IOException, ServletException {
+
+        MicroSlingFilterChain filterChain = new MicroSlingFilterChain(
+            microSling, getFilters());
+        filterChain.doFilter(request, response);
+
+    }
+
+    private Filter[] getFilters() {
+        if (requestFilters == null) {
+            requestFilters = requestFilterList.toArray(new Filter[requestFilterList.size()]);
+        }
+        return requestFilters;
+    }
+
+    void addFilter(final Filter filter) throws ServletException {
+        FilterConfig config = new FilterConfig() {
+            public String getFilterName() {
+                return filter.getClass().getName();
+            }
+
+            public String getInitParameter(String arg0) {
+                // no parameters for now
+                return null;
+            }
+
+            public Enumeration<?> getInitParameterNames() {
+                // no parameters for now
+                return Collections.enumeration(Collections.emptyList());
+            }
+
+            public ServletContext getServletContext() {
+                return microSling.getServletContext();
+            }
+        };
+
+        // initialize the filter and add it to the list
+        filter.init(config);
+        requestFilterList.add(filter);
+
+        // force recreation of filter list
+        requestFilters = null;
+    }
+
+    private static class MicroSlingFilterChain implements FilterChain {
+
+        private final MicroSlingServlet microSlingServlet;
+
+        private final Filter[] requestFilters;
+
+        private int currentFilter;
+
+        private MicroSlingFilterChain(MicroSlingServlet microSlingServlet,
+                Filter[] requestFilters) {
+            this.microSlingServlet = microSlingServlet;
+            this.requestFilters = requestFilters;
+            this.currentFilter = -1;
+        }
+
+        public void doFilter(ServletRequest request, ServletResponse response)
+                throws IOException, ServletException {
+
+            currentFilter++;
+
+            // call further filter if there are any
+            if (currentFilter < requestFilters.length) {
+                requestFilters[currentFilter].doFilter(request, response, this);
+            } else {
+                microSlingServlet.doService(request, response);
+            }
+
+        }
+    }
+}

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=585088&r1=585087&r2=585088&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 02:24:04 2007
@@ -22,13 +22,15 @@
 import java.util.LinkedList;
 import java.util.List;
 
+import javax.servlet.Filter;
+import javax.servlet.GenericServlet;
 import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.microsling.api.HttpStatusCodeException;
-import org.apache.sling.microsling.api.RequestFilter;
 import org.apache.sling.microsling.api.SlingServlet;
 import org.apache.sling.microsling.contenttype.ResponseContentTypeResolverFilter;
 import org.apache.sling.microsling.etc.HttpConstants;
@@ -39,100 +41,120 @@
 import org.apache.sling.microsling.slingservlets.SlingPostServlet;
 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: processes requests using the Sling filters to
+ * select a Resource and to apply processing to it.
  */
-public class MicroSlingServlet extends HttpServlet {
+public class MicroSlingServlet extends GenericServlet {
 
     private static final long serialVersionUID = 1L;
 
-    private List<RequestFilter> requestFilters = new LinkedList<RequestFilter>
();
-    private List<SlingServlet> servlets = new LinkedList<SlingServlet> ();
-    
+    private MicroSlingFilterHelper filterChain;
+
+    private List<SlingServlet> servlets = new LinkedList<SlingServlet>();
+
     @Override
     public void init() throws ServletException {
         super.init();
-        
+
+        // TODO use OSGi to setup this list
+        filterChain = new MicroSlingFilterHelper(this);
+        addFilter(new ResourceResolverFilter());
+        addFilter(new ResponseContentTypeResolverFilter());
+
         try {
+
             // TODO use OSGi to setup this list
-            requestFilters.add(new ResourceResolverFilter());
-            requestFilters.add(new ResponseContentTypeResolverFilter());
-            
-            // TODO use OSGi to setup this list 
             servlets.add(new SlingPostServlet());
             servlets.add(new VelocityTemplatesServlet());
             servlets.add(new RhinoJavascriptServlet());
             servlets.add(new DefaultSlingServlet());
-            
-        } catch(Exception e) {
-            throw new ServletException("init() failed: " + e,e);
+
+        } catch (Exception e) {
+            throw new ServletException("init() failed: " + e, e);
         }
     }
 
-    /** Process all requests by delegating to the first SlingServlet that
-     *  can process the current request.
+    /**
+     * Process all requests by delegating to the first SlingServlet that can
+     * process the current request.
      */
-    protected void service(HttpServletRequest req, HttpServletResponse resp)
-    throws ServletException, IOException {
-        
+    public void service(ServletRequest req, ServletResponse resp)
+            throws ServletException, IOException {
+        filterChain.service(req, resp);
+    }
+
+    /**
+     * Called by
+     * {@link MicroSlingFilterHelper#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}
+     * after all filters have been processed.
+     */
+    void doService(ServletRequest request, ServletResponse response)
+            throws IOException {
+
+        // assume we have those and don't care for the ClassCastException really
+        HttpServletRequest req = (HttpServletRequest) request;
+        HttpServletResponse resp = (HttpServletResponse) response;
+
         try {
-            new SlingRequestContext(getServletContext(),req);
-            
-            // let our RequestFilters enhance the request information
-            // (select a Resource to act on, etc)
-            for(RequestFilter rf : requestFilters) {
-                rf.doFilter(req);
-            }
-            
-            // Select a SlingServlet and delegate the actual request processing to it 
+            // prepare the request context
+            new SlingRequestContext(getServletContext(), req);
+
+            // Select a SlingServlet and delegate the actual request processing
+            // to it
             final SlingServlet selectedServlet = selectSlingServlet(req);
-            if(selectedServlet!=null) {
-                delegateToSlingServlet(selectedServlet,req,resp);
+            if (selectedServlet != null) {
+                delegateToSlingServlet(selectedServlet, req, resp);
             } else {
-                throw new HttpStatusCodeException(500,"no SlingServlet found to process current
request");
+                throw new HttpStatusCodeException(500,
+                    "no SlingServlet found to process current request");
             }
-            
-        } catch(HttpStatusCodeException hts) {
-            resp.sendError(hts.getStatusCode(),hts.getMessage());
-            
-        } catch(Exception e) {
+
+        } catch (HttpStatusCodeException hts) {
+            resp.sendError(hts.getStatusCode(), hts.getMessage());
+
+        } catch (Exception e) {
             final StringWriter sw = new StringWriter();
-            e.printStackTrace(new PrintWriter(sw,true));
-            resp.sendError(500,e.getMessage() + "\n" + sw.toString());
+            e.printStackTrace(new PrintWriter(sw, true));
+            resp.sendError(500, e.getMessage() + "\n" + sw.toString());
         }
-        
     }
-    
+
     /** Select a SlingServlet to process the given request */
-    protected SlingServlet selectSlingServlet(HttpServletRequest req) throws Exception {
-        for(SlingServlet s : servlets) {
-            if(s.canProcess(req)) {
+    protected SlingServlet selectSlingServlet(HttpServletRequest req)
+            throws Exception {
+        for (SlingServlet s : servlets) {
+            if (s.canProcess(req)) {
                 return s;
             }
         }
         return null;
     }
-    
+
     /** Delegate to the given SlingServlet, based on the request HTTP method */
-    protected void delegateToSlingServlet(SlingServlet s,HttpServletRequest req,HttpServletResponse
resp) 
-    throws Exception {
+    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);
+
+        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);
+            throw new HttpStatusCodeException(501,
+                "HTTP Method not implemented:" + method);
         }
     }
 
- }
+    protected void addFilter(Filter filter) throws ServletException {
+        filterChain.addFilter(filter);
+    }
+}



Mime
View raw message