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