Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractDownloadTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package org.apache.archiva.web.remotedownload;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
  20. import junit.framework.TestCase;
  21. import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
  22. import org.apache.archiva.redback.rest.api.model.User;
  23. import org.apache.archiva.redback.rest.api.services.RoleManagementService;
  24. import org.apache.archiva.redback.rest.api.services.UserService;
  25. import org.apache.archiva.redback.rest.services.BaseSetup;
  26. import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
  27. import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
  28. import org.apache.archiva.rest.api.services.ProxyConnectorService;
  29. import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
  30. import org.apache.archiva.rest.api.services.RepositoriesService;
  31. import org.apache.archiva.rest.api.services.RepositoryGroupService;
  32. import org.apache.archiva.rest.api.services.SearchService;
  33. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  34. import org.apache.archiva.webdav.RepositoryServlet;
  35. import org.apache.commons.io.FileUtils;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.apache.commons.lang3.SystemUtils;
  38. import org.apache.cxf.common.util.Base64Utility;
  39. import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
  40. import org.apache.cxf.jaxrs.client.WebClient;
  41. import org.apache.cxf.transport.servlet.CXFServlet;
  42. import org.eclipse.jetty.server.HttpConnectionFactory;
  43. import org.eclipse.jetty.server.Server;
  44. import org.eclipse.jetty.server.ServerConnector;
  45. import org.eclipse.jetty.server.session.SessionHandler;
  46. import org.eclipse.jetty.servlet.ServletContextHandler;
  47. import org.eclipse.jetty.servlet.ServletHolder;
  48. import org.junit.After;
  49. import org.junit.Before;
  50. import org.junit.runner.RunWith;
  51. import org.slf4j.Logger;
  52. import org.slf4j.LoggerFactory;
  53. import org.springframework.web.context.ContextLoaderListener;
  54. import java.nio.file.Files;
  55. import java.nio.file.Path;
  56. import java.nio.file.Paths;
  57. import java.util.ArrayList;
  58. import java.util.Collections;
  59. import java.util.Enumeration;
  60. import java.util.List;
  61. import java.util.concurrent.atomic.AtomicReference;
  62. import java.util.zip.ZipEntry;
  63. import java.util.zip.ZipFile;
  64. /**
  65. * @author Olivier Lamy
  66. */
  67. @RunWith( ArchivaBlockJUnit4ClassRunner.class )
  68. public abstract class AbstractDownloadTest
  69. extends TestCase
  70. {
  71. AtomicReference<Path> projectDir = new AtomicReference<>( );
  72. AtomicReference<Path> basePath = new AtomicReference<>( );
  73. protected List<Path> createdPaths = new ArrayList<>( );
  74. protected final Logger log = LoggerFactory.getLogger( getClass() );
  75. protected static String previousAppServerBase;
  76. public String authorizationHeader = getAdminAuthzHeader();
  77. public Server server = null;
  78. ServerConnector serverConnector;
  79. public int port;
  80. protected Path getProjectDirectory() {
  81. if ( projectDir.get()==null) {
  82. String propVal = System.getProperty("mvn.project.base.dir");
  83. Path newVal;
  84. if (StringUtils.isEmpty(propVal)) {
  85. newVal = Paths.get("").toAbsolutePath();
  86. } else {
  87. newVal = Paths.get(propVal).toAbsolutePath();
  88. }
  89. projectDir.compareAndSet(null, newVal);
  90. }
  91. return projectDir.get();
  92. }
  93. public Path getBasedir()
  94. {
  95. if (basePath.get()==null) {
  96. String baseDir = System.getProperty( "basedir" );
  97. final Path baseDirPath;
  98. if (StringUtils.isNotEmpty( baseDir )) {
  99. baseDirPath = Paths.get( baseDir );
  100. } else {
  101. baseDirPath = getProjectDirectory( );
  102. }
  103. basePath.compareAndSet( null, baseDirPath );
  104. }
  105. return basePath.get( );
  106. }
  107. public static String encode( String uid, String password )
  108. {
  109. return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
  110. }
  111. public static String getAdminAuthzHeader()
  112. {
  113. String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
  114. if ( StringUtils.isBlank( adminPwdSysProps ) )
  115. {
  116. return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, BaseSetup.getAdminPwd() );
  117. }
  118. return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
  119. }
  120. protected abstract String getSpringConfigLocation();
  121. protected String getRestServicesPath()
  122. {
  123. return "restServices";
  124. }
  125. @Before
  126. public void startServer()
  127. throws Exception
  128. {
  129. System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
  130. server = new Server();
  131. serverConnector = new ServerConnector( server, new HttpConnectionFactory() );
  132. server.addConnector( serverConnector );
  133. ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
  134. ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS );
  135. context.setResourceBase( SystemUtils.JAVA_IO_TMPDIR );
  136. context.setSessionHandler( new SessionHandler() );
  137. context.addServlet( servletHolder, "/" + getRestServicesPath() + "/*" );
  138. context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
  139. context.addEventListener( new ContextLoaderListener() );
  140. ServletHolder servletHolderRepo = new ServletHolder( new RepositoryServlet() );
  141. context.addServlet( servletHolderRepo, "/repository/*" );
  142. server.setHandler( context );
  143. server.start();
  144. port = serverConnector.getLocalPort();
  145. log.info( "start server on port {}", this.port );
  146. User user = new User();
  147. user.setEmail( "toto@toto.fr" );
  148. user.setFullName( "the root user" );
  149. user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
  150. user.setPassword( BaseSetup.getAdminPwd() );
  151. getUserService( null ).createAdminUser( user );
  152. }
  153. @After
  154. @Override
  155. public void tearDown()
  156. throws Exception
  157. {
  158. for(Path dir : createdPaths) {
  159. if ( Files.exists( dir)) {
  160. FileUtils.deleteQuietly( dir.toFile( ) );
  161. }
  162. }
  163. createdPaths.clear();
  164. System.clearProperty( "redback.admin.creation.file" );
  165. super.tearDown();
  166. if ( this.server != null )
  167. {
  168. this.server.stop();
  169. }
  170. }
  171. protected ProxyConnectorService getProxyConnectorService()
  172. {
  173. ProxyConnectorService service =
  174. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  175. ProxyConnectorService.class,
  176. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  177. WebClient.client( service ).header( "Authorization", authorizationHeader );
  178. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  179. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
  180. return service;
  181. }
  182. protected RemoteRepositoriesService getRemoteRepositoriesService()
  183. {
  184. RemoteRepositoriesService service =
  185. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  186. RemoteRepositoriesService.class,
  187. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  188. WebClient.client( service ).header( "Authorization", authorizationHeader );
  189. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  190. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
  191. return service;
  192. }
  193. protected ManagedRepositoriesService getManagedRepositoriesService()
  194. {
  195. ManagedRepositoriesService service =
  196. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  197. ManagedRepositoriesService.class,
  198. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  199. WebClient.client( service ).header( "Authorization", authorizationHeader );
  200. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  201. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
  202. return service;
  203. }
  204. protected RepositoryGroupService getRepositoryGroupService()
  205. {
  206. RepositoryGroupService service =
  207. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  208. RepositoryGroupService.class,
  209. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  210. WebClient.client( service ).header( "Authorization", authorizationHeader );
  211. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  212. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
  213. return service;
  214. }
  215. protected RepositoriesService getRepositoriesService()
  216. {
  217. RepositoriesService service =
  218. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  219. RepositoriesService.class,
  220. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  221. WebClient.client( service ).header( "Authorization", authorizationHeader );
  222. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  223. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
  224. return service;
  225. }
  226. protected SearchService getSearchService()
  227. {
  228. SearchService service =
  229. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  230. SearchService.class,
  231. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  232. WebClient.client( service ).header( "Authorization", authorizationHeader );
  233. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  234. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
  235. return service;
  236. }
  237. protected String getBaseUrl()
  238. {
  239. String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
  240. return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
  241. }
  242. protected RoleManagementService getRoleManagementService( String authzHeader )
  243. {
  244. RoleManagementService service =
  245. JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
  246. RoleManagementService.class,
  247. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  248. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  249. // for debuging purpose
  250. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
  251. if ( authzHeader != null )
  252. {
  253. WebClient.client( service ).header( "Authorization", authzHeader );
  254. }
  255. return service;
  256. }
  257. protected UserService getUserService( String authzHeader )
  258. {
  259. UserService service =
  260. JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
  261. UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  262. WebClient.client( service ).header( "Referer", "http://localhost:" + port );
  263. // for debuging purpose
  264. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
  265. if ( authzHeader != null )
  266. {
  267. WebClient.client( service ).header( "Authorization", authzHeader );
  268. }
  269. return service;
  270. }
  271. protected FakeCreateAdminService getFakeCreateAdminService()
  272. {
  273. return JAXRSClientFactory.create(
  274. "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
  275. FakeCreateAdminService.class );
  276. }
  277. protected List<String> getZipEntriesNames( ZipFile zipFile )
  278. {
  279. try
  280. {
  281. List<String> entriesNames = new ArrayList<>();
  282. Enumeration<? extends ZipEntry> entries = zipFile.entries();
  283. while ( entries.hasMoreElements() )
  284. {
  285. entriesNames.add( entries.nextElement().getName() );
  286. }
  287. return entriesNames;
  288. }
  289. catch ( Throwable e )
  290. {
  291. log.info( "fail to get zipEntries {}", e.getMessage(), e );
  292. }
  293. return Collections.emptyList();
  294. }
  295. }