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

cziegeler pushed a commit to branch SLING-9622
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-auth-core.git


The following commit(s) were added to refs/heads/SLING-9622 by this push:
     new 39eec6d  SLING-9622 : Potential solution using a service resource 
resolver to resolve the path
39eec6d is described below

commit 39eec6dfbada7fa851b897bf6a9c7c3311fb35b3
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Tue Aug 4 11:24:21 2020 +0200

    SLING-9622 : Potential solution using a service resource resolver to 
resolve the path
---
 .../sling/auth/core/impl/SlingAuthenticator.java   | 225 ++++++++++++---------
 .../auth/core/impl/SlingAuthenticatorTest.java     | 107 ++++------
 2 files changed, 171 insertions(+), 161 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java 
b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
index 0ee22b3..0e304a6 100644
--- a/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
+++ b/src/main/java/org/apache/sling/auth/core/impl/SlingAuthenticator.java
@@ -47,8 +47,10 @@ import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.auth.Authenticator;
 import org.apache.sling.api.auth.NoAuthenticationHandlerException;
 import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.auth.core.AuthConstants;
 import org.apache.sling.auth.core.AuthUtil;
 import org.apache.sling.auth.core.AuthenticationSupport;
@@ -256,6 +258,11 @@ public class SlingAuthenticator implements Authenticator,
      */
     private static final String AUTH_INFO_PROP_FEEDBACK_HANDLER = 
"$$sling.auth.AuthenticationFeedbackHandler$$";
 
+    /**
+     * Request attribute holding the resolved path
+     */
+    private static final String ATTR_RESOLVED_PATH = 
SlingAuthenticator.class.getName().concat("/resolvedPath");
+
     @Reference
     private ResourceResolverFactory resourceResolverFactory;
 
@@ -332,12 +339,15 @@ public class SlingAuthenticator implements Authenticator,
     @Reference(policy=ReferencePolicy.DYNAMIC, cardinality = 
ReferenceCardinality.OPTIONAL)
     private volatile EventAdmin eventAdmin;
 
+    private ResourceResolver serviceResolver;
+
     // ---------- SCR integration
 
     @Activate
     private void activate(final BundleContext bundleContext,
-            final Config config) {
+            final Config config) throws LoginException {
         modified(config);
+        this.serviceResolver = 
resourceResolverFactory.getServiceResourceResolver(null);
 
         AuthenticatorWebConsolePlugin plugin = new 
AuthenticatorWebConsolePlugin(
             this);
@@ -456,6 +466,10 @@ public class SlingAuthenticator implements Authenticator,
             webConsolePlugin.unregister();
             webConsolePlugin = null;
         }
+        if ( this.serviceResolver != null ) {
+            this.serviceResolver.close();
+            this.serviceResolver = null;
+        }
     }
 
     // --------- AuthenticationSupport interface
@@ -479,29 +493,33 @@ public class SlingAuthenticator implements Authenticator,
     public boolean handleSecurity(HttpServletRequest request,
             HttpServletResponse response) {
 
-        // 0. Nothing to do, if the session is also in the request
-        // this might be the case if the request is handled as a result
-        // of a servlet container include inside another Sling request
-        Object sessionAttr = request.getAttribute(REQUEST_ATTRIBUTE_RESOLVER);
-        if (sessionAttr instanceof ResourceResolver) {
-            log.debug("handleSecurity: Request already authenticated, nothing 
to do");
-            return true;
-        } else if (sessionAttr != null) {
-            // warn and remove existing non-session
-            log.warn("handleSecurity: Overwriting existing ResourceResolver 
attribute ({})", sessionAttr);
-            request.removeAttribute(REQUEST_ATTRIBUTE_RESOLVER);
-        }
+        try {
+            // 0. Nothing to do, if the session is also in the request
+            // this might be the case if the request is handled as a result
+            // of a servlet container include inside another Sling request
+            Object sessionAttr = 
request.getAttribute(REQUEST_ATTRIBUTE_RESOLVER);
+            if (sessionAttr instanceof ResourceResolver) {
+                log.debug("handleSecurity: Request already authenticated, 
nothing to do");
+                return true;
+            } else if (sessionAttr != null) {
+                // warn and remove existing non-session
+                log.warn("handleSecurity: Overwriting existing 
ResourceResolver attribute ({})", sessionAttr);
+                request.removeAttribute(REQUEST_ATTRIBUTE_RESOLVER);
+            }
 
-        boolean process = doHandleSecurity(request, response);
-        if (process && expectAuthenticationHandler(request)) {
-            log.warn("handleSecurity: AuthenticationHandler did not block 
request; access denied");
-            request.removeAttribute(AuthenticationHandler.FAILURE_REASON);
-            request.removeAttribute(AuthenticationHandler.FAILURE_REASON_CODE);
-            AuthUtil.sendInvalid(request, response);
-            return false;
-        }
+            boolean process = doHandleSecurity(request, response);
+            if (process && expectAuthenticationHandler(request)) {
+                log.warn("handleSecurity: AuthenticationHandler did not block 
request; access denied");
+                request.removeAttribute(AuthenticationHandler.FAILURE_REASON);
+                
request.removeAttribute(AuthenticationHandler.FAILURE_REASON_CODE);
+                AuthUtil.sendInvalid(request, response);
+                return false;
+            }
 
-        return process;
+            return process;
+        } finally {
+            request.removeAttribute(ATTR_RESOLVED_PATH);
+        }
     }
 
     private boolean doHandleSecurity(HttpServletRequest request, 
HttpServletResponse response) {
@@ -579,50 +597,54 @@ public class SlingAuthenticator implements Authenticator,
             throw new IllegalStateException("Response already committed");
         }
 
-        // select path used for authentication handler selection
-        final Collection<AbstractAuthenticationHandlerHolder>[] holdersArray = 
this.authHandlerCache
-                .findApplicableHolders(request);
-        final String path = getHandlerSelectionPath(request);
-        boolean done = false;
-        for (int m = 0; !done && m < holdersArray.length; m++) {
-            final Collection<AbstractAuthenticationHandlerHolder> holderList = 
holdersArray[m];
-            if ( holderList != null ) {
-                for (AbstractAuthenticationHandlerHolder holder : holderList) {
-                    if (isNodeRequiresAuthHandler(path, holder.path)) {
-                        log.debug("login: requesting authentication using 
handler: {}",
-                            holder);
-
-                        try {
-                            done = holder.requestCredentials(request, 
response);
-                        } catch (IOException ioe) {
-                            log.error(
-                                "login: Failed sending authentication request 
through handler "
-                                    + holder + ", access forbidden", ioe);
-                            done = true;
-                        }
-                        if (done) {
-                            break;
+        try {
+            // select path used for authentication handler selection
+            final Collection<AbstractAuthenticationHandlerHolder>[] 
holdersArray = this.authHandlerCache
+                    .findApplicableHolders(request);
+            final String path = getHandlerSelectionPath(request);
+            boolean done = false;
+            for (int m = 0; !done && m < holdersArray.length; m++) {
+                final Collection<AbstractAuthenticationHandlerHolder> 
holderList = holdersArray[m];
+                if ( holderList != null ) {
+                    for (AbstractAuthenticationHandlerHolder holder : 
holderList) {
+                        if (isNodeRequiresAuthHandler(path, holder.path)) {
+                            log.debug("login: requesting authentication using 
handler: {}",
+                                holder);
+
+                            try {
+                                done = holder.requestCredentials(request, 
response);
+                            } catch (IOException ioe) {
+                                log.error(
+                                    "login: Failed sending authentication 
request through handler "
+                                        + holder + ", access forbidden", ioe);
+                                done = true;
+                            }
+                            if (done) {
+                                break;
+                            }
                         }
                     }
                 }
             }
-        }
 
-        // fall back to HTTP Basic handler (if not done already)
-        if (!done && httpBasicHandler != null) {
-            done = httpBasicHandler.requestCredentials(request, response);
-        }
+            // fall back to HTTP Basic handler (if not done already)
+            if (!done && httpBasicHandler != null) {
+                done = httpBasicHandler.requestCredentials(request, response);
+            }
 
-        // no handler could send an authentication request, throw
-        if (!done) {
-            int size = 0;
-            for (int m = 0; m < holdersArray.length; m++) {
-                if (holdersArray[m] != null) {
-                    size += holdersArray[m].size();
+            // no handler could send an authentication request, throw
+            if (!done) {
+                int size = 0;
+                for (int m = 0; m < holdersArray.length; m++) {
+                    if (holdersArray[m] != null) {
+                        size += holdersArray[m].size();
+                    }
                 }
+                log.info("login: No handler for request ({} handlers 
available)", size);
+                throw new NoAuthenticationHandlerException();
             }
-            log.info("login: No handler for request ({} handlers available)", 
size);
-            throw new NoAuthenticationHandlerException();
+        } finally {
+            request.removeAttribute(ATTR_RESOLVED_PATH);
         }
     }
 
@@ -642,27 +664,31 @@ public class SlingAuthenticator implements Authenticator,
         // make sure impersonation is dropped
         setSudoCookie(request, response, new AuthenticationInfo("dummy", 
request.getRemoteUser()));
 
-        final String path = getHandlerSelectionPath(request);
-        final Collection<AbstractAuthenticationHandlerHolder>[] holdersArray = 
this.authHandlerCache
-                .findApplicableHolders(request);
-        for (int m = 0; m < holdersArray.length; m++) {
-            final Collection<AbstractAuthenticationHandlerHolder> holderSet = 
holdersArray[m];
-            if (holderSet != null) {
-                for (AbstractAuthenticationHandlerHolder holder : holderSet) {
-                    if (isNodeRequiresAuthHandler(path, holder.path)) {
-                        log.debug("logout: dropping authentication using 
handler: {}",
-                            holder);
-
-                        try {
-                            holder.dropCredentials(request, response);
-                        } catch (IOException ioe) {
-                            log.error(
-                                "logout: Failed dropping authentication 
through handler "
-                                    + holder, ioe);
+        try {
+            final String path = getHandlerSelectionPath(request);
+            final Collection<AbstractAuthenticationHandlerHolder>[] 
holdersArray = this.authHandlerCache
+                    .findApplicableHolders(request);
+            for (int m = 0; m < holdersArray.length; m++) {
+                final Collection<AbstractAuthenticationHandlerHolder> 
holderSet = holdersArray[m];
+                if (holderSet != null) {
+                    for (AbstractAuthenticationHandlerHolder holder : 
holderSet) {
+                        if (isNodeRequiresAuthHandler(path, holder.path)) {
+                            log.debug("logout: dropping authentication using 
handler: {}",
+                                holder);
+
+                            try {
+                                holder.dropCredentials(request, response);
+                            } catch (IOException ioe) {
+                                log.error(
+                                    "logout: Failed dropping authentication 
through handler "
+                                        + holder, ioe);
+                            }
                         }
                     }
                 }
             }
+        } finally {
+            request.removeAttribute(ATTR_RESOLVED_PATH);
         }
 
         if (httpBasicHandler != null) {
@@ -742,25 +768,37 @@ public class SlingAuthenticator implements Authenticator,
     // ---------- internal
 
     private String getPath(HttpServletRequest request) {
-        final StringBuilder sb = new StringBuilder();
-        if (request.getServletPath() != null) {
-            sb.append(request.getServletPath());
-        }
-        if (request.getPathInfo() != null) {
-            sb.append(request.getPathInfo());
+        String path = (String)request.getAttribute(ATTR_RESOLVED_PATH);
+        if ( path == null ) {
+            final StringBuilder sb = new StringBuilder();
+            if (request.getServletPath() != null) {
+                sb.append(request.getServletPath());
+            }
+            if (request.getPathInfo() != null) {
+                sb.append(request.getPathInfo());
+            }
+            path = sb.toString();
+            // Get the path used to select the authenticator, if the 
SlingServlet
+            // itself has been requested without any more info, this will be 
empty
+            // and we assume the root (SLING-722)
+            if (path.length() == 0) {
+                path = "/";
+            }
+
+            if ( !path.equals("/") && serviceResolver != null ) { // NULL 
check to let unit tests pass
+                final Resource mappedResource = serviceResolver.resolve(path);
+                if ( !ResourceUtil.isNonExistingResource(mappedResource) ) {
+                    path = mappedResource.getPath();
+                }
+            }
+            request.setAttribute(ATTR_RESOLVED_PATH, path);
         }
-        return sb.toString();
+        return path;
     }
 
     private AuthenticationInfo getAuthenticationInfo(HttpServletRequest 
request, HttpServletResponse response) {
 
-        // Get the path used to select the authenticator, if the SlingServlet
-        // itself has been requested without any more info, this will be empty
-        // and we assume the root (SLING-722)
         String path = getPath(request);
-        if (path.length() == 0) {
-            path = "/";
-        }
 
         final Collection<AbstractAuthenticationHandlerHolder>[] localArray = 
this.authHandlerCache
                 .findApplicableHolders(request);
@@ -953,9 +991,6 @@ public class SlingAuthenticator implements Authenticator,
     private boolean isAnonAllowed(HttpServletRequest request) {
 
         String path = getPath(request);
-        if (path.length() == 0) {
-            path = "/";
-        }
 
         final Collection<AuthenticationRequirementHolder>[] holderSetArray = 
authRequiredCache
                 .findApplicableHolders(request);
@@ -1502,14 +1537,14 @@ public class SlingAuthenticator implements 
Authenticator,
             if (ctxPath !=  null && path.startsWith(ctxPath)) {
                 path = path.substring(ctxPath.length());
             }
+            if (path == null || path.length() == 0) {
+                path = "/";
+            }
+
         } else {
             path = getPath(request);
         }
 
-        if (path == null || path.length() == 0) {
-            path = "/";
-        }
-
         return path;
     }
 
diff --git 
a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java 
b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
index 7b2e16a..d6b5c3c 100644
--- a/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
+++ b/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
@@ -20,21 +20,20 @@ package org.apache.sling.auth.core.impl;
 
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
 import org.apache.sling.auth.core.spi.AuthenticationFeedbackHandler;
 import org.apache.sling.auth.core.spi.AuthenticationInfo;
-import org.jmock.Expectations;
-import org.jmock.Mockery;
-import org.jmock.integration.junit4.JUnit4Mockery;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
+
 import junitx.util.PrivateAccessor;
 
 public class SlingAuthenticatorTest {
 
-    private final Mockery context = new JUnit4Mockery();
-
     @Test
     public void test_quoteCookieValue() throws UnsupportedEncodingException {
 
@@ -90,21 +89,10 @@ public class SlingAuthenticatorTest {
         authRequiredCache.addHolder(new AuthenticationRequirementHolder("/", 
false, null));
 
         PrivateAccessor.setField(slingAuthenticator, "authRequiredCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
-        context.checking(new Expectations() {
-            {
-                allowing(request).getServletPath();
-                will(returnValue(null));
-                allowing(request).getPathInfo();
-                will(returnValue(null));
-                allowing(request).getServerName();
-                will(returnValue("localhost"));
-                allowing(request).getServerPort();
-                will(returnValue(80));
-                allowing(request).getScheme();
-                will(returnValue("http"));
-            }
-        });
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
+        Mockito.when(request.getServerName()).thenReturn("localhost");
+        Mockito.when(request.getServerPort()).thenReturn(80);
+        Mockito.when(request.getScheme()).thenReturn("http");
 
         Boolean allowed = (Boolean) 
PrivateAccessor.invoke(slingAuthenticator,"isAnonAllowed",  new 
Class[]{HttpServletRequest.class},new Object[]{request});
         Assert.assertTrue(allowed);
@@ -127,17 +115,17 @@ public class SlingAuthenticatorTest {
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, 
PROTECTED_PATH));
 
         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
         buildExpectationsForRequestPathAndAuthPath(request, 
REQUEST_CHILD_NODE, PROTECTED_PATH);
 
         AuthenticationInfo authInfo = (AuthenticationInfo) 
PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
-                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
context.mock(HttpServletResponse.class)});
+                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
Mockito.mock(HttpServletResponse.class)});
         /**
          * The AUTH TYPE defined aboved should  be used for the path /test and 
his children: eg /test/childnode.
          */
         Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
-    
+
     /**
      * Test is OK for same node;
      * @throws Throwable
@@ -154,17 +142,17 @@ public class SlingAuthenticatorTest {
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, 
PROTECTED_PATH));
 
         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
         buildExpectationsForRequestPathAndAuthPath(request, 
REQUEST_CHILD_NODE, PROTECTED_PATH);
 
         AuthenticationInfo authInfo = (AuthenticationInfo) 
PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
-                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
context.mock(HttpServletResponse.class)});
+                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
Mockito.mock(HttpServletResponse.class)});
         /**
          * The AUTH TYPE defined aboved should  be used for the path /test and 
his children: eg /test/childnode.
          */
         Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
-    
+
     /**
      * Test is OK for same node with ending slash;
      * @throws Throwable
@@ -181,17 +169,17 @@ public class SlingAuthenticatorTest {
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, 
PROTECTED_PATH));
 
         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
         buildExpectationsForRequestPathAndAuthPath(request, 
REQUEST_CHILD_NODE, PROTECTED_PATH);
 
         AuthenticationInfo authInfo = (AuthenticationInfo) 
PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
-                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
context.mock(HttpServletResponse.class)});
+                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
Mockito.mock(HttpServletResponse.class)});
         /**
          * The AUTH TYPE defined aboved should  be used for the path /test and 
his children: eg /test/childnode.
          */
         Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
-    
+
     /**
      * Test is OK for same node with extension
      * @throws Throwable
@@ -208,11 +196,11 @@ public class SlingAuthenticatorTest {
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, 
PROTECTED_PATH));
 
         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
         buildExpectationsForRequestPathAndAuthPath(request, 
REQUEST_CHILD_NODE, PROTECTED_PATH);
 
         AuthenticationInfo authInfo = (AuthenticationInfo) 
PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
-                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
context.mock(HttpServletResponse.class)});
+                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
Mockito.mock(HttpServletResponse.class)});
         /**
          * The AUTH TYPE defined aboved should  be used for the path /test and 
his children: eg /test/childnode.
          */
@@ -231,17 +219,17 @@ public class SlingAuthenticatorTest {
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, 
PROTECTED_PATH));
 
         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
         buildExpectationsForRequestPathAndAuthPath(request, 
REQUEST_CHILD_NODE, PROTECTED_PATH);
 
         AuthenticationInfo authInfo = (AuthenticationInfo) 
PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
-                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
context.mock(HttpServletResponse.class)});
+                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
Mockito.mock(HttpServletResponse.class)});
         /**
          * The AUTH TYPE defined aboved should  be used for the path /test and 
his children: eg /test/childnode.
          */
         Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
-    
+
     @Test
     public void test_childNodeShouldHaveAuthenticationInfoLonger() throws 
Throwable {
         final String AUTH_TYPE = "AUTH_TYPE_TEST";
@@ -255,19 +243,19 @@ public class SlingAuthenticatorTest {
         PathBasedHolderCache<AbstractAuthenticationHandlerHolder> 
authRequiredCache = new 
PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, 
PROTECTED_PATH));
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE_LONGER, 
PROTECTED_PATH_LONGER));
-        
+
         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
         buildExpectationsForRequestPathAndAuthPath(request, 
REQUEST_CHILD_NODE, PROTECTED_PATH);
 
         AuthenticationInfo authInfo = (AuthenticationInfo) 
PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
-                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
context.mock(HttpServletResponse.class)});
+                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
Mockito.mock(HttpServletResponse.class)});
         /**
          * The AUTH TYPE defined aboved should  be used for the path /test and 
his children: eg /test/childnode.
          */
         Assert.assertTrue(AUTH_TYPE_LONGER.equals(authInfo.getAuthType()));
     }
-    
+
 
     /**
      * JIRA: SLING-6053
@@ -297,32 +285,32 @@ public class SlingAuthenticatorTest {
         
authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, 
PROTECTED_PATH));
 
         PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", 
authRequiredCache);
-        final HttpServletRequest request = 
context.mock(HttpServletRequest.class);
+        final HttpServletRequest request = 
Mockito.mock(HttpServletRequest.class);
         buildExpectationsForRequestPathAndAuthPath(request, 
REQUEST_NOT_PROTECTED_PATH, PROTECTED_PATH);
 
         AuthenticationInfo authInfo = (AuthenticationInfo) 
PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
-                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
context.mock(HttpServletResponse.class)});
+                new Class[]{HttpServletRequest.class, 
HttpServletResponse.class}, new Object[]{request, 
Mockito.mock(HttpServletResponse.class)});
         /**
          * The AUTH TYPE defined aboved should not be used for the path /test2.
          */
         Assert.assertFalse(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
-    
+
     @Test
     public void test_childNodeAuthenticationHandlerPath() throws Throwable {
         final String requestPath = "/content/test/test2";
         final String handlerPath = "/content/test";
         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
-       
+
         Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, 
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new 
Object[] {requestPath, handlerPath}));
     }
-    
+
     @Test
     public void test_siblingNodeAuthenticationHandlerPath() throws Throwable {
         final String requestPath = 
"/content/test2.html/en/2016/09/19/test.html";
         final String handlerPath = "/content/test";
         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
-        
+
         Assert.assertFalse( 
(boolean)PrivateAccessor.invoke(slingAuthenticator, 
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new 
Object[] {requestPath, handlerPath}));
     }
 
@@ -331,7 +319,7 @@ public class SlingAuthenticatorTest {
         final String requestPath = "/content/test";
         final String handlerPath = "/content/test";
         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
-        
+
         Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, 
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new 
Object[] {requestPath, handlerPath}));
     }
 
@@ -340,7 +328,7 @@ public class SlingAuthenticatorTest {
         final String requestPath = "/content/test";
         final String handlerPath = "/";
         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
-        
+
         Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, 
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new 
Object[] {requestPath, handlerPath}));
     }
 
@@ -349,7 +337,7 @@ public class SlingAuthenticatorTest {
         final String requestPath = 
"/content/test.selector1.selector2.html/en/2016/test.html";
         final String handlerPath = "/content/test";
         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
-        
+
         Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, 
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new 
Object[] {requestPath, handlerPath}));
     }
 
@@ -358,7 +346,7 @@ public class SlingAuthenticatorTest {
         final String requestPath = 
"/content/test.selector1.selector2.html/en/2016/09/19/test.html";
         final String handlerPath = "/content/test2";
         SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
-        
+
         Assert.assertFalse( 
(boolean)PrivateAccessor.invoke(slingAuthenticator, 
"isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new 
Object[] {requestPath, handlerPath}));
     }
 
@@ -393,24 +381,11 @@ public class SlingAuthenticatorTest {
             final String requestPath,
             final String authProtectedPath) {
         {
-            context.checking(new Expectations() {
-                {
-                    allowing(request).getServletPath();
-                    will(returnValue(requestPath));
-                    allowing(request).getPathInfo();
-                    will(returnValue(null));
-                    allowing(request).getServerName();
-                    will(returnValue("localhost"));
-                    allowing(request).getServerPort();
-                    will(returnValue(80));
-                    allowing(request).getScheme();
-                    will(returnValue("http"));
-                    allowing(request).getAttribute("path");
-                    will(returnValue(requestPath));
-                    allowing(request).setAttribute("path", requestPath);
-                    allowing(request).setAttribute("path", authProtectedPath);
-                }
-            });
+            Mockito.when(request.getServletPath()).thenReturn(requestPath);
+            Mockito.when(request.getServerName()).thenReturn("localhost");
+            Mockito.when(request.getServerPort()).thenReturn(80);
+            Mockito.when(request.getScheme()).thenReturn("http");
+            Mockito.when(request.getAttribute("path")).thenReturn(requestPath);
         }
     }
 

Reply via email to