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.commons.lang.math.NumberUtils;
32 import org.apache.struts2.ServletActionContext;
33 import org.apache.struts2.interceptor.SessionAware;
34 import org.codehaus.plexus.redback.users.User;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.context.ApplicationContext;
39 import javax.annotation.PostConstruct;
40 import javax.inject.Inject;
41 import javax.inject.Named;
42 import javax.servlet.http.HttpServletRequest;
43 import java.text.SimpleDateFormat;
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.Properties;
52 * LogEnabled and SessionAware ActionSupport
54 public abstract class AbstractActionSupport
56 implements SessionAware, Auditable
58 protected Map<?, ?> session;
60 protected Logger log = LoggerFactory.getLogger( getClass() );
63 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
67 @Named( value = "repositorySessionFactory" )
68 protected RepositorySessionFactory repositorySessionFactory;
71 protected ApplicationContext applicationContext;
73 private String principal;
76 @Named( value = "archivaRuntimeProperties" )
77 private Properties archivaRuntimeProperties;
80 public void initialize()
85 @SuppressWarnings( "unchecked" )
86 public void setSession( Map map )
91 public void addAuditListener( AuditListener listener )
93 this.auditListeners.add( listener );
96 public void clearAuditListeners()
98 this.auditListeners.clear();
101 public void removeAuditListener( AuditListener listener )
103 this.auditListeners.remove( listener );
106 protected void triggerAuditEvent( String repositoryId, String resource, String action )
108 AuditEvent event = new AuditEvent( repositoryId, getPrincipal(), resource, action );
109 event.setRemoteIP( getRemoteAddr() );
111 for ( AuditListener listener : auditListeners )
113 listener.auditEvent( event );
117 protected void triggerAuditEvent( String resource, String action )
119 AuditEvent event = new AuditEvent( null, getPrincipal(), resource, action );
120 event.setRemoteIP( getRemoteAddr() );
122 for ( AuditListener listener : auditListeners )
124 listener.auditEvent( event );
128 protected void triggerAuditEvent( String action )
130 AuditEvent event = new AuditEvent( null, getPrincipal(), null, action );
131 event.setRemoteIP( getRemoteAddr() );
133 for ( AuditListener listener : auditListeners )
135 listener.auditEvent( event );
139 private String getRemoteAddr()
141 HttpServletRequest request = ServletActionContext.getRequest();
142 return request != null ? request.getRemoteAddr() : null;
145 @SuppressWarnings( "unchecked" )
146 protected String getPrincipal()
148 if ( principal != null )
152 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
155 void setPrincipal( String principal )
157 this.principal = principal;
160 public void setAuditListeners( List<AuditListener> auditListeners )
162 this.auditListeners = auditListeners;
165 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
167 this.repositorySessionFactory = repositorySessionFactory;
170 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
172 //TODO do some caching here !!!
173 // olamy : with plexus we get only roleHint
174 // as per convention we named spring bean role#hint remove role# if exists
175 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
177 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
179 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
181 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
182 beans.put( key, entry.getValue() );
188 protected AuditInformation getAuditInformation()
190 AuditInformation auditInformation = new AuditInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
192 return auditInformation;
195 public String getArchivaVersion()
197 return (String) archivaRuntimeProperties.get( "archiva.version" );
200 public String getArchivaBuildNumber()
202 return (String) archivaRuntimeProperties.get( "archiva.buildNumber" );
205 public String getArchivaBuildTimestamp()
207 return (String) archivaRuntimeProperties.get( "archiva.timestamp" );
210 public String getArchivaBuildTimestampDateStr()
212 SimpleDateFormat sfd = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz", getLocale() );
214 new Date( NumberUtils.createLong( (String) archivaRuntimeProperties.get( "archiva.timestamp" ) ) ) );
218 * dummy information for audit events
222 private static class SimpleUser
226 private String principal;
228 protected SimpleUser( String principal )
230 this.principal = principal;
233 public Object getPrincipal()
235 return this.principal;
238 public String getUsername()
243 public void setUsername( String name )
248 public String getFullName()
253 public void setFullName( String name )
258 public String getEmail()
263 public void setEmail( String address )
268 public String getPassword()
273 public void setPassword( String rawPassword )
278 public String getEncodedPassword()
283 public void setEncodedPassword( String encodedPassword )
288 public Date getLastPasswordChange()
293 public void setLastPasswordChange( Date passwordChangeDate )
298 public List<String> getPreviousEncodedPasswords()
303 public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
308 public void addPreviousEncodedPassword( String encodedPassword )
313 public boolean isPermanent()
318 public void setPermanent( boolean permanent )
323 public boolean isLocked()
328 public void setLocked( boolean locked )
333 public boolean isPasswordChangeRequired()
338 public void setPasswordChangeRequired( boolean changeRequired )
343 public boolean isValidated()
348 public void setValidated( boolean valid )
353 public int getCountFailedLoginAttempts()
358 public void setCountFailedLoginAttempts( int count )
363 public Date getAccountCreationDate()
368 public void setAccountCreationDate( Date date )
373 public Date getLastLoginDate()
378 public void setLastLoginDate( Date date )