]> source.dussan.org Git - archiva.git/blob
c0d491d69fc3e89bd823175174be6f5484a70fdc
[archiva.git] /
1 package org.apache.archiva.remotedownload;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.beans.RemoteRepository;
22 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
23 import org.apache.archiva.security.common.ArchivaRoleConstants;
24 import org.apache.commons.compress.utils.IOUtils;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.wagon.providers.http.HttpWagon;
27 import org.apache.maven.wagon.repository.Repository;
28 import org.eclipse.jetty.server.Server;
29 import org.eclipse.jetty.servlet.ServletContextHandler;
30 import org.eclipse.jetty.servlet.ServletHolder;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import javax.servlet.ServletException;
41 import javax.servlet.http.HttpServlet;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import java.io.File;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Enumeration;
50 import java.util.List;
51 import java.util.zip.ZipEntry;
52 import java.util.zip.ZipFile;
53 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
54
55 /**
56  * @author Olivier Lamy
57  */
58 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
59 public class DownloadArtifactsTest
60     extends AbstractDownloadTest
61 {
62
63     protected Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
64
65     public Server redirectServer = null;
66
67     public int redirectPort;
68
69     public Server repoServer = null;
70
71     public int repoServerPort;
72
73     @BeforeClass
74     public static void setAppServerBase()
75     {
76         previousAppServerBase = System.getProperty( "appserver.base" );
77         System.setProperty( "appserver.base", "target/" + DownloadArtifactsTest.class.getName() );
78     }
79
80
81     @AfterClass
82     public static void resetAppServerBase()
83     {
84         System.setProperty( "appserver.base", previousAppServerBase );
85     }
86
87     protected String getSpringConfigLocation()
88     {
89         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
90     }
91
92     @Before
93     public void startServer()
94         throws Exception
95     {
96         super.startServer();
97
98         // repo handler
99
100         this.repoServer = new Server( 0 );
101
102         ServletHolder shRepo = new ServletHolder( RepoServlet.class );
103         ServletContextHandler contextRepo = new ServletContextHandler();
104
105         contextRepo.setContextPath( "/" );
106         contextRepo.addServlet( shRepo, "/*" );
107
108         repoServer.setHandler( contextRepo );
109         repoServer.start();
110         this.repoServerPort = repoServer.getConnectors()[0].getLocalPort();
111
112         //redirect handler
113
114         this.redirectServer = new Server( 0 );
115         ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
116         ServletContextHandler contextRedirect = new ServletContextHandler();
117         contextRedirect.setAttribute( "redirectToPort", Integer.toString( this.repoServerPort ) );
118
119         contextRedirect.setContextPath( "/" );
120         contextRedirect.addServlet( shRedirect, "/*" );
121
122         redirectServer.setHandler( contextRedirect );
123         redirectServer.start();
124         this.redirectPort = redirectServer.getConnectors()[0].getLocalPort();
125         log.info( "redirect server port {}", redirectPort );
126
127
128
129     }
130
131     @After
132     public void tearDown()
133         throws Exception
134     {
135         super.tearDown();
136         if ( this.redirectServer != null )
137         {
138             this.redirectServer.stop();
139         }
140     }
141
142     @Test
143     public void downloadWithRemoteRedirect()
144         throws Exception
145     {
146         RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
147         remoteRepository.setUrl( "http://localhost:" + redirectPort );
148         getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
149
150         RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
151
152         if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
153                                                          "internal" ) )
154         {
155             roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
156         }
157
158         getUserService( authorizationHeader ).createGuestUser();
159         roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
160
161         roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
162                                                    "guest" );
163
164         getUserService( authorizationHeader ).removeFromCache( "guest" );
165
166         File file = new File( "target/junit-4.9.jar" );
167         if ( file.exists() )
168         {
169             file.delete();
170         }
171
172         HttpWagon httpWagon = new HttpWagon();
173         httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
174
175         httpWagon.get( "/repository/internal/junit/junit/4.9/junit-4.9.jar", file );
176
177         ZipFile zipFile = new ZipFile( file );
178         List<String> entries = getZipEntriesNames( zipFile );
179         ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
180         assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
181                            + FileUtils.readFileToString( file ), zipEntry );
182         zipFile.close();
183         file.deleteOnExit();
184     }
185
186     private List<String> getZipEntriesNames( ZipFile zipFile )
187     {
188         try
189         {
190             List<String> entriesNames = new ArrayList<String>();
191             Enumeration<? extends ZipEntry> entries = zipFile.entries();
192             while ( entries.hasMoreElements() )
193             {
194                 entriesNames.add( entries.nextElement().getName() );
195             }
196             return entriesNames;
197         }
198         catch ( Throwable e )
199         {
200             log.info( "fail to get zipEntries {}", e.getMessage(), e );
201         }
202         return Collections.emptyList();
203     }
204
205
206     public static class RedirectServlet
207         extends HttpServlet
208     {
209         @Override
210         protected void doGet( HttpServletRequest req, HttpServletResponse resp )
211             throws ServletException, IOException
212         {
213
214             LoggerFactory.getLogger( getClass() ).info( "redirect servlet receive: {}", req.getRequestURI() );
215             resp.setStatus( 302 );
216             resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
217                                         + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
218                                         + "<p>The document has moved <a href=\"http://repo.maven.apache.org/maven2/junit/junit/4.9/junit-4.9.jar\">here</a>.</p>\n"
219                                         + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
220                                         + "<html><head>\n" );
221             resp.sendRedirect( "http://localhost:" + getServletContext().getAttribute( "redirectToPort" ) + "/maven2/"
222                                    + req.getRequestURI() );
223         }
224     }
225
226     public static class RepoServlet
227         extends HttpServlet
228     {
229         @Override
230         protected void doGet( HttpServletRequest req, HttpServletResponse resp )
231             throws ServletException, IOException
232         {
233             File jar = new File( System.getProperty( "basedir" ), "src/test/junit-4.9.jar" );
234             IOUtils.copy( new FileInputStream( jar ), resp.getOutputStream() );
235
236         }
237     }
238
239
240 }