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.audit.AuditEvent;
29 import org.apache.archiva.audit.AuditListener;
30 import org.apache.archiva.common.utils.VersionUtil;
31 import org.apache.archiva.indexer.search.SearchResultHit;
32 import org.apache.archiva.maven2.model.Artifact;
33 import org.apache.archiva.metadata.model.ArtifactMetadata;
34 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
35 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
36 import org.apache.archiva.redback.configuration.UserConfiguration;
37 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
38 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
39 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
40 import org.apache.archiva.redback.users.User;
41 import org.apache.archiva.repository.RepositoryContentFactory;
42 import org.apache.archiva.repository.RepositoryException;
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.DefaultRepositoryArchivaTaskScheduler;
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;
65 import java.util.ArrayList;
66 import java.util.Collections;
67 import java.util.HashMap;
68 import java.util.List;
72 * abstract class with common utilities methods
74 * @author Olivier Lamy
77 public abstract class AbstractRestService
80 protected Logger log = LoggerFactory.getLogger( getClass() );
83 private List<AuditListener> auditListeners = new ArrayList<>();
86 protected UserRepositories userRepositories;
90 * FIXME: this could be multiple implementations and needs to be configured.
93 @Named(value = "repositorySessionFactory")
94 protected RepositorySessionFactory repositorySessionFactory;
97 protected ArchivaAdministration archivaAdministration;
100 protected ProxyConnectorAdmin proxyConnectorAdmin;
103 protected ManagedRepositoryAdmin managedRepositoryAdmin;
106 protected RepositoryContentFactory repositoryContentFactory;
109 @Named(value = "archivaTaskScheduler#repository")
110 protected DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
114 @Named(value = "userConfiguration#default")
115 protected UserConfiguration config;
118 protected HttpServletRequest httpServletRequest;
121 protected HttpServletResponse httpServletResponse;
123 protected AuditInformation getAuditInformation()
125 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
126 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
127 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
128 return new AuditInformation( user, remoteAddr );
131 public List<AuditListener> getAuditListeners()
133 return auditListeners;
136 public void setAuditListeners( List<AuditListener> auditListeners )
138 this.auditListeners = auditListeners;
141 protected List<String> getObservableRepos()
145 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
146 return ids == null ? Collections.<String>emptyList() : ids;
148 catch ( PrincipalNotFoundException e )
150 log.warn( e.getMessage(), e );
152 catch ( AccessDeniedException e )
154 log.warn( e.getMessage(), e );
156 catch ( ArchivaSecurityException e )
158 log.warn( e.getMessage(), e );
160 return Collections.emptyList();
163 protected String getPrincipal()
165 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
167 return redbackRequestInformation == null
168 ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
169 : ( redbackRequestInformation.getUser() == null
170 ? config.getString( UserConfigurationKeys.DEFAULT_GUEST )
171 : redbackRequestInformation.getUser().getUsername() );
174 protected String getBaseUrl()
175 throws RepositoryAdminException
177 String applicationUrl = archivaAdministration.getUiConfiguration().getApplicationUrl();
178 if ( StringUtils.isNotBlank( applicationUrl ) )
180 return applicationUrl;
182 return httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + (
183 httpServletRequest.getServerPort() == 80 ? "" : ":" + httpServletRequest.getServerPort() )
184 + httpServletRequest.getContextPath();
187 protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
189 //TODO do some caching here !!!
190 // olamy : with plexus we get only roleHint
191 // as per convention we named spring bean role#hint remove role# if exists
192 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
194 Map<String, T> beans = new HashMap<>( springBeans.size() );
196 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
198 String key = StringUtils.contains( entry.getKey(), '#' )
199 ? StringUtils.substringAfterLast( entry.getKey(), "#" )
201 beans.put( key, entry.getValue() );
206 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
208 AuditEvent auditEvent = new AuditEvent( repositoryId, getPrincipal(), filePath, action );
209 AuditInformation auditInformation = getAuditInformation();
210 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
211 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
212 for ( AuditListener auditListener : getAuditListeners() )
214 auditListener.auditEvent( auditEvent );
222 protected String getArtifactUrl( Artifact artifact )
223 throws ArchivaRestServiceException
225 return getArtifactUrl( artifact, null );
229 protected String getArtifactUrl( Artifact artifact, String repositoryId )
230 throws ArchivaRestServiceException
235 if ( httpServletRequest == null )
240 StringBuilder sb = new StringBuilder( getBaseUrl() );
242 sb.append( "/repository" );
244 // when artifact come from a remote repository when have here the remote repo id
245 // we must replace it with a valid managed one available for the user.
246 if ( StringUtils.isEmpty( repositoryId ) )
248 List<String> userRepos = userRepositories.getObservableRepositoryIds( getPrincipal() );
249 // is it a good one? if yes nothing to
250 // if not search the repo who is proxy for this remote
251 if ( !userRepos.contains( artifact.getContext() ) )
253 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorAdmin.getProxyConnectorAsMap().entrySet() )
255 for ( ProxyConnector proxyConnector : entry.getValue() )
257 if ( StringUtils.equals( "remote-" + proxyConnector.getTargetRepoId(),
258 artifact.getContext() ) //
259 && userRepos.contains( entry.getKey() ) )
261 sb.append( '/' ).append( entry.getKey() );
269 sb.append( '/' ).append( artifact.getContext() );
276 sb.append( '/' ).append( repositoryId );
279 sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
280 sb.append( '/' ).append( artifact.getArtifactId() );
281 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
283 sb.append( '/' ).append( VersionUtil.getBaseVersion( artifact.getVersion() ) );
287 sb.append( '/' ).append( artifact.getVersion() );
289 sb.append( '/' ).append( artifact.getArtifactId() );
290 sb.append( '-' ).append( artifact.getVersion() );
291 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
293 sb.append( '-' ).append( artifact.getClassifier() );
295 // maven-plugin packaging is a jar
296 if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
302 sb.append( '.' ).append( artifact.getFileExtension() );
305 return sb.toString();
307 catch ( Exception e )
309 throw new ArchivaRestServiceException( e.getMessage(),
310 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
314 protected List<Artifact> buildArtifacts( List<ArtifactMetadata> artifactMetadatas, String repositoryId )
315 throws ArchivaRestServiceException
319 if ( artifactMetadatas != null && !artifactMetadatas.isEmpty() )
321 List<Artifact> artifacts = new ArrayList<>( artifactMetadatas.size() );
322 for ( ArtifactMetadata artifact : artifactMetadatas )
325 ArtifactBuilder builder =
326 new ArtifactBuilder().forArtifactMetadata( artifact ).withManagedRepositoryContent(
327 repositoryContentFactory.getManagedRepositoryContent( repositoryId ) );
328 Artifact art = builder.build();
329 art.setUrl( getArtifactUrl( art, repositoryId ) );
330 artifacts.add( art );
334 return Collections.emptyList();
336 catch ( RepositoryException e )
338 log.error( e.getMessage(), e );
339 throw new ArchivaRestServiceException( e.getMessage(),
340 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
344 protected Boolean doScanRepository( String repositoryId, boolean fullScan )
346 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
348 log.info( "scanning of repository with id {} already scheduled", repositoryId );
349 return Boolean.FALSE;
351 RepositoryTask task = new RepositoryTask();
352 task.setRepositoryId( repositoryId );
353 task.setScanAll( fullScan );
356 repositoryTaskScheduler.queueTask( task );
358 catch ( TaskQueueException e )
360 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
366 private static class ModelMapperHolder
368 private static ModelMapper MODEL_MAPPER = new ModelMapper();
372 MODEL_MAPPER.addMappings( new SearchResultHitMap() );
373 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
378 private static class SearchResultHitMap
379 extends PropertyMap<SearchResultHit, Artifact>
382 protected void configure()
384 skip().setId( null );
390 protected ModelMapper getModelMapper()
392 return ModelMapperHolder.MODEL_MAPPER;