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