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