You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2022/09/10 12:42:45 UTC

[sling-org-apache-sling-engine] branch master updated: SLING-11578 : Make authenticator and servlets resovler required for main servlet

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-engine.git


The following commit(s) were added to refs/heads/master by this push:
     new e196b05  SLING-11578 : Make authenticator and servlets resovler required for main servlet
e196b05 is described below

commit e196b05ee2dbef7b5ba1ca96a6adccfbc0fb4861
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sat Sep 10 14:42:39 2022 +0200

    SLING-11578 : Make authenticator and servlets resovler required for main servlet
---
 .../apache/sling/engine/impl/SlingHttpContext.java | 75 +++++++---------------
 .../engine/impl/SlingHttpServletRequestImpl.java   |  2 +-
 .../apache/sling/engine/impl/SlingMainServlet.java | 49 --------------
 .../sling/engine/impl/request/RequestData.java     |  4 --
 4 files changed, 25 insertions(+), 105 deletions(-)

diff --git a/src/main/java/org/apache/sling/engine/impl/SlingHttpContext.java b/src/main/java/org/apache/sling/engine/impl/SlingHttpContext.java
index 25f4b99..ce26fef 100644
--- a/src/main/java/org/apache/sling/engine/impl/SlingHttpContext.java
+++ b/src/main/java/org/apache/sling/engine/impl/SlingHttpContext.java
@@ -30,8 +30,14 @@ import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.auth.core.AuthenticationSupport;
 import org.apache.sling.commons.mime.MimeTypeService;
 import org.apache.sling.engine.impl.parameters.ParameterSupport;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
 import org.osgi.service.http.context.ServletContextHelper;
 import org.osgi.service.http.whiteboard.annotations.RequireHttpWhiteboard;
+import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -40,7 +46,9 @@ import org.slf4j.LoggerFactory;
  * register the {@link SlingMainServlet} with the OSGi HttpService.
  */
 @RequireHttpWhiteboard
-class SlingHttpContext extends ServletContextHelper {
+@Component(service = ServletContextHelper.class)
+@HttpWhiteboardContext(name = SlingMainServlet.SERVLET_CONTEXT_NAME, path = "/")
+public class SlingHttpContext extends ServletContextHelper {
 
     /** Logger */
     private final Logger log = LoggerFactory.getLogger(SlingHttpContext.class);
@@ -50,45 +58,27 @@ class SlingHttpContext extends ServletContextHelper {
      *
      * @see #getMimeType(String)
      */
-    private MimeTypeService mimeTypeService;
+    @Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.OPTIONAL)
+    private volatile MimeTypeService mimeTypeService;
 
     /**
      * Handles security
      *
      * @see #handleSecurity(HttpServletRequest, HttpServletResponse)
      */
-    private AuthenticationSupport authenticationSupport;
+    private final AuthenticationSupport authenticationSupport;
 
-    public void setMimeTypeService(MimeTypeService mimeTypeService) {
-        this.mimeTypeService = mimeTypeService;
+    @Activate
+    public SlingHttpContext(@Reference final AuthenticationSupport support) {
+        this.authenticationSupport = support;
     }
 
-    public void unsetMimeTypeService(MimeTypeService mimeTypeService) {
-        if (this.mimeTypeService == mimeTypeService) {
-            this.mimeTypeService = null;
-        }
-    }
-
-    public void setAuthenticationSupport(
-            AuthenticationSupport authenticationSupport) {
-        this.authenticationSupport = authenticationSupport;
-    }
-
-    public void unsetAuthenticationSupport(
-            AuthenticationSupport authenticationSupport) {
-        if (this.authenticationSupport == authenticationSupport) {
-            this.authenticationSupport = null;
-        }
-    }
-
-    // ---------- HttpContext interface ----------------------------------------
-
     /**
      * Returns the MIME type as resolved by the <code>MimeTypeService</code> or
      * <code>null</code> if the service is not available.
      */
     @Override
-    public String getMimeType(String name) {
+    public String getMimeType(final String name) {
         MimeTypeService mtservice = mimeTypeService;
         if (mtservice != null) {
             return mtservice.getMimeType(name);
@@ -105,15 +95,13 @@ class SlingHttpContext extends ServletContextHelper {
      * through the {@link SlingMainServlet}.
      */
     @Override
-    public URL getResource(String name) {
+    public URL getResource(final String name) {
         return null;
     }
 
     /**
      * Tries to authenticate the request using the
-     * <code>SlingAuthenticator</code>. If the authenticator or the Repository
-     * is missing this method returns <code>false</code> and sends a 503/SERVICE
-     * UNAVAILABLE status back to the client.
+     * <code>SlingAuthenticator</code>.
      */
     @Override
     public boolean handleSecurity(HttpServletRequest request,
@@ -125,28 +113,13 @@ class SlingHttpContext extends ServletContextHelper {
         final String timerName = "handleSecurity";
         t.startTimer(timerName);
 
-        final AuthenticationSupport authenticator = this.authenticationSupport;
-        if (authenticator != null) {
-
-            // SLING-559: ensure correct parameter handling according to
-            // ParameterSupport
-            request = ParameterSupport.getParameterSupportRequestWrapper(request);
-
-            final boolean result = authenticator.handleSecurity(request, response);
-            t.logTimer(timerName, "authenticator {0} returns {1}", authenticator, result);
-            return result;
-        }
-
-        log.error("handleSecurity: AuthenticationSupport service missing. Cannot authenticate request.");
-        log.error("handleSecurity: Possible reason is missing Repository service. Check AuthenticationSupport dependencies.");
-
-        // send 503/SERVICE UNAVAILABLE, flush to ensure delivery
-        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
-            "AuthenticationSupport service missing. Cannot authenticate request.");
-        response.flushBuffer();
+        // SLING-559: ensure correct parameter handling according to
+        // ParameterSupport
+        request = ParameterSupport.getParameterSupportRequestWrapper(request);
 
-        // terminate this request now
-        return false;
+        final boolean result = this.authenticationSupport.handleSecurity(request, response);
+        t.logTimer(timerName, "authenticator {0} returns {1}", this.authenticationSupport, result);
+        return result;
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java b/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java
index 8e91f06..c8cf611 100644
--- a/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java
+++ b/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java
@@ -263,7 +263,7 @@ public class SlingHttpServletRequestImpl extends HttpServletRequestWrapper imple
     public String getResponseContentType() {
         if(responseContentType == null) {
             final String ext = getRequestPathInfo().getExtension();
-            responseContentType = requestData.getMimeType("dummy." + ext);
+            responseContentType = this.getServletContext().getMimeType("dummy." + ext);
         }
         return responseContentType;
     }
diff --git a/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java b/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
index 0ea5aa6..9bbb56c 100644
--- a/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
+++ b/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
@@ -38,7 +38,6 @@ import org.apache.sling.api.request.SlingRequestEvent;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.servlets.ServletResolver;
 import org.apache.sling.auth.core.AuthenticationSupport;
-import org.apache.sling.commons.mime.MimeTypeService;
 import org.apache.sling.engine.SlingRequestProcessor;
 import org.apache.sling.engine.impl.debug.RequestInfoProviderImpl;
 import org.apache.sling.engine.impl.filter.ServletFilterManager;
@@ -62,7 +61,6 @@ import org.osgi.service.component.annotations.ReferencePolicy;
 import org.osgi.service.component.annotations.ReferencePolicyOption;
 import org.osgi.service.component.propertytypes.ServiceDescription;
 import org.osgi.service.component.propertytypes.ServiceVendor;
-import org.osgi.service.http.context.ServletContextHelper;
 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
@@ -189,8 +187,6 @@ public class SlingMainServlet extends GenericServlet {
 
     // new properties
 
-    private final SlingHttpContext slingHttpContext = new SlingHttpContext();
-
     private volatile ServletFilterManager filterManager;
 
     private final SlingRequestProcessorImpl requestProcessor = new SlingRequestProcessorImpl();
@@ -199,8 +195,6 @@ public class SlingMainServlet extends GenericServlet {
 
     private volatile ServiceRegistration<RequestProcessorMBean> requestProcessorMBeanRegistration;
 
-    private volatile ServiceRegistration<ServletContextHelper> contextRegistration;
-
     private volatile ServiceRegistration<Servlet> servletRegistration;
 
     private volatile String configuredServerInfo;
@@ -426,18 +420,6 @@ public class SlingMainServlet extends GenericServlet {
         RequestData.setMaxCallCounter(config.sling_max_calls());
         RequestData.setSlingMainServlet(this);
 
-        if (this.contextRegistration == null) {
-            // register the servlet context
-            final Dictionary<String, String> contextProperties = new Hashtable<>();
-            contextProperties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, SERVLET_CONTEXT_NAME);
-            contextProperties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, SLING_ROOT);
-            contextProperties.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Engine Servlet Context Helper");
-            contextProperties.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
-
-            this.contextRegistration = bundleContext.registerService(ServletContextHelper.class, this.slingHttpContext,
-                    contextProperties);
-        }
-
         String servletName = config.servlet_name();
         if (servletName == null || servletName.isEmpty()) {
             servletName = this.productInfo;
@@ -526,13 +508,6 @@ public class SlingMainServlet extends GenericServlet {
         // first unregister servlet context
         unregisterSlingServletContext();
 
-        // second unregister the servlet context *before* unregistering
-        // and destroying the the sling main servlet
-        if (this.contextRegistration != null) {
-            this.contextRegistration.unregister();
-            this.contextRegistration = null;
-        }
-
         // third unregister and destroy the sling main servlet
         // unregister servlet
         if ( this.servletRegistration != null ) {
@@ -592,32 +567,8 @@ public class SlingMainServlet extends GenericServlet {
         requestProcessor.unsetServletResolver(servletResolver);
     }
 
-    @Reference(name = "MimeTypeService", cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, unbind = "unsetMimeTypeService")
-    public void setMimeTypeService(final MimeTypeService mimeTypeService) {
-        slingHttpContext.setMimeTypeService(mimeTypeService);
-    }
-
-    public void unsetMimeTypeService(final MimeTypeService mimeTypeService) {
-        slingHttpContext.unsetMimeTypeService(mimeTypeService);
-    }
-
-    @Reference(name = "AuthenticationSupport", cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, unbind = "unsetAuthenticationSupport")
-    public void setAuthenticationSupport(
-            final AuthenticationSupport authenticationSupport) {
-        slingHttpContext.setAuthenticationSupport(authenticationSupport);
-    }
-
-    public void unsetAuthenticationSupport(
-            final AuthenticationSupport authenticationSupport) {
-        slingHttpContext.unsetAuthenticationSupport(authenticationSupport);
-    }
-
     // ---------- HttpContext interface ----------------------------------------
 
-    public String getMimeType(String name) {
-        return slingHttpContext.getMimeType(name);
-    }
-
     public <Type> Type adaptTo(Object object, Class<Type> type) {
         AdapterManager adapterManager = this.adapterManager;
         if (adapterManager != null) {
diff --git a/src/main/java/org/apache/sling/engine/impl/request/RequestData.java b/src/main/java/org/apache/sling/engine/impl/request/RequestData.java
index fe1e3cd..ee2a3d5 100644
--- a/src/main/java/org/apache/sling/engine/impl/request/RequestData.java
+++ b/src/main/java/org/apache/sling/engine/impl/request/RequestData.java
@@ -708,10 +708,6 @@ public class RequestData {
         return SLING_MAIN_SERVLET.adaptTo(object, type);
     }
 
-    public String getMimeType(String fileName) {
-        return SLING_MAIN_SERVLET.getMimeType(fileName);
-    }
-
     // ---------- Parameter support -------------------------------------------
 
     public ServletInputStream getInputStream() throws IOException {