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.audit.AuditEvent;
26 import org.apache.archiva.audit.AuditListener;
27 import org.apache.archiva.common.utils.VersionUtil;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
30 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
31 import org.apache.archiva.redback.users.User;
32 import org.apache.archiva.redback.users.UserManager;
33 import org.apache.archiva.rest.api.model.Artifact;
34 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
35 import org.apache.archiva.security.AccessDeniedException;
36 import org.apache.archiva.security.ArchivaSecurityException;
37 import org.apache.archiva.security.PrincipalNotFoundException;
38 import org.apache.archiva.security.UserRepositories;
39 import org.apache.commons.lang.StringUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.context.ApplicationContext;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.ws.rs.core.Context;
48 import javax.ws.rs.core.Response;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.HashMap;
52 import java.util.List;
56 * abstract class with common utilities methods
58 * @author Olivier Lamy
61 public abstract class AbstractRestService
64 protected Logger log = LoggerFactory.getLogger( getClass() );
67 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
70 protected UserRepositories userRepositories;
74 @Named( value = "repositorySessionFactory" )
75 protected RepositorySessionFactory repositorySessionFactory;
78 protected ArchivaAdministration archivaAdministration;
81 protected HttpServletRequest httpServletRequest;
83 protected AuditInformation getAuditInformation()
85 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
86 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
87 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
88 return new AuditInformation( user, remoteAddr );
91 public List<AuditListener> getAuditListeners()
93 return auditListeners;
96 public void setAuditListeners( List<AuditListener> auditListeners )
98 this.auditListeners = auditListeners;
101 protected List<String> getObservableRepos()
105 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
106 return ids == null ? Collections.<String>emptyList() : ids;
108 catch ( PrincipalNotFoundException e )
110 log.warn( e.getMessage(), e );
112 catch ( AccessDeniedException e )
114 log.warn( e.getMessage(), e );
116 catch ( ArchivaSecurityException e )
118 log.warn( e.getMessage(), e );
120 return Collections.emptyList();
123 protected String getPrincipal()
125 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
127 return redbackRequestInformation == null
128 ? UserManager.GUEST_USERNAME
129 : ( redbackRequestInformation.getUser() == null
130 ? UserManager.GUEST_USERNAME
131 : redbackRequestInformation.getUser().getUsername() );
134 protected String getBaseUrl()
135 throws RepositoryAdminException
137 String applicationUrl = archivaAdministration.getUiConfiguration().getApplicationUrl();
138 if ( StringUtils.isNotBlank( applicationUrl ) )
140 return applicationUrl;
142 return httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + (
143 httpServletRequest.getServerPort() == 80 ? "" : ":" + httpServletRequest.getServerPort() )
144 + httpServletRequest.getContextPath();
147 protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
149 //TODO do some caching here !!!
150 // olamy : with plexus we get only roleHint
151 // as per convention we named spring bean role#hint remove role# if exists
152 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
154 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
156 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
158 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
159 beans.put( key, entry.getValue() );
164 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
166 AuditEvent auditEvent = new AuditEvent( repositoryId, getPrincipal(), filePath, action );
167 AuditInformation auditInformation = getAuditInformation();
168 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
169 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
170 for ( AuditListener auditListener : getAuditListeners() )
172 auditListener.auditEvent( auditEvent );
180 protected String getArtifactUrl( Artifact artifact )
181 throws ArchivaRestServiceException
186 if ( httpServletRequest == null )
191 StringBuilder sb = new StringBuilder( getBaseUrl() );
193 sb.append( "/repository" );
195 sb.append( '/' ).append( artifact.getContext() );
197 sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
198 sb.append( '/' ).append( artifact.getArtifactId() );
199 if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
201 sb.append( '/' ).append( VersionUtil.getBaseVersion( artifact.getVersion() ) );
205 sb.append( '/' ).append( artifact.getVersion() );
207 sb.append( '/' ).append( artifact.getArtifactId() );
208 sb.append( '-' ).append( artifact.getVersion() );
209 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
211 sb.append( '-' ).append( artifact.getClassifier() );
213 // maven-plugin packaging is a jar
214 if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
220 sb.append( '.' ).append( artifact.getFileExtension() );
223 return sb.toString();
225 catch ( RepositoryAdminException e )
227 throw new ArchivaRestServiceException( e.getMessage(),
228 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );