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