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.redback.users.User;
30 import org.apache.archiva.security.ArchivaXworkUser;
31 import org.apache.archiva.web.runtime.ArchivaRuntimeInfo;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.struts2.ServletActionContext;
34 import org.apache.struts2.interceptor.SessionAware;
35 import org.apache.archiva.redback.rest.services.RedbackRequestInformation;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.context.ApplicationContext;
40 import javax.annotation.PostConstruct;
41 import javax.inject.Inject;
42 import javax.inject.Named;
43 import javax.servlet.http.HttpServletRequest;
44 import java.text.SimpleDateFormat;
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.HashMap;
48 import java.util.List;
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 private ArchivaRuntimeInfo archivaRuntimeInfo;
79 public void initialize()
84 @SuppressWarnings( "unchecked" )
85 public void setSession( Map map )
90 public void addAuditListener( AuditListener listener )
92 this.auditListeners.add( listener );
95 public void clearAuditListeners()
97 this.auditListeners.clear();
100 public void removeAuditListener( AuditListener listener )
102 this.auditListeners.remove( listener );
105 protected void triggerAuditEvent( String repositoryId, String resource, String action )
107 AuditEvent event = new AuditEvent( repositoryId, getPrincipal(), resource, action );
108 event.setRemoteIP( getRemoteAddr() );
110 for ( AuditListener listener : auditListeners )
112 listener.auditEvent( event );
116 protected void triggerAuditEvent( String resource, String action )
118 AuditEvent event = new AuditEvent( null, getPrincipal(), resource, action );
119 event.setRemoteIP( getRemoteAddr() );
121 for ( AuditListener listener : auditListeners )
123 listener.auditEvent( event );
127 protected void triggerAuditEvent( String action )
129 AuditEvent event = new AuditEvent( null, getPrincipal(), null, action );
130 event.setRemoteIP( getRemoteAddr() );
132 for ( AuditListener listener : auditListeners )
134 listener.auditEvent( event );
138 private String getRemoteAddr()
140 HttpServletRequest request = ServletActionContext.getRequest();
141 return request != null ? request.getRemoteAddr() : null;
144 @SuppressWarnings( "unchecked" )
145 protected String getPrincipal()
147 if ( principal != null )
151 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
154 void setPrincipal( String principal )
156 this.principal = principal;
159 public void setAuditListeners( List<AuditListener> auditListeners )
161 this.auditListeners = auditListeners;
164 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
166 this.repositorySessionFactory = repositorySessionFactory;
169 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
171 //TODO do some caching here !!!
172 // olamy : with plexus we get only roleHint
173 // as per convention we named spring bean role#hint remove role# if exists
174 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
176 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
178 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
180 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
181 beans.put( key, entry.getValue() );
187 protected AuditInformation getAuditInformation()
189 AuditInformation auditInformation = new AuditInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
191 return auditInformation;
194 protected RedbackRequestInformation getRedbackRequestInformation()
196 return new RedbackRequestInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
199 public String getArchivaVersion()
201 return archivaRuntimeInfo.getVersion();
204 public String getArchivaBuildNumber()
206 return archivaRuntimeInfo.getBuildNumber();
209 public String getArchivaBuildTimestamp()
211 return Long.toString(
212 archivaRuntimeInfo.getTimestamp() );
215 public String getArchivaBuildTimestampDateStr()
217 SimpleDateFormat sfd = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz", getLocale() );
218 return sfd.format( new Date( archivaRuntimeInfo.getTimestamp() ) );
222 * dummy information for audit events
226 private static class SimpleUser
230 private String principal;
232 protected SimpleUser( String principal )
234 this.principal = principal;
237 public Object getPrincipal()
239 return this.principal;
242 public String getUsername()
244 return this.principal;
247 public void setUsername( String name )
252 public String getFullName()
257 public void setFullName( String name )
262 public String getEmail()
267 public void setEmail( String address )
272 public String getPassword()
277 public void setPassword( String rawPassword )
282 public String getEncodedPassword()
287 public void setEncodedPassword( String encodedPassword )
292 public Date getLastPasswordChange()
297 public void setLastPasswordChange( Date passwordChangeDate )
302 public List<String> getPreviousEncodedPasswords()
307 public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
312 public void addPreviousEncodedPassword( String encodedPassword )
317 public boolean isPermanent()
322 public void setPermanent( boolean permanent )
327 public boolean isLocked()
332 public void setLocked( boolean locked )
337 public boolean isPasswordChangeRequired()
342 public void setPasswordChangeRequired( boolean changeRequired )
347 public boolean isValidated()
352 public void setValidated( boolean valid )
357 public int getCountFailedLoginAttempts()
362 public void setCountFailedLoginAttempts( int count )
367 public Date getAccountCreationDate()
372 public void setAccountCreationDate( Date date )
377 public Date getLastLoginDate()
382 public void setLastLoginDate( Date date )