You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractRestService.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package org.apache.archiva.rest.services;
  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. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.AuditInformation;
  21. import org.apache.archiva.admin.model.RepositoryAdminException;
  22. import org.apache.archiva.admin.model.admin.ArchivaAdministration;
  23. import org.apache.archiva.admin.model.beans.ProxyConnector;
  24. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  25. import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
  26. import org.apache.archiva.metadata.model.facets.AuditEvent;
  27. import org.apache.archiva.repository.events.AuditListener;
  28. import org.apache.archiva.common.utils.VersionUtil;
  29. import org.apache.archiva.indexer.search.SearchResultHit;
  30. import org.apache.archiva.maven2.model.Artifact;
  31. import org.apache.archiva.metadata.model.ArtifactMetadata;
  32. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  33. import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
  34. import org.apache.archiva.redback.configuration.UserConfiguration;
  35. import org.apache.archiva.redback.configuration.UserConfigurationKeys;
  36. import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
  37. import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
  38. import org.apache.archiva.redback.users.User;
  39. import org.apache.archiva.repository.RepositoryContentFactory;
  40. import org.apache.archiva.repository.RepositoryException;
  41. import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
  42. import org.apache.archiva.rest.services.utils.ArtifactBuilder;
  43. import org.apache.archiva.scheduler.repository.DefaultRepositoryArchivaTaskScheduler;
  44. import org.apache.archiva.scheduler.repository.model.RepositoryTask;
  45. import org.apache.archiva.security.AccessDeniedException;
  46. import org.apache.archiva.security.ArchivaSecurityException;
  47. import org.apache.archiva.security.PrincipalNotFoundException;
  48. import org.apache.archiva.security.UserRepositories;
  49. import org.apache.commons.lang.StringUtils;
  50. import org.modelmapper.ModelMapper;
  51. import org.modelmapper.PropertyMap;
  52. import org.modelmapper.convention.MatchingStrategies;
  53. import org.slf4j.Logger;
  54. import org.slf4j.LoggerFactory;
  55. import org.springframework.context.ApplicationContext;
  56. import javax.inject.Inject;
  57. import javax.inject.Named;
  58. import javax.servlet.http.HttpServletRequest;
  59. import javax.servlet.http.HttpServletResponse;
  60. import javax.ws.rs.core.Context;
  61. import javax.ws.rs.core.Response;
  62. import java.util.ArrayList;
  63. import java.util.Collections;
  64. import java.util.HashMap;
  65. import java.util.List;
  66. import java.util.Map;
  67. /**
  68. * abstract class with common utilities methods
  69. *
  70. * @author Olivier Lamy
  71. * @since 1.4-M1
  72. */
  73. public abstract class AbstractRestService
  74. {
  75. protected Logger log = LoggerFactory.getLogger( getClass() );
  76. @Inject
  77. private List<AuditListener> auditListeners = new ArrayList<>();
  78. @Inject
  79. protected UserRepositories userRepositories;
  80. /**
  81. * FIXME: this could be multiple implementations and needs to be configured.
  82. */
  83. @Inject
  84. @Named(value = "repositorySessionFactory")
  85. protected RepositorySessionFactory repositorySessionFactory;
  86. @Inject
  87. protected ArchivaAdministration archivaAdministration;
  88. @Inject
  89. protected ProxyConnectorAdmin proxyConnectorAdmin;
  90. @Inject
  91. protected ManagedRepositoryAdmin managedRepositoryAdmin;
  92. @Inject
  93. protected RepositoryContentFactory repositoryContentFactory;
  94. @Inject
  95. @Named(value = "archivaTaskScheduler#repository")
  96. protected DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
  97. @Inject
  98. @Named(value = "userConfiguration#default")
  99. protected UserConfiguration config;
  100. @Context
  101. protected HttpServletRequest httpServletRequest;
  102. @Context
  103. protected HttpServletResponse httpServletResponse;
  104. protected AuditInformation getAuditInformation()
  105. {
  106. RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
  107. User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
  108. String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
  109. return new AuditInformation( user, remoteAddr );
  110. }
  111. public List<AuditListener> getAuditListeners()
  112. {
  113. return auditListeners;
  114. }
  115. public void setAuditListeners( List<AuditListener> auditListeners )
  116. {
  117. this.auditListeners = auditListeners;
  118. }
  119. protected List<String> getObservableRepos()
  120. {
  121. try
  122. {
  123. List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
  124. return ids == null ? Collections.<String>emptyList() : ids;
  125. }
  126. catch ( PrincipalNotFoundException e )
  127. {
  128. log.warn( e.getMessage(), e );
  129. }
  130. catch ( AccessDeniedException e )
  131. {
  132. log.warn( e.getMessage(), e );
  133. }
  134. catch ( ArchivaSecurityException e )
  135. {
  136. log.warn( e.getMessage(), e );
  137. }
  138. return Collections.emptyList();
  139. }
  140. protected String getPrincipal()
  141. {
  142. RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
  143. return redbackRequestInformation == null
  144. ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
  145. : ( redbackRequestInformation.getUser() == null
  146. ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
  147. : redbackRequestInformation.getUser().getUsername() );
  148. }
  149. protected String getBaseUrl()
  150. throws RepositoryAdminException
  151. {
  152. String applicationUrl = archivaAdministration.getUiConfiguration().getApplicationUrl();
  153. if ( StringUtils.isNotBlank( applicationUrl ) )
  154. {
  155. return applicationUrl;
  156. }
  157. return httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + (
  158. httpServletRequest.getServerPort() == 80 ? "" : ":" + httpServletRequest.getServerPort() )
  159. + httpServletRequest.getContextPath();
  160. }
  161. protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
  162. {
  163. //TODO do some caching here !!!
  164. // olamy : with plexus we get only roleHint
  165. // as per convention we named spring bean role#hint remove role# if exists
  166. Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
  167. Map<String, T> beans = new HashMap<>( springBeans.size() );
  168. for ( Map.Entry<String, T> entry : springBeans.entrySet() )
  169. {
  170. String key = StringUtils.contains( entry.getKey(), '#' )
  171. ? StringUtils.substringAfterLast( entry.getKey(), "#" )
  172. : entry.getKey();
  173. beans.put( key, entry.getValue() );
  174. }
  175. return beans;
  176. }
  177. protected void triggerAuditEvent( String repositoryId, String filePath, String action )
  178. {
  179. AuditEvent auditEvent = new AuditEvent( repositoryId, getPrincipal(), filePath, action );
  180. AuditInformation auditInformation = getAuditInformation();
  181. auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
  182. auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
  183. for ( AuditListener auditListener : getAuditListeners() )
  184. {
  185. auditListener.auditEvent( auditEvent );
  186. }
  187. }
  188. /**
  189. * @param artifact
  190. * @return
  191. */
  192. protected String getArtifactUrl( Artifact artifact )
  193. throws ArchivaRestServiceException
  194. {
  195. return getArtifactUrl( artifact, null );
  196. }
  197. protected String getArtifactUrl( Artifact artifact, String repositoryId )
  198. throws ArchivaRestServiceException
  199. {
  200. try
  201. {
  202. if ( httpServletRequest == null )
  203. {
  204. return null;
  205. }
  206. StringBuilder sb = new StringBuilder( getBaseUrl() );
  207. sb.append( "/repository" );
  208. // when artifact come from a remote repository when have here the remote repo id
  209. // we must replace it with a valid managed one available for the user.
  210. if ( StringUtils.isEmpty( repositoryId ) )
  211. {
  212. List<String> userRepos = userRepositories.getObservableRepositoryIds( getPrincipal() );
  213. // is it a good one? if yes nothing to
  214. // if not search the repo who is proxy for this remote
  215. if ( !userRepos.contains( artifact.getContext() ) )
  216. {
  217. for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorAdmin.getProxyConnectorAsMap().entrySet() )
  218. {
  219. for ( ProxyConnector proxyConnector : entry.getValue() )
  220. {
  221. if ( StringUtils.equals( "remote-" + proxyConnector.getTargetRepoId(),
  222. artifact.getContext() ) //
  223. && userRepos.contains( entry.getKey() ) )
  224. {
  225. sb.append( '/' ).append( entry.getKey() );
  226. }
  227. }
  228. }
  229. }
  230. else
  231. {
  232. sb.append( '/' ).append( artifact.getContext() );
  233. }
  234. }
  235. else
  236. {
  237. sb.append( '/' ).append( repositoryId );
  238. }
  239. sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
  240. sb.append( '/' ).append( artifact.getArtifactId() );
  241. if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
  242. {
  243. sb.append( '/' ).append( VersionUtil.getBaseVersion( artifact.getVersion() ) );
  244. }
  245. else
  246. {
  247. sb.append( '/' ).append( artifact.getVersion() );
  248. }
  249. sb.append( '/' ).append( artifact.getArtifactId() );
  250. sb.append( '-' ).append( artifact.getVersion() );
  251. if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
  252. {
  253. sb.append( '-' ).append( artifact.getClassifier() );
  254. }
  255. // maven-plugin packaging is a jar
  256. if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
  257. {
  258. sb.append( "jar" );
  259. }
  260. else
  261. {
  262. sb.append( '.' ).append( artifact.getFileExtension() );
  263. }
  264. return sb.toString();
  265. }
  266. catch ( Exception e )
  267. {
  268. throw new ArchivaRestServiceException( e.getMessage(),
  269. Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
  270. }
  271. }
  272. protected List<Artifact> buildArtifacts( List<ArtifactMetadata> artifactMetadatas, String repositoryId )
  273. throws ArchivaRestServiceException
  274. {
  275. try
  276. {
  277. if ( artifactMetadatas != null && !artifactMetadatas.isEmpty() )
  278. {
  279. List<Artifact> artifacts = new ArrayList<>( artifactMetadatas.size() );
  280. for ( ArtifactMetadata artifact : artifactMetadatas )
  281. {
  282. ArtifactBuilder builder =
  283. new ArtifactBuilder().forArtifactMetadata( artifact ).withManagedRepositoryContent(
  284. repositoryContentFactory.getManagedRepositoryContent( repositoryId ) );
  285. Artifact art = builder.build();
  286. art.setUrl( getArtifactUrl( art, repositoryId ) );
  287. artifacts.add( art );
  288. }
  289. return artifacts;
  290. }
  291. return Collections.emptyList();
  292. }
  293. catch ( RepositoryException e )
  294. {
  295. log.error( e.getMessage(), e );
  296. throw new ArchivaRestServiceException( e.getMessage(),
  297. Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
  298. }
  299. }
  300. protected Boolean doScanRepository( String repositoryId, boolean fullScan )
  301. {
  302. if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
  303. {
  304. log.info( "scanning of repository with id {} already scheduled", repositoryId );
  305. return Boolean.FALSE;
  306. }
  307. RepositoryTask task = new RepositoryTask();
  308. task.setRepositoryId( repositoryId );
  309. task.setScanAll( fullScan );
  310. try
  311. {
  312. repositoryTaskScheduler.queueTask( task );
  313. }
  314. catch ( TaskQueueException e )
  315. {
  316. log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
  317. return false;
  318. }
  319. return true;
  320. }
  321. private static class ModelMapperHolder
  322. {
  323. private static ModelMapper MODEL_MAPPER = new ModelMapper();
  324. static
  325. {
  326. MODEL_MAPPER.addMappings( new SearchResultHitMap() );
  327. MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
  328. }
  329. }
  330. private static class SearchResultHitMap
  331. extends PropertyMap<SearchResultHit, Artifact>
  332. {
  333. @Override
  334. protected void configure()
  335. {
  336. skip().setId( null );
  337. }
  338. }
  339. ;
  340. protected ModelMapper getModelMapper()
  341. {
  342. return ModelMapperHolder.MODEL_MAPPER;
  343. }
  344. }