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.proxyconnector.ProxyConnectorAdmin;
27 import org.apache.archiva.common.utils.VersionUtil;
28 import org.apache.archiva.indexer.search.SearchResultHit;
29 import org.apache.archiva.maven2.model.Artifact;
30 import org.apache.archiva.metadata.model.ArtifactMetadata;
31 import org.apache.archiva.metadata.model.facets.AuditEvent;
32 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
33 import org.apache.archiva.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.ManagedRepository;
40 import org.apache.archiva.repository.ManagedRepositoryContent;
41 import org.apache.archiva.repository.RepositoryException;
42 import org.apache.archiva.repository.RepositoryRegistry;
43 import org.apache.archiva.metadata.audit.AuditListener;
44 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
45 import org.apache.archiva.rest.services.utils.ArtifactBuilder;
46 import org.apache.archiva.scheduler.repository.model.RepositoryArchivaTaskScheduler;
47 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
48 import org.apache.archiva.security.AccessDeniedException;
49 import org.apache.archiva.security.ArchivaSecurityException;
50 import org.apache.archiva.security.PrincipalNotFoundException;
51 import org.apache.archiva.security.UserRepositories;
52 import org.apache.commons.lang3.StringUtils;
53 import org.modelmapper.ModelMapper;
54 import org.modelmapper.PropertyMap;
55 import org.modelmapper.convention.MatchingStrategies;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.springframework.context.ApplicationContext;
60 import javax.inject.Inject;
61 import javax.inject.Named;
62 import javax.servlet.http.HttpServletRequest;
63 import javax.servlet.http.HttpServletResponse;
64 import javax.ws.rs.core.Context;
65 import javax.ws.rs.core.Response;
66 import java.util.ArrayList;
67 import java.util.Collection;
68 import java.util.Collections;
69 import java.util.HashMap;
70 import java.util.List;
74 * abstract class with common utilities methods
76 * @author Olivier Lamy
79 public abstract class AbstractRestService
82 protected final Logger log = LoggerFactory.getLogger( getClass() );
85 private List<AuditListener> auditListeners = new ArrayList<>();
88 protected UserRepositories userRepositories;
92 * FIXME: this could be multiple implementations and needs to be configured.
95 @Named(value = "repositorySessionFactory")
96 protected RepositorySessionFactory repositorySessionFactory;
99 protected ArchivaAdministration archivaAdministration;
102 protected ProxyConnectorAdmin proxyConnectorAdmin;
105 protected RepositoryRegistry repositoryRegistry;
108 @Named(value = "archivaTaskScheduler#repository")
109 protected RepositoryArchivaTaskScheduler repositoryTaskScheduler;
113 @Named(value = "userConfiguration#default")
114 protected UserConfiguration config;
117 protected HttpServletRequest httpServletRequest;
120 protected HttpServletResponse httpServletResponse;
122 protected AuditInformation getAuditInformation()
124 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
125 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
126 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
127 return new AuditInformation( user, remoteAddr );
130 public List<AuditListener> getAuditListeners()
132 return auditListeners;
135 public void setAuditListeners( List<AuditListener> auditListeners )
137 this.auditListeners = auditListeners;
140 protected List<String> getObservableRepos()
144 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
145 return ids == null ? Collections.<String>emptyList() : ids;
147 catch ( PrincipalNotFoundException e )
149 log.warn( e.getMessage(), e );
151 catch ( AccessDeniedException e )
153 log.warn( e.getMessage(), e );
155 catch ( ArchivaSecurityException e )
157 log.warn( e.getMessage(), e );
159 return Collections.emptyList();
162 protected String getPrincipal()
164 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
166 return redbackRequestInformation == null
167 ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
168 : ( redbackRequestInformation.getUser() == null
169 ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
170 : redbackRequestInformation.getUser().getUsername() );
173 protected String getBaseUrl()
174 throws RepositoryAdminException
176 String applicationUrl = archivaAdministration.getUiConfiguration().getApplicationUrl();
177 if ( StringUtils.isNotBlank( applicationUrl ) )
179 return applicationUrl;
181 return httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + (
182 httpServletRequest.getServerPort() == 80 ? "" : ":" + httpServletRequest.getServerPort() )
183 + httpServletRequest.getContextPath();
186 protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
188 //TODO do some caching here !!!
189 // olamy : with plexus we get only roleHint
190 // as per convention we named spring bean role#hint remove role# if exists
191 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
193 Map<String, T> beans = new HashMap<>( springBeans.size() );
195 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
197 String key = StringUtils.contains( entry.getKey(), '#' )
198 ? StringUtils.substringAfterLast( entry.getKey(), "#" )
200 beans.put( key, entry.getValue() );
205 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
207 AuditEvent auditEvent = new AuditEvent( repositoryId, getPrincipal(), filePath, action );
208 AuditInformation auditInformation = getAuditInformation();
209 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
210 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
211 for ( AuditListener auditListener : getAuditListeners() )
213 auditListener.auditEvent( auditEvent );
221 protected String getArtifactUrl( Artifact artifact )
222 throws ArchivaRestServiceException
224 return getArtifactUrl( artifact, null );
228 protected String getArtifactUrl( Artifact artifact, String repositoryId )
229 throws ArchivaRestServiceException
234 if ( httpServletRequest == null )
239 StringBuilder sb = new StringBuilder( getBaseUrl() );
241 sb.append( "/repository" );
243 // when artifact come from a remote repository when have here the remote repo id
244 // we must replace it with a valid managed one available for the user.
245 if ( StringUtils.isEmpty( repositoryId ) )
247 List<String> userRepos = userRepositories.getObservableRepositoryIds( getPrincipal() );
248 // is it a good one? if yes nothing to
249 // if not search the repo who is proxy for this remote
250 if ( !userRepos.contains( artifact.getContext() ) )
252 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorAdmin.getProxyConnectorAsMap().entrySet() )
254 for ( ProxyConnector proxyConnector : entry.getValue() )
256 if ( StringUtils.equals( "remote-" + proxyConnector.getTargetRepoId(),
257 artifact.getContext() ) //
258 && userRepos.contains( entry.getKey() ) )
260 sb.append( '/' ).append( entry.getKey() );
268 sb.append( '/' ).append( artifact.getContext() );
275 sb.append( '/' ).append( repositoryId );
278 sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
279 sb.append( '/' ).append( artifact.getArtifactId() );
280 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
282 sb.append( '/' ).append( VersionUtil.getBaseVersion( artifact.getVersion() ) );
286 sb.append( '/' ).append( artifact.getVersion() );
288 sb.append( '/' ).append( artifact.getArtifactId() );
289 sb.append( '-' ).append( artifact.getVersion() );
290 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
292 sb.append( '-' ).append( artifact.getClassifier() );
294 // maven-plugin packaging is a jar
295 if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
301 sb.append( '.' ).append( artifact.getFileExtension() );
304 return sb.toString();
306 catch ( Exception e )
308 throw new ArchivaRestServiceException( e.getMessage(),
309 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
313 protected List<Artifact> buildArtifacts( Collection<ArtifactMetadata> artifactMetadatas, String repositoryId )
314 throws ArchivaRestServiceException
318 if ( artifactMetadatas != null && !artifactMetadatas.isEmpty() )
320 List<Artifact> artifacts = new ArrayList<>( artifactMetadatas.size() );
321 for ( ArtifactMetadata artifact : artifactMetadatas )
324 String repoId = repositoryId != null ? repositoryId : artifact.getRepositoryId();
325 if ( repoId == null ) {
326 throw new IllegalStateException( "Repository Id is null" );
328 ManagedRepository repo = repositoryRegistry.getManagedRepository( repoId );
330 throw new RepositoryException( "Repository not found "+repoId );
332 ManagedRepositoryContent content = repo.getContent( );
333 ArtifactBuilder builder =
334 new ArtifactBuilder().forArtifactMetadata( artifact ).withManagedRepositoryContent(
336 Artifact art = builder.build();
337 art.setUrl( getArtifactUrl( art, repositoryId ) );
338 artifacts.add( art );
342 return Collections.emptyList();
344 catch ( RepositoryException e )
346 log.error( e.getMessage(), e );
347 throw new ArchivaRestServiceException( e.getMessage(),
348 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
352 protected Boolean doScanRepository( String repositoryId, boolean fullScan )
354 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
356 log.info( "scanning of repository with id {} already scheduled", repositoryId );
357 return Boolean.FALSE;
359 RepositoryTask task = new RepositoryTask();
360 task.setRepositoryId( repositoryId );
361 task.setScanAll( fullScan );
364 repositoryTaskScheduler.queueTask( task );
366 catch ( TaskQueueException e )
368 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
374 private static class ModelMapperHolder
376 private static ModelMapper MODEL_MAPPER = new ModelMapper();
380 MODEL_MAPPER.addMappings( new SearchResultHitMap() );
381 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
386 private static class SearchResultHitMap
387 extends PropertyMap<SearchResultHit, Artifact>
390 protected void configure()
392 skip().setId( null );
398 protected ModelMapper getModelMapper()
400 return ModelMapperHolder.MODEL_MAPPER;