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.codehaus.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;
50 import java.util.Properties;
53 * LogEnabled and SessionAware ActionSupport
55 public abstract class AbstractActionSupport
57 implements SessionAware, Auditable
59 protected Map<?, ?> session;
61 protected Logger log = LoggerFactory.getLogger( getClass() );
64 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
68 @Named( value = "repositorySessionFactory" )
69 protected RepositorySessionFactory repositorySessionFactory;
72 protected ApplicationContext applicationContext;
74 private String principal;
77 @Named( value = "archivaRuntimeProperties" )
78 private Properties archivaRuntimeProperties;
81 public void initialize()
86 @SuppressWarnings( "unchecked" )
87 public void setSession( Map map )
92 public void addAuditListener( AuditListener listener )
94 this.auditListeners.add( listener );
97 public void clearAuditListeners()
99 this.auditListeners.clear();
102 public void removeAuditListener( AuditListener listener )
104 this.auditListeners.remove( listener );
107 protected void triggerAuditEvent( String repositoryId, String resource, String action )
109 AuditEvent event = new AuditEvent( repositoryId, getPrincipal(), resource, action );
110 event.setRemoteIP( getRemoteAddr() );
112 for ( AuditListener listener : auditListeners )
114 listener.auditEvent( event );
118 protected void triggerAuditEvent( String resource, String action )
120 AuditEvent event = new AuditEvent( null, getPrincipal(), resource, action );
121 event.setRemoteIP( getRemoteAddr() );
123 for ( AuditListener listener : auditListeners )
125 listener.auditEvent( event );
129 protected void triggerAuditEvent( String action )
131 AuditEvent event = new AuditEvent( null, getPrincipal(), null, action );
132 event.setRemoteIP( getRemoteAddr() );
134 for ( AuditListener listener : auditListeners )
136 listener.auditEvent( event );
140 private String getRemoteAddr()
142 HttpServletRequest request = ServletActionContext.getRequest();
143 return request != null ? request.getRemoteAddr() : null;
146 @SuppressWarnings( "unchecked" )
147 protected String getPrincipal()
149 if ( principal != null )
153 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
156 void setPrincipal( String principal )
158 this.principal = principal;
161 public void setAuditListeners( List<AuditListener> auditListeners )
163 this.auditListeners = auditListeners;
166 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
168 this.repositorySessionFactory = repositorySessionFactory;
171 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
173 //TODO do some caching here !!!
174 // olamy : with plexus we get only roleHint
175 // as per convention we named spring bean role#hint remove role# if exists
176 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
178 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
180 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
182 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
183 beans.put( key, entry.getValue() );
189 protected AuditInformation getAuditInformation()
191 AuditInformation auditInformation = new AuditInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
193 return auditInformation;
196 protected RedbackRequestInformation getRedbackRequestInformation()
198 return new RedbackRequestInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
201 public String getArchivaVersion()
203 return (String) archivaRuntimeProperties.get( "archiva.version" );
206 public String getArchivaBuildNumber()
208 return (String) archivaRuntimeProperties.get( "archiva.buildNumber" );
211 public String getArchivaBuildTimestamp()
213 return (String) archivaRuntimeProperties.get( "archiva.timestamp" );
216 public String getArchivaBuildTimestampDateStr()
218 SimpleDateFormat sfd = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz", getLocale() );
220 new Date( NumberUtils.createLong( (String) archivaRuntimeProperties.get( "archiva.timestamp" ) ) ) );
224 * dummy information for audit events
228 private static class SimpleUser
232 private String principal;
234 protected SimpleUser( String principal )
236 this.principal = principal;
239 public Object getPrincipal()
241 return this.principal;
244 public String getUsername()
246 return this.principal;
249 public void setUsername( String name )
254 public String getFullName()
259 public void setFullName( String name )
264 public String getEmail()
269 public void setEmail( String address )
274 public String getPassword()
279 public void setPassword( String rawPassword )
284 public String getEncodedPassword()
289 public void setEncodedPassword( String encodedPassword )
294 public Date getLastPasswordChange()
299 public void setLastPasswordChange( Date passwordChangeDate )
304 public List<String> getPreviousEncodedPasswords()
309 public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
314 public void addPreviousEncodedPassword( String encodedPassword )
319 public boolean isPermanent()
324 public void setPermanent( boolean permanent )
329 public boolean isLocked()
334 public void setLocked( boolean locked )
339 public boolean isPasswordChangeRequired()
344 public void setPasswordChangeRequired( boolean changeRequired )
349 public boolean isValidated()
354 public void setValidated( boolean valid )
359 public int getCountFailedLoginAttempts()
364 public void setCountFailedLoginAttempts( int count )
369 public Date getAccountCreationDate()
374 public void setAccountCreationDate( Date date )
379 public Date getLastLoginDate()
384 public void setLastLoginDate( Date date )