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