incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1512369 - in /sling/trunk/bundles/servlets/resolver: ./ src/main/java/org/apache/sling/servlets/resolver/internal/ src/test/java/org/apache/sling/servlets/resolver/internal/
Date Fri, 09 Aug 2013 16:07:57 GMT
Author: bdelacretaz
Date: Fri Aug  9 16:07:57 2013
New Revision: 1512369

URL: http://svn.apache.org/r1512369
Log:
SLING-3006 - use SlingRequestPathInfo to decompose URLs for webconsole plugin

Added:
    sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/DecomposedURL.java
  (with props)
    sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/DecomposedURLTest.java
  (with props)
Modified:
    sling/trunk/bundles/servlets/resolver/pom.xml
    sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java

Modified: sling/trunk/bundles/servlets/resolver/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/pom.xml?rev=1512369&r1=1512368&r2=1512369&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/pom.xml (original)
+++ sling/trunk/bundles/servlets/resolver/pom.xml Fri Aug  9 16:07:57 2013
@@ -61,6 +61,13 @@
                         <Private-Package>
                             org.apache.sling.servlets.resolver.*
                         </Private-Package>
+                        <Import-Package>
+                            !org.apache.sling.engine.impl.*,
+                            *
+                        </Import-Package>
+                        <Embed-Dependency>
+                            org.apache.sling.engine;inline="org/apache/sling/engine/impl/request/SlingRequestPathInfo*",
+                        </Embed-Dependency>                
                     </instructions>
                 </configuration>
             </plugin>

Added: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/DecomposedURL.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/DecomposedURL.java?rev=1512369&view=auto
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/DecomposedURL.java
(added)
+++ sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/DecomposedURL.java
Fri Aug  9 16:07:57 2013
@@ -0,0 +1,56 @@
+/*
+ * 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.servlets.resolver.internal;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.sling.api.request.RequestPathInfo;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.SyntheticResource;
+import org.apache.sling.engine.impl.request.SlingRequestPathInfo;
+
+/** Used by the ServletResolverWebConsolePlugin to decompose URLs */
+public class DecomposedURL {
+    final RequestPathInfo requestPathInfo;
+
+    DecomposedURL(String urlString) {
+        
+        // For the path, take everything up to the first dot
+        String fullPath = urlString;
+        if(urlString.contains("http")) {
+            try {
+                fullPath = new URL(urlString).getPath();
+            } catch(MalformedURLException ignore) {
+            }
+        }
+        final int firstDot = fullPath.indexOf(".");
+        
+        final ResourceMetadata metadata = new ResourceMetadata();
+        final Resource r = new SyntheticResource(null, metadata, null);
+        metadata.setResolutionPath(firstDot < 0 ? fullPath : fullPath.substring(0, firstDot));
+        metadata.setResolutionPathInfo(firstDot < 0 ? null : fullPath.substring(firstDot));
+        requestPathInfo = new SlingRequestPathInfo(r);
+    }
+    
+    RequestPathInfo getRequestPathInfo() {
+        return requestPathInfo;
+    }
+}

Propchange: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/DecomposedURL.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/DecomposedURL.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java?rev=1512369&r1=1512368&r2=1512369&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
(original)
+++ sling/trunk/bundles/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/internal/SlingServletResolver.java
Fri Aug  9 16:07:57 2013
@@ -62,6 +62,7 @@ import org.apache.sling.api.SlingConstan
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.request.RequestProgressTracker;
 import org.apache.sling.api.request.RequestUtil;
 import org.apache.sling.api.resource.LoginException;
@@ -1166,49 +1167,10 @@ public class SlingServletResolver
             }
         }
 
-        class DecomposedURL {
-            final String extension;
-            final String path;
-            final String[] selectors;
-
-            DecomposedURL(String url) {
-                if (url != null) {
-                    final int lastDot = url.lastIndexOf('.');
-                    final int firstDot = url.indexOf('.');
-                    if (lastDot > 0) {
-                        final int slashInExtension = url.indexOf('/', lastDot);
-                        // strip suffix, if any
-                        if (slashInExtension > 0) {
-                            extension = url.substring(lastDot + 1, slashInExtension);
-                        } else {
-                            extension = url.substring(lastDot + 1);
-                        }
-
-                        path = url.substring(0, firstDot);
-                        if (lastDot != firstDot) {
-                            // has selectors
-                            final String selectorString = url.substring(firstDot + 1, lastDot);
-                            selectors = selectorString.split("\\.");
-                        } else {
-                            selectors = new String[0];
-                        }
-                    } else {
-                        extension = "";
-                        path = url;
-                        selectors = new String[0];
-                    }
-                } else {
-                    extension = "";
-                    path = "";
-                    selectors = new String[0];
-                }
-            }
-        }
-
         @Override
         protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
             final String url = request.getParameter(PARAMETER_URL);
-            final DecomposedURL decomposed = new DecomposedURL(url);
+            final RequestPathInfo requestPathInfo = new DecomposedURL(url).getRequestPathInfo();
             String method = request.getParameter(PARAMETER_METHOD);
             if (StringUtils.isBlank(method)) {
                 method = "GET";
@@ -1258,31 +1220,42 @@ public class SlingServletResolver
                     tdContent(pw);
                     pw.println("<dl>");
                     pw.println("<dt>Path</dt>");
-                    pw.println("<dd>" + decomposed.path + "</dd>");
+                    pw.println("<dd>" + requestPathInfo.getResourcePath() + "</dd>");
                     pw.println("<dt>Selectors</dt>");
                     pw.print("<dd>");
-                    if (decomposed.selectors.length == 0) {
+                    if (requestPathInfo.getSelectors().length == 0) {
                         pw.print("&lt;none&gt;");
                     } else {
                         pw.print("[");
-                        pw.print(StringUtils.join(decomposed.selectors, ", "));
+                        pw.print(StringUtils.join(requestPathInfo.getSelectors(), ", "));
                         pw.print("]");
                     }
                     pw.println("</dd>");
                     pw.println("<dt>Extension</dt>");
-                    pw.println("<dd>" + decomposed.extension + "</dd>");
+                    pw.println("<dd>" + requestPathInfo.getExtension() + "</dd>");
+                    pw.println("</dl>");
+                    pw.println("</dd>");
+                    pw.println("<dt>Suffix</dt>");
+                    pw.println("<dd>" + requestPathInfo.getSuffix() + "</dd>");
                     pw.println("</dl>");
                     closeTd(pw);
                     closeTr(pw);
                 }
 
-                if (StringUtils.isNotBlank(decomposed.path)) {
+                if (StringUtils.isNotBlank(requestPathInfo.getResourcePath())) {
                     final Collection<Resource> servlets;
-                    Resource resource = resourceResolver.resolve(decomposed.path);
+                    Resource resource = resourceResolver.resolve(requestPathInfo.getResourcePath());
                     if (resource.adaptTo(Servlet.class) != null) {
                         servlets = Collections.singleton(resource);
                     } else {
-                        final ResourceCollector locationUtil = ResourceCollector.create(resource,
defaultWorkspaceName, decomposed.extension, executionPaths, defaultExtensions, method, decomposed.selectors);
+                        final ResourceCollector locationUtil = ResourceCollector.create(
+                                resource, 
+                                defaultWorkspaceName, 
+                                requestPathInfo.getExtension(), 
+                                executionPaths, 
+                                defaultExtensions, 
+                                method, 
+                                requestPathInfo.getSelectors());
                         servlets = locationUtil.getServlets(resourceResolver);
                     }
                     tr(pw);
@@ -1292,7 +1265,7 @@ public class SlingServletResolver
                     if (servlets == null || servlets.isEmpty()) {
                         pw.println("Could not find a suitable servlet for this request!");
                     } else {
-                        pw.println("Candidate servlets and scripts in order of preference:<br/>");
+                        pw.println("Candidate servlets and scripts in order of preference
for method " + method + ":<br/>");
                         pw.println("<ol class='servlets'>");
                         Iterator<Resource> iterator = servlets.iterator();
                         outputServlets(pw, iterator);

Added: sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/DecomposedURLTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/DecomposedURLTest.java?rev=1512369&view=auto
==============================================================================
--- sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/DecomposedURLTest.java
(added)
+++ sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/DecomposedURLTest.java
Fri Aug  9 16:07:57 2013
@@ -0,0 +1,94 @@
+/*
+ * 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.servlets.resolver.internal;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.api.request.RequestPathInfo;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class DecomposedURLTest {
+    private final RequestPathInfo rpi;
+    private final String path;
+    private final String extension;
+    private final String selectors;
+    private String suffix;
+    
+    @Parameters(name="{0}")
+    public static List<Object[]> data() {
+            final List<Object[]> result = new ArrayList<Object[]>();
+            result.add(new Object[] { 
+                    "http://localhost:8080/libs/foo/content/something/formitems.truc.who.json/image/vnd/xnd/knd.xml",
+                    "/libs/foo/content/something/formitems",
+                    "json",
+                    "truc.who",
+                    "/image/vnd/xnd/knd.xml"
+            }); 
+            result.add(new Object[] { 
+                    "/libs/foo.a4.print.html",
+                    "/libs/foo",
+                    "html",
+                    "a4.print",
+                    null
+            }); 
+            result.add(new Object[] { 
+                    "/libs/bar.html",
+                    "/libs/bar",
+                    "html",
+                    null,
+                    null
+            }); 
+            return result;
+    }
+    
+    public DecomposedURLTest(String input, String path, String extension, String selectors,
String suffix) {
+        rpi = new DecomposedURL(input).getRequestPathInfo();
+        this.path = path;
+        this.extension = extension;
+        this.selectors = selectors;
+        this.suffix = suffix;
+    }
+    
+    @Test
+    public void checkPath() {
+        assertEquals(path, rpi.getResourcePath());
+    }
+    
+    @Test
+    public void checkExtension() {
+        assertEquals(extension, rpi.getExtension());
+    }
+    
+    @Test
+    public void checkSelectors() {
+        assertEquals(selectors, rpi.getSelectorString());
+    }
+    
+    @Test
+    public void checkSuffix() {
+        assertEquals(suffix, rpi.getSuffix());
+    }
+}

Propchange: sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/DecomposedURLTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/internal/DecomposedURLTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message