]> source.dussan.org Git - archiva.git/blob
fc377c439ca635718a7aa25dd62e8bf412007119
[archiva.git] /
1 package org.apache.maven.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 java.io.File;
23 import java.io.IOException;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
33 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
34 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
35 import org.apache.maven.archiva.model.ArtifactReference;
36 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
37 import org.apache.maven.archiva.policies.ChecksumPolicy;
38 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
39 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
40 import org.apache.maven.archiva.policies.ReleasesPolicy;
41 import org.apache.maven.archiva.policies.SnapshotsPolicy;
42 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
43 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
44 import org.mortbay.jetty.Handler;
45 import org.mortbay.jetty.Request;
46 import org.mortbay.jetty.Server;
47 import org.mortbay.jetty.handler.AbstractHandler;
48
49 /**
50  * Integration test for connecting over a HTTP proxy.
51  * 
52  * @version $Id: ManagedDefaultTransferTest.java 677852 2008-07-18 08:16:24Z brett $
53  */
54 public class HttpProxyTransferTest
55     extends PlexusInSpringTestCase
56 {
57     private static final String PROXY_ID = "proxy";
58
59     private static final String MANAGED_ID = "default-managed-repository";
60
61     private static final String PROXIED_ID = "proxied1";
62
63     private static final String PROXIED_BASEDIR = "src/test/repositories/proxied1";
64
65     private RepositoryProxyConnectors proxyHandler;
66
67     private ArchivaConfiguration config;
68
69     private ManagedRepositoryContent managedDefaultRepository;
70
71     private Server server;
72
73     @Override
74     protected void setUp()
75         throws Exception
76     {
77         super.setUp();
78      
79         // Setup source repository (using default layout)
80         String repoPath = "target/test-repository/managed/" + getName();
81
82         File destRepoDir = new File( repoPath );
83
84         // Cleanout destination dirs.
85         if ( destRepoDir.exists() )
86         {
87             FileUtils.deleteDirectory( destRepoDir );
88         }
89
90         // Make the destination dir.
91         destRepoDir.mkdirs();
92
93         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
94         repo.setId( MANAGED_ID );
95         repo.setName( "Default Managed Repository" );
96         repo.setLocation( repoPath );
97         repo.setLayout( "default" );
98
99         ManagedRepositoryContent repoContent =
100             (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
101         repoContent.setRepository( repo );
102         managedDefaultRepository = repoContent;
103
104         config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
105         config.getConfiguration().addManagedRepository( repo );
106
107         Handler handler = new AbstractHandler()
108         {
109             public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
110                 throws IOException, ServletException
111             {
112                 response.setContentType( "text/plain" );
113                 response.setStatus( HttpServletResponse.SC_OK );
114                 response.getWriter().print( "get-default-layout-1.0.jar\n\n" );
115                 assertNotNull( request.getHeader( "Proxy-Connection" ) );
116                 
117                 ( (Request) request ).setHandled( true );
118             }
119         };
120
121         server = new Server( 0 );
122         server.setHandler( handler );
123         server.start();
124
125         int port = server.getConnectors()[0].getLocalPort();
126
127         NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
128         proxyConfig.setHost( "localhost" );
129         proxyConfig.setPort( port );
130         proxyConfig.setProtocol( "http" );
131         proxyConfig.setId( PROXY_ID );
132         config.getConfiguration().addNetworkProxy( proxyConfig );
133         
134         // Setup target (proxied to) repository.
135         RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
136
137         repoConfig.setId( PROXIED_ID );
138         repoConfig.setName( "Proxied Repository 1" );
139         repoConfig.setLayout( "default" );
140         repoConfig.setUrl( "http://www.example.com/" );
141
142         config.getConfiguration().addRemoteRepository( repoConfig );
143
144         // Setup the proxy handler.
145         proxyHandler = (RepositoryProxyConnectors) lookup( RepositoryProxyConnectors.class.getName() );
146     }
147
148     @Override
149     protected void tearDown()
150         throws Exception
151     {
152         super.tearDown();
153         
154         server.stop();
155     }
156
157     public void testGetOverHttpProxy()
158         throws Exception
159     {
160         assertNull( System.getProperty( "http.proxyHost" ) );
161         assertNull( System.getProperty( "http.proxyPort" ) );
162         
163         String path = "org/apache/maven/test/get-default-layout/1.0/get-default-layout-1.0.jar";
164
165         // Configure Connector (usually done within archiva.xml configuration)
166         addConnector();
167
168         File expectedFile = new File( new File( managedDefaultRepository.getRepoRoot() ), path );
169         ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
170
171         // Attempt the proxy fetch.
172         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
173
174         File sourceFile = new File( PROXIED_BASEDIR, path );
175         assertNotNull( "Expected File should not be null.", expectedFile );
176         assertNotNull( "Actual File should not be null.", downloadedFile );
177
178         assertTrue( "Check actual file exists.", downloadedFile.exists() );
179         assertEquals( "Check filename path is appropriate.", expectedFile.getCanonicalPath(),
180                       downloadedFile.getCanonicalPath() );
181         assertEquals( "Check file path matches.", expectedFile.getAbsolutePath(), downloadedFile.getAbsolutePath() );
182
183         String expectedContents = FileUtils.readFileToString( sourceFile, null );
184         String actualContents = FileUtils.readFileToString( downloadedFile, null );
185         assertEquals( "Check file contents.", expectedContents, actualContents );
186         
187         assertNull( System.getProperty( "http.proxyHost" ) );
188         assertNull( System.getProperty( "http.proxyPort" ) );
189     }
190
191     private void addConnector()
192     {
193         ProxyConnectorConfiguration connectorConfig = new ProxyConnectorConfiguration();
194         connectorConfig.setProxyId( PROXY_ID );
195         connectorConfig.setSourceRepoId( MANAGED_ID );
196         connectorConfig.setTargetRepoId( PROXIED_ID );
197         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.FIX );
198         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ONCE );
199         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.ONCE );
200         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
201         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS,
202                                    PropagateErrorsDownloadPolicy.QUEUE );
203         connectorConfig.addPolicy( ProxyConnectorConfiguration.POLICY_PROPAGATE_ERRORS_ON_UPDATE,
204                                    PropagateErrorsOnUpdateDownloadPolicy.NOT_PRESENT );
205
206         int count = config.getConfiguration().getProxyConnectors().size();
207         config.getConfiguration().addProxyConnector( connectorConfig );
208
209         // Proper Triggering ...
210         String prefix = "proxyConnectors.proxyConnector(" + count + ")";
211         ( (MockConfiguration) config ).triggerChange( prefix + ".sourceRepoId", connectorConfig.getSourceRepoId() );
212     }
213 }