View Javadoc
1   package org.apache.archiva.webdav;
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  
23  import com.gargoylesoftware.htmlunit.WebClient;
24  import org.apache.archiva.configuration.ProxyConnectorConfiguration;
25  import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26  import org.apache.archiva.policies.CachedFailuresPolicy;
27  import org.apache.archiva.policies.ChecksumPolicy;
28  import org.apache.archiva.policies.ReleasesPolicy;
29  import org.apache.archiva.policies.SnapshotsPolicy;
30  import org.apache.commons.io.FileUtils;
31  import org.eclipse.jetty.server.Server;
32  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
33  import org.eclipse.jetty.servlet.DefaultServlet;
34  import org.eclipse.jetty.servlet.ServletContextHandler;
35  import org.eclipse.jetty.servlet.ServletHolder;
36  import org.junit.After;
37  import org.junit.Before;
38  
39  import javax.servlet.http.HttpServletResponse;
40  import java.io.File;
41  import java.nio.charset.Charset;
42  
43  import static org.assertj.core.api.Assertions.assertThat;
44  import org.junit.Rule;
45  
46  /**
47   * AbstractRepositoryServletProxiedTestCase
48   */
49  public abstract class AbstractRepositoryServletProxiedTestCase
50      extends AbstractRepositoryServletTestCase
51  {
52      class RemoteRepoInfo
53      {
54          public String id;
55  
56          public String url;
57  
58          public String context;
59  
60          public Server server;
61  
62          public File root;
63  
64          public RemoteRepositoryConfiguration config;
65      }
66  
67      protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
68  
69      protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
70  
71      protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
72  
73      protected static final long ONE_DAY = ( ONE_HOUR * 24 );
74  
75      protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
76  
77      protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
78  
79      protected static final long OLDER = ( -1 );
80  
81      protected static final long NEWER = 0;
82  
83      protected static final int EXPECT_MANAGED_CONTENTS = 1;
84  
85      protected static final int EXPECT_REMOTE_CONTENTS = 2;
86  
87      protected static final int EXPECT_NOT_FOUND = 3;
88  
89      protected static final boolean HAS_MANAGED_COPY = true;
90  
91      protected static final boolean NO_MANAGED_COPY = false;
92  
93      protected RemoteRepoInfo remoteCentral;
94  
95      protected RemoteRepoInfo remoteSnapshots;
96  
97      @Rule
98      public ArchivaTemporaryFolderRule repoRootInternali = new ArchivaTemporaryFolderRule();
99      
100     @Before
101     @Override
102     public void setUp()
103         throws Exception
104     {
105         super.setUp();
106         startRepository();
107     }
108 
109     @Override
110     @After
111     public void tearDown()
112         throws Exception
113     {
114         shutdownServer( remoteCentral );
115         shutdownServer( remoteSnapshots );
116         super.tearDown();
117     }
118 
119     protected RemoteRepoInfo createServer( String id )
120         throws Exception
121     {
122         RemoteRepoInfo repo = new RemoteRepoInfo();
123         repo.id = id;
124         repo.context = "/" + id;
125         repo.root = repoRootInternali.getRoot();/*Files.createTempDirectory(
126             "temp" ).toFile();*/// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" );
127 
128         // Remove exising root contents.
129         if ( repo.root.exists() )
130         {
131             FileUtils.deleteDirectory( repo.root );
132         }
133 
134         // Establish root directory.
135         if ( !repo.root.exists() )
136         {
137             repo.root.mkdirs();
138         }
139 
140         repo.server = new Server( 0 );
141         ContextHandlerCollection contexts = new ContextHandlerCollection();
142         repo.server.setHandler( contexts );
143 
144         ServletContextHandler context = new ServletContextHandler();
145         context.setContextPath( repo.context );
146         context.setResourceBase( repo.root.getAbsolutePath() );
147         context.setAttribute( "dirAllowed", true );
148         context.setAttribute( "maxCacheSize", 0 );
149 
150         ServletHolder sh = new ServletHolder( DefaultServlet.class );
151         context.addServlet( sh, "/" );
152 
153         contexts.addHandler( context );
154 
155         repo.server.start();
156 
157         int port = repo.server.getConnectors()[0].getLocalPort();
158         repo.url = "http://localhost:" + port + repo.context;
159         log.info( "Remote HTTP Server started on {}", repo.url );
160 
161         repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
162 
163         return repo;
164     }
165 
166     protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
167         throws Exception
168     {
169 
170         WebClient client = newClient();
171         int status = client.getPage( remoteRepo.url ).getWebResponse().getStatusCode();
172         assertThat( status ).isEqualTo( HttpServletResponse.SC_OK );
173 
174     }
175 
176     private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
177                                  String snapshotsPolicy )
178     {
179         ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
180         connector.setSourceRepoId( repoId );
181         connector.setTargetRepoId( remoteRepo.id );
182         connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
183         connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
184         connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
185         connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
186 
187         archivaConfiguration.getConfiguration().addProxyConnector( connector );
188     }
189 
190     protected void shutdownServer( RemoteRepoInfo remoteRepo )
191     {
192         if ( remoteRepo != null )
193         {
194             if ( remoteRepo.server != null )
195             {
196                 if ( remoteRepo.server.isRunning() )
197                 {
198                     try
199                     {
200                         remoteRepo.server.stop();
201                         // int graceful = remoteRepo.server.getGracefulShutdown();
202                         // System.out.println( "server set to graceful shutdown: " + graceful );
203                         // remoteRepo = null;
204                     }
205                     catch ( Exception e )
206                     {
207                         e.printStackTrace( System.err );
208                     }
209                 }
210             }
211         }
212     }
213 
214     protected File populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
215         throws Exception
216     {
217         File destFile = new File( remoteRepo.root, path );
218         if ( destFile.exists() )
219         {
220             destFile.delete();
221         }
222         destFile.getParentFile().mkdirs();
223         FileUtils.writeStringToFile( destFile, contents, Charset.defaultCharset() );
224         return destFile;
225     }
226 
227     protected void setupCentralRemoteRepo()
228         throws Exception
229     {
230         remoteCentral = createServer( "central" );
231 
232         assertServerSetupCorrectly( remoteCentral );
233 
234         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
235             archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
236         if ( remoteRepositoryConfiguration != null )
237         {
238             archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
239         }
240 
241         archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
242         setupCleanRepo( remoteCentral.root );
243     }
244 
245     protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
246     {
247         setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
248     }
249 
250     protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
251     {
252         setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
253     }
254 
255     protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
256     {
257         setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
258     }
259 
260     protected void setupSnapshotsRemoteRepo()
261         throws Exception
262     {
263         remoteSnapshots = createServer( "snapshots" );
264 
265         assertServerSetupCorrectly( remoteSnapshots );
266         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
267             archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
268         if ( remoteRepositoryConfiguration != null )
269         {
270             archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
271         }
272         archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
273         setupCleanRepo( remoteSnapshots.root );
274     }
275 
276 
277 }