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.audit.AuditEvent;
\r
25 import org.apache.archiva.audit.AuditListener;
\r
26 import org.apache.archiva.audit.Auditable;
\r
27 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
\r
28 import org.apache.maven.archiva.security.ArchivaXworkUser;
\r
29 import org.apache.struts2.ServletActionContext;
\r
30 import org.apache.struts2.interceptor.SessionAware;
\r
31 import org.slf4j.Logger;
\r
32 import org.slf4j.LoggerFactory;
\r
34 import java.util.ArrayList;
\r
35 import java.util.List;
\r
36 import java.util.Map;
\r
37 import javax.servlet.http.HttpServletRequest;
\r
40 * LogEnabled and SessionAware ActionSupport
\r
42 public abstract class PlexusActionSupport
\r
43 extends ActionSupport
\r
44 implements SessionAware, Auditable
\r
46 protected Map<?, ?> session;
\r
48 protected Logger log = LoggerFactory.getLogger( getClass() );
\r
51 * @plexus.requirement role="org.apache.archiva.audit.AuditListener"
\r
53 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
\r
56 * @plexus.requirement
\r
58 protected RepositorySessionFactory repositorySessionFactory;
\r
60 private String principal;
\r
62 @SuppressWarnings( "unchecked" )
\r
63 public void setSession( Map map )
\r
68 public void addAuditListener( AuditListener listener )
\r
70 this.auditListeners.add( listener );
\r
73 public void clearAuditListeners()
\r
75 this.auditListeners.clear();
\r
78 public void removeAuditListener( AuditListener listener )
\r
80 this.auditListeners.remove( listener );
\r
83 protected void triggerAuditEvent( String repositoryId, String resource, String action )
\r
85 AuditEvent event = new AuditEvent( repositoryId, getPrincipal(), resource, action );
\r
86 event.setRemoteIP( getRemoteAddr() );
\r
88 for ( AuditListener listener : auditListeners )
\r
90 listener.auditEvent( event );
\r
94 protected void triggerAuditEvent( String resource, String action )
\r
96 AuditEvent event = new AuditEvent( null, getPrincipal(), resource, action );
\r
97 event.setRemoteIP( getRemoteAddr() );
\r
99 for ( AuditListener listener : auditListeners )
\r
101 listener.auditEvent( event );
\r
105 protected void triggerAuditEvent( String action )
\r
107 AuditEvent event = new AuditEvent( null, getPrincipal(), null, action );
\r
108 event.setRemoteIP( getRemoteAddr() );
\r
110 for ( AuditListener listener : auditListeners )
\r
112 listener.auditEvent( event );
\r
116 private String getRemoteAddr()
\r
118 HttpServletRequest request = ServletActionContext.getRequest();
\r
119 return request != null ? request.getRemoteAddr() : null;
\r
122 @SuppressWarnings( "unchecked" )
\r
123 protected String getPrincipal()
\r
125 if ( principal != null )
\r
129 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
\r
132 void setPrincipal( String principal )
\r
134 this.principal = principal;
\r
137 public void setAuditListeners( List<AuditListener> auditListeners )
\r
139 this.auditListeners = auditListeners;
\r
142 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
\r
144 this.repositorySessionFactory = repositorySessionFactory;
\r