1 package org.apache.archiva.proxy;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
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;
67 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
69 import static org.junit.Assert.*;
72 * Integration test for connecting over a HTTP proxy.
76 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
77 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
78 public class HttpProxyTransferTest
80 private static final String PROXY_ID = "proxy";
82 private static final String MANAGED_ID = "default-managed-repository";
84 private static final String PROXIED_ID = "proxied1";
86 private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
88 private RepositoryProxyConnectors proxyHandler;
90 private ArchivaConfiguration config;
92 private ManagedRepositoryContent managedDefaultRepository;
95 private ApplicationContext applicationContext;
97 private Server server;
103 proxyHandler = applicationContext.getBean( "repositoryProxyConnectors#test", RepositoryProxyConnectors.class );
105 config = applicationContext.getBean( "archivaConfiguration#mock", ArchivaConfiguration.class );
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();
111 // Setup source repository (using default layout)
112 String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
114 Path destRepoDir = Paths.get( repoPath );
116 // Cleanout destination dirs.
117 if ( Files.exists(destRepoDir))
119 FileUtils.deleteDirectory( destRepoDir.toFile() );
122 // Make the destination dir.
123 Files.createDirectories(destRepoDir);
125 MavenManagedRepository repo = new MavenManagedRepository( MANAGED_ID, "Default Managed Repository" );
126 repo.setLocation( new URI(repoPath) );
127 repo.setLayout( "default" );
129 ManagedRepositoryContent repoContent =
130 applicationContext.getBean( "managedRepositoryContent#default", ManagedRepositoryContent.class );
132 repoContent.setRepository( repo );
133 managedDefaultRepository = repoContent;
135 ( (DefaultManagedRepositoryAdmin) applicationContext.getBean(
136 ManagedRepositoryAdmin.class ) ).setArchivaConfiguration( config );
138 RepositoryRegistry managedRepositoryAdmin = applicationContext.getBean( RepositoryRegistry.class );
139 if ( managedRepositoryAdmin.getManagedRepository( repo.getId() ) == null )
141 managedRepositoryAdmin.putRepository( repo );
144 //config.getConfiguration().addManagedRepository( repo );
146 Handler handler = new AbstractHandler()
149 public void handle( String s, Request request, HttpServletRequest httpServletRequest,
150 HttpServletResponse response )
151 throws IOException, ServletException
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" ) );
158 ( (Request) request ).setHandled( true );
161 public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
162 throws IOException, ServletException
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" ) );
169 ( (Request) request ).setHandled( true );
173 server = new Server( );
174 ServerConnector serverConnector = new ServerConnector( server, new HttpConnectionFactory());
175 server.addConnector( serverConnector );
176 server.setHandler( handler );
179 int port = serverConnector.getLocalPort();
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 );
188 // Setup target (proxied to) repository.
189 RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
191 repoConfig.setId( PROXIED_ID );
192 repoConfig.setName( "Proxied Repository 1" );
193 repoConfig.setLayout( "default" );
194 repoConfig.setUrl( "http://www.example.com/" );
196 config.getConfiguration().addRemoteRepository( repoConfig );
201 public void tearDown()
208 public void testGetOverHttpProxy()
211 Assertions.assertThat( System.getProperty( "http.proxyHost" ) ).isEmpty();
212 Assertions.assertThat( System.getProperty( "http.proxyPort" ) ).isEmpty();
214 String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
216 // Configure Connector (usually done within archiva.xml configuration)
219 Path expectedFile = Paths.get( managedDefaultRepository.getRepoRoot() ).resolve( path );
220 ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
222 // Attempt the proxy fetch.
223 Path downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
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 );
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));
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 );
237 Assertions.assertThat( System.getProperty( "http.proxyHost" ) ).isEmpty();
238 Assertions.assertThat( System.getProperty( "http.proxyPort" ) ).isEmpty();
241 private void addConnector()
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 );
256 int count = config.getConfiguration().getProxyConnectors().size();
257 config.getConfiguration().addProxyConnector( connectorConfig );
259 // Proper Triggering ...
260 String prefix = "proxyConnectors.proxyConnector(" + count + ")";
261 ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );