incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r591251 - in /incubator/sling/trunk/microsling/microsling-core/src: main/java/org/apache/sling/microsling/contenttype/ main/java/org/apache/sling/microsling/request/helpers/ main/java/org/apache/sling/microsling/scripting/helpers/ main/java...
Date Fri, 02 Nov 2007 07:48:06 GMT
Author: fmeschbe
Date: Fri Nov  2 00:48:05 2007
New Revision: 591251

URL: http://svn.apache.org/viewvc?rev=591251&view=rev
Log:
SLING-88 Adapt to new RequestPathInfo spec regarding selectors, extension and suffix return
values
and add new test cases for bordercases like trailing dot and consecutive dots in the selector
string

Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfo.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
    incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfoTest.java

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java?rev=591251&r1=591250&r2=591251&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
Fri Nov  2 00:48:05 2007
@@ -44,12 +44,16 @@
         @Override
         public String getResponseContentType() {
             if (responseContentType == null) {
-                String file = "dummy."
-                    + getSlingRequest().getRequestPathInfo().getExtension();
-                final String contentType = getFilterConfig().getServletContext().getMimeType(
-                    file);
-                if (contentType != null) {
-                    responseContentType = contentType;
+                String ext = getSlingRequest().getRequestPathInfo().getExtension();
+                if (ext != null) {
+                    String file = "dummy." + ext;
+                    final String contentType = getFilterConfig().getServletContext().getMimeType(
+                        file);
+                    if (contentType != null) {
+                        responseContentType = contentType;
+                    } else {
+                        responseContentType = DEFAULT_RESPONSE_CONTENT_TYPE;
+                    }
                 } else {
                     responseContentType = DEFAULT_RESPONSE_CONTENT_TYPE;
                 }

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfo.java?rev=591251&r1=591250&r2=591251&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfo.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfo.java
Fri Nov  2 00:48:05 2007
@@ -43,7 +43,7 @@
 
     private final String resourcePath;
 
-    private final static String EMPTY = "";
+    private final static String[] NO_SELECTORS = new String[0];
 
     /** break requestPath as required by SlingRequestPathInfo */
     public MicroslingRequestPathInfo(Resource r, String requestPath) {
@@ -65,29 +65,49 @@
             pathToParse = pathToParse.substring(resourcePath.length());
         }
 
-        // separate selectors/ext from the suffix
-        int firstSlash = pathToParse.indexOf('/');
-        String pathToSplit;
-        if (firstSlash < 0) {
-            pathToSplit = pathToParse;
-            suffix = EMPTY;
-        } else {
-            pathToSplit = pathToParse.substring(0, firstSlash);
-            suffix = pathToParse.substring(firstSlash);
-        }
+        if (pathToParse.startsWith("/")) {
+
+            // only a suffix exists
+            selectorString = null;
+            selectors = NO_SELECTORS;
+            extension = null;
+            suffix = pathToParse;
 
-        int lastDot = pathToSplit.lastIndexOf('.');
-        if (lastDot <= 0) {
-            // negative if pathToSplit is empty (in case all is suffix)
-            // zero if pathToSplit just contains the extension
-            selectorString = EMPTY;
-            selectors = new String[0];
-            extension = pathToSplit.substring(lastDot+1);
         } else {
-            // assume the string to start with a dot
-            selectorString = pathToSplit.substring(1, lastDot);
-            selectors = selectorString.split("\\.");
-            extension = pathToSplit.substring(lastDot + 1);
+
+            // separate selectors/ext from the suffix
+            int firstSlash = pathToParse.indexOf('/');
+            String pathToSplit;
+            if (firstSlash < 0) {
+                pathToSplit = pathToParse;
+                suffix = null;
+            } else {
+                pathToSplit = pathToParse.substring(0, firstSlash);
+                suffix = pathToParse.substring(firstSlash);
+            }
+
+
+            int lastDot = pathToSplit.lastIndexOf('.');
+
+            if (lastDot <= 1) {
+
+                // no selectors if only extension exists or selectors is empty
+                selectorString = null;
+                selectors = NO_SELECTORS;
+
+            } else {
+
+                // no selectors if splitting would give an empty array
+                String tmpSel = pathToSplit.substring(1, lastDot);
+                selectors = tmpSel.split("\\.");
+                selectorString = (selectors.length > 0) ? tmpSel : null;
+
+            }
+
+            // extension only if lastDot is not trailing
+            extension = (lastDot + 1 < pathToSplit.length())
+                    ? pathToSplit.substring(lastDot + 1)
+                    : null;
         }
     }
 

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java?rev=591251&r1=591250&r2=591251&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/scripting/helpers/ScriptFilenameBuilder.java
Fri Nov  2 00:48:05 2007
@@ -27,31 +27,31 @@
  *    <li>Desired response Content-Type</li>
  *    <li>Desired script extension</li>
  *  </ul>
- *  
+ *
  *  See ScriptFilenameBuilderTest for examples.
- *  
+ *
  *  Note that names can include partial paths, for example we return
  *  "print/a4/html.js" for a GET request for an html document with
  *  selectors "print.a4".
  */
 public class ScriptFilenameBuilder {
 
-    /** @return a name like "html.js" or "print/a4/html.vlt" or "POST.js" */ 
+    /** @return a name like "html.js" or "print/a4/html.vlt" or "POST.js" */
     public String buildScriptFilename(String methodName,String selectors,String contentType,String
scriptExtension) {
         final StringBuffer sb = new StringBuffer();
-        
+
         // path before filename:
         // add selectors in front of the filename if any, replacing dots in them by slashes
         // so that print.a4 becomes print/a4/
-        if(selectors != null && selectors.length() > 0) {
+        if(selectors != null) {
             sb.append(selectors.toLowerCase().replace('.','/'));
             sb.append('/');
         }
-        
+
         // filename:
         if(methodName==null || methodName.length() == 0) {
             sb.append("NO_METHOD");
-            
+
         } else if(HttpConstants.METHOD_GET.equalsIgnoreCase(methodName)) {
             // for the GET method, use the simplified content-type, lowercased,
             // as the filename.
@@ -63,13 +63,13 @@
                 final String [] splitContentType = contentType.split("/");
                 sb.append(splitContentType[splitContentType.length - 1].toLowerCase());
             }
-            
+
         } else {
             // for other methods use the method name
             sb.append(methodName.toUpperCase());
         }
-        
-        // extension: use desired script extension 
+
+        // extension: use desired script extension
         sb.append(".");
         if(scriptExtension == null || scriptExtension.length()==0) {
             sb.append("NO_EXT");
@@ -78,5 +78,5 @@
         }
         return sb.toString();
     }
-    
+
 }

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java?rev=591251&r1=591250&r2=591251&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
Fri Nov  2 00:48:05 2007
@@ -50,14 +50,14 @@
  */
 public class DefaultSlingServlet extends SlingAllMethodsServlet {
     private static final long serialVersionUID = -2259461041692895761L;
-    
+
     private Map<String, Servlet> renderingServlets = new HashMap <String, Servlet>();
-    
+
     public DefaultSlingServlet() {
         renderingServlets.put("txt", new PlainTextRendererServlet());
         renderingServlets.put("html", new DefaultHtmlRendererServlet());
     }
-    
+
     @Override
     /** Delegate rendering to one of our renderingServlets, based on the request extension
*/
     protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp)
@@ -80,11 +80,11 @@
                 "Resource not found: " + r.getURI());
         }
 
-        // make sure we have an Item, and render it via one of our renderingServlets 
+        // make sure we have an Item, and render it via one of our renderingServlets
         final Object data = r.getRawData();
         if(data!=null && (data instanceof Item)) {
             String ext = req.getRequestPathInfo().getExtension();
-            if(ext==null || ext.length() == 0) {
+            if (ext == null) {
                 ext = "txt";
             }
             final Servlet s = renderingServlets.get(ext);
@@ -97,7 +97,7 @@
                         + ", use one of these extensions: " + renderingServlets.keySet()
                 );
             }
-            
+
         } else {
             throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_IMPLEMENTED,
                 "Not implemented: resource " + req.getResource().getURI()
@@ -138,7 +138,7 @@
             if(current.hasNodes()) {
                 final RequestPathInfo pathInfo = req.getRequestPathInfo();
                 final String parentPath = pathInfo.getResourcePath();
-                final String newNodePath = (pathInfo.getSuffix() == null || pathInfo.getSuffix().length()
== 0)
+                final String newNodePath = (pathInfo.getSuffix() == null)
                         ? String.valueOf(System.currentTimeMillis())
                         : pathInfo.getSuffix();
                 current = deepCreateNode(s, parentPath + "/" + newNodePath);

Modified: incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfoTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfoTest.java?rev=591251&r1=591250&r2=591251&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfoTest.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/request/helpers/MicroslingRequestPathInfoTest.java
Fri Nov  2 00:48:05 2007
@@ -29,8 +29,9 @@
         RequestPathInfo p = new MicroslingRequestPathInfo(
             new MockResource("/"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
-        assertEquals("", p.getExtension());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
         assertEquals("/some/path.print.a4.html/some/suffix", p.getSuffix());
     }
 
@@ -43,12 +44,73 @@
         }
     }
 
+    public void testTrailingDot() {
+        RequestPathInfo p = new MicroslingRequestPathInfo(
+            new MockResource("/some/path"), "/some/path.");
+        assertEquals("/some/path", p.getResourcePath());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertNull(p.getSuffix());
+    }
+
+    public void testTrailingDotWithSuffix() {
+        RequestPathInfo p = new MicroslingRequestPathInfo(
+            new MockResource("/some/path"), "/some/path./suffix");
+        assertEquals("/some/path", p.getResourcePath());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertEquals("/suffix", p.getSuffix());
+    }
+
+    public void testTrailingDotDot() {
+        RequestPathInfo p = new MicroslingRequestPathInfo(
+            new MockResource("/some/path"), "/some/path..");
+        assertEquals("/some/path", p.getResourcePath());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertNull(p.getSuffix());
+    }
+
+    public void testTrailingDotDotWithSuffix() {
+        RequestPathInfo p = new MicroslingRequestPathInfo(
+            new MockResource("/some/path"), "/some/path../suffix");
+        assertEquals("/some/path", p.getResourcePath());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertEquals("/suffix", p.getSuffix());
+    }
+
+    public void testTrailingDotDotDot() {
+        RequestPathInfo p = new MicroslingRequestPathInfo(
+            new MockResource("/some/path"), "/some/path...");
+        assertEquals("/some/path", p.getResourcePath());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertNull(p.getSuffix());
+    }
+
+    public void testTrailingDotDotDotWithSuffix() {
+        RequestPathInfo p = new MicroslingRequestPathInfo(
+            new MockResource("/some/path"), "/some/path.../suffix");
+        assertEquals("/some/path", p.getResourcePath());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertEquals("/suffix", p.getSuffix());
+    }
+
     public void testSimpleSuffix() {
         RequestPathInfo p = new MicroslingRequestPathInfo(
             new MockResource("/"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
-        assertEquals("", p.getExtension());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
         assertEquals("/some/path.print.a4.html/some/suffix", p.getSuffix());
     }
 
@@ -57,6 +119,9 @@
             new MockResource("/some/path"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/some/path", p.getResourcePath());
         assertEquals("print.a4", p.getSelectorString());
+        assertEquals(2, p.getSelectors().length);
+        assertEquals("print", p.getSelectors()[0]);
+        assertEquals("a4", p.getSelectors()[1]);
         assertEquals("html", p.getExtension());
         assertEquals("/some/suffix", p.getSuffix());
     }
@@ -65,27 +130,30 @@
         RequestPathInfo p = new MicroslingRequestPathInfo(
             new MockResource("/"), null);
         assertEquals("/", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
-        assertEquals("", p.getExtension());
-        assertEquals("", p.getSuffix());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertNull(p.getSuffix());
     }
 
     public void testPathOnly() {
         RequestPathInfo p = new MicroslingRequestPathInfo(new MockResource(
             "/some/path/here"), "/some/path/here");
         assertEquals("/some/path/here", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
-        assertEquals("", p.getExtension());
-        assertEquals("", p.getSuffix());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
+        assertNull(p.getSuffix());
     }
 
     public void testPathAndExtensionOnly() {
         RequestPathInfo p = new MicroslingRequestPathInfo(new MockResource(
             "/some/path/here"), "/some/path/here.html");
         assertEquals("/some/path/here", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
         assertEquals("html", p.getExtension());
-        assertEquals("", p.getSuffix());
+        assertNull(p.getSuffix());
     }
 
     public void testPathAndOneSelectorOnly() {
@@ -93,15 +161,18 @@
             "/some/path/here"), "/some/path/here.print.html");
         assertEquals("/some/path/here", p.getResourcePath());
         assertEquals("print", p.getSelectorString());
+        assertEquals(1, p.getSelectors().length);
+        assertEquals("print", p.getSelectors()[0]);
         assertEquals("html", p.getExtension());
-        assertEquals("", p.getSuffix());
+        assertNull(p.getSuffix());
     }
 
     public void testPathExtAndSuffix() {
         RequestPathInfo p = new MicroslingRequestPathInfo(new MockResource(
             "/some/path/here"), "/some/path/here.html/something");
         assertEquals("/some/path/here", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
         assertEquals("html", p.getExtension());
         assertEquals("/something", p.getSuffix());
     }
@@ -121,8 +192,9 @@
         RequestPathInfo p = new MicroslingRequestPathInfo(new MockResource(
             "/some"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/some", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
-        assertEquals("", p.getExtension());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
         assertEquals("/path.print.a4.html/some/suffix", p.getSuffix());
     }
 
@@ -131,6 +203,9 @@
             "/some/path"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/some/path", p.getResourcePath());
         assertEquals("print.a4", p.getSelectorString());
+        assertEquals(2, p.getSelectors().length);
+        assertEquals("print", p.getSelectors()[0]);
+        assertEquals("a4", p.getSelectors()[1]);
         assertEquals("html", p.getExtension());
         assertEquals("/some/suffix", p.getSuffix());
     }
@@ -140,6 +215,8 @@
             "/some/path.print"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/some/path.print", p.getResourcePath());
         assertEquals("a4", p.getSelectorString());
+        assertEquals(1, p.getSelectors().length);
+        assertEquals("a4", p.getSelectors()[0]);
         assertEquals("html", p.getExtension());
         assertEquals("/some/suffix", p.getSuffix());
     }
@@ -148,7 +225,8 @@
         RequestPathInfo p = new MicroslingRequestPathInfo(new MockResource(
             "/some/path.print.a4"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/some/path.print.a4", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
         assertEquals("html", p.getExtension());
         assertEquals("/some/suffix", p.getSuffix());
     }
@@ -157,8 +235,9 @@
         RequestPathInfo p = new MicroslingRequestPathInfo(new MockResource(
             "/some/path.print.a4.html"), "/some/path.print.a4.html/some/suffix");
         assertEquals("/some/path.print.a4.html", p.getResourcePath());
-        assertEquals("", p.getSelectorString());
-        assertEquals("", p.getExtension());
+        assertNull(p.getSelectorString());
+        assertEquals(0, p.getSelectors().length);
+        assertNull(p.getExtension());
         assertEquals("/some/suffix", p.getSuffix());
     }
 



Mime
View raw message