1 package org.apache.maven.archiva.web.action;
\r
4 * Licensed to the Apache Software Foundation (ASF) under one
\r
5 * or more contributor license agreements. See the NOTICE file
\r
6 * distributed with this work for additional information
\r
7 * regarding copyright ownership. The ASF licenses this file
\r
8 * to you under the Apache License, Version 2.0 (the
\r
9 * "License"); you may not use this file except in compliance
\r
10 * with the License. You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing,
\r
15 * software distributed under the License is distributed on an
\r
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
\r
17 * KIND, either express or implied. See the License for the
\r
18 * specific language governing permissions and limitations
\r
19 * under the License.
\r
22 import com.opensymphony.xwork2.ActionContext;
\r
23 import com.opensymphony.xwork2.ActionSupport;
\r
24 import org.apache.archiva.admin.AuditInformation;
\r
25 import org.apache.archiva.audit.AuditEvent;
\r
26 import org.apache.archiva.audit.AuditListener;
\r
27 import org.apache.archiva.audit.Auditable;
\r
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
\r
29 import org.apache.commons.lang.StringUtils;
\r
30 import org.apache.archiva.security.ArchivaXworkUser;
\r
31 import org.apache.struts2.ServletActionContext;
\r
32 import org.apache.struts2.interceptor.SessionAware;
\r
33 import org.codehaus.plexus.redback.users.User;
\r
34 import org.slf4j.Logger;
\r
35 import org.slf4j.LoggerFactory;
\r
36 import org.springframework.context.ApplicationContext;
\r
38 import javax.annotation.PostConstruct;
\r
39 import javax.inject.Inject;
\r
40 import javax.inject.Named;
\r
41 import javax.servlet.http.HttpServletRequest;
\r
42 import java.util.ArrayList;
\r
43 import java.util.Date;
\r
44 import java.util.HashMap;
\r
45 import java.util.List;
\r
46 import java.util.Map;
\r
49 * LogEnabled and SessionAware ActionSupport
\r
51 public abstract class AbstractActionSupport
\r
52 extends ActionSupport
\r
53 implements SessionAware, Auditable
\r
55 protected Map<?, ?> session;
\r
57 protected Logger log = LoggerFactory.getLogger( getClass() );
\r
60 * plexus.requirement role="org.apache.archiva.audit.AuditListener"
\r
63 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
\r
66 * plexus.requirement
\r
69 @Named( value = "repositorySessionFactory" )
\r
70 protected RepositorySessionFactory repositorySessionFactory;
\r
73 protected ApplicationContext applicationContext;
\r
75 private String principal;
\r
78 public void initialize()
\r
83 @SuppressWarnings( "unchecked" )
\r
84 public void setSession( Map map )
\r
89 public void addAuditListener( AuditListener listener )
\r
91 this.auditListeners.add( listener );
\r
94 public void clearAuditListeners()
\r
96 this.auditListeners.clear();
\r
99 public void removeAuditListener( AuditListener listener )
\r
101 this.auditListeners.remove( listener );
\r
104 protected void triggerAuditEvent( String repositoryId, String resource, String action )
\r
106 AuditEvent event = new AuditEvent( repositoryId, getPrincipal(), resource, action );
\r
107 event.setRemoteIP( getRemoteAddr() );
\r
109 for ( AuditListener listener : auditListeners )
\r
111 listener.auditEvent( event );
\r
115 protected void triggerAuditEvent( String resource, String action )
\r
117 AuditEvent event = new AuditEvent( null, getPrincipal(), resource, action );
\r
118 event.setRemoteIP( getRemoteAddr() );
\r
120 for ( AuditListener listener : auditListeners )
\r
122 listener.auditEvent( event );
\r
126 protected void triggerAuditEvent( String action )
\r
128 AuditEvent event = new AuditEvent( null, getPrincipal(), null, action );
\r
129 event.setRemoteIP( getRemoteAddr() );
\r
131 for ( AuditListener listener : auditListeners )
\r
133 listener.auditEvent( event );
\r
137 private String getRemoteAddr()
\r
139 HttpServletRequest request = ServletActionContext.getRequest();
\r
140 return request != null ? request.getRemoteAddr() : null;
\r
143 @SuppressWarnings( "unchecked" )
\r
144 protected String getPrincipal()
\r
146 if ( principal != null )
\r
150 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
\r
153 void setPrincipal( String principal )
\r
155 this.principal = principal;
\r
158 public void setAuditListeners( List<AuditListener> auditListeners )
\r
160 this.auditListeners = auditListeners;
\r
163 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
\r
165 this.repositorySessionFactory = repositorySessionFactory;
\r
168 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
\r
170 //TODO do some caching here !!!
\r
171 // olamy : with plexus we get only roleHint
\r
172 // as per convention we named spring bean role#hint remove role# if exists
\r
173 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
\r
175 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
\r
177 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
\r
179 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
\r
180 beans.put( key, entry.getValue() );
\r
186 protected AuditInformation getAuditInformation()
\r
188 AuditInformation auditInformation = new AuditInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
\r
190 return auditInformation;
\r
194 * dummy information for audit events
\r
197 private static class SimpleUser
\r
201 private String principal;
\r
203 protected SimpleUser( String principal )
\r
205 this.principal = principal;
\r
208 public Object getPrincipal()
\r
210 return this.principal;
\r
213 public String getUsername()
\r
218 public void setUsername( String name )
\r
223 public String getFullName()
\r
228 public void setFullName( String name )
\r
233 public String getEmail()
\r
238 public void setEmail( String address )
\r
243 public String getPassword()
\r
248 public void setPassword( String rawPassword )
\r
253 public String getEncodedPassword()
\r
258 public void setEncodedPassword( String encodedPassword )
\r
263 public Date getLastPasswordChange()
\r
268 public void setLastPasswordChange( Date passwordChangeDate )
\r
273 public List<String> getPreviousEncodedPasswords()
\r
278 public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
\r
283 public void addPreviousEncodedPassword( String encodedPassword )
\r
288 public boolean isPermanent()
\r
293 public void setPermanent( boolean permanent )
\r
298 public boolean isLocked()
\r
303 public void setLocked( boolean locked )
\r
308 public boolean isPasswordChangeRequired()
\r
313 public void setPasswordChangeRequired( boolean changeRequired )
\r
318 public boolean isValidated()
\r
323 public void setValidated( boolean valid )
\r
328 public int getCountFailedLoginAttempts()
\r
333 public void setCountFailedLoginAttempts( int count )
\r
338 public Date getAccountCreationDate()
\r
343 public void setAccountCreationDate( Date date )
\r
348 public Date getLastLoginDate()
\r
353 public void setLastLoginDate( Date date )
\r