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.audit.AuditEvent;
23 import org.apache.archiva.audit.AuditListener;
24 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
25 import org.apache.archiva.security.AccessDeniedException;
26 import org.apache.archiva.security.ArchivaSecurityException;
27 import org.apache.archiva.security.PrincipalNotFoundException;
28 import org.apache.archiva.security.UserRepositories;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.archiva.redback.users.User;
31 import org.apache.archiva.redback.users.UserManager;
32 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
33 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.context.ApplicationContext;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.ws.rs.core.Context;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
49 * abstract class with common utilities methods
51 * @author Olivier Lamy
54 public abstract class AbstractRestService
57 protected Logger log = LoggerFactory.getLogger( getClass() );
60 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
63 protected UserRepositories userRepositories;
67 @Named( value = "repositorySessionFactory" )
68 protected RepositorySessionFactory repositorySessionFactory;
71 protected HttpServletRequest httpServletRequest;
73 protected AuditInformation getAuditInformation()
75 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
76 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
77 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
78 return new AuditInformation( user, remoteAddr );
81 public List<AuditListener> getAuditListeners()
83 return auditListeners;
86 public void setAuditListeners( List<AuditListener> auditListeners )
88 this.auditListeners = auditListeners;
91 protected List<String> getObservableRepos()
95 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
96 return ids == null ? Collections.<String>emptyList() : ids;
98 catch ( PrincipalNotFoundException e )
100 log.warn( e.getMessage(), e );
102 catch ( AccessDeniedException e )
104 log.warn( e.getMessage(), e );
106 catch ( ArchivaSecurityException e )
108 log.warn( e.getMessage(), e );
110 return Collections.emptyList();
113 protected String getPrincipal()
115 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
117 return redbackRequestInformation == null
118 ? UserManager.GUEST_USERNAME
119 : ( redbackRequestInformation.getUser() == null
120 ? UserManager.GUEST_USERNAME
121 : redbackRequestInformation.getUser().getUsername() );
124 protected String getBaseUrl( HttpServletRequest req )
126 return req.getScheme() + "://" + req.getServerName() + ( req.getServerPort() == 80
128 : ":" + req.getServerPort() ) + req.getContextPath();
131 protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
133 //TODO do some caching here !!!
134 // olamy : with plexus we get only roleHint
135 // as per convention we named spring bean role#hint remove role# if exists
136 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
138 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
140 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
142 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
143 beans.put( key, entry.getValue() );
148 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
150 AuditEvent auditEvent = new AuditEvent( repositoryId, getPrincipal(), filePath, action );
151 AuditInformation auditInformation = getAuditInformation();
152 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
153 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
154 for ( AuditListener auditListener : getAuditListeners() )
156 auditListener.auditEvent( auditEvent );