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