]> source.dussan.org Git - archiva.git/blob
8b8d08e05c7593cf73931f9d3259343ffb1f4baf
[archiva.git] /
1 package org.apache.archiva.proxy;
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 org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
25 import org.apache.archiva.proxy.model.RepositoryProxyConnectors;
26 import org.apache.archiva.repository.RepositoryRegistry;
27 import org.apache.archiva.repository.maven2.MavenManagedRepository;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.archiva.configuration.ArchivaConfiguration;
30 import org.apache.archiva.configuration.NetworkProxyConfiguration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.archiva.model.ArtifactReference;
34 import org.apache.archiva.policies.CachedFailuresPolicy;
35 import org.apache.archiva.policies.ChecksumPolicy;
36 import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
37 import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
38 import org.apache.archiva.policies.ReleasesPolicy;
39 import org.apache.archiva.policies.SnapshotsPolicy;
40 import org.apache.archiva.repository.ManagedRepositoryContent;
41 import org.assertj.core.api.Assertions;
42 import org.eclipse.jetty.server.Handler;
43 import org.eclipse.jetty.server.HttpConnectionFactory;
44 import org.eclipse.jetty.server.Request;
45 import org.eclipse.jetty.server.Server;
46 import org.eclipse.jetty.server.ServerConnector;
47 import org.eclipse.jetty.server.handler.AbstractHandler;
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 javax.servlet.ServletException;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59 import java.io.IOException;
60 import java.net.URI;
61 import java.nio.charset.Charset;
62 import java.nio.file.Files;
63 import java.nio.file.Path;
64 import java.nio.file.Paths;
65 import java.util.Locale;
66
67 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
68
69 import static org.junit.Assert.*;
70
71 /**
72  * Integration test for connecting over a HTTP proxy.
73  *
74  *
75  */
76 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
77 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
78 public class HttpProxyTransferTest
79 {
80     private static final String PROXY_ID = "proxy";
81
82     private static final String MANAGED_ID = "default-managed-repository";
83
84     private static final String PROXIED_ID = "proxied1";
85
86     private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
87
88     private RepositoryProxyConnectors proxyHandler;
89
90     private ArchivaConfiguration config;
91
92     private ManagedRepositoryContent managedDefaultRepository;
93
94     @Inject
95     private ApplicationContext applicationContext;
96
97     private Server server;
98
99     @Before
100     public void setUp()
101         throws Exception
102     {
103         proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
104
105         config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
106
107         // clear from previous tests - TODO the spring context should be initialised per test instead, or the config
108         // made a complete mock
109         config.getConfiguration().getProxyConnectors().clear();
110
111         // Setup source repository (using default layout)
112         String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
113
114         Path destRepoDir = Paths.get( repoPath );
115
116         // Cleanout destination dirs.
117         if ( Files.exists(destRepoDir))
118         {
119             FileUtils.deleteDirectory( destRepoDir.toFile() );
120         }
121
122         // Make the destination dir.
123         Files.createDirectories(destRepoDir);
124
125         MavenManagedRepository repo = new MavenManagedRepository( MANAGED_ID, "Default Managed Repository" );
126         repo.setLocation( new URI(repoPath) );
127         repo.setLayout( "default" );
128
129         ManagedRepositoryContent repoContent =
130             applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
131
132         repoContent.setRepository( repo );
133         managedDefaultRepository = repoContent;
134
135         ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
136             ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
137
138         RepositoryRegistry managedRepositoryAdmin = applicationContext.getBean( RepositoryRegistry.class );
139         if ( managedRepositoryAdmin.getManagedRepository( repo.getId() ) == null )
140         {
141             managedRepositoryAdmin.putRepository( repo );
142         }
143
144         //config.getConfiguration().addManagedRepository( repo );
145
146         Handler handler = new AbstractHandler()
147         {
148             @Override
149             public void handle( String s, Request request, HttpServletRequest httpServletRequest,
150                                 HttpServletResponse response )
151                 throws IOException, ServletException
152             {
153                 response.setContentType( "text/plain" );
154                 response.setStatus( HttpServletResponse.SC_OK );
155                 response.getWriter().print( "get-default-layout-1.0.jar\n\n" );
156                 assertNotNull( request.getHeader( "Proxy-Connection" ) );
157
158                 ( (Request) request ).setHandled( true );
159             }
160
161             public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
162                 throws IOException, ServletException
163             {
164                 response.setContentType( "text/plain" );
165                 response.setStatus( HttpServletResponse.SC_OK );
166                 response.getWriter().print( "get-default-layout-1.0.jar\n\n" );
167                 assertNotNull( request.getHeader( "Proxy-Connection" ) );
168
169                 ( (Request) request ).setHandled( true );
170             }
171         };
172
173         server = new Server(  );
174         ServerConnector serverConnector = new ServerConnector( server, new HttpConnectionFactory());
175         server.addConnector( serverConnector );
176         server.setHandler( handler );
177         server.start();
178
179         int port = serverConnector.getLocalPort();
180
181         NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
182         proxyConfig.setHost( "localhost" );
183         proxyConfig.setPort( port );
184         proxyConfig.setProtocol( "http" );
185         proxyConfig.setId( PROXY_ID );
186         config.getConfiguration().addNetworkProxy( proxyConfig );
187
188         // Setup target (proxied to) repository.
189         RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
190
191         repoConfig.setId( PROXIED_ID );
192         repoConfig.setName( "Proxied Repository 1" );
193         repoConfig.setLayout( "default" );
194         repoConfig.setUrl( "http://www.example.com/" );
195
196         config.getConfiguration().addRemoteRepository( repoConfig );
197
198     }
199
200     @After
201     public void tearDown()
202         throws Exception
203     {
204         server.stop();
205     }
206
207     @Test
208     public void testGetOverHttpProxy()
209         throws Exception
210     {
211         Assertions.assertThat( System.getProperty( "http.proxyHost" ) ).isEmpty();
212         Assertions.assertThat( System.getProperty( "http.proxyPort" ) ).isEmpty();
213
214         String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
215
216         // Configure Connector (usually done within archiva.xml configuration)
217         addConnector();
218
219         Path expectedFile = Paths.get( managedDefaultRepository.getRepoRoot() ).resolve( path );
220         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
221
222         // Attempt the proxy fetch.
223         Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
224
225         Path sourceFile = Paths.get( PROXIED_BASEDIR, path );
226         assertNotNull( "Expected File should not be null.", expectedFile );
227         assertNotNull( "Actual File should not be null.", downloadedFile );
228
229         assertTrue( "Check actual file exists.", Files.exists(downloadedFile));
230         assertTrue( "Check filename path is appropriate.", Files.isSameFile( expectedFile, downloadedFile));
231         assertTrue( "Check file path matches.", Files.isSameFile( expectedFile, downloadedFile));
232
233         String expectedContents = FileUtils.readFileToString( sourceFile.toFile(), Charset.defaultCharset() );
234         String actualContents = FileUtils.readFileToString( downloadedFile.toFile(), Charset.defaultCharset() );
235         assertEquals( "Check file contents.", expectedContents, actualContents );
236
237         Assertions.assertThat( System.getProperty( "http.proxyHost" ) ).isEmpty();
238         Assertions.assertThat( System.getProperty( "http.proxyPort" ) ).isEmpty();
239     }
240
241     private void addConnector()
242     {
243         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
244         connectorConfig.setProxyId( PROXY_ID );
245         connectorConfig.setSourceRepoId( MANAGED_ID );
246         connectorConfig.setTargetRepoId( PROXIED_ID );
247         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
248         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
249         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
250         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
251         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
252                                    PropagateErrorsDownloadPolicy.QUEUE );
253         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
254                                    PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
255
256         int count = config.getConfiguration().getProxyConnectors().size();
257         config.getConfiguration().addProxyConnector( connectorConfig );
258
259         // Proper Triggering ...
260         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
261         ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
262     }
263 }