]> source.dussan.org Git - archiva.git/blob
e133d2fb3b9d884249891632e7756d0eb0098fdb
[archiva.git] /
1 package org.apache.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 junit.framework.TestCase;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.beans.RepositoryGroup;
26 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
27 import org.apache.archiva.admin.repository.DefaultRepositoryCommonValidator;
28 import org.apache.archiva.admin.repository.group.DefaultRepositoryGroupAdmin;
29 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
30 import org.apache.archiva.common.filelock.FileLockManager;
31 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
32 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
33 import org.apache.archiva.configuration.ArchivaConfiguration;
34 import org.apache.archiva.configuration.Configuration;
35 import org.apache.archiva.configuration.FileTypes;
36 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
37 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
38 import org.apache.archiva.proxy.ProxyRegistry;
39 import org.apache.archiva.repository.EditableManagedRepository;
40 import org.apache.archiva.repository.BaseRepositoryContentLayout;
41 import org.apache.archiva.repository.RemoteRepository;
42 import org.apache.archiva.repository.RemoteRepositoryContent;
43 import org.apache.archiva.repository.Repository;
44 import org.apache.archiva.repository.RepositoryContent;
45 import org.apache.archiva.repository.RepositoryContentFactory;
46 import org.apache.archiva.repository.RepositoryContentProvider;
47 import org.apache.archiva.repository.RepositoryException;
48 import org.apache.archiva.repository.RepositoryRegistry;
49 import org.apache.archiva.repository.RepositoryType;
50 import org.apache.archiva.repository.maven.content.ManagedDefaultRepositoryContent;
51 import org.apache.archiva.repository.maven.content.MavenRepositoryRequestInfo;
52 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
53 import org.apache.commons.lang3.StringUtils;
54 import org.apache.jackrabbit.webdav.DavException;
55 import org.apache.jackrabbit.webdav.DavResourceLocator;
56 import org.apache.jackrabbit.webdav.DavServletRequest;
57 import org.apache.jackrabbit.webdav.DavServletResponse;
58 import org.easymock.EasyMock;
59 import org.easymock.IMocksControl;
60 import org.junit.After;
61 import org.junit.Before;
62 import org.junit.Test;
63 import org.junit.runner.RunWith;
64 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
65 import org.springframework.context.ApplicationContext;
66 import org.springframework.context.ConfigurableApplicationContext;
67 import org.springframework.test.context.ContextConfiguration;
68
69 import javax.inject.Inject;
70 import java.nio.file.Path;
71 import java.nio.file.Paths;
72 import java.util.ArrayList;
73 import java.util.HashSet;
74 import java.util.List;
75 import java.util.Locale;
76 import java.util.Set;
77 import java.util.concurrent.atomic.AtomicReference;
78
79 import static org.easymock.EasyMock.*;
80
81 /**
82  * ArchivaDavResourceFactoryTest
83  */
84 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
85 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
86 public class ArchivaDavResourceFactoryTest
87     extends TestCase
88 {
89     private AtomicReference<Path> projectBase = new AtomicReference<>();
90
91     private static final String RELEASES_REPO = "releases";
92
93     private static final String INTERNAL_REPO = "internal";
94
95     private static final String LOCAL_MIRROR_REPO = "local-mirror";
96
97     private static final String LEGACY_REPO = "legacy-repo";
98
99     private static final String LOCAL_REPO_GROUP = "local";
100
101     private OverridingArchivaDavResourceFactory resourceFactory;
102
103     private IMocksControl requestControl;
104
105     private DavServletRequest request;
106
107     private IMocksControl repoRequestControl;
108
109     private MavenRepositoryRequestInfo repoRequest;
110
111     private IMocksControl responseControl;
112
113     private DavServletResponse response;
114
115     private IMocksControl archivaConfigurationControl;
116
117     private ArchivaConfiguration archivaConfiguration;
118
119     private Configuration config;
120
121     private IMocksControl repoContentFactoryControl;
122
123     private RepositoryContentFactory repoFactory;
124
125     @Inject
126     ApplicationContext applicationContext;
127
128     @Inject
129     PlexusSisuBridge plexusSisuBridge;
130
131     @Inject
132     DefaultManagedRepositoryAdmin defaultManagedRepositoryAdmin;
133
134     @Inject
135     RepositoryRegistry repositoryRegistry;
136
137     @Inject
138     RemoteRepositoryAdmin remoteRepositoryAdmin;
139
140     @Inject
141     ProxyRegistry proxyRegistry;
142
143
144     @Inject
145     DefaultRepositoryGroupAdmin defaultRepositoryGroupAdmin;
146
147     @Inject
148     List<? extends ArtifactMappingProvider> artifactMappingProviders;
149
150     @Inject
151     FileLockManager fileLockManager;
152
153     @Inject
154     FileTypes fileTypes;
155
156     public Path getProjectBase() {
157         if (this.projectBase.get()==null) {
158             String pathVal = System.getProperty("mvn.project.base.dir");
159             Path baseDir;
160             if (StringUtils.isEmpty(pathVal)) {
161                 baseDir= Paths.get("").toAbsolutePath();
162             } else {
163                 baseDir = Paths.get(pathVal).toAbsolutePath();
164             }
165             this.projectBase.compareAndSet(null, baseDir);
166         }
167         return this.projectBase.get();
168     }
169
170     @Before
171     @Override
172     public void setUp()
173         throws Exception
174     {
175         super.setUp();
176
177         requestControl = createControl();
178         request = requestControl.createMock( DavServletRequest.class );
179
180         responseControl = createControl();
181         response = responseControl.createMock( DavServletResponse.class );
182         //responseControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
183
184         archivaConfigurationControl = createControl();
185         archivaConfiguration = archivaConfigurationControl.createMock( ArchivaConfiguration.class );
186
187         config = new Configuration();
188         expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2, 20 );
189         expect (archivaConfiguration.getDefaultLocale()).andReturn( Locale.getDefault() ).anyTimes();
190         archivaConfiguration.addListener( EasyMock.anyObject(  ) );
191         expectLastCall().times(0, 4);
192         archivaConfiguration.save( config );
193
194         expectLastCall().times( 0, 5 );
195         archivaConfigurationControl.replay();
196
197         defaultManagedRepositoryAdmin.setArchivaConfiguration( archivaConfiguration );
198         repositoryRegistry.setArchivaConfiguration( archivaConfiguration );
199         repositoryRegistry.reload();
200         ( (DefaultRepositoryCommonValidator) defaultManagedRepositoryAdmin.getRepositoryCommonValidator() ).setArchivaConfiguration(
201             archivaConfiguration );
202         if ( defaultManagedRepositoryAdmin.getManagedRepository( RELEASES_REPO ) == null )
203         {
204             defaultManagedRepositoryAdmin.addManagedRepository(
205                 createManagedRepository( RELEASES_REPO, getProjectBase().resolve( "target/test-classes/" + RELEASES_REPO ).toString(),
206                                          "default" ), false, null );
207         }
208         if ( defaultManagedRepositoryAdmin.getManagedRepository( INTERNAL_REPO ) == null )
209         {
210             defaultManagedRepositoryAdmin.addManagedRepository(
211                 createManagedRepository( INTERNAL_REPO, getProjectBase().resolve( "target/test-classes/" + INTERNAL_REPO ).toString(),
212                                          "default" ), false, null );
213         }
214         RepositoryGroup repoGroupConfig = new RepositoryGroup();
215         repoGroupConfig.setId( LOCAL_REPO_GROUP );
216         repoGroupConfig.addRepository( RELEASES_REPO );
217         repoGroupConfig.addRepository( INTERNAL_REPO );
218
219         defaultRepositoryGroupAdmin.setArchivaConfiguration( archivaConfiguration );
220         if ( defaultManagedRepositoryAdmin.getManagedRepository( LOCAL_REPO_GROUP ) == null )
221         {
222             defaultRepositoryGroupAdmin.addRepositoryGroup( repoGroupConfig, null );
223         }
224
225         repoContentFactoryControl = createControl();
226         repoFactory = repoContentFactoryControl.createMock( RepositoryContentFactory.class );
227
228         repoRequestControl = createControl();
229         repoRequest = repoRequestControl.createMock( MavenRepositoryRequestInfo.class );
230
231         resourceFactory =
232             new OverridingArchivaDavResourceFactory( applicationContext, plexusSisuBridge, archivaConfiguration );
233         resourceFactory.setArchivaConfiguration( archivaConfiguration );
234         proxyRegistry.getAllHandler().get(RepositoryType.MAVEN).clear();
235         proxyRegistry.getAllHandler().get(RepositoryType.MAVEN).add(new OverridingRepositoryProxyHandler(this));
236         resourceFactory.setProxyRegistry(proxyRegistry);
237         resourceFactory.setRemoteRepositoryAdmin( remoteRepositoryAdmin );
238         resourceFactory.setManagedRepositoryAdmin( defaultManagedRepositoryAdmin );
239         resourceFactory.setRepositoryRegistry( repositoryRegistry );
240     }
241
242     private ManagedRepository createManagedRepository( String id, String location, String layout )
243     {
244         ManagedRepository repoConfig = new ManagedRepository( Locale.getDefault());
245         repoConfig.setId( id );
246         repoConfig.setName( id );
247         repoConfig.setLocation( location );
248         repoConfig.setLayout( layout );
249
250         return repoConfig;
251     }
252
253     private BaseRepositoryContentLayout createManagedRepositoryContent( String repoId )
254         throws RepositoryAdminException
255     {
256         org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( repoId );
257         BaseRepositoryContentLayout repoContent = new ManagedDefaultRepositoryContent(repo, artifactMappingProviders, fileTypes, fileLockManager);
258         if (repo!=null && repo instanceof EditableManagedRepository)
259         {
260             ( (EditableManagedRepository) repo ).setContent( repoContent );
261         }
262         return repoContent;
263     }
264
265     private RepositoryContentProvider createRepositoryContentProvider( BaseRepositoryContentLayout content) {
266         Set<RepositoryType> TYPES = new HashSet<>(  );
267         TYPES.add(RepositoryType.MAVEN);
268         return new RepositoryContentProvider( )
269         {
270
271
272             @Override
273             public boolean supportsLayout( String layout )
274             {
275                 return true;
276             }
277
278             @Override
279             public Set<RepositoryType> getSupportedRepositoryTypes( )
280             {
281                 return TYPES;
282             }
283
284             @Override
285             public boolean supports( RepositoryType type )
286             {
287                 return true;
288             }
289
290             @Override
291             public RemoteRepositoryContent createRemoteContent( RemoteRepository repository ) throws RepositoryException
292             {
293                 return null;
294             }
295
296             @Override
297             public BaseRepositoryContentLayout createManagedContent( org.apache.archiva.repository.ManagedRepository repository ) throws RepositoryException
298             {
299                 content.setRepository( repository );
300                 return content;
301             }
302
303             @Override
304             public <T extends RepositoryContent, V extends Repository> T createContent( Class<T> clazz, V repository ) throws RepositoryException
305             {
306                 return null;
307             }
308         };
309     }
310
311     @After
312     @Override
313     public void tearDown()
314         throws Exception
315     {
316         super.tearDown();
317         String appserverBase = System.getProperty( "appserver.base" );
318         if ( StringUtils.isNotEmpty( appserverBase ) )
319         {
320             org.apache.archiva.common.utils.FileUtils.deleteDirectory( Paths.get( appserverBase ) );
321         }
322     }
323
324     // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
325     @Test
326     public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
327         throws Exception
328     {
329         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
330             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
331                                                                     new ArchivaDavLocatorFactory() );
332
333         BaseRepositoryContentLayout internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
334         BaseRepositoryContentLayout releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
335
336         try
337         {
338             archivaConfigurationControl.reset();
339
340             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 );
341
342             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
343
344             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
345
346             expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo );
347
348             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
349
350             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
351
352             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
353
354             expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( true );
355
356             expect(
357                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
358                 "legacy" );
359
360             expect( repoRequest.toArtifactReference(
361                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null );
362
363             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
364             ) ).andReturn(
365                 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
366                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
367
368             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
369
370             expect( repoRequest.isArchetypeCatalog(
371                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
372             archivaConfigurationControl.replay();
373             requestControl.replay();
374             repoContentFactoryControl.replay();
375             repoRequestControl.replay();
376
377             resourceFactory.createResource( locator, request, response );
378
379             archivaConfigurationControl.verify();
380             requestControl.verify();
381             repoContentFactoryControl.verify();
382             repoRequestControl.verify();
383
384             fail( "A DavException with 401 error code should have been thrown." );
385         }
386         catch ( DavException e )
387         {
388             assertEquals( 401, e.getErrorCode() );
389         }
390     }
391
392     @Test
393     public void testRepositoryGroupLastRepositoryRequiresAuthentication()
394         throws Exception
395     {
396         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
397             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
398                                                                     new ArchivaDavLocatorFactory() );
399
400         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
401         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
402         repoGroup.setId( LOCAL_REPO_GROUP );
403         repoGroup.addRepository( INTERNAL_REPO );
404         repoGroup.addRepository( RELEASES_REPO );
405
406         repoGroups.add( repoGroup );
407
408         config.setRepositoryGroups( repoGroups );
409
410         BaseRepositoryContentLayout internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
411
412         BaseRepositoryContentLayout releasesRepo = createManagedRepositoryContent( RELEASES_REPO );
413
414         try
415         {
416             archivaConfigurationControl.reset();
417
418             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 );
419
420             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
421
422             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
423
424             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
425
426             expect( repoFactory.getManagedRepositoryContent( RELEASES_REPO ) ).andReturn( releasesRepo );
427
428             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 2 );
429
430             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
431
432             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
433
434             expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
435
436             expect(
437                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
438                 "legacy" );
439
440             expect( repoRequest.toArtifactReference(
441                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null );
442
443             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
444             ) ).andReturn(
445                 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
446                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
447
448
449             expect( repoRequest.isArchetypeCatalog(
450                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false );
451             archivaConfigurationControl.replay();
452             requestControl.replay();
453             repoContentFactoryControl.replay();
454             repoRequestControl.replay();
455
456             resourceFactory.createResource( locator, request, response );
457
458             archivaConfigurationControl.verify();
459             requestControl.verify();
460             repoContentFactoryControl.verify();
461             repoRequestControl.verify();
462
463             fail( "A DavException with 401 error code should have been thrown." );
464         }
465         catch ( DavException e )
466         {
467             assertEquals( 401, e.getErrorCode() );
468         }
469     }
470
471     @Test
472     public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
473         throws Exception
474     {
475         DavResourceLocator locator = new ArchivaDavResourceLocator( "", "/repository/" + LOCAL_REPO_GROUP
476             + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP,
477                                                                     new ArchivaDavLocatorFactory() );
478
479         defaultManagedRepositoryAdmin.addManagedRepository(
480             createManagedRepository( LOCAL_MIRROR_REPO, Paths.get( "target/test-classes/local-mirror" ).toString(),
481                                      "default" ), false, null );
482
483         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
484         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
485         repoGroup.setId( LOCAL_REPO_GROUP );
486         repoGroup.addRepository( INTERNAL_REPO );
487         repoGroup.addRepository( LOCAL_MIRROR_REPO );
488
489         repoGroups.add( repoGroup );
490
491         config.setRepositoryGroups( repoGroups );
492
493         BaseRepositoryContentLayout internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
494         BaseRepositoryContentLayout localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
495
496         repositoryRegistry.putRepositoryGroup( repoGroup );
497
498         try
499         {
500             archivaConfigurationControl.reset();
501
502             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 3 );
503
504             expect( request.getMethod() ).andReturn( "GET" ).times( 5 );
505
506             expect( request.getPathInfo() ).andReturn( "org/apache/archiva" ).times( 0, 2 );
507
508             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
509
510             expect( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ) ).andReturn( localMirrorRepo );
511
512             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 4 );
513
514             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 4 );
515
516             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
517
518             expect( repoRequest.isSupportFile( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 );
519
520             expect(
521                 repoRequest.getLayout( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn(
522                 "legacy" ).times( 2 );
523
524             expect( repoRequest.toArtifactReference(
525                 "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( null ).times( 2 );
526
527             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
528             ) ).andReturn(
529                 Paths.get( config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
530                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString() );
531
532             expect( repoRequest.toNativePath( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar"
533             ) )
534                 .andReturn( Paths.get( config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
535                                       "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).toString());
536
537             expect( repoRequest.isArchetypeCatalog( "org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ) ).andReturn( false ).times( 2 );
538             archivaConfigurationControl.replay();
539             requestControl.replay();
540             repoContentFactoryControl.replay();
541             repoRequestControl.replay();
542
543             resourceFactory.createResource( locator, request, response );
544
545             archivaConfigurationControl.verify();
546             requestControl.verify();
547             repoContentFactoryControl.verify();
548             repoRequestControl.verify();
549
550             fail( "A DavException with 404 error code should have been thrown." );
551         }
552         catch ( DavException e )
553         {
554             assertEquals( 404, e.getErrorCode() );
555         }
556     }
557
558     // MRM-1239
559     @Test
560     public void testRequestArtifactMetadataThreePartsRepoHasDefaultLayout()
561         throws Exception
562     {
563         // should fetch metadata 
564         DavResourceLocator locator =
565             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml",
566                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
567
568         BaseRepositoryContentLayout internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
569
570         // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
571         MavenRepositoryRequestInfo repoRequest = new MavenRepositoryRequestInfo(internalRepo.getRepository() );
572
573         try
574         {
575             archivaConfigurationControl.reset();
576
577             expect( request.getMethod() ).andReturn( "GET" ).times( 4 );
578
579             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 );
580
581             expect( request.getContextPath() ).andReturn( "" ).times( 1 );
582
583             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
584
585             expect( request.getRequestURI() ).andReturn( "http://localhost:8080/archiva/repository/" + INTERNAL_REPO + "/eclipse/jdtcore/maven-metadata.xml" );
586             response.setHeader( "Pragma", "no-cache" );
587
588             expectLastCall();
589
590             response.setHeader( "Cache-Control", "no-cache" );
591
592             expectLastCall();
593
594             response.setDateHeader( eq("Last-Modified"), anyLong() );
595             expectLastCall();
596
597             archivaConfigurationControl.replay();
598             repoContentFactoryControl.replay();
599             requestControl.replay();
600             responseControl.replay();
601
602             resourceFactory.createResource( locator, request, response );
603
604             archivaConfigurationControl.verify();
605             repoContentFactoryControl.verify();
606             requestControl.verify();
607             responseControl.verify();
608         }
609         catch ( DavException e )
610         {
611             e.printStackTrace();
612             fail( "A DavException should not have been thrown! "+e.getMessage() );
613         }
614     }
615
616     @Test
617     public void testRequestArtifactMetadataTwoPartsRepoHasDefaultLayout()
618         throws Exception
619     {
620         // should not fetch metadata
621         DavResourceLocator locator =
622             new ArchivaDavResourceLocator( "", "/repository/" + INTERNAL_REPO + "/eclipse/maven-metadata.xml",
623                                            INTERNAL_REPO, new ArchivaDavLocatorFactory() );
624
625         BaseRepositoryContentLayout internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
626
627         try
628         {
629             archivaConfigurationControl.reset();
630
631             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2 );
632
633             expect( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ) ).andReturn( internalRepo );
634
635             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
636
637             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 );
638
639             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
640
641             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
642
643             archivaConfigurationControl.replay();
644             repoContentFactoryControl.replay();
645             requestControl.replay();
646
647             resourceFactory.createResource( locator, request, response );
648
649             archivaConfigurationControl.verify();
650             repoContentFactoryControl.verify();
651             requestControl.verify();
652
653             fail( "A 404 error should have been thrown!" );
654         }
655         catch ( DavException e )
656         {
657             assertEquals( 404, e.getErrorCode() );
658         }
659     }
660
661     @Test
662     public void testRequestMetadataRepoIsLegacy()
663         throws Exception
664     {
665         BaseRepositoryContentLayout legacyRepo = createManagedRepositoryContent( LEGACY_REPO );
666         ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
667         RepositoryContentProvider provider = createRepositoryContentProvider(legacyRepo );
668         beanFactory.registerSingleton("repositoryContentProvider#legacy", provider);
669         RepositoryContentFactory repoContentFactory = applicationContext.getBean( "repositoryContentFactory#default", RepositoryContentFactory.class );
670         repoContentFactory.getRepositoryContentProviders().add(provider);
671         defaultManagedRepositoryAdmin.addManagedRepository(
672             createManagedRepository( LEGACY_REPO, getProjectBase().resolve( "target/test-classes/" + LEGACY_REPO ).toString(),
673                 "legacy" ), false, null );
674
675         DavResourceLocator locator =
676             new ArchivaDavResourceLocator( "", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml",
677                                            LEGACY_REPO, new ArchivaDavLocatorFactory() );
678
679
680         try
681         {
682             archivaConfigurationControl.reset();
683
684             expect( archivaConfiguration.getConfiguration() ).andReturn( config ).times( 2 );
685
686             expect( repoFactory.getManagedRepositoryContent( LEGACY_REPO ) ).andReturn( legacyRepo );
687
688             expect( request.getMethod() ).andReturn( "GET" ).times( 3 );
689
690             expect( request.getRemoteAddr() ).andReturn( "http://localhost:8080" ).times( 3 );
691
692             expect( request.getDavSession() ).andReturn( new ArchivaDavSession() ).times( 2 );
693
694             expect( request.getContextPath() ).andReturn( "" ).times( 2 );
695
696             archivaConfigurationControl.replay();
697             repoContentFactoryControl.replay();
698             requestControl.replay();
699
700             resourceFactory.createResource( locator, request, response );
701
702             archivaConfigurationControl.verify();
703             repoContentFactoryControl.verify();
704             requestControl.verify();
705
706             fail( "A 404 error should have been thrown!" );
707         }
708         catch ( DavException e )
709         {
710             assertEquals( 404, e.getErrorCode() );
711         }
712     }
713
714     class OverridingArchivaDavResourceFactory
715         extends ArchivaDavResourceFactory
716     {
717
718         OverridingArchivaDavResourceFactory( ApplicationContext applicationContext, PlexusSisuBridge plexusSisuBridge,
719                                              ArchivaConfiguration archivaConfiguration )
720             throws PlexusSisuBridgeException
721         {
722             super( applicationContext, archivaConfiguration );
723         }
724
725         @Override
726         protected boolean isAuthorized( DavServletRequest request, String repositoryId )
727             throws DavException
728         {
729             if ( RELEASES_REPO.equals( repositoryId ) )
730             {
731                 throw new UnauthorizedDavException( repositoryId,
732                                                     "You are not authenticated and authorized to access any repository." );
733             }
734             else
735             {
736                 return true;
737             }
738         }
739
740         @Override
741         protected String getActivePrincipal( DavServletRequest request )
742         {
743             return "guest";
744         }
745     }
746
747 }