]> source.dussan.org Git - archiva.git/blob
eae41013f9dda95eb6a00b66e3244f28a9384e0c
[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.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
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.HttpConnectionFactory;
29 import org.eclipse.jetty.server.Server;
30 import org.eclipse.jetty.server.ServerConnector;
31 import org.eclipse.jetty.servlet.ServletContextHandler;
32 import org.eclipse.jetty.servlet.ServletHolder;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import javax.servlet.ServletException;
43 import javax.servlet.http.HttpServlet;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import java.io.File;
47 import java.io.IOException;
48 import java.nio.file.Files;
49 import java.util.List;
50 import java.util.zip.ZipEntry;
51 import java.util.zip.ZipFile;
52
53 /**
54  * @author Olivier Lamy
55  */
56 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
57 public class DownloadArtifactsTest
58     extends AbstractDownloadTest
59 {
60
61     protected Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
62
63     public Server redirectServer = null;
64
65     public int redirectPort;
66
67     public Server repoServer = null;
68
69     public int repoServerPort;
70
71     @BeforeClass
72     public static void setAppServerBase()
73     {
74         previousAppServerBase = System.getProperty( "appserver.base" );
75         System.setProperty( "appserver.base", "target/" + DownloadArtifactsTest.class.getName() );
76     }
77
78
79     @AfterClass
80     public static void resetAppServerBase()
81     {
82         System.setProperty( "appserver.base", previousAppServerBase );
83     }
84
85     @Override
86     protected String getSpringConfigLocation()
87     {
88         return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
89     }
90
91     @Override
92
93     @Before
94     public void startServer()
95         throws Exception
96     {
97         super.startServer();
98
99         // repo handler
100
101         this.repoServer = new Server(  );
102         ServerConnector repoServerConnector = new ServerConnector( this.repoServer, new HttpConnectionFactory());
103         this.repoServer.addConnector( repoServerConnector );
104
105         ServletHolder shRepo = new ServletHolder( RepoServlet.class );
106         ServletContextHandler contextRepo = new ServletContextHandler();
107
108         contextRepo.setContextPath( "/" );
109         contextRepo.addServlet( shRepo, "/*" );
110
111         repoServer.setHandler( contextRepo );
112
113         repoServer.start();
114         this.repoServerPort = repoServerConnector.getLocalPort();
115
116         //redirect handler
117
118         this.redirectServer = new Server( );
119         ServerConnector redirectServerConnector = new ServerConnector( this.redirectServer, new HttpConnectionFactory());
120         this.redirectServer.addConnector( redirectServerConnector );
121
122         ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
123         ServletContextHandler contextRedirect = new ServletContextHandler();
124         contextRedirect.setAttribute( "redirectToPort", Integer.toString( this.repoServerPort ) );
125
126         contextRedirect.setContextPath( "/" );
127         contextRedirect.addServlet( shRedirect, "/*" );
128
129         redirectServer.setHandler( contextRedirect );
130         redirectServer.start();
131         this.redirectPort = redirectServerConnector.getLocalPort();
132         log.info( "redirect server port {}", redirectPort );
133
134     }
135
136     @After
137     @Override
138     public void tearDown()
139         throws Exception
140     {
141         super.tearDown();
142         if ( this.redirectServer != null )
143         {
144             this.redirectServer.stop();
145         }
146     }
147
148     @Test
149     public void downloadWithRemoteRedirect()
150         throws Exception
151     {
152         RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
153         remoteRepository.setUrl( "http://localhost:" + redirectPort );
154         getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
155
156         RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
157
158         if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
159                                                          "internal" ) )
160         {
161             roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
162         }
163
164         getUserService( authorizationHeader ).createGuestUser();
165         roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
166
167         roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
168                                                    "guest" );
169
170         getUserService( authorizationHeader ).removeFromCache( "guest" );
171
172         File file = new File( "target/junit-4.9.jar" );
173         if ( file.exists() )
174         {
175             file.delete();
176         }
177
178         HttpWagon httpWagon = new HttpWagon();
179         httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
180
181         httpWagon.get( "/repository/internal/junit/junit/4.9/junit-4.9.jar", file );
182
183         ZipFile zipFile = new ZipFile( file );
184         List<String> entries = getZipEntriesNames( zipFile );
185         ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
186         assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
187                            + FileUtils.readFileToString( file ), zipEntry );
188         zipFile.close();
189         file.deleteOnExit();
190     }
191
192
193     public static class RedirectServlet
194         extends HttpServlet
195     {
196         @Override
197         protected void doGet( HttpServletRequest req, HttpServletResponse resp )
198             throws ServletException, IOException
199         {
200
201             LoggerFactory.getLogger( getClass() ).info( "redirect servlet receive: {}", req.getRequestURI() );
202             resp.setStatus( 302 );
203             resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
204                                         + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
205                                         + "<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"
206                                         + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
207                                         + "<html><head>\n" );
208             resp.sendRedirect( "http://localhost:" + getServletContext().getAttribute( "redirectToPort" ) + "/maven2/"
209                                    + req.getRequestURI() );
210         }
211     }
212
213     public static class RepoServlet
214         extends HttpServlet
215     {
216         @Override
217         protected void doGet( HttpServletRequest req, HttpServletResponse resp )
218             throws ServletException, IOException
219         {
220             File jar = new File( System.getProperty( "basedir" ), "src/test/junit-4.9.jar" );
221             Files.copy( jar.toPath(), resp.getOutputStream() );
222
223         }
224     }
225
226
227 }