You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2023/06/20 13:57:34 UTC
[sling-org-apache-sling-servlets-resolver] 01/01: SLING-11911 Use resource resolver in servlet resolver web console plugin
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch feature/use-resource-resolver-in-web-console
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git
commit 3df9e7505c91d4399f22aa9feadeff7d31c24ba0
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue Jun 20 15:57:27 2023 +0200
SLING-11911 Use resource resolver in servlet resolver web console plugin
Get rid of heuristics with regards to the URL decomposition.
---
.../internal/console/WebConsolePlugin.java | 22 ++++++----------------
.../internal/console/DecomposedURLTest.java | 13 +------------
.../internal/console/WebConsolePluginTest.java | 5 +----
3 files changed, 8 insertions(+), 32 deletions(-)
diff --git a/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java b/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java
index 0615b6c..7882aa5 100644
--- a/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePlugin.java
@@ -84,13 +84,6 @@ public class WebConsolePlugin extends HttpServlet {
private static final String PARAMETER_METHOD = "method";
private static final String SERVICE_USER_CONSOLE = "console";
- private static final String CONSOLE_PATH_WARNING =
- "Note that in a real Sling request, the path might vary depending on the existence of"
- + " resources that partially match it. "
- + "<br/>This utility does not take this into account and uses the first dot to split"
- + " between path and selectors/extension. "
- + "<br/>As a workaround, you can replace dots with underline characters, for example, when " +
- "testing such a URL.";
@Reference(target="("+ServiceUserMapped.SUBSERVICENAME+"=" + SERVICE_USER_CONSOLE + ")")
private ServiceUserMapped consoleServiceUserMapped; // NOSONAR
@@ -124,7 +117,7 @@ public class WebConsolePlugin extends HttpServlet {
@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
final String url = request.getParameter(PARAMETER_URL);
- final RequestPathInfo requestPathInfo = getRequestPathInfo(url);
+
String method = request.getParameter(PARAMETER_METHOD);
if (StringUtils.isBlank(method)) {
method = "GET";
@@ -133,7 +126,7 @@ public class WebConsolePlugin extends HttpServlet {
String requestURI = request.getRequestURI();
try (final ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object)SERVICE_USER_CONSOLE))) {
final PrintWriter pw = response.getWriter();
-
+ final RequestPathInfo requestPathInfo = getRequestPathInfo(url, resourceResolver);
if (requestURI.endsWith("json")) {
pw.println("{");
if (StringUtils.isNotBlank(url)) {
@@ -142,7 +135,6 @@ public class WebConsolePlugin extends HttpServlet {
if (StringUtils.isNotBlank(requestPathInfo.getResourcePath())) {
printJSONCandidatesElement(pw, resourceResolver, requestPathInfo, method);
}
- pw.printf(" \"warningMsg\" : \"%s\",%n", CONSOLE_PATH_WARNING.replace("<br/>", ""));
pw.printf(" \"method\" : \"%s\"%n", StringEscapeUtils.escapeJson(method));
pw.print("}");
@@ -323,8 +315,6 @@ public class WebConsolePlugin extends HttpServlet {
pw.println("<dt>Path</dt>");
dd(pw);
pw.print(ResponseUtil.escapeXml(requestPathInfo.getResourcePath()));
- pw.print("<br/>");
- pw.print(String.format("<em>%s</em>", CONSOLE_PATH_WARNING));
closeDd(pw);
pw.println("<dt>Selectors</dt>");
dd(pw);
@@ -474,15 +464,14 @@ public class WebConsolePlugin extends HttpServlet {
}
}
- static RequestPathInfo getRequestPathInfo(String urlString) {
+ static RequestPathInfo getRequestPathInfo(String urlString, ResourceResolver resourceResolver) {
- if(urlString == null) {
+ if (urlString == null) {
urlString = "";
}
- // For the path, take everything up to the first dot
String fullPath = urlString;
- if(urlString.contains("http")) {
+ if (urlString.contains("http")) {
try {
fullPath = new URL(urlString).getPath();
} catch(MalformedURLException ignore) {
@@ -490,6 +479,7 @@ public class WebConsolePlugin extends HttpServlet {
}
}
return SlingUriBuilder.create()
+ .setResourceResolver(resourceResolver)
.setPath(fullPath)
.build();
}
diff --git a/src/test/java/org/apache/sling/servlets/resolver/internal/console/DecomposedURLTest.java b/src/test/java/org/apache/sling/servlets/resolver/internal/console/DecomposedURLTest.java
index c3cae7f..f660323 100644
--- a/src/test/java/org/apache/sling/servlets/resolver/internal/console/DecomposedURLTest.java
+++ b/src/test/java/org/apache/sling/servlets/resolver/internal/console/DecomposedURLTest.java
@@ -62,8 +62,6 @@ public class DecomposedURLTest {
null
});
- // dots are always considered separators between path and selectors/extension,
- // as mentioned on the webconsole plugin path warning.
result.add(new Object[] {
"/home/users/geometrixx-outdoors/emily.andrews@mailinator.com/profile.form.html/content/geometrixx-outdoors/en/user/profile",
"/home/users/geometrixx-outdoors/emily",
@@ -72,20 +70,11 @@ public class DecomposedURLTest {
"/profile.form.html/content/geometrixx-outdoors/en/user/profile"
});
- // the trick is to replace dots with _, which gives the same results as
- // if a resource exists at ...@mailinator.com/profile
- result.add(new Object[] {
- "/home/users/geometrixx-outdoors/emily_andrews@mailinator_com/profile.form.html/content/geometrixx-outdoors/en/user/profile",
- "/home/users/geometrixx-outdoors/emily_andrews@mailinator_com/profile",
- "html",
- "form",
- "/content/geometrixx-outdoors/en/user/profile"
- });
return result;
}
public DecomposedURLTest(String input, String path, String extension, String selectors, String suffix) {
- rpi = WebConsolePlugin.getRequestPathInfo(input);
+ rpi = WebConsolePlugin.getRequestPathInfo(input, null);
this.path = path;
this.extension = extension;
this.selectors = selectors;
diff --git a/src/test/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePluginTest.java b/src/test/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePluginTest.java
index cc77fda..b5b0d2f 100644
--- a/src/test/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePluginTest.java
+++ b/src/test/java/org/apache/sling/servlets/resolver/internal/console/WebConsolePluginTest.java
@@ -245,10 +245,7 @@ public class WebConsolePluginTest {
"<td class='content' colspan='2'><dl>\n" +
"<dt>Path</dt>\n" +
"<dd>\n" +
- "/test<br/><em>Note that in a real Sling request, the path might vary depending on the existence of " +
- "resources that partially match it. <br/>This utility does not take this into account and uses the " +
- "first dot to split between path and selectors/extension. <br/>As a workaround, you can replace dots " +
- "with underline characters, for example, when testing such a URL.</em></dd><dt>Selectors</dt>\n" +
+ "/test</dd><dt>Selectors</dt>\n" +
"<dd>\n" +
"[1]</dd><dt>Extension</dt>\n" +
"<dd>\n" +