FELIX-1860: Applied patch from Justin Edelson
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@881057 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java
index ab00a18..f334771 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java
@@ -19,6 +19,8 @@
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletRequest;
+import org.osgi.service.http.HttpContext;
+
final class ServletHandlerRequest
extends HttpServletRequestWrapper
{
@@ -31,6 +33,17 @@
super(req);
this.alias = alias;
}
+
+ @Override
+ public String getAuthType()
+ {
+ String authType = (String) getAttribute(HttpContext.AUTHENTICATION_TYPE);
+ if (authType != null) {
+ return authType;
+ }
+
+ return super.getAuthType();
+ }
@Override
public String getPathInfo()
@@ -49,6 +62,17 @@
final String info = getPathInfo();
return (null == info) ? null : getRealPath(info);
}
+
+ @Override
+ public String getRemoteUser()
+ {
+ String remoteUser = (String) getAttribute(HttpContext.REMOTE_USER);
+ if (remoteUser != null) {
+ return remoteUser;
+ }
+
+ return super.getRemoteUser();
+ }
@Override
public String getServletPath()
diff --git a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java
index 151bf00..c991cf0 100644
--- a/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java
+++ b/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java
@@ -17,6 +17,7 @@
package org.apache.felix.http.base.internal.handler;
import org.mockito.Mockito;
+import org.osgi.service.http.HttpContext;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Before;
@@ -24,21 +25,30 @@
public class ServletHandlerRequestTest
{
+ private HttpServletRequest superReq1;
+ private HttpServletRequest superReq2;
+
private HttpServletRequest req1;
private HttpServletRequest req2;
@Before
public void setUp()
{
- HttpServletRequest superReq = Mockito.mock(HttpServletRequest.class);
- Mockito.when(superReq.getContextPath()).thenReturn("/mycontext");
- Mockito.when(superReq.getRequestURI()).thenReturn("/mycontext/request/to/resource");
- this.req1 = new ServletHandlerRequest(superReq, "/");
+ this.superReq1 = Mockito.mock(HttpServletRequest.class);
+ Mockito.when(this.superReq1.getContextPath()).thenReturn("/mycontext");
+ Mockito.when(this.superReq1.getRequestURI()).thenReturn("/mycontext/request/to/resource");
+ Mockito.when(this.superReq1.getAttribute(HttpContext.AUTHENTICATION_TYPE)).thenReturn(HttpServletRequest.BASIC_AUTH);
+ Mockito.when(this.superReq1.getAttribute(HttpContext.REMOTE_USER)).thenReturn("felix");
+ this.req1 = new ServletHandlerRequest(this.superReq1, "/");
- superReq = Mockito.mock(HttpServletRequest.class);
- Mockito.when(superReq.getContextPath()).thenReturn("/mycontext");
- Mockito.when(superReq.getRequestURI()).thenReturn("/mycontext/myservlet/request/to/resource;jsession=123");
- this.req2 = new ServletHandlerRequest(superReq, "/myservlet");
+ this.superReq2 = Mockito.mock(HttpServletRequest.class);
+ Mockito.when(this.superReq2.getContextPath()).thenReturn("/mycontext");
+ Mockito.when(this.superReq2.getRequestURI()).thenReturn("/mycontext/myservlet/request/to/resource;jsession=123");
+ Mockito.when(this.superReq2.getAttribute(HttpContext.AUTHENTICATION_TYPE)).thenReturn(null);
+ Mockito.when(this.superReq2.getAuthType()).thenReturn(HttpServletRequest.DIGEST_AUTH);
+ Mockito.when(this.superReq2.getAttribute(HttpContext.REMOTE_USER)).thenReturn(null);
+ Mockito.when(this.superReq2.getRemoteUser()).thenReturn("sling");
+ this.req2 = new ServletHandlerRequest(this.superReq2, "/myservlet");
}
@Test
@@ -54,4 +64,30 @@
Assert.assertEquals("", this.req1.getServletPath());
Assert.assertEquals("/myservlet", this.req2.getServletPath());
}
+
+ @Test
+ public void testGetAuthType()
+ {
+ Assert.assertEquals(HttpServletRequest.BASIC_AUTH, this.req1.getAuthType());
+ Mockito.verify(this.superReq1).getAttribute(HttpContext.AUTHENTICATION_TYPE);
+ Mockito.verifyNoMoreInteractions(this.superReq1);
+
+ Assert.assertEquals(HttpServletRequest.DIGEST_AUTH, this.req2.getAuthType());
+ Mockito.verify(this.superReq2).getAttribute(HttpContext.AUTHENTICATION_TYPE);
+ Mockito.verify(this.superReq2).getAuthType();
+ Mockito.verifyNoMoreInteractions(this.superReq2);
+ }
+
+ @Test
+ public void testGetRemoteUser()
+ {
+ Assert.assertEquals("felix", this.req1.getRemoteUser());
+ Mockito.verify(this.superReq1).getAttribute(HttpContext.REMOTE_USER);
+ Mockito.verifyNoMoreInteractions(this.superReq1);
+
+ Assert.assertEquals("sling", this.req2.getRemoteUser());
+ Mockito.verify(this.superReq2).getAttribute(HttpContext.REMOTE_USER);
+ Mockito.verify(this.superReq2).getRemoteUser();
+ Mockito.verifyNoMoreInteractions(this.superReq2);
+ }
}