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