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