]> source.dussan.org Git - archiva.git/blob
b2f38fdb430f011146aeab45b8c56231da04f61e
[archiva.git] /
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.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.io.FileUtils;
28 import org.apache.jackrabbit.webdav.DavException;
29 import org.apache.jackrabbit.webdav.DavResourceLocator;
30 import org.apache.jackrabbit.webdav.DavServletRequest;
31 import org.apache.jackrabbit.webdav.DavServletResponse;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.Configuration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
36 import org.apache.maven.archiva.proxy.DefaultRepositoryProxyConnectors;
37 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
38 import org.apache.maven.archiva.repository.RepositoryContentFactory;
39 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
40 import org.apache.maven.archiva.repository.content.RepositoryRequest;
41 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
42 import org.easymock.MockControl;
43 import org.easymock.classextension.MockClassControl;
44
45 /**
46  * ArchivaDavResourceFactoryTest
47  */
48 public class ArchivaDavResourceFactoryTest
49     extends PlexusInSpringTestCase
50 {
51     private static final String RELEASES_REPO = "releases";
52
53     private static final String INTERNAL_REPO = "internal";
54
55     private static final String LOCAL_MIRROR_REPO = "local-mirror";
56
57     private static final String LEGACY_REPO = "legacy-repo";
58
59     private static final String LOCAL_REPO_GROUP = "local";
60
61     private OverridingArchivaDavResourceFactory resourceFactory;
62
63     private MockControl requestControl;
64
65     private DavServletRequest request;
66
67     private MockControl repoRequestControl;
68
69     private RepositoryRequest repoRequest;
70
71     private MockControl responseControl;
72
73     private DavServletResponse response;
74
75     private MockControl archivaConfigurationControl;
76
77     private ArchivaConfiguration archivaConfiguration;
78
79     private Configuration config;
80
81     private MockControl repoContentFactoryControl;
82
83     private RepositoryContentFactory repoFactory;
84
85     public void setUp()
86         throws Exception
87     {
88         super.setUp();
89
90         requestControl = MockControl.createControl( DavServletRequest.class );
91         request = (DavServletRequest) requestControl.getMock();
92
93         responseControl = MockControl.createControl( DavServletResponse.class );
94         response = (DavServletResponse) responseControl.getMock();
95         responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
96
97         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
98         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
99
100         config = new Configuration();
101         config.addManagedRepository( createManagedRepository( RELEASES_REPO, new File( getBasedir(),
102                                                                                        "target/test-classes/" +
103                                                                                            RELEASES_REPO ).getPath(),
104                                                               "default" ) );
105         config.addManagedRepository( createManagedRepository( INTERNAL_REPO, new File( getBasedir(),
106                                                                                        "target/test-classes/" +
107                                                                                            INTERNAL_REPO ).getPath(),
108                                                               "default" ) );
109
110         RepositoryGroupConfiguration repoGroupConfig = new RepositoryGroupConfiguration();
111         repoGroupConfig.setId( LOCAL_REPO_GROUP );
112         repoGroupConfig.addRepository( RELEASES_REPO );
113         repoGroupConfig.addRepository( INTERNAL_REPO );
114
115         config.addRepositoryGroup( repoGroupConfig );
116
117         repoContentFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
118         repoFactory = (RepositoryContentFactory) repoContentFactoryControl.getMock();
119
120         repoRequestControl = MockClassControl.createControl( RepositoryRequest.class );
121         repoRequest = (RepositoryRequest) repoRequestControl.getMock();
122
123         resourceFactory = new OverridingArchivaDavResourceFactory();
124         resourceFactory.setArchivaConfiguration( archivaConfiguration );
125         resourceFactory.setRepositoryFactory( repoFactory );
126         resourceFactory.setRepositoryRequest( repoRequest );
127         resourceFactory.setConnectors( new OverridingRepositoryProxyConnectors() );
128     }
129
130     private ManagedRepositoryConfiguration createManagedRepository( String id, String location, String layout )
131     {
132         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
133         repoConfig.setId( id );
134         repoConfig.setName( id );
135         repoConfig.setLocation( location );
136         repoConfig.setLayout( layout );
137
138         return repoConfig;
139     }
140
141     private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
142     {
143         ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
144         repoContent.setRepository( config.findManagedRepositoryById( repoId ) );
145
146         return repoContent;
147     }
148
149     public void tearDown()
150         throws Exception
151     {
152         super.tearDown();
153     }
154
155     // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
156     public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
157         throws Exception
158     {
159         DavResourceLocator locator =
160             new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP +
161                 "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
162                                            new ArchivaDavLocatorFactory() );
163
164         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
165
166         try
167         {
168             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
169             requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
170             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
171                                                        createManagedRepositoryContent( RELEASES_REPO ) );
172             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
173             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
174             repoRequestControl.expectAndReturn(
175                                                 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
176                                                 false );
177             repoRequestControl.expectAndReturn(
178                                                 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
179                                                 false );
180             repoRequestControl.expectAndReturn(
181                                                 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
182                                                 null );
183             repoRequestControl.expectAndReturn(
184                                                 repoRequest.toNativePath(
185                                                                           "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
186                                                                           internalRepo ),
187                                                 new File(
188                                                           config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
189                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
190             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
191                                                        internalRepo );
192
193             archivaConfigurationControl.replay();
194             requestControl.replay();
195             repoContentFactoryControl.replay();
196             repoRequestControl.replay();
197
198             resourceFactory.createResource( locator, request, response );
199
200             archivaConfigurationControl.verify();
201             requestControl.verify();
202             repoContentFactoryControl.verify();
203             repoRequestControl.verify();
204
205             fail( "A DavException with 401 error code should have been thrown." );
206         }
207         catch ( DavException e )
208         {
209             assertEquals( 401, e.getErrorCode() );
210         }
211     }
212
213     public void testRepositoryGroupLastRepositoryRequiresAuthentication()
214         throws Exception
215     {
216         DavResourceLocator locator =
217             new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP +
218                 "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
219                                            new ArchivaDavLocatorFactory() );
220
221         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
222         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
223         repoGroup.setId( LOCAL_REPO_GROUP );
224         repoGroup.addRepository( INTERNAL_REPO );
225         repoGroup.addRepository( RELEASES_REPO );
226
227         repoGroups.add( repoGroup );
228
229         config.setRepositoryGroups( repoGroups );
230
231         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
232
233         try
234         {
235             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
236             requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
237             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
238                                                        internalRepo );
239             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
240                                                        createManagedRepositoryContent( RELEASES_REPO ) );
241             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
242             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
243             repoRequestControl.expectAndReturn(
244                                                 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
245                                                 false );
246             repoRequestControl.expectAndReturn(
247                                                 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
248                                                 false );
249             repoRequestControl.expectAndReturn(
250                                                 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
251                                                 null );
252             repoRequestControl.expectAndReturn(
253                                                 repoRequest.toNativePath(
254                                                                           "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
255                                                                           internalRepo ),
256                                                 new File(
257                                                           config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
258                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
259
260             archivaConfigurationControl.replay();
261             requestControl.replay();
262             repoContentFactoryControl.replay();
263             repoRequestControl.replay();
264
265             resourceFactory.createResource( locator, request, response );
266
267             archivaConfigurationControl.verify();
268             requestControl.verify();
269             repoContentFactoryControl.verify();
270             repoRequestControl.verify();
271
272             fail( "A DavException with 401 error code should have been thrown." );
273         }
274         catch ( DavException e )
275         {
276             assertEquals( 401, e.getErrorCode() );
277         }
278     }
279
280     public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
281         throws Exception
282     {
283         DavResourceLocator locator =
284             new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP +
285                 "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
286                                            new ArchivaDavLocatorFactory() );
287
288         config.addManagedRepository( createManagedRepository( LOCAL_MIRROR_REPO,
289                                                               new File( getBasedir(),
290                                                                         "target/test-classes/local-mirror" ).getPath(),
291                                                               "default" ) );
292
293         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
294         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
295         repoGroup.setId( LOCAL_REPO_GROUP );
296         repoGroup.addRepository( INTERNAL_REPO );
297         repoGroup.addRepository( LOCAL_MIRROR_REPO );
298
299         repoGroups.add( repoGroup );
300
301         config.setRepositoryGroups( repoGroups );
302
303         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
304         ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
305
306         try
307         {
308             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
309             requestControl.expectAndReturn( request.getMethod(), "GET", 4 );
310             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
311                                                        internalRepo );
312             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ),
313                                                        localMirrorRepo );
314             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 4 );
315             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 4 );
316             repoRequestControl.expectAndReturn(
317                                                 repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
318                                                 false, 2 );
319             repoRequestControl.expectAndReturn(
320                                                 repoRequest.isDefault( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
321                                                 false, 2 );
322             repoRequestControl.expectAndReturn(
323                                                 repoRequest.toArtifactReference( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
324                                                 null, 2 );
325             repoRequestControl.expectAndReturn(
326                                                 repoRequest.toNativePath(
327                                                                           "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
328                                                                           internalRepo ),
329                                                 new File(
330                                                           config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
331                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
332
333             repoRequestControl.expectAndReturn(
334                                                 repoRequest.toNativePath(
335                                                                           "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
336                                                                           localMirrorRepo ),
337                                                 new File(
338                                                           config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
339                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
340
341             archivaConfigurationControl.replay();
342             requestControl.replay();
343             repoContentFactoryControl.replay();
344             repoRequestControl.replay();
345
346             resourceFactory.createResource( locator, request, response );
347
348             archivaConfigurationControl.verify();
349             requestControl.verify();
350             repoContentFactoryControl.verify();
351             repoRequestControl.verify();
352
353             fail( "A DavException with 404 error code should have been thrown." );
354         }
355         catch ( DavException e )
356         {
357             assertEquals( 404, e.getErrorCode() );
358         }
359     }
360
361     // MRM-1239
362     public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
363         throws Exception
364     {
365         // should fetch metadata 
366         DavResourceLocator locator =
367             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml", INTERNAL_REPO,
368                                            new ArchivaDavLocatorFactory() );
369
370         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
371
372         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
373         RepositoryRequest repoRequest = (RepositoryRequest) lookup( RepositoryRequest.class );
374         resourceFactory.setRepositoryRequest( repoRequest );
375
376         try
377         {
378             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
379             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
380                                                        internalRepo );
381             requestControl.expectAndReturn( request.getMethod(), "GET", 3 );
382             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 3 );
383             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
384             requestControl.expectAndReturn( request.getRequestURI(), "http://localhost:8080/archiva/repository/" +
385                 INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" );
386             response.addHeader( "Pragma", "no-cache" );
387             responseControl.setVoidCallable();
388
389             response.addHeader( "Cache-Control", "no-cache" );
390             responseControl.setVoidCallable();
391
392             long date = 2039842134;
393             response.addDateHeader( "last-modified", date );
394             responseControl.setVoidCallable();
395
396             archivaConfigurationControl.replay();
397             repoContentFactoryControl.replay();
398             requestControl.replay();
399             responseControl.replay();
400
401             resourceFactory.createResource( locator, request, response );
402
403             archivaConfigurationControl.verify();
404             repoContentFactoryControl.verify();
405             requestControl.verify();
406             responseControl.verify();
407         }
408         catch ( DavException e )
409         {
410             fail( "A DavException should not have been thrown!" );
411         }
412     }
413
414     public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
415         throws Exception
416     {
417         // should not fetch metadata
418         DavResourceLocator locator =
419             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml", INTERNAL_REPO,
420                                            new ArchivaDavLocatorFactory() );
421
422         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
423
424         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
425         RepositoryRequest repoRequest = (RepositoryRequest) lookup( RepositoryRequest.class );
426         resourceFactory.setRepositoryRequest( repoRequest );
427
428         try
429         {
430             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
431             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
432                                                        internalRepo );
433             requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
434             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
435             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
436
437             archivaConfigurationControl.replay();
438             repoContentFactoryControl.replay();
439             requestControl.replay();
440
441             resourceFactory.createResource( locator, request, response );
442
443             archivaConfigurationControl.verify();
444             repoContentFactoryControl.verify();
445             requestControl.verify();
446
447             fail( "A 404 error should have been thrown!" );
448         }
449         catch ( DavException e )
450         {
451             assertEquals( 404, e.getErrorCode() );
452         }
453     }
454
455     public void testRequestMetadataRepoIsLegacy()
456         throws Exception
457     {
458         config.addManagedRepository( createManagedRepository( LEGACY_REPO, new File( getBasedir(),
459                                                                                      "target/test-classes/" +
460                                                                                          LEGACY_REPO ).getPath(),
461                                                               "legacy" ) );
462         DavResourceLocator locator =
463             new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml", LEGACY_REPO,
464                                            new ArchivaDavLocatorFactory() );
465
466         ManagedRepositoryContent legacyRepo = createManagedRepositoryContent( LEGACY_REPO );
467
468         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
469         RepositoryRequest repoRequest = (RepositoryRequest) lookup( RepositoryRequest.class );
470         resourceFactory.setRepositoryRequest( repoRequest );
471
472         try
473         {
474             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
475             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LEGACY_REPO ),
476                                                        legacyRepo );
477             requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
478             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
479             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
480
481             archivaConfigurationControl.replay();
482             repoContentFactoryControl.replay();
483             requestControl.replay();
484
485             resourceFactory.createResource( locator, request, response );
486
487             archivaConfigurationControl.verify();
488             repoContentFactoryControl.verify();
489             requestControl.verify();
490
491             fail( "A 404 error should have been thrown!" );
492         }
493         catch ( DavException e )
494         {
495             assertEquals( 404, e.getErrorCode() );
496         }
497     }
498
499     class OverridingArchivaDavResourceFactory
500         extends ArchivaDavResourceFactory
501     {
502         protected boolean isAuthorized( DavServletRequest request, String repositoryId )
503             throws DavException
504         {
505             if ( RELEASES_REPO.equals( repositoryId ) )
506             {
507                 throw new UnauthorizedDavException( repositoryId,
508                                                     "You are not authenticated and authorized to access any repository." );
509             }
510             else
511             {
512                 return true;
513             }
514         }
515
516         protected String getActivePrincipal( DavServletRequest request )
517         {
518             return "guest";
519         }
520     }
521
522     class OverridingRepositoryProxyConnectors
523         extends DefaultRepositoryProxyConnectors
524     {
525         public File fetchMetatadaFromProxies( ManagedRepositoryContent repository, String logicalPath )
526         {
527             File target = new File( repository.getRepoRoot(), logicalPath );
528             try
529             {
530                 FileUtils.copyFile( new File( getBasedir(), "target/test-classes/maven-metadata.xml" ), target );
531             }
532             catch ( IOException e )
533             {
534
535             }
536
537             return target;
538         }
539     }
540 }