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 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
\r
64 @Named( value = "repositorySessionFactory" )
\r
65 protected RepositorySessionFactory repositorySessionFactory;
\r
68 protected ApplicationContext applicationContext;
\r
70 private String principal;
\r
73 public void initialize()
\r
78 @SuppressWarnings( "unchecked" )
\r
79 public void setSession( Map map )
\r
84 public void addAuditListener( AuditListener listener )
\r
86 this.auditListeners.add( listener );
\r
89 public void clearAuditListeners()
\r
91 this.auditListeners.clear();
\r
94 public void removeAuditListener( AuditListener listener )
\r
96 this.auditListeners.remove( listener );
\r
99 protected void triggerAuditEvent( String repositoryId, String resource, String action )
\r
101 AuditEvent event = new AuditEvent( repositoryId, getPrincipal(), resource, action );
\r
102 event.setRemoteIP( getRemoteAddr() );
\r
104 for ( AuditListener listener : auditListeners )
\r
106 listener.auditEvent( event );
\r
110 protected void triggerAuditEvent( String resource, String action )
\r
112 AuditEvent event = new AuditEvent( null, getPrincipal(), resource, action );
\r
113 event.setRemoteIP( getRemoteAddr() );
\r
115 for ( AuditListener listener : auditListeners )
\r
117 listener.auditEvent( event );
\r
121 protected void triggerAuditEvent( String action )
\r
123 AuditEvent event = new AuditEvent( null, getPrincipal(), null, action );
\r
124 event.setRemoteIP( getRemoteAddr() );
\r
126 for ( AuditListener listener : auditListeners )
\r
128 listener.auditEvent( event );
\r
132 private String getRemoteAddr()
\r
134 HttpServletRequest request = ServletActionContext.getRequest();
\r
135 return request != null ? request.getRemoteAddr() : null;
\r
138 @SuppressWarnings( "unchecked" )
\r
139 protected String getPrincipal()
\r
141 if ( principal != null )
\r
145 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
\r
148 void setPrincipal( String principal )
\r
150 this.principal = principal;
\r
153 public void setAuditListeners( List<AuditListener> auditListeners )
\r
155 this.auditListeners = auditListeners;
\r
158 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
\r
160 this.repositorySessionFactory = repositorySessionFactory;
\r
163 protected <T> Map<String, T> getBeansOfType( Class<T> clazz )
\r
165 //TODO do some caching here !!!
\r
166 // olamy : with plexus we get only roleHint
\r
167 // as per convention we named spring bean role#hint remove role# if exists
\r
168 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
\r
170 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
\r
172 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
\r
174 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
\r
175 beans.put( key, entry.getValue() );
\r
181 protected AuditInformation getAuditInformation()
\r
183 AuditInformation auditInformation = new AuditInformation( new SimpleUser( getPrincipal() ), getRemoteAddr() );
\r
185 return auditInformation;
\r
189 * dummy information for audit events
\r
192 private static class SimpleUser
\r
196 private String principal;
\r
198 protected SimpleUser( String principal )
\r
200 this.principal = principal;
\r
203 public Object getPrincipal()
\r
205 return this.principal;
\r
208 public String getUsername()
\r
213 public void setUsername( String name )
\r
218 public String getFullName()
\r
223 public void setFullName( String name )
\r
228 public String getEmail()
\r
233 public void setEmail( String address )
\r
238 public String getPassword()
\r
243 public void setPassword( String rawPassword )
\r
248 public String getEncodedPassword()
\r
253 public void setEncodedPassword( String encodedPassword )
\r
258 public Date getLastPasswordChange()
\r
263 public void setLastPasswordChange( Date passwordChangeDate )
\r
268 public List<String> getPreviousEncodedPasswords()
\r
273 public void setPreviousEncodedPasswords( List<String> encodedPasswordList )
\r
278 public void addPreviousEncodedPassword( String encodedPassword )
\r
283 public boolean isPermanent()
\r
288 public void setPermanent( boolean permanent )
\r
293 public boolean isLocked()
\r
298 public void setLocked( boolean locked )
\r
303 public boolean isPasswordChangeRequired()
\r
308 public void setPasswordChangeRequired( boolean changeRequired )
\r
313 public boolean isValidated()
\r
318 public void setValidated( boolean valid )
\r
323 public int getCountFailedLoginAttempts()
\r
328 public void setCountFailedLoginAttempts( int count )
\r
333 public Date getAccountCreationDate()
\r
338 public void setAccountCreationDate( Date date )
\r
343 public Date getLastLoginDate()
\r
348 public void setLastLoginDate( Date date )
\r