1 package org.apache.archiva.web.action;
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 com.opensymphony.xwork2.ActionContext;
23 import com.opensymphony.xwork2.ActionSupport;
24 import org.apache.archiva.admin.model.AuditInformation;
25 import org.apache.archiva.audit.AuditEvent;
26 import org.apache.archiva.audit.AuditListener;
27 import org.apache.archiva.audit.Auditable;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.security.ArchivaXworkUser;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.struts2.ServletActionContext;
32 import org.apache.struts2.interceptor.SessionAware;
33 import org.codehaus.plexus.redback.users.User;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.context.ApplicationContext;
38 import javax.annotation.PostConstruct;
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import javax.servlet.http.HttpServletRequest;
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.HashMap;
45 import java.util.List;
49 * LogEnabled and SessionAware ActionSupport
51 public abstract class AbstractActionSupport
53 implements SessionAware, Auditable
55 protected Map<?, ?> session;
57 protected Logger log = LoggerFactory.getLogger( getClass() );
60 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
64 @Named( value = "repositorySessionFactory" )
65 protected RepositorySessionFactory repositorySessionFactory;
68 protected ApplicationContext applicationContext;
70 private String principal;
73 public void initialize()
78 @SuppressWarnings( "unchecked" )
79 public void setSession( Map map )
84 public void addAuditListener( AuditListener listener )
86 this.auditListeners.add( listener );
89 public void clearAuditListeners()
91 this.auditListeners.clear();
94 public void removeAuditListener( AuditListener listener )
96 this.auditListeners.remove( listener );
99 protected void triggerAuditEvent( String repositoryId, String resource, String action )
101 AuditEvent event = new AuditEvent( repositoryId, getPrincipal(), resource, action );
102 event.setRemoteIP( getRemoteAddr() );
104 for ( AuditListener listener : auditListeners )
106 listener.auditEvent( event );
110 protected void triggerAuditEvent( String resource, String action )
112 AuditEvent event = new AuditEvent( null, getPrincipal(), resource, action );
113 event.setRemoteIP( getRemoteAddr() );
115 for ( AuditListener listener : auditListeners )
117 listener.auditEvent( event );
121 protected void triggerAuditEvent( String action )
123 AuditEvent event = new AuditEvent( null, getPrincipal(), null, action );
124 event.setRemoteIP( getRemoteAddr() );
126 for ( AuditListener listener : auditListeners )
128 listener.auditEvent( event );
132 private String getRemoteAddr()
134 HttpServletRequest request = ServletActionContext.getRequest();
135 return request != null ? request.getRemoteAddr() : null;
138 @SuppressWarnings( "unchecked" )
139 protected String getPrincipal()
141 if ( principal != null )
145 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
148 void setPrincipal( String principal )
150 this.principal = principal;
153 public void setAuditListeners( List<AuditListener> auditListeners )
155 this.auditListeners = auditListeners;
158 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
160 this.repositorySessionFactory = repositorySessionFactory;
163 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
165 //TODO do some caching here !!!
166 // olamy : with plexus we get only roleHint
167 // as per convention we named spring bean role#hint remove role# if exists
168 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
170 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
172 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
174 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
175 beans.put( key, entry.getValue() );
181 protected AuditInformation getAuditInformation()
183 AuditInformation auditInformation = new AuditInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
185 return auditInformation;
189 * dummy information for audit events
192 private static class SimpleUser
196 private String principal;
198 protected SimpleUser( String principal )
200 this.principal = principal;
203 public Object getPrincipal()
205 return this.principal;
208 public String getUsername()
213 public void setUsername( String name )
218 public String getFullName()
223 public void setFullName( String name )
228 public String getEmail()
233 public void setEmail( String address )
238 public String getPassword()
243 public void setPassword( String rawPassword )
248 public String getEncodedPassword()
253 public void setEncodedPassword( String encodedPassword )
258 public Date getLastPasswordChange()
263 public void setLastPasswordChange( Date passwordChangeDate )
268 public List<String> getPreviousEncodedPasswords()
273 public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
278 public void addPreviousEncodedPassword( String encodedPassword )
283 public boolean isPermanent()
288 public void setPermanent( boolean permanent )
293 public boolean isLocked()
298 public void setLocked( boolean locked )
303 public boolean isPasswordChangeRequired()
308 public void setPasswordChangeRequired( boolean changeRequired )
313 public boolean isValidated()
318 public void setValidated( boolean valid )
323 public int getCountFailedLoginAttempts()
328 public void setCountFailedLoginAttempts( int count )
333 public Date getAccountCreationDate()
338 public void setAccountCreationDate( Date date )
343 public Date getLastLoginDate()
348 public void setLastLoginDate( Date date )