incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r582268 - in /incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl: ComponentContextImpl.java ComponentRequestImpl.java ContentData.java RequestData.java filter/ComponentFilterChainHelper.java
Date Fri, 05 Oct 2007 13:40:03 GMT
Author: fmeschbe
Date: Fri Oct  5 06:40:02 2007
New Revision: 582268

URL: http://svn.apache.org/viewvc?rev=582268&view=rev
Log:
SLING-39 RequestDispatcher should use ContentResolver to try to resolve paths to Content objects

Modified:
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestImpl.java
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ContentData.java
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java
    incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/filter/ComponentFilterChainHelper.java

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java?rev=582268&r1=582267&r2=582268&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
(original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentContextImpl.java
Fri Oct  5 06:40:02 2007
@@ -38,6 +38,11 @@
 import org.apache.sling.component.ComponentRequestDispatcher;
 import org.apache.sling.component.ComponentResponse;
 import org.apache.sling.component.Content;
+import org.apache.sling.content.ContentManager;
+import org.apache.sling.core.impl.resolver.ResolvedURLImpl;
+import org.apache.sling.core.resolver.ContentResolver;
+import org.apache.sling.core.resolver.ResolvedURL;
+import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,12 +52,25 @@
 public class ComponentContextImpl implements ComponentContext {
 
     /** default log */
-    private static final Logger log = LoggerFactory.getLogger(ComponentContextImpl.class);
+    private final Logger log = LoggerFactory.getLogger(ComponentContextImpl.class);
 
     private ComponentRequestHandlerImpl requestHandler;
 
-    public ComponentContextImpl(ComponentRequestHandlerImpl requestHandler) {
+    private ServiceTracker contentResolver;
+
+    ComponentContextImpl(ComponentRequestHandlerImpl requestHandler) {
         this.requestHandler = requestHandler;
+
+        this.contentResolver = new ServiceTracker(
+            requestHandler.getBundleContext(), ContentResolver.class.getName(),
+            null);
+        this.contentResolver.open();
+    }
+
+    void dispose() {
+        if (contentResolver != null) {
+            contentResolver.close();
+        }
     }
 
     /**
@@ -114,7 +132,10 @@
             return null;
         }
 
-        return new ComponentRequestDispatcherImpl(content);
+        ResolvedURL resolvedURL = new ResolvedURLImpl(content.getPath(),
+            content);
+
+        return new ComponentRequestDispatcherImpl(resolvedURL);
     }
 
     public RequestDispatcher getRequestDispatcher(String path) {
@@ -144,6 +165,7 @@
 
     /*
      * (non-Javadoc)
+     *
      * @see javax.servlet.ServletContext#getServerInfo()
      */
     public String getServerInfo() {
@@ -170,7 +192,8 @@
     }
 
     /**
-     * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
+     * @see javax.servlet.ServletContext#setAttribute(java.lang.String,
+     *      java.lang.Object)
      */
     public void setAttribute(String name, Object object) {
         getServletContext().removeAttribute(name);
@@ -209,41 +232,45 @@
         return getServletContext().getServlets();
     }
 
-    //---------- Inner class --------------------------------------------------
+    // ---------- Inner class --------------------------------------------------
 
     private class ComponentRequestDispatcherImpl implements
             ComponentRequestDispatcher {
 
-        private final Content content;
+        private ResolvedURL resolvedURL;
+
         private final String path;
 
-        private ComponentRequestDispatcherImpl(Content content) {
-            this.content = content;
-            this.path = content.getPath();
+        private ComponentRequestDispatcherImpl(ResolvedURL resolvedURL) {
+            this.resolvedURL = resolvedURL;
+            this.path = resolvedURL.getOriginalURL();
         }
 
         private ComponentRequestDispatcherImpl(String path) {
-            this.content = null;
+            this.resolvedURL = null;
             this.path = path;
         }
 
-        public void include(ServletRequest request, ServletResponse response) throws ServletException,
IOException {
-            // if content is null, try to resolve it using the path
-            Content target;
-            if (content != null) {
-                target = content;
-            } else {
+        public void include(ServletRequest request, ServletResponse response)
+                throws ServletException, IOException {
+
+            if (resolvedURL == null) {
+
                 // this may throw an exception in case loading fails, which is
                 // ok here, if no content is available at that path null is
                 // return, which results in using the servlet container
                 ComponentRequest cRequest = RequestData.unwrap(request);
-
                 String absPath = getAbsolutePath(cRequest, path);
-                target = cRequest.getContent(absPath);
+
+                ContentResolver cr = (ContentResolver) contentResolver.getService();
+                ContentManager cm = RequestData.getRequestData(cRequest).getContentManager();
+                if (cr != null && cm != null) {
+                    resolvedURL = cr.resolveURL(cm, absPath);
+                }
             }
 
-            if (target != null) {
-                requestHandler.includeContent(request, response, target);
+            if (resolvedURL != null) {
+                requestHandler.includeContent(request, response, resolvedURL);
             } else {
                 requestHandler.includeServlet(request, response, path);
             }
@@ -258,7 +285,8 @@
             }
         }
 
-        public void forward(ServletRequest request, ServletResponse response) throws ServletException,
IOException {
+        public void forward(ServletRequest request, ServletResponse response)
+                throws ServletException, IOException {
             // TODO Auto-generated method stub
             // TODO, use servlet container dispatcher !!
         }

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestImpl.java?rev=582268&r1=582267&r2=582268&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestImpl.java
(original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ComponentRequestImpl.java
Fri Oct  5 06:40:02 2007
@@ -397,35 +397,35 @@
      * @see org.apache.sling.core.component.ComponentRequest#getSelectorString()
      */
     public String getSelectorString() {
-        return this.getRequestData().getSelectorString();
+        return this.getRequestData().getContentData().getSelectorString();
     }
 
     /**
      * @see org.apache.sling.core.component.ComponentRequest#getSelectors()
      */
     public String[] getSelectors() {
-        return this.getRequestData().getSelectors();
+        return this.getRequestData().getContentData().getSelectors();
     }
 
     /**
      * @see org.apache.sling.core.component.ComponentRequest#getSelector(int)
      */
     public String getSelector(int i) {
-        return this.getRequestData().getSelector(i);
+        return this.getRequestData().getContentData().getSelector(i);
     }
 
     /**
      * @see org.apache.sling.core.component.ComponentRequest#getExtension()
      */
     public String getExtension() {
-        return this.getRequestData().getExtension();
+        return this.getRequestData().getContentData().getExtension();
     }
 
     /**
      * @see org.apache.sling.core.component.ComponentRequest#getSuffix()
      */
     public String getSuffix() {
-        return this.getRequestData().getSuffix();
+        return this.getRequestData().getContentData().getSuffix();
     }
 
     public String getQueryString() {

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ContentData.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ContentData.java?rev=582268&r1=582267&r2=582268&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ContentData.java (original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/ContentData.java Fri
Oct  5 06:40:02 2007
@@ -30,6 +30,7 @@
 import org.apache.sling.core.impl.output.BufferProvider;
 import org.apache.sling.core.impl.output.BufferedPrintWriter;
 import org.apache.sling.core.impl.output.BufferedServletOutputStream;
+import org.apache.sling.core.resolver.ResolvedURL;
 
 
 /**
@@ -41,7 +42,7 @@
  */
 public class ContentData implements BufferProvider {
 
-    private Content content;
+    private ResolvedURL resolvedURL;
 
     private Component component;
 
@@ -49,13 +50,13 @@
     private int requestedBufferSize;
     private BufferProvider parent;
 
-    public ContentData(Content content, BufferProvider parent) {
-        this.content = content;
+    public ContentData(ResolvedURL resolvedURL, BufferProvider parent) {
+        this.resolvedURL = resolvedURL;
         this.parent = parent;
     }
 
     /* package */ void dispose() {
-        this.content = null;
+        this.resolvedURL = null;
 
         // flush buffer contents to output
         try {
@@ -69,7 +70,7 @@
      * @return the content
      */
     public Content getContent() {
-        return this.content;
+        return resolvedURL.getContent();
     }
 
     /**
@@ -142,5 +143,34 @@
         this.buffer = writer;
 
         return writer;
+    }
+
+    //---------- URL Resolution results ---------------------------------------
+
+    public String getOriginalURL() {
+        return resolvedURL.getOriginalURL();
+    }
+
+    public String getExtension() {
+        return resolvedURL.getExtension();
+    }
+
+    /**
+     * The i-th selector string or null if i<0 or i>getSelectors().length
+     */
+    public String getSelector(int i) {
+        return resolvedURL.getSelector(i);
+    }
+
+    public String[] getSelectors() {
+        return resolvedURL.getSelectors();
+    }
+
+    public String getSelectorString() {
+        return resolvedURL.getSelectorString();
+    }
+
+    public String getSuffix() {
+        return resolvedURL.getSuffix();
     }
 }

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java?rev=582268&r1=582267&r2=582268&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java (original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/RequestData.java Fri
Oct  5 06:40:02 2007
@@ -40,11 +40,11 @@
 import org.apache.sling.component.ComponentRequestWrapper;
 import org.apache.sling.component.ComponentResponse;
 import org.apache.sling.component.ComponentResponseWrapper;
-import org.apache.sling.component.Content;
 import org.apache.sling.content.ContentManager;
 import org.apache.sling.core.Constants;
 import org.apache.sling.core.impl.output.BufferProvider;
 import org.apache.sling.core.impl.parameters.ParameterSupport;
+import org.apache.sling.core.resolver.ResolvedURL;
 import org.apache.sling.core.theme.Theme;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -60,7 +60,7 @@
 public class RequestData implements BufferProvider {
 
     /** default log */
-    private static final Logger log = LoggerFactory.getLogger(RequestData.class);
+    private final Logger log = LoggerFactory.getLogger(RequestData.class);
 
     /** The original servlet Servlet Request Object */
     private HttpServletRequest servletRequest;
@@ -108,26 +108,11 @@
 
     private Theme theme;
 
-    /** The request URI after applying any fake URLs */
-    private String originalURL;
-
-    /** The mapped selectors */
-    private String[] selectors = new String[0];
-
-    /** The combined selectors string */
-    private String combinedSelectorString = "";
-
-    /** The extension */
-    private String extension = "";
-
-    /** the suffix */
-    private String suffix = "";
-
     /** the current ContentData */
     private ContentData currentContentData;
 
     /** the stack of ContentData objects */
-    private LinkedList contentDataStack;
+    private LinkedList<ContentData> contentDataStack;
 
     public RequestData(HttpServletRequest request, HttpServletResponse response) {
         this.servletRequest = request;
@@ -146,7 +131,7 @@
         // clear the content data stack
         if (this.contentDataStack != null) {
             while (!this.contentDataStack.isEmpty()) {
-                ContentData cd = (ContentData) this.contentDataStack.removeLast();
+                ContentData cd = this.contentDataStack.removeLast();
                 cd.dispose();
             }
         }
@@ -318,11 +303,11 @@
 
     // ---------- Content inclusion stacking -----------------------------------
 
-    public void pushContent(Content content) {
+    public void pushContent(ResolvedURL resolvedURL) {
         BufferProvider parent;
         if (this.currentContentData != null) {
             if (this.contentDataStack == null) {
-                this.contentDataStack = new LinkedList();
+                this.contentDataStack = new LinkedList<ContentData>();
             }
 
             // remove the request attributes if the stack is empty now
@@ -337,7 +322,7 @@
             parent = this;
         }
 
-        this.currentContentData = new ContentData(content, parent);
+        this.currentContentData = new ContentData(resolvedURL, parent);
     }
 
     public void popContent() {
@@ -348,7 +333,7 @@
 
         if (this.contentDataStack != null && !this.contentDataStack.isEmpty()) {
             // remove the topmost content data object
-            this.currentContentData = (ContentData) this.contentDataStack.removeLast();
+            this.currentContentData = this.contentDataStack.removeLast();
 
             // remove the request attributes if the stack is empty now
             if (this.contentDataStack.isEmpty()) {
@@ -578,62 +563,6 @@
         // provide the current theme to components as a request attribute
         // TODO - We should define a well known constant for this
         this.servletRequest.setAttribute(Theme.class.getName(), theme);
-    }
-
-    public void setOriginalURL(String originalURL) {
-        this.originalURL = originalURL;
-    }
-
-    public String getOriginalURL() {
-        return this.originalURL;
-    }
-
-    /**
-     * The selectors to set here is the string of dot-separated words after the
-     * path but before any optional subsequent slashes. This string includes the
-     * request URL extension, which is extracted here, too.
-     */
-    public void setSelectorsExtension(String selectors) {
-        int lastDot = selectors.lastIndexOf('.');
-        if (lastDot < 0) {
-            // no selectors, just the extension
-            this.extension = selectors;
-            return;
-        }
-
-        // extension comes after last dot, rest are selectors
-        this.extension = selectors.substring(lastDot + 1);
-
-        // cut off extension to split selectors
-        this.combinedSelectorString = selectors.substring(0, lastDot);
-        this.selectors = this.combinedSelectorString.split("\\.");
-    }
-
-    public String getExtension() {
-        return this.extension;
-    }
-
-    /**
-     * The i-th selector string or null if i&lt;0 or i&gt;getSelectors().length
-     */
-    public String getSelector(int i) {
-        return (i >= 0 && i < this.selectors.length) ? this.selectors[i] :
null;
-    }
-
-    public String[] getSelectors() {
-        return this.selectors;
-    }
-
-    public String getSelectorString() {
-        return this.combinedSelectorString;
-    }
-
-    public void setSuffix(String suffix) {
-        this.suffix = suffix;
-    }
-
-    public String getSuffix() {
-        return this.suffix;
     }
 
     // ---------- BufferProvider -----------------------------------------

Modified: incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/filter/ComponentFilterChainHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/filter/ComponentFilterChainHelper.java?rev=582268&r1=582267&r2=582268&view=diff
==============================================================================
--- incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/filter/ComponentFilterChainHelper.java
(original)
+++ incubator/sling/trunk/core/src/main/java/org/apache/sling/core/impl/filter/ComponentFilterChainHelper.java
Fri Oct  5 06:40:02 2007
@@ -25,40 +25,46 @@
 import org.apache.sling.component.ComponentFilter;
 
 /**
- * The <code>ComponentFilterChainHelper</code> class is used by Sling to support
- * building lists of <code>RenderFilter</code>s. To ensure filter ordering,
- * each filter is optionally registered with an ordering index. If none is
- * provided the default ordering index is Integer.MAX_VALUE to append the filter
- * to the end of the list.
+ * The <code>ComponentFilterChainHelper</code> class is used by Sling to
+ * support building lists of <code>RenderFilter</code>s. To ensure filter
+ * ordering, each filter is optionally registered with an ordering index. If
+ * none is provided the default ordering index is Integer.MAX_VALUE to append
+ * the filter to the end of the list.
  */
 public class ComponentFilterChainHelper {
 
-    SortedSet filterList;
+    SortedSet<FilterListEntry> filterList;
 
     ComponentFilter[] filters;
 
     public ComponentFilterChainHelper() {
     }
 
-    public ComponentFilter addFilter(ComponentFilter filter) {
-        return this.addFilter(filter, filter, Integer.MAX_VALUE);
-    }
-
     public synchronized ComponentFilter addFilter(ComponentFilter filter,
-            Object filterId, int order) {
+            Long filterId, int order) {
         this.filters = null;
         if (this.filterList == null) {
-            this.filterList = new TreeSet();
+            this.filterList = new TreeSet<FilterListEntry>();
         }
         this.filterList.add(new FilterListEntry(filter, filterId, order));
         return filter;
     }
 
+    public synchronized ComponentFilter[] removeAllFilters() {
+        // will be returned after cleaning the lists
+        ComponentFilter[] removedFilters = getFilters();
+
+        filters = null;
+        filterList = null;
+
+        return removedFilters;
+    }
+
     public synchronized ComponentFilter removeFilter(ComponentFilter filter) {
         if (this.filterList != null) {
             this.filters = null;
-            for (Iterator fi=this.filterList.iterator(); fi.hasNext(); ) {
-                FilterListEntry test = (FilterListEntry) fi.next();
+            for (Iterator<FilterListEntry> fi = filterList.iterator(); fi.hasNext();)
{
+                FilterListEntry test = fi.next();
                 if (test.getFilter().equals(filter)) {
                     fi.remove();
                     return test.getFilter();
@@ -73,8 +79,8 @@
     public synchronized ComponentFilter removeFilterById(Object filterId) {
         if (this.filterList != null) {
             this.filters = null;
-            for (Iterator fi=this.filterList.iterator(); fi.hasNext(); ) {
-                FilterListEntry test = (FilterListEntry) fi.next();
+            for (Iterator<FilterListEntry> fi = filterList.iterator(); fi.hasNext();)
{
+                FilterListEntry test = fi.next();
                 if (test.getFitlerId() == filterId
                     || (test.getFitlerId() != null && test.getFitlerId().equals(
                         filterId))) {
@@ -97,8 +103,9 @@
             if (this.filterList != null && !this.filterList.isEmpty()) {
                 ComponentFilter[] tmp = new ComponentFilter[this.filterList.size()];
                 int i = 0;
-                for (Iterator fi = this.filterList.iterator(); fi.hasNext(); i++) {
-                    tmp[i] = ((FilterListEntry) fi.next()).getFilter();
+                for (FilterListEntry entry : filterList) {
+                    tmp[i] = entry.getFilter();
+                    i++;
                 }
                 this.filters = tmp;
             }
@@ -106,15 +113,15 @@
         return this.filters;
     }
 
-    private static class FilterListEntry implements Comparable {
+    private static class FilterListEntry implements Comparable<FilterListEntry> {
 
         private ComponentFilter filter;
 
-        private Object filterId;
+        private Long filterId;
 
         private int order;
 
-        FilterListEntry(ComponentFilter filter, Object filterId, int order) {
+        FilterListEntry(ComponentFilter filter, Long filterId, int order) {
             this.filter = filter;
             this.filterId = filterId;
             this.order = order;
@@ -124,7 +131,7 @@
             return this.filter;
         }
 
-        Object getFitlerId() {
+        Long getFitlerId() {
             return this.filterId;
         }
 
@@ -132,12 +139,11 @@
          * Note: this class has a natural ordering that is inconsistent with
          * equals.
          */
-        public int compareTo(Object obj) {
-            if (this == obj || this.equals(obj)) {
+        public int compareTo(FilterListEntry other) {
+            if (this == other || this.equals(other)) {
                 return 0;
             }
 
-            FilterListEntry other = (FilterListEntry) obj;
             if (this.order < other.order) {
                 return -1;
             } else if (this.order > other.order) {
@@ -145,9 +151,8 @@
             }
 
             // if the filterId is comparable and the other is of the same class
-            if (this.filterId instanceof Comparable && other.filterId != null
-                && this.filterId.getClass() == other.filterId.getClass()) {
-                int comp = ((Comparable) this.filterId).compareTo(other.filterId);
+            if (filterId != null && other.filterId != null) {
+                int comp = filterId.compareTo(other.filterId);
                 if (comp != 0) {
                     return comp;
                 }



Mime
View raw message