View Javadoc

1   package org.apache.maven.archiva.webdav;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.BufferedReader;
23  import java.io.IOException;
24  import java.io.UnsupportedEncodingException;
25  import java.security.Principal;
26  import java.util.Enumeration;
27  import java.util.Locale;
28  import java.util.Map;
29  
30  import javax.servlet.RequestDispatcher;
31  import javax.servlet.ServletInputStream;
32  import javax.servlet.http.Cookie;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.servlet.http.HttpSession;
36  
37  import junit.framework.TestCase;
38  
39  import org.apache.jackrabbit.webdav.DavSessionProvider;
40  import org.apache.jackrabbit.webdav.WebdavRequest;
41  import org.apache.jackrabbit.webdav.WebdavRequestImpl;
42  import org.apache.maven.archiva.security.ServletAuthenticator;
43  import org.codehaus.plexus.redback.authentication.AuthenticationDataSource;
44  import org.codehaus.plexus.redback.authentication.AuthenticationException;
45  import org.codehaus.plexus.redback.authentication.AuthenticationResult;
46  import org.codehaus.plexus.redback.authorization.AuthorizationException;
47  import org.codehaus.plexus.redback.authorization.UnauthorizedException;
48  import org.codehaus.plexus.redback.policy.AccountLockedException;
49  import org.codehaus.plexus.redback.policy.MustChangePasswordException;
50  import org.codehaus.plexus.redback.system.SecuritySession;
51  import org.codehaus.plexus.redback.users.User;
52  import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
53  
54  public class ArchivaDavSessionProviderTest extends TestCase
55  {
56      private DavSessionProvider sessionProvider;
57      
58      private WebdavRequest request;
59  
60      @Override
61      protected void setUp()
62          throws Exception
63      {
64          super.setUp();
65          sessionProvider = new ArchivaDavSessionProvider( new ServletAuthenticatorMock(), new HttpAuthenticatorMock() );
66          request = new WebdavRequestImpl(new HttpServletRequestMock(), null);
67      }
68      
69      public void testAttachSession()
70          throws Exception
71      {
72          assertNull(request.getDavSession());
73          sessionProvider.attachSession(request);
74          assertNotNull(request.getDavSession());
75      }
76      
77      public void testReleaseSession()
78          throws Exception
79      {
80          assertNull(request.getDavSession());
81          sessionProvider.attachSession(request);
82          assertNotNull(request.getDavSession());
83          
84          sessionProvider.releaseSession(request);
85          assertNull(request.getDavSession());
86      }
87      
88      @SuppressWarnings("unchecked")
89      private class HttpServletRequestMock implements HttpServletRequest
90      {
91          public Object getAttribute(String arg0) {
92              throw new UnsupportedOperationException("Not supported yet.");
93          }
94  
95          public Enumeration getAttributeNames() {
96              throw new UnsupportedOperationException("Not supported yet.");
97          }
98  
99          public String getCharacterEncoding() {
100             throw new UnsupportedOperationException("Not supported yet.");
101         }
102 
103         public int getContentLength() {
104             throw new UnsupportedOperationException("Not supported yet.");
105         }
106 
107         public String getContentType() {
108             throw new UnsupportedOperationException("Not supported yet.");
109         }
110 
111         public ServletInputStream getInputStream()
112             throws IOException
113         {
114             throw new UnsupportedOperationException("Not supported yet.");
115         }
116 
117         public String getLocalAddr() {
118             throw new UnsupportedOperationException("Not supported yet.");
119         }
120 
121         public String getLocalName() {
122             throw new UnsupportedOperationException("Not supported yet.");
123         }
124 
125         public int getLocalPort() {
126             throw new UnsupportedOperationException("Not supported yet.");
127         }
128 
129         public Locale getLocale() {
130             throw new UnsupportedOperationException("Not supported yet.");
131         }
132 
133         public Enumeration getLocales()
134         {
135             throw new UnsupportedOperationException("Not supported yet.");
136         }
137 
138         public String getParameter(String arg0)
139         {
140             throw new UnsupportedOperationException("Not supported yet.");
141         }
142 
143         public Map getParameterMap()
144         {
145             throw new UnsupportedOperationException("Not supported yet.");
146         }
147 
148         public Enumeration getParameterNames()
149         {
150             throw new UnsupportedOperationException("Not supported yet.");
151         }
152 
153         public String[] getParameterValues(String arg0)
154         {
155             throw new UnsupportedOperationException("Not supported yet.");
156         }
157 
158         public String getProtocol() {
159             throw new UnsupportedOperationException("Not supported yet.");
160         }
161 
162         public BufferedReader getReader()
163             throws IOException 
164         {
165             throw new UnsupportedOperationException("Not supported yet.");
166         }
167 
168         public String getRealPath(String arg0)
169         {
170             throw new UnsupportedOperationException("Not supported yet.");
171         }
172 
173         public String getRemoteAddr()
174         {
175             throw new UnsupportedOperationException("Not supported yet.");
176         }
177 
178         public String getRemoteHost()
179         {
180             throw new UnsupportedOperationException("Not supported yet.");
181         }
182 
183         public int getRemotePort()
184         {
185             throw new UnsupportedOperationException("Not supported yet.");
186         }
187 
188         public RequestDispatcher getRequestDispatcher(String arg0)
189         {
190             throw new UnsupportedOperationException("Not supported yet.");
191         }
192 
193         public String getScheme()
194         {
195             return "";
196         }
197 
198         public String getServerName()
199         {
200             throw new UnsupportedOperationException("Not supported yet.");
201         }
202 
203         public int getServerPort()
204         {
205             throw new UnsupportedOperationException("Not supported yet.");
206         }
207 
208         public boolean isSecure()
209         {
210             throw new UnsupportedOperationException("Not supported yet.");
211         }
212 
213         public void removeAttribute(String arg0)
214         {
215             throw new UnsupportedOperationException("Not supported yet.");
216         }
217 
218         public void setAttribute(String arg0, Object arg1)
219         {
220             throw new UnsupportedOperationException("Not supported yet.");
221         }
222 
223         public void setCharacterEncoding(String arg0)
224             throws UnsupportedEncodingException
225         {
226             throw new UnsupportedOperationException("Not supported yet.");
227         }
228         
229 
230         public String getAuthType()
231         {
232             throw new UnsupportedOperationException("Not supported yet.");
233         }
234 
235         public String getContextPath()
236         {
237             return "/";
238         }
239 
240         public Cookie[] getCookies()
241         {
242             throw new UnsupportedOperationException("Not supported yet.");
243         }
244 
245         public long getDateHeader(String arg0) 
246         {
247             throw new UnsupportedOperationException("Not supported yet.");
248         }
249 
250         public String getHeader(String arg0) {
251             return "";
252         }
253 
254         public Enumeration getHeaderNames()
255         {
256             throw new UnsupportedOperationException("Not supported yet.");
257         }
258 
259         public Enumeration getHeaders(String arg0) 
260         {
261             throw new UnsupportedOperationException("Not supported yet.");
262         }
263 
264         public int getIntHeader(String arg0) 
265         {
266             throw new UnsupportedOperationException("Not supported yet.");
267         }
268 
269         public String getMethod()
270         {
271             throw new UnsupportedOperationException("Not supported yet.");
272         }
273 
274         public String getPathInfo()
275         {
276             throw new UnsupportedOperationException("Not supported yet.");
277         }
278 
279         public String getPathTranslated()
280         {
281             throw new UnsupportedOperationException("Not supported yet.");
282         }
283 
284         public String getQueryString()
285         {
286             throw new UnsupportedOperationException("Not supported yet.");
287         }
288 
289         public String getRemoteUser()
290         {
291             throw new UnsupportedOperationException("Not supported yet.");
292         }
293 
294         public String getRequestURI()
295         {
296             return "/";
297         }
298 
299         public StringBuffer getRequestURL() 
300         {
301             throw new UnsupportedOperationException("Not supported yet.");
302         }
303 
304         public String getRequestedSessionId() 
305         {
306             throw new UnsupportedOperationException("Not supported yet.");
307         }
308 
309         public String getServletPath() 
310         {
311             throw new UnsupportedOperationException("Not supported yet.");
312         }
313 
314         public HttpSession getSession(boolean arg0) 
315         {
316             throw new UnsupportedOperationException("Not supported yet.");
317         }
318 
319         public HttpSession getSession() 
320         {
321             throw new UnsupportedOperationException("Not supported yet.");
322         }
323 
324         public Principal getUserPrincipal() 
325         {
326             throw new UnsupportedOperationException("Not supported yet.");
327         }
328 
329         public boolean isRequestedSessionIdFromCookie() 
330         {
331             throw new UnsupportedOperationException("Not supported yet.");
332         }
333 
334         public boolean isRequestedSessionIdFromURL() 
335         {
336             throw new UnsupportedOperationException("Not supported yet.");
337         }
338 
339         public boolean isRequestedSessionIdFromUrl() 
340         {
341             throw new UnsupportedOperationException("Not supported yet.");
342         }
343 
344         public boolean isRequestedSessionIdValid() 
345         {
346             throw new UnsupportedOperationException("Not supported yet.");
347         }
348 
349         public boolean isUserInRole(String arg0) 
350         {
351             throw new UnsupportedOperationException("Not supported yet.");
352         }
353     }
354     
355     private class ServletAuthenticatorMock implements ServletAuthenticator
356     {
357         public boolean isAuthenticated(HttpServletRequest arg0, AuthenticationResult arg1)
358             throws AuthenticationException, AccountLockedException, MustChangePasswordException
359         {
360             return true;
361         }
362 
363         public boolean isAuthorized( HttpServletRequest request, SecuritySession securitySession, String repositoryId,
364                                      String permission )
365             throws AuthorizationException, UnauthorizedException
366         {
367             return true;
368         }
369 
370         public boolean isAuthorized( String principal, String repoId, String permission )
371             throws UnauthorizedException
372         {
373             return true;
374         }   
375     }
376     
377     private class HttpAuthenticatorMock extends HttpAuthenticator
378     {
379         @Override
380         public void challenge(HttpServletRequest arg0, HttpServletResponse arg1, String arg2, AuthenticationException arg3)
381             throws IOException
382         {
383             //Do nothing
384         }
385 
386         @Override
387         public AuthenticationResult getAuthenticationResult(HttpServletRequest arg0, HttpServletResponse arg1)
388             throws AuthenticationException, AccountLockedException, MustChangePasswordException
389         {
390             return new AuthenticationResult();
391         }
392         
393 
394         @Override
395         public AuthenticationResult authenticate(AuthenticationDataSource arg0, HttpSession httpSession)
396             throws AuthenticationException, AccountLockedException, MustChangePasswordException
397         {
398             return new AuthenticationResult();
399         }
400 
401         @Override
402         public void authenticate(HttpServletRequest arg0, HttpServletResponse arg1)
403             throws AuthenticationException
404         {
405             //Do nothing
406         }
407 
408         @Override
409         public SecuritySession getSecuritySession(HttpSession httpSession)
410         {
411             return super.getSecuritySession(httpSession);
412         }
413 
414         @Override
415         public User getSessionUser(HttpSession httpSession)
416         {
417             return super.getSessionUser(httpSession);
418         }
419 
420         @Override
421         public boolean isAlreadyAuthenticated(HttpSession httpSession)
422         {
423             return super.isAlreadyAuthenticated(httpSession);
424         }        
425     }
426 }