1 package org.apache.archiva.rest.services;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.admin.model.AuditInformation;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
25 import org.apache.archiva.admin.model.beans.ProxyConnector;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
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.model.facets.AuditEvent;
33 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
34 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
35 import org.apache.archiva.redback.configuration.UserConfiguration;
36 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
37 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
38 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
39 import org.apache.archiva.redback.users.User;
40 import org.apache.archiva.repository.RepositoryContentFactory;
41 import org.apache.archiva.repository.RepositoryException;
42 import org.apache.archiva.repository.events.AuditListener;
43 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
44 import org.apache.archiva.rest.services.utils.ArtifactBuilder;
45 import org.apache.archiva.scheduler.repository.model.RepositoryArchivaTaskScheduler;
46 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
47 import org.apache.archiva.security.AccessDeniedException;
48 import org.apache.archiva.security.ArchivaSecurityException;
49 import org.apache.archiva.security.PrincipalNotFoundException;
50 import org.apache.archiva.security.UserRepositories;
51 import org.apache.commons.lang.StringUtils;
52 import org.modelmapper.ModelMapper;
53 import org.modelmapper.PropertyMap;
54 import org.modelmapper.convention.MatchingStrategies;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.context.ApplicationContext;
59 import javax.inject.Inject;
60 import javax.inject.Named;
61 import javax.servlet.http.HttpServletRequest;
62 import javax.servlet.http.HttpServletResponse;
63 import javax.ws.rs.core.Context;
64 import javax.ws.rs.core.Response;
68 * abstract class with common utilities methods
70 * @author Olivier Lamy
73 public abstract class AbstractRestService
76 protected final Logger log = LoggerFactory.getLogger( getClass() );
79 private List<AuditListener> auditListeners = new ArrayList<>();
82 protected UserRepositories userRepositories;
86 * FIXME: this could be multiple implementations and needs to be configured.
89 @Named(value = "repositorySessionFactory")
90 protected RepositorySessionFactory repositorySessionFactory;
93 protected ArchivaAdministration archivaAdministration;
96 protected ProxyConnectorAdmin proxyConnectorAdmin;
99 protected ManagedRepositoryAdmin managedRepositoryAdmin;
102 protected RepositoryContentFactory repositoryContentFactory;
105 @Named(value = "archivaTaskScheduler#repository")
106 protected RepositoryArchivaTaskScheduler repositoryTaskScheduler;
110 @Named(value = "userConfiguration#default")
111 protected UserConfiguration config;
114 protected HttpServletRequest httpServletRequest;
117 protected HttpServletResponse httpServletResponse;
119 protected AuditInformation getAuditInformation()
121 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
122 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
123 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
124 return new AuditInformation( user, remoteAddr );
127 public List<AuditListener> getAuditListeners()
129 return auditListeners;
132 public void setAuditListeners( List<AuditListener> auditListeners )
134 this.auditListeners = auditListeners;
137 protected List<String> getObservableRepos()
141 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
142 return ids == null ? Collections.<String>emptyList() : ids;
144 catch ( PrincipalNotFoundException e )
146 log.warn( e.getMessage(), e );
148 catch ( AccessDeniedException e )
150 log.warn( e.getMessage(), e );
152 catch ( ArchivaSecurityException e )
154 log.warn( e.getMessage(), e );
156 return Collections.emptyList();
159 protected String getPrincipal()
161 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
163 return redbackRequestInformation == null
164 ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
165 : ( redbackRequestInformation.getUser() == null
166 ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
167 : redbackRequestInformation.getUser().getUsername() );
170 protected String getBaseUrl()
171 throws RepositoryAdminException
173 String applicationUrl = archivaAdministration.getUiConfiguration().getApplicationUrl();
174 if ( StringUtils.isNotBlank( applicationUrl ) )
176 return applicationUrl;
178 return httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + (
179 httpServletRequest.getServerPort() == 80 ? "" : ":" + httpServletRequest.getServerPort() )
180 + httpServletRequest.getContextPath();
183 protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
185 //TODO do some caching here !!!
186 // olamy : with plexus we get only roleHint
187 // as per convention we named spring bean role#hint remove role# if exists
188 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
190 Map<String, T> beans = new HashMap<>( springBeans.size() );
192 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
194 String key = StringUtils.contains( entry.getKey(), '#' )
195 ? StringUtils.substringAfterLast( entry.getKey(), "#" )
197 beans.put( key, entry.getValue() );
202 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
204 AuditEvent auditEvent = new AuditEvent( repositoryId, getPrincipal(), filePath, action );
205 AuditInformation auditInformation = getAuditInformation();
206 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
207 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
208 for ( AuditListener auditListener : getAuditListeners() )
210 auditListener.auditEvent( auditEvent );
218 protected String getArtifactUrl( Artifact artifact )
219 throws ArchivaRestServiceException
221 return getArtifactUrl( artifact, null );
225 protected String getArtifactUrl( Artifact artifact, String repositoryId )
226 throws ArchivaRestServiceException
231 if ( httpServletRequest == null )
236 StringBuilder sb = new StringBuilder( getBaseUrl() );
238 sb.append( "/repository" );
240 // when artifact come from a remote repository when have here the remote repo id
241 // we must replace it with a valid managed one available for the user.
242 if ( StringUtils.isEmpty( repositoryId ) )
244 List<String> userRepos = userRepositories.getObservableRepositoryIds( getPrincipal() );
245 // is it a good one? if yes nothing to
246 // if not search the repo who is proxy for this remote
247 if ( !userRepos.contains( artifact.getContext() ) )
249 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorAdmin.getProxyConnectorAsMap().entrySet() )
251 for ( ProxyConnector proxyConnector : entry.getValue() )
253 if ( StringUtils.equals( "remote-" + proxyConnector.getTargetRepoId(),
254 artifact.getContext() ) //
255 && userRepos.contains( entry.getKey() ) )
257 sb.append( '/' ).append( entry.getKey() );
265 sb.append( '/' ).append( artifact.getContext() );
272 sb.append( '/' ).append( repositoryId );
275 sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
276 sb.append( '/' ).append( artifact.getArtifactId() );
277 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
279 sb.append( '/' ).append( VersionUtil.getBaseVersion( artifact.getVersion() ) );
283 sb.append( '/' ).append( artifact.getVersion() );
285 sb.append( '/' ).append( artifact.getArtifactId() );
286 sb.append( '-' ).append( artifact.getVersion() );
287 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
289 sb.append( '-' ).append( artifact.getClassifier() );
291 // maven-plugin packaging is a jar
292 if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
298 sb.append( '.' ).append( artifact.getFileExtension() );
301 return sb.toString();
303 catch ( Exception e )
305 throw new ArchivaRestServiceException( e.getMessage(),
306 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
310 protected List<Artifact> buildArtifacts( Collection<ArtifactMetadata> artifactMetadatas, String repositoryId )
311 throws ArchivaRestServiceException
315 if ( artifactMetadatas != null && !artifactMetadatas.isEmpty() )
317 List<Artifact> artifacts = new ArrayList<>( artifactMetadatas.size() );
318 for ( ArtifactMetadata artifact : artifactMetadatas )
321 String repoId = repositoryId != null ? repositoryId : artifact.getRepositoryId();
322 if ( repoId == null ) {
323 throw new IllegalStateException( "Repository Id is null" );
326 ArtifactBuilder builder =
327 new ArtifactBuilder().forArtifactMetadata( artifact ).withManagedRepositoryContent(
328 repositoryContentFactory.getManagedRepositoryContent( repoId ) );
329 Artifact art = builder.build();
330 art.setUrl( getArtifactUrl( art, repositoryId ) );
331 artifacts.add( art );
335 return Collections.emptyList();
337 catch ( RepositoryException e )
339 log.error( e.getMessage(), e );
340 throw new ArchivaRestServiceException( e.getMessage(),
341 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
345 protected Boolean doScanRepository( String repositoryId, boolean fullScan )
347 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
349 log.info( "scanning of repository with id {} already scheduled", repositoryId );
350 return Boolean.FALSE;
352 RepositoryTask task = new RepositoryTask();
353 task.setRepositoryId( repositoryId );
354 task.setScanAll( fullScan );
357 repositoryTaskScheduler.queueTask( task );
359 catch ( TaskQueueException e )
361 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
367 private static class ModelMapperHolder
369 private static ModelMapper MODEL_MAPPER = new ModelMapper();
373 MODEL_MAPPER.addMappings( new SearchResultHitMap() );
374 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
379 private static class SearchResultHitMap
380 extends PropertyMap<SearchResultHit, Artifact>
383 protected void configure()
385 skip().setId( null );
391 protected ModelMapper getModelMapper()
393 return ModelMapperHolder.MODEL_MAPPER;