1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
24 import org.apache.archiva.audit.AuditEvent;
25 import org.apache.archiva.audit.AuditListener;
26 import org.apache.archiva.common.utils.VersionUtil;
27 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
28 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
29 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
30 import org.apache.archiva.redback.users.User;
31 import org.apache.archiva.redback.users.UserManager;
32 import org.apache.archiva.rest.api.model.Artifact;
33 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
34 import org.apache.archiva.security.AccessDeniedException;
35 import org.apache.archiva.security.ArchivaSecurityException;
36 import org.apache.archiva.security.PrincipalNotFoundException;
37 import org.apache.archiva.security.UserRepositories;
38 import org.apache.commons.lang.StringUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.ApplicationContext;
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.ws.rs.core.Context;
47 import javax.ws.rs.core.Response;
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.List;
55 * abstract class with common utilities methods
57 * @author Olivier Lamy
60 public abstract class AbstractRestService
63 protected Logger log = LoggerFactory.getLogger( getClass() );
66 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
69 protected UserRepositories userRepositories;
73 @Named( value = "repositorySessionFactory" )
74 protected RepositorySessionFactory repositorySessionFactory;
77 protected ArchivaAdministration archivaAdministration;
80 protected HttpServletRequest httpServletRequest;
82 protected AuditInformation getAuditInformation()
84 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
85 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
86 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
87 return new AuditInformation( user, remoteAddr );
90 public List<AuditListener> getAuditListeners()
92 return auditListeners;
95 public void setAuditListeners( List<AuditListener> auditListeners )
97 this.auditListeners = auditListeners;
100 protected List<String> getObservableRepos()
104 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
105 return ids == null ? Collections.<String>emptyList() : ids;
107 catch ( PrincipalNotFoundException e )
109 log.warn( e.getMessage(), e );
111 catch ( AccessDeniedException e )
113 log.warn( e.getMessage(), e );
115 catch ( ArchivaSecurityException e )
117 log.warn( e.getMessage(), e );
119 return Collections.emptyList();
122 protected String getPrincipal()
124 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
126 return redbackRequestInformation == null
127 ? UserManager.GUEST_USERNAME
128 : ( redbackRequestInformation.getUser() == null
129 ? UserManager.GUEST_USERNAME
130 : redbackRequestInformation.getUser().getUsername() );
133 protected String getBaseUrl()
134 throws RepositoryAdminException
136 String applicationUrl = archivaAdministration.getUiConfiguration().getApplicationUrl();
137 if ( StringUtils.isNotBlank( applicationUrl ) )
139 return applicationUrl;
141 return httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + (
142 httpServletRequest.getServerPort() == 80 ? "" : ":" + httpServletRequest.getServerPort() )
143 + httpServletRequest.getContextPath();
146 protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
148 //TODO do some caching here !!!
149 // olamy : with plexus we get only roleHint
150 // as per convention we named spring bean role#hint remove role# if exists
151 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
153 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
155 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
157 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
158 beans.put( key, entry.getValue() );
163 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
165 AuditEvent auditEvent = new AuditEvent( repositoryId, getPrincipal(), filePath, action );
166 AuditInformation auditInformation = getAuditInformation();
167 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
168 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
169 for ( AuditListener auditListener : getAuditListeners() )
171 auditListener.auditEvent( auditEvent );
179 protected String getArtifactUrl( Artifact artifact, String version )
180 throws ArchivaRestServiceException
185 if ( httpServletRequest == null )
190 StringBuilder sb = new StringBuilder( getBaseUrl() );
192 sb.append( "/repository" );
194 sb.append( '/' ).append( artifact.getContext() );
196 sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
197 sb.append( '/' ).append( artifact.getArtifactId() );
198 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
200 sb.append( '/' ).append( VersionUtil.getBaseVersion( artifact.getVersion() ) );
204 sb.append( '/' ).append( artifact.getVersion() );
206 sb.append( '/' ).append( artifact.getArtifactId() );
207 sb.append( '-' ).append( artifact.getVersion() );
208 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
210 sb.append( '-' ).append( artifact.getClassifier() );
212 // maven-plugin packaging is a jar
213 if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
219 sb.append( '.' ).append( artifact.getPackaging() );
222 return sb.toString();
224 catch ( RepositoryAdminException e )
226 throw new ArchivaRestServiceException( e.getMessage(),
227 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );